Skip to content

Commit 41acdd8

Browse files
committed
stm32/rtc: Make sure RTC is using LSE on N6 MCUs.
Signed-off-by: Damien George <[email protected]>
1 parent 63c94fe commit 41acdd8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

ports/stm32/rtc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,27 @@ void rtc_init_start(bool force_init) {
134134
if (!force_init) {
135135
bool rtc_running = false;
136136
#if defined(STM32N6)
137+
// Note: the low-level boot on the N6 seems to always enable the RTC and the LSI, and
138+
// switch the RTC to LSI mode. So the logic below needs to account for that:
139+
// - if LSE is ready then switch back to the LSE
140+
// - even if LSI is ready, don't use it if the board is configured to use LSE
141+
uint32_t rtc_clock_source = LL_RCC_GetRTCClockSource();
137142
if (LL_RCC_IsEnabledRTC()
138-
&& LL_RCC_GetRTCClockSource() == LL_RCC_RTC_CLKSOURCE_LSE
139143
&& LL_RCC_LSE_IsReady()) {
140144
// LSE is enabled & ready --> no need to (re-)init RTC
141145
rtc_running = true;
146+
if (rtc_clock_source != LL_RCC_RTC_CLKSOURCE_LSE) {
147+
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
148+
}
142149
// remove Backup Domain write protection
143150
HAL_PWR_EnableBkUpAccess();
144151
// Clear source Reset Flag
145152
__HAL_RCC_CLEAR_RESET_FLAGS();
146153
// provide some status information
147154
rtc_info |= 0x40000;
148-
} else if (LL_RCC_IsEnabledRTC()
149-
&& LL_RCC_GetRTCClockSource() == LL_RCC_RTC_CLKSOURCE_LSI) {
155+
} else if (!rtc_use_lse
156+
&& LL_RCC_IsEnabledRTC()
157+
&& rtc_clock_source == LL_RCC_RTC_CLKSOURCE_LSI) {
150158
// LSI configured as the RTC clock source --> no need to (re-)init RTC
151159
rtc_running = true;
152160
// remove Backup Domain write protection

0 commit comments

Comments
 (0)