Hal Rtc alarm Interurpt firing at startup possible bug report on stm32wl55
Dear community,
I recently faced a possible anomaly in Stm32 cube configuration when setting up the RTC alarm:
the rtc alarm interrupt was firing immediatley during initialization.
- Everything was setup via Cube, no manual code involved.
- Problem begins immediately when in void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc) the RTC IRQ is enabled, after that it start to fire non-stop, at this point no alarm has been configured yet.
This because in the HAL_RTC_AlarmIRQHandler(&hrtc) the alarm interrupt flag is cleared only if hrtc->IsEnabled.RtcFeatures is evaluated true, since the configuration hasn't been carried out yet, the interrupt flag is not cleared and the program gets stuck.
My supposition is that the interrupt fires because of the RTC alarm registers content at reset, I haven't investigated but it seems the only logical reason.
My solution is to ignore hrtc->IsEnabled.RtcFeatures when clearing the alarm interrupt flags.
BEFORE
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
{
uint32_t tmp = READ_REG(RTC->MISR) & READ_REG(hrtc->IsEnabled.RtcFeatures);
if ((tmp& RTC_MISR_ALRAMF) != 0U)
{
/* Clear the AlarmA interrupt pending bit */
WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
/* Call Compare Match registered Callback */
hrtc->AlarmAEventCallback(hrtc);
#else
HAL_RTC_AlarmAEventCallback(hrtc);
#endif
}
if (( tmp & RTC_MISR_ALRBMF) != 0U)
{
/* Clear the AlarmB interrupt pending bit */
WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
/* Call Compare Match registered Callback */
hrtc->AlarmBEventCallback(hrtc);
#else
HAL_RTCEx_AlarmBEventCallback(hrtc);
#endif
}
/* Change RTC state */
hrtc->State = HAL_RTC_STATE_READY;
}AFTER
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
{
uint32_t tmp = READ_REG(RTC->MISR) & READ_REG(hrtc->IsEnabled.RtcFeatures);
if ((tmp& RTC_MISR_ALRAMF) != 0U)
{
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
/* Call Compare Match registered Callback */
hrtc->AlarmAEventCallback(hrtc);
#else
HAL_RTC_AlarmAEventCallback(hrtc);
#endif
}
/* Clear Flag*/
if( READ_REG(RTC->MISR) & RTC_MISR_ALRAMF) != 0U ){
/* Clear the AlarmA interrupt pending bit */
WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
}
if (( tmp & RTC_MISR_ALRBMF) != 0U)
{
#if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
/* Call Compare Match registered Callback */
hrtc->AlarmBEventCallback(hrtc);
#else
HAL_RTCEx_AlarmBEventCallback(hrtc);
#endif
}
/* Clear flag */
if (( READ_REG(RTC->MISR) & RTC_MISR_ALRBMF) != 0U)
{
/* Clear the AlarmB interrupt pending bit */
WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
}
/* Change RTC state */
hrtc->State = HAL_RTC_STATE_READY;
}