From 1663fa768a1e2de532cd5eb171da63aabeb4c00f Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Tue, 14 Jun 2022 23:19:23 +0200 Subject: [PATCH] timeout during lse oscillator initialization --- system/src/cmsis/stm32l4xx/system_stm32l4xx.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/system/src/cmsis/stm32l4xx/system_stm32l4xx.c b/system/src/cmsis/stm32l4xx/system_stm32l4xx.c index 4e887c7..3ba07e4 100644 --- a/system/src/cmsis/stm32l4xx/system_stm32l4xx.c +++ b/system/src/cmsis/stm32l4xx/system_stm32l4xx.c @@ -115,7 +115,7 @@ * @{ */ -#define SYSTEM_CLOCK_RTC_CLOCK_TIMEOUT 0xFFFF +#define SYSTEM_CLOCK_RTC_CLOCK_TIMEOUT 0x3FFFF #if !defined (HSE_VALUE) @@ -426,6 +426,11 @@ int system_clock_configure_l4(void) } void system_clock_start_rtc_l4(void) { + + if ((RCC->BDCR & RCC_BDCR_LSERDY) == 0) { + return; + } + // starting RTC RCC->BDCR |= RCC_BDCR_RTCEN; @@ -482,6 +487,9 @@ int system_clock_configure_rtc_l4(void) { timeout_counter++; } + // reset timeout timer + timeout_counter = 0; + // but clear reset flag before RCC->BDCR &= (0xFFFFFFFF ^ RCC_BDCR_BDRST); @@ -495,7 +503,13 @@ int system_clock_configure_rtc_l4(void) { RCC->BDCR |= RCC_BDCR_LSEON; // wait for LSE to start - while((RCC->BDCR & RCC_BDCR_LSERDY) == 0); + while((RCC->BDCR & RCC_BDCR_LSERDY) == 0) { + if (timeout_counter++ > SYSTEM_CLOCK_RTC_CLOCK_TIMEOUT) { + retval = -1; + + break; + } + } } // starting and configuring the RTC itself