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

Program sort of freezes or doesn't after a while when I change something completely unrelated (or so I think)

Hi everyone,

I have a problem I can't sort out. My program runs fine indefinitely if I set one variable in a function at one point but if I don't do that, the program, or at least the debugging stops after a while. The program sort of continues (I can send a few messages from my app to it) but after a while it just doesn't seem to respond.

So.

I added a debug print of "." in the main loop which has a 500ms delay in it. These are some startup messages I get when the program runs fine. The dots just keeps going.

app_timer: RTC: initialized!
 app: Erasing flash page
 app: --> Event received: erased 1 page from address 0x7F000.
 app: Converting a program to a buffer
 app: --> Event received: wrote 112 bytes at address 0x7F000.
 app: Converting a buffer to a program
 app: Accelerometers initialized. 25
 app: Accelerometers initialized. 24
 app: State --> NOT_CONNECTED
 app: end
..................................................................................................................

The dots just keeps going and going.

BUT! If I don't make that function call and set that variable the debugging (and parts of the program) stops after exactly the same amount of time every time:
 app_timer: RTC: initialized!
 app: Erasing flash page
 app: --> Event received: erased 1 page from address 0x7F000.
 app: Converting a program to a buffer
 app: --> Event received: wrote 112 bytes at address 0x7F000.
 app: Converting a buffer to a program
 app: Accelerometers initialized. 25
 app: Accelerometers initialized. 24
 app: State --> NOT_CONNECTED
 app: end
....................

Compare the number of dots in the two examples.

The weird thing now is that the difference is in the beginning of main.c. If I make a function call to a function located in an included file called:

set_potentiometer_resistance(&m_twi_1, DIGITAL_POT_I2C_ADDRESS_MAX5434, 25000);

In that function I only do this:

ret_code_t set_potentiometer_resistance(nrf_drv_twi_t const * p_instance, uint8_t address, int32_t resistance) {
    actual_potentiometer_resistance = resistance;
    ret_code_t err_code;
//    uint8_t payload[2];
//    payload[0] = DIGITAL_POT_MAX5434_COMMAND_VREG;
//    payload[1] = get_potentiometer_tap_position_from_resistance(resistance);
//    
//    err_code = nrf_drv_twi_tx(p_instance, address, &payload, sizeof(payload), false);
    return err_code;
}

The actual_potentiometer_resistance variable is placed in the header file of the potentiometer function.
static int32_t actual_potentiometer_resistance;
 

I can set it to zero, it works just as well. But if I try to set the value of that variable instead of calling the function, like this:

actual_potentiometer_resistance = 0;
instead of:
set_potentiometer_resistance(&m_twi_1, DIGITAL_POT_I2C_ADDRESS_MAX5434, 25000);
It doesn't work. It stops after the same amount of time.


I can't understund this behavior. It feels like I'm changing something totally unrelated. When I've had similar issues earlier, if the program stops after a while or the debugging stops after a while, all of those problems have been due to me not freeing a malloc:ed variable or so. But what could cause this?

Thanks in advance,
Samuel

  • Hi again Karl,

    I followed your example and replaced all my static variables from my header files with externs and defined them in the source files instead. I remember looking at some of the Nordic library files before starting this project, trying to follow the same pattern but apparently got it all confused. I see now that many source files have static variables but no header files so I got it wrong. Slight smile

    Anyway, the problem went away after doing this so many, many thanks! I still can't say exactly what was going on but I don't need to know either. 

    I really appreciate you addressing this although the problem formulation was fuzzy. I've enjoyed working with Nordic chips much thanks to this community!

    One other question: regarding 

    #define SOME_CONSTANT 2

    I see that for example app_button.c and .h both have #defines and sometimes defines are placed in the source file and sometimes in the header file. Where would you put your defines?

    Thanks again Karl!

Related