Skip to main content
DavePfz
Senior
Aug 20, 2026 6:23 PM UTC
Solved

Problem with RTC_ALARM_C on STM32WB5MM

I’m trying to set an alarm occurring on the 5 minute marks. However, after I set the alarm and try to read it back I always get 0 minutes and no interrupts. My code without the error handling is as follows.

	Status = HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

Status = HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A); // Deactivate before setting

/** Enable the Alarm A
*/
sAlarm.AlarmTime.Minutes = 5 * (((sTime.Minutes + 5) % 60) / 5);
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_SECONDS;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;

sAlarm.Alarm = RTC_ALARM_A;
Status = HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN);

What am I missing?

Thanks

DavePfz
DavePfzBest answer
Senior
Aug 21, 2026 3:31 PM UTC

Once I saw the problem it was obvious, but the path I took was devious. When I started coding, I tested various parts individually. For example, I tested the RTC alarm capability and went on. One of the later issues was reading and writing to the backup registers. To do this I surrounded the write to the registers with HAL_PWR_EnableBkUpAccess() and HAL_PWR_DisableBkUpAccess(). That all worked OK.

Then I started to combine things and the RTC alarm no longer worked - and thus this post.

What I discovered is that the code generated by STM32CubeMX leaves the backup access enabled. This allowed the write to the alarm. When I later code disabled it, I couldn’t write to the RTC alarm. Note that the write routine always returned HAL_OK, so there was no reason on my part to suspect this issue.

I am puzzled as to how the backup access is intended to be used and why the STM32CubeMX code leaves it enabled. Anyway, I’m over that issue.

Hope this helps someone else!

 

2 replies

DavePfz
DavePfzAuthor
Senior
Aug 20, 2026 9:22 PM UTC

For any who may be looking, this may be of help.

If I follow the SetAlarm routine by an immediate HAL_RTCC_Get_Alarm, I always get a time value of all zeros. It’s as though the write is not allowed. I have traced this through the SetAlarm routine and it doesn’t make sense to me. The values look right but they don’t register. (?)

DavePfz
DavePfzAuthorBest answer
Senior
Aug 21, 2026 3:31 PM UTC

Once I saw the problem it was obvious, but the path I took was devious. When I started coding, I tested various parts individually. For example, I tested the RTC alarm capability and went on. One of the later issues was reading and writing to the backup registers. To do this I surrounded the write to the registers with HAL_PWR_EnableBkUpAccess() and HAL_PWR_DisableBkUpAccess(). That all worked OK.

Then I started to combine things and the RTC alarm no longer worked - and thus this post.

What I discovered is that the code generated by STM32CubeMX leaves the backup access enabled. This allowed the write to the alarm. When I later code disabled it, I couldn’t write to the RTC alarm. Note that the write routine always returned HAL_OK, so there was no reason on my part to suspect this issue.

I am puzzled as to how the backup access is intended to be used and why the STM32CubeMX code leaves it enabled. Anyway, I’m over that issue.

Hope this helps someone else!