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

understanding app_timer functionality

I am using app_timer to measure the time taken to execute a function. I am using ble_app_template code. As mentioned in the documentation, app_timer needs to initialize the LFCLK. It doesn't need to initialize a LFCLK if softdevice is used. But in the template, timer is created even before initialization of LFCLK. LFCLK is initialized in the ble_stack_init() which is after the timers_init() function.

  1. Why is the timer working even without initializing LFCLK ?

    Source code is present here : github.com/.../silver-umbrella

    These are the code snippets :

     int main(void)
     {
         uint32_t err_code;
         bool     erase_bonds;
     
         err_code = NRF_LOG_INIT(NULL);
         APP_ERROR_CHECK(err_code);
     
     	timers_init();
         buttons_leds_init(&erase_bonds);
         ble_stack_init();
     	timeslot_sd_init();
         peer_manager_init(erase_bonds);
         if (erase_bonds == true)
         {
             NRF_LOG_INFO("Bonds erased!\r\n");
         }
         gap_params_init();
         advertising_init();
     		
         application_timers_start();
         err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
         APP_ERROR_CHECK(err_code);
     
         // This is my function
     	cal_RPA_gen_time();
     		
         for (;;)
         {
             if (NRF_LOG_PROCESS() == false)
             {
                 power_manage();
             }
         }
     }
    
     static void application_timers_start(void)
     {
         uint32_t err_code;
         //starting timer for 100 secs
         err_code = app_timer_start(m_app_timer_id, APP_TIMER_TICKS(100000, APP_TIMER_PRESCALER), NULL);
         APP_ERROR_CHECK(err_code);
     	SEGGER_RTT_printf(0, "Timer Started\n\n");
     }
    
  2. If I call the function timers_init() after ble_stack_init(), timer is not started (no output on RTT viewer). Why ?

  3. If I decrease the advertising interval to less than 20 ms i.e. min advertising interval, I get this kind of output for infinite time :

    0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created 0> Timer Created

    Is there any relation between advertising module and RTC1 ?

  4. Also, when I try to set the gap address when timer is running, I am getting undesired output on RTT viewer. It seems like the timer get restarted when a new address is set. Why ?

This is the desired output :

image description

This is the ouput I am getting :

image description

Thanks in advance.

  • Hi,

    1. The app_timer library will not check if the LFCLK is running. You will be able to initialize and create a timer without having started the LFCLK. You are also able to call the start and stop functions also, but the RTC will not start counting as it has no clock source running.

    2. If you call timers_init() after buttons_leds_init(..), then bsp_init(..) inside buttons_leds_init(..) will return NRF_ERROR_INVALID_STATE as app_timer module is not initialized. Since you have not defined DEBUG globally, your application will restart when calling APP_ERROR_CHECK(err_code). See here for more info about why your application is resetting.

    3. The minimum advertising interval is 20ms (see BLE_GAP_ADV_INTERVAL_MIN and here), so you will get an error (and reset) if you try to set it lower than that.

    4. This output also indicates that you reset. Probably you get an error when you try to set the address. Define DEBUG in your preprocessor symbols, go into debug and see where the error come from and which error code it is (here is a list of SoftDevice global error codes). You can put a breakpoint in the while loop in app_error_save_and_stop(..) in app_error.c to get this information (after the code hits the breakpoint).

    Ole

  • Regarding 4th point, I tried to debug and I am getting error code : 0x00003202 I get this error while setting the RPA : sd_ble_gap_address_set() I mentioned in this post, how I am setting the RPA : setting RPA

  • Error code 0x3202 is BLE_ERROR_GAP_INVALID_BLE_ADDR (NRF_GAP_ERR_BASE + 0x0002 = NRF_ERROR_STK_BASE_NUM + 0x0200 + 0x002 = 0x3000 + 0x0200 + 0x0002). And as the description says: "The upper two bits of the address do not correspond to the specified address type."

Related