Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

app_timer stops when BLE advertising times out

Hi,

I work on the nRF52840 DK  with the ble_app_uart example and I want to add a timer using app_timer api,

in order to create RTC time/date timestamps for my sensors raw data.

 As a first step, I followed the application timer tutorial and I created a simple repeated timer that blinks an LED every 100 mSec .

In the following code I pasted only the main() function and the timer functions I wrote. Everything else is the same with the ble_uart example

static void timer_handler(void * p_context)
{
  bsp_board_led_invert(BSP_BOARD_LED_3);
}

void timerCreator(void){
  ret_code_t err_timer;
  
  APP_TIMER_DEF(m_timer);

  err_timer = app_timer_create(&m_timer, APP_TIMER_MODE_REPEATED, timer_handler);
  APP_ERROR_CHECK(err_timer);

  err_timer = app_timer_start(m_timer,APP_TIMER_TICKS(100),NULL);
  APP_ERROR_CHECK(err_timer);
}
//========================================================================================================

/**@brief Application main function.
 */

int main(void)
{
    // Initialize.
    bool erase_bonds;
    gpiote_init();
    uart_init();
    log_init();
    timers_init();
    
    timerCreator();                   //<<<=============  Create RTC Timer
    
    buttons_leds_init(&erase_bonds);
    power_management_init();
 
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    advertising_start();
    
    // Start execution.
    printf("\nUART started.\r\n");
    NRF_LOG_INFO("Debug logging for UART over RTT started.");
    

    uint16_t length = 0;
    
    while(1)
    {
       idle_state_handle();
    }
}

I noticed that the LED blinks as expected , but only for the time the device is advertising( 180 seconds).

When the device stops advertising due to timeout , my  repeated timer also stops and the LED stays off. Why is that happening?

When I connect my smartphone to the device and no timeout occurs, the repeated timer will work continuously just the way I want.

 I assume that when advertising timeout occurs,  the app timer is disabled somehow.

I tried to fix it by setting the flag APP_TIMER_KEEPS_RTC_ACTIVE to 1, but with no success.

I also tried to read the  elapsed_ticks_acquire() but it always returns 0.

How can I use the app timer  to create a timer that works independently without  it being disabled after advertising time-out or interrupted by BLE in general ?

Thank you for your time.

Related