Skip to main content
Associate
November 30, 2023
Question

STM32WL how to enable RTC subsecond underflow interrupt

  • November 30, 2023
  • 1 reply
  • 1125 views

STM32WL lorawan middleware use a RTC subsecond underflow interrupt update tick instead systick,but the subsecond underflow interrupt never entered.It seems interrupt not enabled, so I manually enable RTC subsecond underflow interrupt but the handler still never accessed.

 

 

/* RTC init function */
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 = RTC_PREDIV_A;
 hrtc.Init.SynchPrediv = RTC_PREDIV_S;
 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
 hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
 hrtc.Init.BinMode = RTC_BINARY_NONE;
 if (HAL_RTC_Init(&hrtc) != HAL_OK)
 {
 Error_Handler();
 }

 /* USER CODE BEGIN Check_RTC_BKUP */

 /* USER CODE END Check_RTC_BKUP */

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

 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
 {
 Error_Handler();
 }

 /** Enable the Alarm A
 */
 sAlarm.AlarmTime.Hours = 0x0;
 sAlarm.AlarmTime.Minutes = 0x0;
 sAlarm.AlarmTime.Seconds = 0x0;
 sAlarm.AlarmTime.SubSeconds = 0x0;
 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 = 0x1;
 sAlarm.Alarm = RTC_ALARM_A;
 if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
 {
 Error_Handler();
 }

 /** Enable the WakeUp
 */
 if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16, 0) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN RTC_Init 2 */

 /* USER CODE END RTC_Init 2 */

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{

 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
 if(rtcHandle->Instance==RTC)
 {
 /* USER CODE BEGIN RTC_MspInit 0 */

 /* USER CODE END RTC_MspInit 0 */

 /** Initializes the peripherals clocks
 */
 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /* RTC clock enable */
 __HAL_RCC_RTC_ENABLE();
 __HAL_RCC_RTCAPB_CLK_ENABLE();

 /* RTC interrupt Init */
 HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
 /* USER CODE BEGIN RTC_MspInit 1 */

 /* USER CODE END RTC_MspInit 1 */
 }
}

 

 

PS: As far, I can’t find any configuration about subsecond underflow interrupt in CubeMX and any code snippet in offical example project.

This topic has been closed for replies.

1 reply

ST Employee
December 5, 2023

Hello Simon,

Some points that are not included in the code you provided :

  • Enabling the SSRU IRQ: TAMP_STAMP_LSECSS_SSRU_IRQHandler
  • Calling the HAL_RTCEx_SSRUIRQHandler() function in the TAMP_STAMP_LSECSS_SSRU_IRQn interrupt
  • Enabling the SSRU IT with the HAL_RTCEx_SetSSRU_IT() function inside the MX_RTC_Init() function
  • Rewriting the weak function HAL_RTCEx_SSRUEventCallback() with your code

Additionally, by default, Cube MX sets the wake-up counter to 0. You should modify it to a value such as 0x0800, which will give you an interrupt every second with the wake-up clock selected with RTC/16.

 

Best Regards,

Victor