cancel
Showing results for 
Search instead for 
Did you mean: 

Set RTC Alarm Interrupt using HAL Drivers STM32F4

SWint.1
Associate

Hi,

I want to trigger the RTC alarm interrupt 10 seconds after the currently read time.

The interrupt also triggers sometimes, but not every time the function "SetAlarmAInterrupt" is called.

What could be the reason that the interrupt only trigger from time to time?

LSI is used:

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
    ErrorHandler();
  }

Following is the corresponding code part:

MxRtcInit(void) {
  __HAL_RCC_SYSCFG_CLK_ENABLE();
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_RCC_RTC_ENABLE();
 
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 249;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK) {
    ErrorHandler();
  }
 
  timeRtc.Hours = 0x0;
  timeRtc.Minutes = 0x0;
  timeRtc.Seconds = 0x0;
  timeRtc.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  timeRtc.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &timeRtc, RTC_FORMAT_BCD) != HAL_OK) {
    ErrorHandler();
  }
 
  dateRtc.WeekDay = RTC_WEEKDAY_MONDAY;
  dateRtc.Month = RTC_MONTH_JANUARY;
  dateRtc.Date = 0x1;
  dateRtc.Year = 0x0;
  if (HAL_RTC_SetDate(&hrtc, &dateRtc, RTC_FORMAT_BCD) != HAL_OK) {
    ErrorHandler();
  }
 
  alarmRtc.AlarmTime.Hours = 0x0;
  alarmRtc.AlarmTime.Minutes = 0x0;
  alarmRtc.AlarmTime.Seconds = 0x0;
  alarmRtc.AlarmTime.SubSeconds = 0x0;
  alarmRtc.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  alarmRtc.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  alarmRtc.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
  alarmRtc.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  alarmRtc.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  alarmRtc.AlarmDateWeekDay = 0x1;
  alarmRtc.Alarm = RTC_ALARM_A;
  HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
}
 
SetAlarmAInterrupt(void){
HAL_RTC_GetTime(&hrtc, &timeRtc, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &dateRtc, RTC_FORMAT_BIN);
 
alarmRtc.AlarmTime.Hours = timeRtc.Hours;
alarmRtc.AlarmTime.Minutes = timeRtc.Minutes;
uint8_t seconds = timeRtc.Seconds + 10;
      if (seconds >= 60) {
        alarmRtc.AlarmTime.Minutes++;
        if (alarmRtc.AlarmTime.Minutes >= 60) {
          alarmRtc.AlarmTime.Hours++;      
        alarmRtc.AlarmTime.Seconds = seconds - 60;
      } else {
        alarmRtc.AlarmTime.Seconds = seconds;
      }
 if (HAL_RTC_SetAlarm_IT(&hrtc, &alarmRtc, RTC_FORMAT_BCD) != HAL_OK) {
    ErrorHandler();
  }
}

And here is the Interrupt handled:

void RTC_Alarm_IRQHandler(void) {
  HAL_RTC_AlarmIRQHandler(&hrtc);
  HAL_RTC_GetTime(&hrtc, &timeRtc, RTC_FORMAT_BIN);
  HAL_RTC_GetDate(&hrtc, &dateRtc, RTC_FORMAT_BIN);
}

While debugging, when stop at the end of RTC_Alarm_IRQHandler. This could be read for example at timeRtc:

Time at beginning of SetAlarmAInterrupt: 0:1:8

Alarm time HAL_RTC_GetAlarm: 0:1:18

Time read at the end of the Interrupt: 0:1:12

So the Alarm-Interrupt occur 6 seconds to early. But next time SetAlarmAInterrupt is called the Interrupt doesn´t occurs, or the 3 times that are read matches.

Does anyone have an idea why this happens?

1 REPLY 1
SWint.1
Associate

Solved.

I´ve used different RTC_FORMAT types. Only using RTC_FORMAT_BIN solved this.