diff options
author | Damien George <damien.p.george@gmail.com> | 2015-03-22 21:52:20 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-03-22 21:52:20 +0000 |
commit | 8657342973b8c92f88ba0ac3913c721e521b83c0 (patch) | |
tree | 1d13c0188c88818c29f1a6bc53b020f98e38cc93 | |
parent | e38b892144cd4ca8538c6b8eb1a6fa98c41b46db (diff) | |
download | micropython-8657342973b8c92f88ba0ac3913c721e521b83c0.tar.gz micropython-8657342973b8c92f88ba0ac3913c721e521b83c0.zip |
stmhal: Correctly clear wake-up flag before entering standby mode.
-rw-r--r-- | stmhal/modpyb.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index 2e4642fad9..2c5c19903e 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -453,7 +453,35 @@ MP_DEFINE_CONST_FUN_OBJ_0(pyb_stop_obj, pyb_stop); /// \function standby() STATIC mp_obj_t pyb_standby(void) { + // We need to clear the PWR wake-up-flag before entering standby, since + // the flag may have been set by a previous wake-up event. Furthermore, + // we need to disable the wake-up sources while clearing this flag, so + // that if a source is active it does actually wake the device. + // See section 5.3.7 of RM0090. + + // Note: we only support RTC ALRA, ALRB, WUT and TS. + // TODO support TAMP and WKUP (PA0 external pin). + uint32_t irq_bits = RTC_CR_ALRAIE | RTC_CR_ALRBIE | RTC_CR_WUTIE | RTC_CR_TSIE; + + // save RTC interrupts + uint32_t save_irq_bits = RTC->CR & irq_bits; + + // disable RTC interrupts + RTC->CR &= ~irq_bits; + + // clear RTC wake-up flags + RTC->ISR &= ~(RTC_ISR_ALRAF | RTC_ISR_ALRBF | RTC_ISR_WUTF | RTC_ISR_TSF); + + // clear global wake-up flag + PWR->CR |= PWR_CR_CWUF; + + // enable previously-enabled RTC interrupts + RTC->CR |= save_irq_bits; + + // enter standby mode HAL_PWR_EnterSTANDBYMode(); + // we never return; MCU is reset on exit from standby + return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby); |