STM32 RTC freeze when power is off
I want to use internal RTC Calendar with internal Clock(LSI).
It's not going to work. From the Reference Manual
4.1.3 Battery backup domain
Backup domain description
To retain the content of the RTC backup registers, backup SRAM, and supply the RTC when VDD is turned off, VBAT pin can be connected to an optional standby voltage supplied by a battery or by another source. To allow the RTC to operate even when the main digital supply (VDD) is turned off, the VBAT pin powers the following blocks:
- The RTC
- The LSE oscillator
- The backup SRAM when the low-power backup regulator is enabled
- PC13 to PC15 I/Os, plus PI8 I/O (when available)
The switch to the VBAT supply is controlled by the power-down reset embedded in the Reset block.
Unfortunately the LSI clock is not on the list, that's why it and the RTC stops when VDD is gone.
I want to use internal RTC Calendar with internal Clock(LSI).
I think that's why it doesn't work. According to the AN4759 Using hardware real-time clock (RTC) in low-power modes with STM32 microcontrollers, page 36/51 we can read the following statement :
Using the right power reduction mode
Depending on the application constraints, such as the maximum or average current consumption, the frequency of wake-ups, or alternatively the maximum wake-up time, several low-power modes can be used. The RTC peripheral can be active in the following low-power modes:
- Sleep mode
- Low-power Run mode
- Low-power Sleep mode
- Stop mode if the RTC clock is provided by LSE or LSI(a)
- Standby mode if the RTC clock is provided by LSE or LSI
- Shutdown mode if the RTC clock is provided by LSE(b)
Since you are using the LSI, the lowest power mode achievable for your RTC peripheral is the Standby mode. The shutdown mode is reached when you power off your system (even if you have VBAT connected), which is your case.
The fact that VBAT is connected allows you to keep the RTC value before shutting down the main power supply but here, the fact that you are using the LSI power off the RTC peripheral, that's why its value doesn't change.
If you want to achieve what you want (having the RTC working even when the main power supply is off BUT with VBAT connected), you have to use the LSE.