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

Indicating advertisement with a timer

I have a custom design buildt on ble_app_uart peripheral example from SDK 12.2.0. I use a timer that I start from case BLE_ADV_FAST in on_adv_evt to send a message to a display instead of the bsp led indicator (i use no leds to save power)

This is my on_adv_evt function:

static void on_adv_evt(ble_adv_evt_t ble_adv_evt){
   uint32_t err_code;
   switch (ble_adv_evt)
   {
       case BLE_ADV_EVT_FAST:
           dispClear();
           err_code = app_timer_start(m_advertisement_indicator, APP_TIMER_TICKS(ADVERTISEMENT_INDICATOR, APP_TIMER_PRESCALER), NULL);
           APP_ERROR_CHECK(err_code);
           display_print(STATUS,DEVICE_NAME,12);
           // Grab a sample from ADC 
           saadc_init();
           nrf_drv_saadc_sample();
           break;
       case BLE_ADV_EVT_IDLE:
           sleep_mode_enter();
           break;
       default:
           break;
   }
}

It works well when booting the application, but if I disconnect, the code in the BLE_ADV_FAST case are executed (text in display is updated), but for some reason the code in the timer is not called within the time given, and it seems to be different length of time, every time it fires from a disconnect.

It is strange that the other commands; displClear(); display_print(...) and adc init/sample is called and executed but the timer is not started..

Any idea what might cause this? Perhaps I should go for a different approach?

Edit: updated question with regards to Hung Bui's comment

  • Hi Erltot,

    Which example and SDK are you testing on ?

    Note that when we disconnected from the central, we may start with ADV_MODE_DIRECTED instead of BLE_ADV_MODE_FAST. That's why you may not receive the timeout from the timer.

  • @Erltot: Please add a break point inside ble_advertising_start() and check which advertising mode is entered after the device disconnect. on_disconnected() will be called after the connection is Disconnected.

  • In the breakpoint I get in ble_advertising_start() is BLE_ADV_MODE_DIRECTED as you suggested.

    I tried adding both (not at the same time)

    case BLE_ADV_MODE_DIRECTED:

    case BLE_ADV_EVT_DIRECTED:

    just above the other case in my on_adv_evt, it didn't do much...

    When I checked the ble_adv_evt in the event when I disconnect it shows BLE_ADV_EVT_FAST.. I don't understand why does the other parts under the case for BLE_ADV_EVT_FAST get executed and not the timer?

  • Seems like app_timer_start() called but the timer is not really started ? Have you set the timer with APP_TIMER_MODE_SINGLE_SHOT mode ? Please attach your full code.

  • I use repeated mode, which i call once before the big while

    err_code = app_timer_create(&m_advertisement_indicator, APP_TIMER_MODE_REPEATED, advertisement_indicator);

    APP_ERROR_CHECK(err_code);

    I start the timer for the first time in on_adv_evt switch shown above, and I stop it in the p_ble_evt switc in the BLE_GAP_EVT_CONNECTED case:

    err_code = app_timer_stop(m_advertisement_indicator);

    APP_ERROR_CHECK(err_code);

    Sorry, I can't attach the full code

    I added a print to RTT in the advertisement_indicator event and noticed it seems to be a rubberband effekt going on, after a while the timer starts but the prints all came simultanously.. Could it be the soft device blocking the event from running, and when it finally runs it executes several times?

Related