Skip to main content
DavePfz
Senior
Oct 29, 2026 6:06 PM UTC
Solved

Writing to backup registers causes problems with the wake-up timer.

Here’s the situation. The system is built on the STM32WB5MM with basic code written using FreeRTOS. All works well, so now I add the BLE HeartRate application. If I prevent my code from running using a while (1) [ osDelay(10); } loop, the STM BLE Toolbox on my Android phone works fine displaying data. However, if I allow my code to run, the Toolbox connects, but no data is displayed. Pursuing this in my code, I determined that the wake-up interrupts were not occurring as they should.

By moving where I stop my code, I have narrowed it down to the following sequence:

HAL_PWR_EnableBkUpAccess();

HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR19, Value);

HAL_PWR_DisableBkUpAccess();

If I simply comment out the last line allowing backup access to remain on. It also works. However, that leaves the backup registers vulnerable. (Note: I use this sequence frequently to protect data that must be retained through a power cycle. VBAT is provided by a coin cell. So, a general solution is preferable.)

This is the first I’ve worked with the wake-up timer. In researching this problem, I have not been able to determine that the “standard” code supporting the wake-up timers would pose any conflict like this.

Can anyone point me in any particular direction?

DavePfz
DavePfzBest answer
Senior
Oct 31, 2026 3:02 PM UTC

The problem is that in main.c the SystemClock_Config() routine calls the HAL_PWR_EnableBkUpAccess() and leaves the access open. Thus, when the HAL_RTCEx_SetWakeUpTimer() routine is called in MX_RTC_Init() the access is wide open. Then when my program went to update one of the backup registers and called HAL_PWR_DisableBkUpAccess(), it was closed for further access. This caused problems for the BLE routines which needed access to the wake up registers.

What I found was that if you intend to protect the backup registers, the following routines need to be ‘surrounded’ by an enable/disable pair (list taken from section 40.2.3 of UM2442 HAL Drivers:

HAL_RTCEx_SetWakeUpTimer()

HAL_RTCEx_SetWakeUpTimer_IT()

HAL_RTCEx_DeactivateWakeUpTimer()

HAL_RTCEx_WakeUpTimerIRQHandler()

HAL_RTCEx_WakeUpTimerEventCallback()

In addition the following routines must also be ‘surrounded’

HW_TS_Init()

HW_TS_Create()

HW_TS_Start()

HW_TS_Stop()

HW_TS_Delete()

Also, for “completeness”, SystemClockConfig() should disable access before exiting, but this would probably affect almost all of the the ST demo examples!

I believe this to be complete, but would need someone more familiar with the processor to verify. It may also have unseen side effects on other driver code that I have not yet used.

 

2 replies

Pavel A.
Oct 30, 2026 10:35 PM UTC

While waiting for more helpful replies - try a quick experiment: put __disable_irq() and __enable_irq() around the HAL_PWR_DisableBkUpAccess()

 

 

 

DavePfz
DavePfzAuthorBest answer
Senior
Oct 31, 2026 3:02 PM UTC

The problem is that in main.c the SystemClock_Config() routine calls the HAL_PWR_EnableBkUpAccess() and leaves the access open. Thus, when the HAL_RTCEx_SetWakeUpTimer() routine is called in MX_RTC_Init() the access is wide open. Then when my program went to update one of the backup registers and called HAL_PWR_DisableBkUpAccess(), it was closed for further access. This caused problems for the BLE routines which needed access to the wake up registers.

What I found was that if you intend to protect the backup registers, the following routines need to be ‘surrounded’ by an enable/disable pair (list taken from section 40.2.3 of UM2442 HAL Drivers:

HAL_RTCEx_SetWakeUpTimer()

HAL_RTCEx_SetWakeUpTimer_IT()

HAL_RTCEx_DeactivateWakeUpTimer()

HAL_RTCEx_WakeUpTimerIRQHandler()

HAL_RTCEx_WakeUpTimerEventCallback()

In addition the following routines must also be ‘surrounded’

HW_TS_Init()

HW_TS_Create()

HW_TS_Start()

HW_TS_Stop()

HW_TS_Delete()

Also, for “completeness”, SystemClockConfig() should disable access before exiting, but this would probably affect almost all of the the ST demo examples!

I believe this to be complete, but would need someone more familiar with the processor to verify. It may also have unseen side effects on other driver code that I have not yet used.