cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Alarm on STM32F401RCT6

Hesseltjuuh
Associate III

Hello, 

I've been using the RTC lately and it works correctly except for one thing, the alarm. 

Below you can see the function HAL generated

static void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};
  RTC_AlarmTypeDef sAlarm = {0};

  /* USER CODE BEGIN RTC_Init 1 */
  /* USER CODE END RTC_Init 1 */
  /** Initialize RTC Only
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  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)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /*if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0xBEBE)
  {
    // Write Back Up Register 1 Data
    HAL_PWR_EnableBkUpAccess();

    // Writes a data in a RTC Backup data Register 1
    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xBEBE);
  } else {
	  //If there is a value in the backup register we dont need to set the RTC so we immediately exit the function
	  return;
  }*/
  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */
  sTime.Hours = 0;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 0;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /** Enable the Alarm A
  */
  sAlarm.AlarmTime.Hours = 0;
  sAlarm.AlarmTime.Minutes = 0;
  sAlarm.AlarmTime.Seconds = 20;
  sAlarm.AlarmTime.SubSeconds = 0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 1;
  sAlarm.Alarm = RTC_ALARM_A;
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */
  /* USER CODE END RTC_Init 2 */

}

 

And here how i configured it in HAL 

Hesseltjuuh_0-1698702547947.png

Hesseltjuuh_1-1698702558649.png

 

 And here I set a breakpoint in the interrupt file:

Hesseltjuuh_2-1698702596937.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

Hello again @Hesseltjuuh

The RTC's current time and date should indeed match the alarm time and date.

Please refer to this article which is made for seconds. 

You should still  mask hours and days for it to work in the long run as @Tinnagit already said. 

 

 

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.

View solution in original post

6 REPLIES 6
Sarra.S
ST Employee

Hello @Hesseltjuuh

Your configuration looks okay, could you specify more, what is the alarm issue exactly that you've observed? 

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.

Tinnagit
Senior II

It's about Alarm Mask.
Alarm Mask is used for ignore that field to compare with your alarm time.
As your config, You have no mask in any field so RTC are going to alarm on every 0:0:20.000 on Monday.
and If you want to alarm at second 20th every minute, you should try to mask in another field except second field.
RTC is going to compare your alarm time in second field 20th to RTC time in second field and alarm if it matched.

The problem is that it doesnt trigger an interrupt, i reset my RTC so the RTC and Alarm should match each other perfectly after i boot it on but it never triggers an interrupt.

I know but i also reset my RTC so the date and alarm should match without the masks. (i also tried it before with the masks enabled but didnt work either)

Sarra.S
ST Employee

Hello again @Hesseltjuuh

The RTC's current time and date should indeed match the alarm time and date.

Please refer to this article which is made for seconds. 

You should still  mask hours and days for it to work in the long run as @Tinnagit already said. 

 

 

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.

Clyde_S
Associate II

I too am having trouble with getting interrupts on seconds.   I have set two alarms, one for 00:00:00 on alarm A, and that works, toggling the LED, but the seconds alarm never fires.  I am guessing I have not set up the handlers correctly.  The NVIC is checked.  The above mentioned article appears to be a help, but I am not getting there.

 

Here is my stm32l4xx_it.d

 

void RTC_Alarm_IRQHandler(void)
{
  /* USER CODE BEGIN RTC_Alarm_IRQn 0 */

  /* USER CODE END RTC_Alarm_IRQn 0 */
  HAL_RTC_AlarmIRQHandler(&hrtc);
  /* USER CODE BEGIN RTC_Alarm_IRQn 1 */

  /* USER CODE END RTC_Alarm_IRQn 1 */
}

/* USER CODE BEGIN 1 */




void HAL_RTC_AlarmAEventCallback (RTC_HandleTypeDef *hrtc)
{
RTC_AlarmTypeDef sAlarm;

	 HAL_GPIO_TogglePin(GPIOB, RED_LED_Pin);   // turn red on
	 HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, FORMAT_BCD);
}

void HAL_RTC_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
{
	  RTC_AlarmTypeDef sAlarm;

	  HAL_RTC_GetAlarm(hrtc,&sAlarm,RTC_ALARM_B,FORMAT_BCD);
	  HAL_GPIO_TogglePin(GPIOB, BLUE_LED_Pin);
	  HAL_Delay (100);
	  HAL_GPIO_TogglePin(GPIOB, BLUE_LED_Pin);

	   if(sAlarm.AlarmTime.Seconds>58)
	  {
	    sAlarm.AlarmTime.Seconds=0;
	  }
	  else
	  {
	    sAlarm.AlarmTime.Seconds=sAlarm.AlarmTime.Seconds+1;
	  }

	  HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, FORMAT_BCD);  // prime it for the next second
}
/* USER CODE END 1 */

   

Attached are my alarm configureations.  I tried the seconds mask both ways, enabled and disabled with no difference.

pic2.PNGpic1.PNG