Skip to main content
DavePfz
Associate III
August 20, 2026
Solved

Problem with RTC_ALARM_C on STM32WB5MM

  • August 20, 2026
  • 2 replies
  • 64 views

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

Best answer by DavePfz

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
Associate III
August 20, 2026

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
Associate III
August 21, 2026

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!