cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L0 RTC AlarmA - set to interrupt at the desired HRS and MINUTES setting

TMari.2
Associate II

I have working code for an STM32L0 device that uses Alarm A of the RTC to wake / interrupt at the desired minute interval. I now require the ability to wake / interrupt at the desired hours AND minutes time. Previously all masks were masked except for the Alarm A minutes mask. Now all masks except for the Alarm A minutes AND hours masks are masked. I believe it should work as before only now the alarm needs to see a matching hours AND minutes value in order to cause the interrup. Is my understanding correct ? So, for example, if the current time is 0 hrs and 0 minutes and I set AlarmTime.Hours = 1 and AlarmTime.Minutes = 1 then I would expect a single interrupt 61 minutes from now.

5 REPLIES 5
Peter BENSCH
ST Employee

Welcome, @Community member​, to the community!

This is essentially a correct assumption, as the RTC works with alarm times in the same way as an alarm clock on the bedside table: a signal is given at a given time. In your case, the interrupt will be raised at 01:01am.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thank-you very much Peter.

The existing, known-working, code for minutes-based interrupts works as expected and basically follows the process:

i/  set masks:   sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS|RTC_ALARMMASK_SECONDS;

ii/ set minutes: sAlarm.AlarmTime.Minutes = whatever 

iii/ enable interrupt: if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)

  { Error_Handler(); }

This scheme is only very slightly modified for the minutes + hours situation:

i/  set masks:   sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_SECONDS;

ii/ set minutes: sAlarm.AlarmTime.Minutes = whatever 

sAlarm.AlarmTime.Hours = whatever 

iii/ enable interrupt: if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)

  { Error_Handler(); }

In the existing minutes-based interrupt case an interrupt is generated after, say, 2 minutes if that is the desired interval setting - this is correct.

What seems to be happening in the minutes + hours case is that an interrupt is generated immediately rather than after 2 minutes - this is with sAlarm.AlarmTime.Hours = 0 and sAlarm.AlarmTime.Minutes = 2 and with a starting point of sTime.Hours = sTime.Minutes = 0. Any ideas ? Does it matter what order the AlarmMasks are set compared to the AlarmTimes ? I would guess not especially as the interrupt enabling happens afterwards ?

Thanks again.

Well, you are confusing the RTC functions Wake-Up (time difference) and Alarm (absolute time). 

Please check in your application which start time you set at the power-on reset. If this is e.g. 03:00:00 and the alarm time is set to 2 minutes and masked for the minutes, the interrupt will be triggered at 03:02:00, so it looks like after 2 minutes, but then only after one hour, when the condition xx:02:xx applies again.

If, on the other hand, your start time is e.g. 03:50:00, the interrupt will only be triggered at 04:02:00, i.e. after 12 minutes, followed by the next one at 05:02:00, etc.

Details can be found in the reference manual of the family you are using, section Real-Time Clock (RTC), subsections Programmable Alarms and Periodic Auto-Wakeup.

Now, if you want to use the Wake-Up function, e.g. to wake up the system every 2 minutes, you only need to set the wake-up timer with either:

  • HAL_RTCEx_SetWakeUpTimer() - polling mode
  • HAL_RTCEx_SetWakeUpTimer_IT() - interrupt after the time difference has elapsed

For the interrupt, you can put the action to be executed into the function

RTC_WKUP_IRQHandler().

Does it answer your question?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
TMari.2
Associate II

hello Peter,

It's all working exactly as expected - there are no problems with the minutes+hours based interrupts (as expected).

The issue was something else unrelated to the fundamentals of RTC alarm minutes+hours interrupts (accessing the sAlarm structure in different source files even with one declared as extern seemed to be a problem).

Thank-you very much for your help, and apologies for not having provided you with an update sooner.

Peter BENSCH
ST Employee

Great!

If the problem is resolved, please mark this topic as answered by selecting Select as best under your preferred answer. This will help other users find that answer faster.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.