cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Alarm interrupt skipped sometimes

Florian Moser
Senior

Hi all!

uC: STM32L433VCT

CubeMX, CubeIDE, CubeProgrammer

I've configured my RTC to set an alarm every minute. When an alarm occurs, the next alarm gets set and so on.

About once to twice a day, the rtc alarm is skipped, so there's a continous error, because no future alarms are set.

My function to calculate the next alarm is correct (rtc_get_next_time()) and the alarm was set to the correct time, regarding to the rtc handle (RTC_HandleTypeDef hrtc;) Just the alarm interrupt doesn't get triggered.

It is not happening at the same time of the day, it's pretty random. It never happend from 23:xx to 0:xx.

Here's how I set the alarm:

void main_set_rtc_alarm(uint8_t hour, uint8_t minute, uint8_t second)
{
	RTC_AlarmTypeDef sAlarm = {0};
	sAlarm.AlarmTime.Hours = hour;
	sAlarm.AlarmTime.Minutes = minute;
	sAlarm.AlarmTime.Seconds = second;
	sAlarm.AlarmTime.SubSeconds = 0x0;
	sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
	sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
	sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
	sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
	sAlarm.AlarmDateWeekDay = 0x1;
	sAlarm.Alarm = RTC_ALARM_A;
 
	HAL_StatusTypeDef state = HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN);
 
	if (state != HAL_OK)
	{
		Error_Handler();
	}
}

When the alarm occurs:

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
	timestamp_t next = rtc_get_next_time(rtc_get_current_time(),delay); // delay is 60s
        main_set_rtc_alarm(next.hour, next.minute, next.second);
}

I know this only works correct for delays up to 24h. That's just for testing purposes.

There are lots of different interrupts in this program. Maybe this causes the problem?

Thank you!

20 REPLIES 20

> There are lots of different interrupts in this program.

Do they wake up the system too? If yes, then if it goes back to sleep, is there a check whether the RTC-alarm hasn't go off meantime?

JW

Florian Moser
Senior

The system never goes into sleep mode. I don't need the alarm to wake up the system, I just misuse it as a timer.

OK.

Read out the RTC registers content in case when the alarm happens and compare them to case when it does not happen.

If you use external interrupts or any interrupts going through EXTI, try disabling them to see if this makes any difference.

JW

Florian Moser
Senior

I'll have to wait till it happens again 😂 I'm going to share my results later.

Yeah there are external interrupts enabled. I'll also try that.

Thanks!

Jack Peacock_2
Senior III

When you process the alarm interrupt check to make sure you haven't passed the next alarm setting already. The latency involved in responding to and resetting the alarm might be causing a miss on the exact minute.

As a check you can mask the minutes to fire at multiple minute settings. This will tell you if the problem is post dating the alarm setting.

Jack Peacock

I don't really understand what you mean. When the rtc interrupt occurs, I get the current time from the rtc and add one minute to that time. How can i pass the next alarm? Only way is if this process would take more than one minute.

Maybe I misunderstood your message

Florian Moser
Senior

I've disabled the external interrupts and it still happened.

It happend while two uart interfaces were receiving data.

It's rare that these interfaces are receiving data at the same time, but it's possible. I would say that this can also happen 2-3 times a day.

Florian Moser
Senior

Now I've a timer, that is set to one minute and two seconds. The timer gets reset with every rtc alarm.

If there's a timer interrupt, the alarm has been missed.

This happened a few minutes ago. The rtc alarm was set to 13:27:12 in the RTC register. The timer interrupt happened at 13:27:14, so the RTC alarm was set up correctly, but I'm missing the interrupt.

My Code:

void application_alarm_missed(void)
{
	error_handle(APP_RTC_ALARM_MISSED,SOFT);
	RTC_AlarmTypeDef AlarmA;
	HAL_RTC_GetAlarm(&hrtc,&AlarmA,RTC_ALARM_A,RTC_FORMAT_BIN);
        timestamp_t now = rtc_get_current_time();
        //breakpoint here
	application_set_next_alarm();
}

Read out and check/post the RTC registers content. There's a long way from your code to the registers.

Also, do what Jack suggested above: mask the minutes. In that way, you should get an interrupt every minute. Observe what happens.

Also, how do you know the alarm has or has not occured?

JW