This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I want to log time stamp in units of one second using app_timer

The problem is shown in the picture below.

It is normal for 10 seconds after activating the timer.

As you can see in the picture, the value of the time stamp changes abnormally.

This is the timer handler source code.

There is nothing special.

After 10 seconds, lorawan_send function is excuted, there is a problem here.

Using sprintf is a problem.

I have omitted sprintf and it works noramlly.

It is the process of converting a buffer to a byte string and should not be omitted.

I uploaded the main source code for reference.

How can i solve that problem?

Parents
  • Hi 

    If I understand you correctly the time_stamp_timer_handler(..) function appears to run at 1 second intervals as expected, but the value of the time_stamp variable is corrupted?

    Then the most obvious problem would be that the time_stamp variable is located after the confirmed_send_packet buffer in memory, and somehow the sprintf function is writing too far into the confirmed_send_packet buffer and overwriting the time_stamp variable instead. 

    808466482 corresponds to 0x30303832, or "0082" in Ascii, which strengthens this theory. 

    What is the size of the confirmed_send_packet array?

    Best regards
    Torbjørn

  • Global Variable declaration

    static uint8_t confirmed_msg_buffer[14];

    static char confirmed_send_packet[]="";

  • Hi 

    There's your problem ;)

    If you initialize the confirmed_send_packet[] buffer to "" it will only contain a single byte (the 0 termination character). Once you start writing to any index > 0 in the array you are writing outside the array itself, corrupting other variables. 

    You have to initialize the array to be large enough for future writes to it. 

    Looking at your code it would have to be at least 2*14 + 1 = 29 bytes in size, so if you change the initialization like this it should work: 

    static char confirmed_send_packet[29]="";

    Best regards
    Torbjørn

Reply
  • Hi 

    There's your problem ;)

    If you initialize the confirmed_send_packet[] buffer to "" it will only contain a single byte (the 0 termination character). Once you start writing to any index > 0 in the array you are writing outside the array itself, corrupting other variables. 

    You have to initialize the array to be large enough for future writes to it. 

    Looking at your code it would have to be at least 2*14 + 1 = 29 bytes in size, so if you change the initialization like this it should work: 

    static char confirmed_send_packet[29]="";

    Best regards
    Torbjørn

Children
Related