From e4f59a00201b31903b11a6d30dddedc1457ed2ff Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 4 Feb 2022 08:30:39 +1100 Subject: [PATCH] stm32/rtc: Use LL_RTC functions to simplify some MCU-specific code. This also fixes a possible race condition when exiting initialisation mode: reading then writing to ISR (via ISR &= ~RTC_ISR_INIT) will clear any flags that were set by the hardware between the read and the write. The correct way to clear just the INIT bit is to just do a single write via ISR = ~RTC_ISR_INIT, which will not clear any other flags (they must be written to 0 to clear), and that is exactly what LL_RTC_DisableInitMode does. Signed-off-by: Damien George --- ports/stm32/rtc.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c index 2d6459e239..1164e71e08 100644 --- a/ports/stm32/rtc.c +++ b/ports/stm32/rtc.c @@ -339,11 +339,7 @@ STATIC HAL_StatusTypeDef PYB_RTC_Init(RTC_HandleTypeDef *hrtc) { hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16); // Exit Initialization mode - #if defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H7B3xx) || defined(STM32H7B3xxQ) - hrtc->Instance->ICSR &= (uint32_t) ~RTC_ICSR_INIT; - #else - hrtc->Instance->ISR &= (uint32_t) ~RTC_ISR_INIT; - #endif + LL_RTC_DisableInitMode(hrtc->Instance); #if defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H7B3xx) || defined(STM32H7B3xxQ) // do nothing @@ -702,13 +698,8 @@ mp_obj_t pyb_rtc_wakeup(size_t n_args, const mp_obj_t *args) { RTC->CR &= ~RTC_CR_WUTE; // wait until WUTWF is set - #if defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H7B3xx) || defined(STM32H7B3xxQ) - while (!(RTC->ICSR & RTC_ICSR_WUTWF)) { + while (!LL_RTC_IsActiveFlag_WUTW(RTC)) { } - #else - while (!(RTC->ISR & RTC_ISR_WUTWF)) { - } - #endif if (enable) { // program WUT