diff --git a/src/rp2_common/pico_sleep/sleep.c b/src/rp2_common/pico_sleep/sleep.c index 8de37fb..4b31b2b 100644 --- a/src/rp2_common/pico_sleep/sleep.c +++ b/src/rp2_common/pico_sleep/sleep.c @@ -107,18 +107,27 @@ void sleep_goto_sleep_until(datetime_t *t, rtc_callback_t callback) { // We should have already called the sleep_run_from_dormant_source function assert(dormant_source_valid(_dormant_source)); + // Save these register states to be able to restore afer waking up: + uint clock0_orig = clocks_hw->sleep_en0; + uint clock1_orig = clocks_hw->sleep_en1; + uint save = scb_hw->scr; + // Turn off all clocks when in sleep mode except for RTC clocks_hw->sleep_en0 = CLOCKS_SLEEP_EN0_CLK_RTC_RTC_BITS; clocks_hw->sleep_en1 = 0x0; rtc_set_alarm(t, callback); - uint save = scb_hw->scr; // Enable deep sleep at the proc scb_hw->scr = save | M0PLUS_SCR_SLEEPDEEP_BITS; // Go to sleep __wfi(); + + // If these registers are not restored, any __wfi() will take the proc to deep sleep! (Any sleep_ms() for example.) + clocks_hw->sleep_en0 = clock0_orig; + clocks_hw->sleep_en1 = clock1_orig; + scb_hw->scr = save; } static void _go_dormant(void) {