'STM32 HAL Library RTC Alarm

I am having a problem about RTC Alarm configuration. First of all, I tried example code which is provided by Cube, and It works. But in my code, it doesn't work at all. I am guessing the RTC formats are not matching. even so, I tried all possibilities both BCD and BIN.

Reading Time&Date functions are working. I can show them on OLED screen. But can't generate alarm interrupt. What am I doing wrong?

void StartRTC_Routine(void)
{
    HAL_PWR_EnableBkUpAccess();
    __HAL_RCC_RTC_ENABLE();
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;

    if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0) != RTC_BKP_DATE_TIME_UPDTATED)             
    {
        /*RTC Init*/
        MX_RTC_Init();

        RTC_Default();
        HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,RTC_BKP_DATE_TIME_UPDTATED);                  
    }
    else
    {
        /* Check if the Power On Reset flag is set */
        if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
        {
            MyFlag = 0xBA;
        }
        /* Check if the Soft Reset flag is set */
        if(__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET)
        {
            MyFlag = 0xFB;
        }       
    }
}



void RTC_TimeShow(uint8_t* showtime)
{  

  /* Get the RTC current Time */
  HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BCD);

  /* Display time Format : hh:mm:ss */
  sprintf((char*)showtime,"%02X:%02X:%02X",stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);

    ssd1306_SetCursor(20,20);
    ssd1306_WriteString((char*)showtime,Font_11x18,White);
    ssd1306_UpdateScreen();
} 


/*  Initial Values of RTC   **/
void RTC_Default(void)
{
    sdatestructureget.Date    = 0x14; 
    sdatestructureget.Year    = 0x17;
    sdatestructureget.WeekDay = 0x05;
    sdatestructureget.Month   = 0x01;

  stimestructureget.Hours   = 0x02;
  stimestructureget.Minutes = 0x04;
  stimestructureget.Seconds = 0x30;
  stimestructureget.TimeFormat = RTC_HOURFORMAT_24;
  stimestructureget.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
  stimestructureget.StoreOperation = RTC_STOREOPERATION_RESET;

    HAL_RTC_SetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BCD);
    HAL_RTC_SetTime(&hrtc, &stimestructureget, RTC_FORMAT_BCD);
}


void Set_Alarm(void)
{
  /*#################  Configure the RTC Alarm peripheral #################*/
  salarmstructure.Alarm = RTC_ALARM_A;
  salarmstructure.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
  salarmstructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
  salarmstructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
  salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
  salarmstructure.AlarmTime.TimeFormat = RTC_HOURFORMAT_24;
  salarmstructure.AlarmTime.Hours   = 0x02;
  salarmstructure.AlarmTime.Minutes = 0x34;
  salarmstructure.AlarmTime.Seconds = 0x10;
  salarmstructure.AlarmTime.SubSeconds = 00;    

    HAL_RTC_SetAlarm_IT(&hrtc,&salarmstructure,RTC_FORMAT_BCD);

}


void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Turn LED2 on: Alarm generation */
  HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
}


Solution 1:[1]

Some things you could check:

  • Make sure you call Set_Alarm() as i cannot see a function call in your code sample.
  • Try configuring your alarm to go off every second and set a breakpoint in HAL_RTC_AlarmAEventCallback() and see if the function is run.

Solution 2:[2]

You need to enable NVIC configuration for RTC in order to use _IT methods (ex. HAL_RTC_SetAlarm_IT). If you are using CubeMx this can be done simple in Configuration -> RTC, then go to NVIC.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Justegard
Solution 2 Colateral