Skip to main content
DavePfz
Senior
October 16, 2026
Solved

Problem loading STM32WB5MM RTC prescalers

I have used STM32CubeMX to change the prescalers in the STM32_WPAN section. However, the code itself never uses the new values. It appears that “something” has marked the RTC as already initialized and it won’t execute that code.

How can I force my code to load the new values?

Nite: The values that are being loaded came from when I enabled the BLE code, but it causes the RTC to run too slow.

DavePfz
DavePfzBest answer
Senior
October 19, 2026

After reviewing the couple of replies along with the design, it occurred to me that this design is slightly different from the many eval boards available. Specifically, this design implements the VBAT with a Lithium coin cell. As a result whenever, the program (re)starts, the previous value for RTC_FLAG_INITS has been retained and the hardware is working as designed.

I was looking for a software solution, but as is mentioned in several places, whenever the write protection is removed the clock would stop. In this case, stopping for a few instruction cycles would not have a significant effect. However, once the pre-scalers have been set, there is no reason to reload them.

So, to let the system work as intended, the solution is to: Load the new code. Remove all power including the backup cell. Allow the hardware to fully discharge. On power up the software will ‘see’ that the flag is not set and it will initialize with the new values and set the flag. Once that is complete, as long as the coin cell remains in place, the code will not repeat.

The hardware and software are working as intended!

4 replies

Andrew Neil
Super User
October 17, 2026

I have used STM32CubeMX to change the prescalers in the STM32_WPAN section. However, the code itself never uses the new values

Not sure what you mean by that?

If you look at the generated initialisation code, does it contain the values that you entered?

If not, see:

 

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
Senior
October 17, 2026

The new values are correctly defined in app_conf.h and loaded into the hrtc structure in rtc.c. and normally the values are loaded into the registers in HAL_RTC_Init(). But when I followed the code, when it got to the line:

if (__HAL_RTC_IS_CALENDAR_INITIALIZED(hrtc) == 0U)

That test fails so the code immediately returns. I was looking for a way to cause the test to fail, but it eludes me.

mbarg.1
Senior III
October 19, 2026

From STM code:

/**

* @brief Check whether the RTC Calendar is initialized.

* @param __HANDLE__ specifies the RTC handle.

* @retval None

*/

#if defined(TAMP)

#define __HAL_RTC_IS_CALENDAR_INITIALIZED(__HANDLE__) (((((__HANDLE__)->Instance->ICSR) & (RTC_FLAG_INITS)) == RTC_FLAG_INITS) ? 1U : 0U)

It seems you are calling twice HAL_RTC_Init() and the secontime calendar is already initialized.

Remember that you must call __HAL_RTC_WRITEPROTECTION_DISABLE() before entering init mode after first time.

I suggest you put a breakpoint to RTC registers and re-start the program to see when it set RTC registers first time.

 

 

DavePfz
DavePfzAuthorBest answer
Senior
October 19, 2026

After reviewing the couple of replies along with the design, it occurred to me that this design is slightly different from the many eval boards available. Specifically, this design implements the VBAT with a Lithium coin cell. As a result whenever, the program (re)starts, the previous value for RTC_FLAG_INITS has been retained and the hardware is working as designed.

I was looking for a software solution, but as is mentioned in several places, whenever the write protection is removed the clock would stop. In this case, stopping for a few instruction cycles would not have a significant effect. However, once the pre-scalers have been set, there is no reason to reload them.

So, to let the system work as intended, the solution is to: Load the new code. Remove all power including the backup cell. Allow the hardware to fully discharge. On power up the software will ‘see’ that the flag is not set and it will initialize with the new values and set the flag. Once that is complete, as long as the coin cell remains in place, the code will not repeat.

The hardware and software are working as intended!