Skip to main content
DavePfz
Associate III
July 22, 2026
Solved

Problem with STM32WB5MMG calendar on battery

  • July 22, 2026
  • 2 replies
  • 28 views

I have a design with a CR2032 cell connected to VBAT on the STM32WB5MM module. This was set up using CubeMX and there I enabled the LSE.

In my code I am able to set the date and time and observe that it increments as expected. However, as soon I power the circuit down and back up the RTC is reset to the default. I have verified that VBAT is 3.1V.

I have not yet tried the backup registers.

What am I missing?

Best answer by Andrew Neil

So does your code start by setting the RTC to default?

If it does, you’ll need to stop that ...

2 replies

Andrew Neil
Andrew NeilBest answer
Super User
July 22, 2026

So does your code start by setting the RTC to default?

If it does, you’ll need to stop that ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
DavePfz
DavePfzAuthor
Associate III
July 22, 2026

Good point. In searching the code, I found that CubeMX includes that code by default and not between “USER CODE” markers. I simply put a return at the appropriate spot and it works.

It would be nice if CubeMX gave an option to not initialize the RTC.

Thanks, Dave

mƎALLEm
ST Technical Moderator
July 22, 2026

You need to initialize the RTC but not the RTC calendar: hour and date. / two different things.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
mƎALLEm
ST Technical Moderator
July 22, 2026

Hello,

As said by ​@Andrew Neil , most probably you are automatically doing the initialization of the hour and the date at the beginning of the application by setting them to 0.

This is how you can implement it (inspired from a STM32F4 example): 

Set a flag (water mark value: select your own value) in the buck up register to tell you have already configured the RTC calendar:

static void RTC_CalendarConfig(void)
{
/* RTC calendar config here */
.
.
.

/*##-3- Writes a data in a RTC Backup data Register0 #######################*/
HAL_RTCEx_BKUPWrite(&RtcHandle,RTC_BKP_DR0,0x32F2);
}

In the startup in main, you need to check if the that water mark value is written in the bkp register:

  /*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
/* Read the Back Up Register 0 Data */
if(HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) != 0x32F2)
{
/* Configure RTC Calendar */
RTC_CalendarConfig();
}

If not written configure the RTC calendar.

Hope that helps.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.