Putting CPU into sleep mode until button is pressed

Hi there,

I'm currently using the nRF52 DK (PCA10040) and nRF5 SDK 17.1.0.

Our device needs to permanently connected to a battery, only wake up on a button press and then execute the code. Before the button is pressed, nothing needs to be done (no advertising, etc.). I have read a lot about SYSTEM_ON and SYSTEM_OFF and I realized SYSTEM_OFF is not suitable for this application (since it will reset the chip and start execution from scratch).

That is why I tried using sd_app_evt_wait(), but this does not work at all. It seems that sd_app_evt_wait() returns immediately.

Here is my code:

static void permanent_sleep()
{
    ret_code_t err_code;

    // configure Button 4 with SENSE enabled
    nrf_gpio_cfg_sense_input(BSP_BOARD_BUTTON_3, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);

    NRF_LOG_INFO("Going to sleep...");

    // enter sleep mode
    err_code = sd_app_evt_wait();
    APP_ERROR_CHECK(err_code);

    NRF_LOG_INFO("Woke up...");
}

int main(void)
{
    bool erase_bonds;
    uint32_t err_code;

    // Initialize.
    log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();

    // enter sleep mode
    permanent_sleep();
    
    // remaining code should be executed from here
}

Any idea why this is not working? Most examples I have looked at use idle_state_handle() -> pwr_mgmt_run() within the main loop, but this does not work for me because advertising would need to be started before that.

I'm grateful for any hints how to fix this!

Related