Skip to main content
Led
Associate III
November 2, 2023
Question

RTC Alarm+Standby: Waking up from standby works once, but returns immediatelly afterwards

  • November 2, 2023
  • 1 reply
  • 1450 views

Hi all,

I configured RTC and standby to wake up after 5 seconds, once standby is entered. This works fine after flashing the program. But once waken up from standby, if going to standby again, the device returns from standby immediately.

Beside the RTC, I have also the SYS_WKUP pin configured to wakup the device. When disabling the RTC alarm, this feature alone works as expected.


What I checked:

  • cleared flags RTC_FLAG_WUTF, RTC_FLAG_ALRAF, RTC_FLAG_ALRBF (even though I only use Alarm A)
  • checked NRST and SYS_WKUP pins with oscilloscope
  • verified configured time. The RTC time is currently always set again at startup to avoid time setting issues.
  • compared registers for the working standby alarm and the others. Register for RTC, PWR, EXTI look the same.

Here is how I configured the peripheral:

HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);

HalRtc_GetDateAndTime(&nowtime); /* own function to get the time */

HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A);
HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_B);
__HAL_GPIO_EXTI_CLEAR_IT(0x2000);
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRBF);

RTC_AlarmTypeDef sAlarm = { 0 };
sAlarm.AlarmTime.Hours = nowtime.hour;
sAlarm.AlarmTime.Minutes = nowtime.min;
sAlarm.AlarmTime.Seconds = nowtime.sec + 5;
sAlarm.AlarmTime.SubSeconds = 0U;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_MINUTES | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_DATEWEEKDAY; /* Select which items shall be INORED (!) */
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;/* Select to match by date, not day of week */
sAlarm.AlarmDateWeekDay = nowtime.day;

sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK) {
 status = eError;
}

if (status == eOk) {
 HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
}

HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
HAL_Delay(100);
if ((PWR->CSR & 0x100) && ((RTC->ISR & 0x100) == 0)) { /* just assure again the bits are set as expected */
 HAL_PWR_EnterSTANDBYMode();
}

After searching and debugging for hours, I hope someone can help me.

Thanks in advance, denise

This topic has been closed for replies.

1 reply

ST Employee
November 8, 2023

Hello @Led 

Have you already seen this thread

 

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.
Led
LedAuthor
Associate III
November 8, 2023

Hi Sarra,

Thanks a lot for the support and the link to the thread.

I was not aware of the thread and I gave it a try. Unfortunately, it did not improve the situation.

In mean time I implemented a workaround: I use IWDG to wake up the system, and whenever it wakes the system from standby, I check if the RTC_ISR_ALRAF bit is set. If not, I just return into standby mode. This works well, however it has the drawback that the time is not very accurate.

Implementing this, I had another unexpected case: I thought that the RTC handler 'hrtc' represents the registers in 'hrtc.Instance'. But this is not the case, I had to access the registers directly by address. Maybe my origin issue is also linked on the fact that the handler 'hrtc' should not be used to reset and set flags.

MM..1
Chief III
November 8, 2023

Code not show how you use it, but maybe your miss is not knowledge , that wake from standby = reset MCU.

If you dont handle this right result is back to standby alltime.