2025-01-21 04:29 AM
I have an application running on STM32G491 which uses the RTC. There is an external 32768Hz crystal and a backup coin cell battery (I have checked that it is not depleted: 3.12V). There are two issues:
Scenario #1: I pull the main battery, wait a bit, re-insert it, and restart the firmware (I hold a power button until a power latch is toggled on by the firmware). The RTC loses the time and resets to zero roughly half the time. The RTC initialisation code calls HAL_RCCEx_PeriphCLKConfig() and then HAL_RTC_Init(). I periodically call HAL_RTC_GetTime() followed by HAL_RTC_GetDate() while the application is running.
Scenario #2: I don't pull the main battery but power down the device a bit more gracefully (I hold the power button until the power latch is toggled off). Then I restart the firmware as above. In this case it the RTC does not lose the time but it seems that the RTC does not increment during the powered-off period.
SystemClock_Config() enables the LSE and the RTC uses this as its clock source. However, I get the same issues if I switch to the LSI.
What could cause these issues?
Thanks.
2025-02-24 02:51 AM - edited 2025-02-24 02:51 AM
Hello @unicyclebloke,
For scenario1, this is probably related to the RTC initialization sequence, the sequence should be similar to:
HAL_PWR_EnableBkUpAccess(); // Enable access to the backup domain
__HAL_RCC_BKP_CLK_ENABLE(); // Enable the backup domain clock
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); // Configure the RTC clock source
HAL_RTC_Init(&RtcHandle); // Initialize the RTC
The backup domain should remain powered by the VBAT pin when the main battery is removed, try verifying the backup registers are not reset!
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.