RTC alarm problem only with some MCUs
Hi, I have a strange behavior with the RTC alarm only with some MCU.
In my project use the STM32L476, when I set the alarm the MCU wake up without problem, but this work fine only with some board. On the other boards with the same MCU and the same FW they don't wake up from standby.
I use LSE, a battery and the ALARM_A.
The MCU gets the time and date from the RTC, sets the alarm and entering in standby.
I use the HAL_RTC_GetTime() and HAL_RTC_GetDate() to get time and date.
I tried to Bypass the shadow registers, but the problem there is always.
Someone can help me!
Below RTC_init() code:
/* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;
/**Initialize RTC Only */
hrtc.Instance = RTC;
/* Set the BYPSHAD bit */
RTC->CR |= RTC_CR_BYPSHAD; //Bypass the shadow registers
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
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;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initialize RTC and set the Time and Date */
sTime.Hours = 10;
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(__FILE__, __LINE__);
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_AUGUST;
sDate.Date = 1;
sDate.Year = 18;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Enable the Alarm A */
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 0;
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(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
}
}Below the set_alarm() code:
/* set alarm function */
void set_alarm(uint8_t hours, uint8_t minutes, uint8_t seconds, alarm_type_t alarm_type) {
RTC_AlarmTypeDef sAlarm;
uint32_t type;
switch (alarm_type) {
case ALARM_A:
type = RTC_ALARM_A;
break;
case ALARM_B:
type = RTC_ALARM_B;
break;
case ALARM_AB:
type = RTC_ALARM_A;
break;
default:
return;
}
/**Enable the Alarm A */
sAlarm.AlarmTime.Hours = hours;
sAlarm.AlarmTime.Minutes = minutes;
sAlarm.AlarmTime.Seconds = seconds;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = type;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK) {
_Error_Handler(__FILE__, __LINE__);
}
if (alarm_type == ALARM_AB) {
sAlarm.Alarm = RTC_ALARM_B;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK) {
_Error_Handler(__FILE__, __LINE__);
}
}
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}Below the system_standby() code:
void system_standby (void) {
/** deactivate_periodic_wakeup */
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
__HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
/** enter standby */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSTANDBYMode();
//HAL_PWREx_EnterSHUTDOWNMode();
while(1);
}Thank you guys!
