Skip to content

Commit 3e14709

Browse files
dron0gusrusefillc
authored andcommitted
f4 RTC: fixup
1 parent 094f3bb commit 3e14709

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

firmware/hw_layer/ports/stm32/stm32f4/hw_ports.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ HW_LAYER_PORT = $(PROJECT_DIR)/hw_layer/ports/stm32/stm32f4/stm32f4xx_hal_flash.
22
$(PROJECT_DIR)/hw_layer/ports/stm32/stm32f4/stm32f4xx_hal_flash_ex.c
33

44
HW_LAYER_PORT_CPP += $(PROJECT_DIR)/hw_layer/ports/stm32/stm32f4/mpu_util.cpp \
5-
$(PROJECT_DIR)/hw_layer/ports/stm32/stm32_adc_v2.cpp
5+
$(PROJECT_DIR)/hw_layer/ports/stm32/stm32_adc_v2.cpp \
6+
$(PROJECT_DIR)/hw_layer/ports/stm32/stm32f4/stm32f4xx_rtc.cpp
67

78
MCU = cortex-m4
89
USE_FPU = hard
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @file stm32f7xx_rtc.cpp
3+
* @brief Real Time Clock STM32F7xx switched from LSE to LSI
4+
*
5+
* @date Jan 8, 2025
6+
* @author Andrey Gusakov
7+
*/
8+
9+
#include "pch.h"
10+
11+
#if HAL_USE_RTC
12+
13+
/* switch to LSE clock if ECU previously falledback to LSI clock.
14+
* This will preserve current time and set it back to RTC after reinit
15+
* On STM32 change of RTC source clock can be done only through reset of whole backup domain
16+
* This reset does no affect backup ram */
17+
void hal_lld_rtc_fixup(void)
18+
{
19+
#if (STM32_RTCSEL == STM32_RTCSEL_LSE)
20+
// we need some more time than defined in RUSEFI_STM32_LSE_WAIT_MAX
21+
// this is safe as we check that LSE is runnig before reseting BKP domain (stopping LSE)
22+
// After that we are starting LSE again and waiting for LSERDY.
23+
int timeout = 1000000000;
24+
if ((RCC->BDCR & STM32_RTCSEL_MASK) == STM32_RTCSEL) {
25+
// Backup domain is already driven by expected clock
26+
return;
27+
}
28+
if ((RCC->BDCR & RCC_BDCR_LSERDY) == 0) {
29+
// LSE is failed to start
30+
efiPrintf("LSE in not ready");
31+
return;
32+
}
33+
34+
efiPrintf("Switching RTC to LSE clock");
35+
36+
// Get current time
37+
RTCDateTime timespec;
38+
rtcGetTime(&RTCD1, &timespec);
39+
40+
// Reset BKP domain
41+
// The BKPSRAM is not affected by this reset
42+
// This will also reset LSEON
43+
RCC->BDCR |= RCC_BDCR_BDRST;
44+
RCC->BDCR &= ~RCC_BDCR_BDRST;
45+
46+
#if defined(STM32_LSE_BYPASS)
47+
// LSE Bypass.
48+
RCC->BDCR |= RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
49+
#else
50+
// No LSE Bypass.
51+
RCC->BDCR |= RCC_BDCR_LSEON;
52+
#endif
53+
// Waits until LSE is stable or times out.
54+
while (((RCC->BDCR & RCC_BDCR_LSERDY) == 0) && (timeout--)) {
55+
//this is executed when RTOS is not ready
56+
//chThdSleepMilliseconds(1);
57+
}
58+
59+
// Lets check again
60+
if (RCC->BDCR & RCC_BDCR_LSERDY) {
61+
RCC->BDCR |= STM32_RTCSEL;
62+
} else {
63+
// LSE is failed to start
64+
efiPrintf("LSE in not ready after restart attemp");
65+
// Keep initing
66+
RCC->BDCR |= RUSEFI_STM32_LSE_WAIT_MAX_RTCSEL;
67+
}
68+
69+
/* RTC clock enabled.*/
70+
RCC->BDCR |= RCC_BDCR_RTCEN;
71+
72+
// init RTC again
73+
rtcInit();
74+
// Set previously saved time
75+
rtcSetTime(&RTCD1, &timespec);
76+
#endif
77+
}
78+
79+
#endif //HAL_USE_RTC

0 commit comments

Comments
 (0)