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

Variable Not Incrementing with nrf_delay_ms()

I have the follow code in my application for a PCA10028 program from the nRF51 Development Kit (S110 v9.0):

int main(void)
{
    int i = 0;
    while(true)
    {
        SEGGER_RTT_printf(0, "%d\n", i);
        i++;

        nrf_delay_ms(1000);
    }
}

Output:

0
0
0
0

I want the variable "i" to increment 1,2,3... but it looks like during the nrf_delay_ms() the device is resetting my variable to 0 every time.

Is there any way to have this variable persisted without the use of "pstorage.h"?

Parents
  • Thank you for the help, it turns out the culprit was not nrf_delay_ms() as I thought.

    The full version of the code I was running included a call to advertising_start() in the while(true) loop.

    That advertising_start() function looked like this:

    /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        uint32_t err_code;
    
        err_code = sd_ble_gap_adv_start(&m_adv_params);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
        APP_ERROR_CHECK(err_code);
    }
    

    Commenting out the first APP_ERROR_CHECK() call so that it looks like this

    /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        uint32_t err_code;
    
        err_code = sd_ble_gap_adv_start(&m_adv_params);
        //APP_ERROR_CHECK(err_code);
    
        err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
        APP_ERROR_CHECK(err_code);
    }
    

    fixed the issue. Commenting out the other APP_ERROR_CHECK() (or any other line in this method, or line in the main() method) has no effect. The only line that affects my variables being reinitialized is that line I commented out.

    I have no idea why that would be happening, but my application works perfectly now since commenting that out.

    I have both the int i = 0; in the main function and an external bool m_flip = false; that were getting reset by that APP_ERROR_CHECK() call in advertising_start(). I would be very interested to know how that function is affecting the memory for those variables.

Reply
  • Thank you for the help, it turns out the culprit was not nrf_delay_ms() as I thought.

    The full version of the code I was running included a call to advertising_start() in the while(true) loop.

    That advertising_start() function looked like this:

    /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        uint32_t err_code;
    
        err_code = sd_ble_gap_adv_start(&m_adv_params);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
        APP_ERROR_CHECK(err_code);
    }
    

    Commenting out the first APP_ERROR_CHECK() call so that it looks like this

    /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        uint32_t err_code;
    
        err_code = sd_ble_gap_adv_start(&m_adv_params);
        //APP_ERROR_CHECK(err_code);
    
        err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
        APP_ERROR_CHECK(err_code);
    }
    

    fixed the issue. Commenting out the other APP_ERROR_CHECK() (or any other line in this method, or line in the main() method) has no effect. The only line that affects my variables being reinitialized is that line I commented out.

    I have no idea why that would be happening, but my application works perfectly now since commenting that out.

    I have both the int i = 0; in the main function and an external bool m_flip = false; that were getting reset by that APP_ERROR_CHECK() call in advertising_start(). I would be very interested to know how that function is affecting the memory for those variables.

Children
Related