From 3b18ccb1f41b7109e79a4f969c028d37c9c32c16 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Sat, 14 Jun 2025 16:18:15 -0400 Subject: [PATCH] sam d21: incorporate insights from Adafruit's Sleepydog library --- chips/samd21/system_samd21.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/chips/samd21/system_samd21.c b/chips/samd21/system_samd21.c index ffd15a9..15e9731 100644 --- a/chips/samd21/system_samd21.c +++ b/chips/samd21/system_samd21.c @@ -211,6 +211,20 @@ bool set_cpu_frequency(uint32_t freq) { } void _enter_standby_mode() { + // Don't fully power down flash when in sleep + NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; + + // Set deep sleep mode SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + + // Insight from Adafruit Sleepydog library: due to a hardware bug on the SAMD21, + // the SysTick interrupts become active before the flash has powered up from + // sleep, causing a hard fault. To prevent this the SysTick interrupts are disabled + // before entering sleep mode. + SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk; // Disable SysTick interrupts + + __DSB(); __WFI(); + + SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable SysTick interrupts }