From f0107683d51da52ba4477bb652aa0fb64ee50a2b Mon Sep 17 00:00:00 2001 From: lpxiao Date: Wed, 5 Jul 2023 22:17:53 +0800 Subject: [PATCH] =?UTF-8?q?1.arch/arm/src/stm32/stm32=5Frtcounter.c=C2=A0?= =?UTF-8?q?=C2=A0=20up=5Frtc=5Finitialize=20Possible=20stall=202.arch/arm/?= =?UTF-8?q?src/stm32/stm32=5Frtcounter.c=20up=5Frtc=5Fsettime=20memory=20h?= =?UTF-8?q?ardfault?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arch/arm/src/stm32/stm32_rtcounter.c | 71 +++++++++++++++++----------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/arch/arm/src/stm32/stm32_rtcounter.c b/arch/arm/src/stm32/stm32_rtcounter.c index 3a3d54600ba..4fddcc5ec4f 100644 --- a/arch/arm/src/stm32/stm32_rtcounter.c +++ b/arch/arm/src/stm32/stm32_rtcounter.c @@ -171,6 +171,30 @@ volatile bool g_rtc_enabled = false; * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: stm32_rtc_waitlasttask + * + * Description: + * wait task done + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static inline void stm32_rtc_waitlasttask(void) +{ + /* Previous write is done? */ + + while ((getreg16(STM32_RTC_CRL) & RTC_CRL_RTOFF) == 0) + { + stm32_waste(); + } +} + /**************************************************************************** * Name: stm32_rtc_beginwr * @@ -187,12 +211,7 @@ volatile bool g_rtc_enabled = false; static inline void stm32_rtc_beginwr(void) { - /* Previous write is done? */ - - while ((getreg16(STM32_RTC_CRL) & RTC_CRL_RTOFF) == 0) - { - stm32_waste(); - } + stm32_rtc_waitlasttask(); /* Enter Config mode, Set Value and Exit */ @@ -216,13 +235,7 @@ static inline void stm32_rtc_beginwr(void) static inline void stm32_rtc_endwr(void) { modifyreg16(STM32_RTC_CRL, RTC_CRL_CNF, 0); - - /* Wait for the write to actually reach RTC registers */ - - while ((getreg16(STM32_RTC_CRL) & RTC_CRL_RTOFF) == 0) - { - stm32_waste(); - } + stm32_rtc_waitlasttask(); } /**************************************************************************** @@ -384,6 +397,15 @@ int up_rtc_initialize(void) putreg16(RTC_MAGIC, RTC_MAGIC_REG); } + modifyreg16(STM32_RCC_BDCR, 0, RCC_BDCR_LSEON); + + /* Wait for the LSE clock to be ready */ + + while ((getreg16(STM32_RCC_BDCR) & RCC_BDCR_LSERDY) == 0) + { + stm32_waste(); + } + /* Select the lower power external 32,768Hz (Low-Speed External, LSE) * oscillator as RTC Clock Source and enable the Clock. */ @@ -393,12 +415,10 @@ int up_rtc_initialize(void) /* Enable RTC and wait for RSF */ modifyreg16(STM32_RCC_BDCR, 0, RCC_BDCR_RTCEN); - - /* TODO: Possible stall? - * should we set the timeout period? and return with -1 - */ + stm32_rtc_waitlasttask(); stm32_rtc_wait4rsf(); + stm32_rtc_waitlasttask(); /* Configure prescaler, note that these are write-only registers */ @@ -408,6 +428,7 @@ int up_rtc_initialize(void) stm32_rtc_endwr(); stm32_rtc_wait4rsf(); + stm32_rtc_waitlasttask(); #ifdef CONFIG_RTC_HIRES /* Enable overflow interrupt - alarm interrupt is enabled in @@ -631,7 +652,6 @@ int up_rtc_settime(const struct timespec *tp) { struct rtc_regvals_s regvals; irqstate_t flags; - uint16_t cntl; /* Break out the time values */ @@ -646,16 +666,11 @@ int up_rtc_settime(const struct timespec *tp) * register (hi-res mode only) */ - do - { - stm32_rtc_beginwr(); - putreg16(RTC_MAGIC, RTC_MAGIC_TIME_SET); - putreg16(regvals.cnth, STM32_RTC_CNTH); - putreg16(regvals.cntl, STM32_RTC_CNTL); - cntl = getreg16(STM32_RTC_CNTL); - stm32_rtc_endwr(); - } - while (cntl != regvals.cntl); + stm32_rtc_beginwr(); + putreg16(regvals.cnth, STM32_RTC_CNTH); + putreg16(regvals.cntl, STM32_RTC_CNTL); + stm32_rtc_endwr(); + putreg16(RTC_MAGIC_TIME_SET, RTC_MAGIC_REG); #ifdef CONFIG_RTC_HIRES putreg16(regvals.ovf, RTC_TIMEMSB_REG);