summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-01-27 13:17:22 +1100
committerDamien George <damien.p.george@gmail.com>2019-01-27 13:21:31 +1100
commitc2886868b9ccbe8580f57135bb29779a5d36c1e8 (patch)
treede07cd1c0d2990a4c8e7cbdd3aa2353beb3509e0
parent42863830be2d19c7dcdf7ccf1fa66168b1bdc13a (diff)
stm32/rtc: Check RTCEN=1 when testing if RTC is already running on boot.
It can be that LSEON and LSERDY are set yet the RTC is not enabled (this can happen for example when coming out of the ST DFU mode on an F405 with the RTC not previously initialised). In such a case the RTC is never started because the code thinks it's already running. This patch fixes this case by always checking if RTCEN is set when booting up (and also testing for a valid RTCSEL value in the case of using an LSE).
-rw-r--r--ports/stm32/rtc.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/ports/stm32/rtc.c b/ports/stm32/rtc.c
index 1999dfb38..250c34bcf 100644
--- a/ports/stm32/rtc.c
+++ b/ports/stm32/rtc.c
@@ -124,7 +124,9 @@ void rtc_init_start(bool force_init) {
rtc_need_init_finalise = false;
if (!force_init) {
- if ((RCC->BDCR & (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) == (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) {
+ uint32_t bdcr = RCC->BDCR;
+ if ((bdcr & (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL | RCC_BDCR_LSEON | RCC_BDCR_LSERDY))
+ == (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL_0 | RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) {
// LSE is enabled & ready --> no need to (re-)init RTC
// remove Backup Domain write protection
HAL_PWR_EnableBkUpAccess();
@@ -133,7 +135,8 @@ void rtc_init_start(bool force_init) {
// provide some status information
rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
return;
- } else if ((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) {
+ } else if ((bdcr & (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL))
+ == (RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL_1)) {
// LSI configured as the RTC clock source --> no need to (re-)init RTC
// remove Backup Domain write protection
HAL_PWR_EnableBkUpAccess();