rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stm32f7xx_rtc.cpp
Go to the documentation of this file.
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 */
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 |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
49#else
50 // No LSE Bypass.
51 RCC->BDCR |= STM32_LSEDRV | 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
void hal_lld_rtc_fixup(void)