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

Sleep Immediately after Beacon Advertisement

I'm using the following code to make the beacon sleep:

static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t        err_code;
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
        {
            break;
        }
        case BLE_GAP_EVT_TIMEOUT:
        {
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
            {
                sd_ble_gap_adv_stop();
                ble_fsm_set_state(Ble_Fsm_State_Idle);
            }
            break;
        }
        default:
        {
            break;
        }
    }
}

Once the state is set to idle, the nrf51 chip goes to sleep and the current draw drops to the order of uA. My only problem here is that the minimum timeout value is 1s and this means that the chip is burning power for 1s. Ideally, I would like the beacon to just advertise once and then go to sleep immediately. What is the best way to do this? Adding a synchronous delay of 5 ms? Any callback that I can use instead?

Parents
  • What do you mean by "sleep"? I assume that nRF5x chip is going to POWER ON SLEEP (wait for event/interrupt) always when nothing is running on MCU so even between adv. events. If you mean POWER OFF state then sure you can do it but no BLE activity can be running during that time, you need to manage wake-up by some external GPIO interrupt line (button or some RTC capable chip if you expect to wake-up periodically).

Reply
  • What do you mean by "sleep"? I assume that nRF5x chip is going to POWER ON SLEEP (wait for event/interrupt) always when nothing is running on MCU so even between adv. events. If you mean POWER OFF state then sure you can do it but no BLE activity can be running during that time, you need to manage wake-up by some external GPIO interrupt line (button or some RTC capable chip if you expect to wake-up periodically).

Children
  • Hmm.. I guess my main question here is where is the best place to call sd_ble_gap_adv_stop()? If I don't call it, the radio remains ON and the nrf51 sucks a lot of power. Is the best way to just simply start advertising, add a 20 ms delay and then call sd_ble_gap_adv_stop()? Does my comment make sense?

  • I'm afraid that's misunderstanding of how low-power modes and Nordic BLE stack (Soft Devices) work. Normally chip IS in the lowest power mode (SLEEP) all the time between radio events. So if you want to run any kind of time-dependent logic there is usually not much to be done except shutting down all HW peripheral blocks enabled in your custom APP code. If you want to go to POWER OFF (where basically whole chip is turned off and the only wake-up is GPIO or similar event which will cause situation similar to RESET) then simply call sd_power_system_off() (or equivalent with SD disabled) but note that all activities will stop not only advertisement.

  • You probably want to read this ultimate guide how to optimize nRF5x power consumption (written for nRF51 but very much applicable also for nRF52).

  • Actually, what you're saying is exactly what I'm trying to do but I guess my description is awful. The chip is not in the lowest power mode because the radio is on from a sd_ble_gap_adv_start() call and needs to be turned off with a sd_ble_gap_adv_stop() call. So, I guess my question actually translates to how do I know when the advertisement is complete and that I can call sd_ble_gap_adv_stop()? Is there some event that I should watch out for?

  • Not really. Radio as such (the specific part of nRF5x SoC which does 2.4GHz radio Tx/Rx) is most of the time OFF. The call sd_ble_gap_adv_start is just function call from Soft Device (stack) API and you can be sure that stack isn't stupid to power radio all the time, it just runs RTC timer to wake it put for the shortest time necessary. So yes, by turning the advertisement off will safe some power but it will be only very little for these few missing Tx/Rx bursts of radio (typically up to 1.5ms per whole adv. interval, see charts in Soft Device specification) and the power to run RTC timer. If you want to count single adv. events then you have two options:

    (1/3)

Related