Hi, I'm trying to implement a sleep mechanism in my app. I based my code on the Power Management Example.
I use a button to put the app to sleep and the same button to wake it up. I want to also be able to put the app to sleep after a timer expires and to be able to wake it up with the same button.
However this does not work. After going to sleep (initiating the sleep function from the timer handler) the app does not want to wake up after button push. It works when the sleep is induced by the button press.
How can this be done? What am I doing wrong?
My code below:
// initiation
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.frequency = NRF_TIMER_FREQ_31250Hz;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
timer_cfg.mode = NRF_TIMER_MODE_TIMER;
err_code = nrf_drv_timer_init(&SLEEP_TIMER, &timer_cfg, SLEEP_TIMER_event_handler);
APP_ERROR_CHECK(err_code);
err_code = bsp_event_to_button_action_assign(BUTTON_ID, BSP_BUTTON_ACTION_RELEASE, BSP_EVENT_SLEEP);
APP_ERROR_CHECK(err_code);
...
void manual_sleep(void)
{
uint32_t err_code;
NRF_LOG_INFO("going to sleep...");
err_code = bsp_buttons_disable();
// Suppress NRF_ERROR_NOT_SUPPORTED return code.
UNUSED_VARIABLE(err_code);
err_code = bsp_wakeup_button_enable(BUTTON_ID);
// Suppress NRF_ERROR_NOT_SUPPORTED return code.
UNUSED_VARIABLE(err_code);
bsp_board_leds_off();
bsp_board_led_on(L2R);
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
}
static void bsp_evt_handler(bsp_event_t evt)
{
#if NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED
nrf_pwr_mgmt_feed();
NRF_LOG_INFO("Power management fed");
#endif // NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED
switch (evt)
{
case BSP_EVENT_SLEEP:
manual_sleep();
default:
break;
}
}
...
// this code is where the app goes idle
nrf_drv_timer_extended_compare(&SLEEP_TIMER, NRF_TIMER_CC_CHANNEL2, SLEEP_TIMEOUT, NRF_TIMER_SHORT_COMPARE2_STOP_MASK, true);
nrf_drv_timer_clear(&SLEEP_TIMER);
nrf_drv_timer_enable(&SLEEP_TIMER);
...
// this code is where the app stops being idle
nrf_drv_timer_disable(&SLEEP_TIMER);
nrf_drv_timer_clear(&SLEEP_TIMER);