cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to re-enter standby after clearing standby and wakup flags

IDavi.942
Associate II

I am having touble re-entering standby after a RTC wakup. I am using the STM32L412 (Nucleo-32)

I have written some code that enables and sets the RTC, then sets alarm A and alarm B, before going into standby. After the correct time has elapsed, alarm A wakes up mcu. I then check to see if the system was resumed by standby, and if it has clear the standby and wakup flag, before entering standby again to wait for alarm B to be triggered. The problem is that the mcu instantly wakes up and again clears the flags and goes into standby. It keeps looping round this indefinitely.

The only thing I have found to stop the mcu for instantly waking back up is to run:

HAL_PWREx_DisableInternalWakeUpLine();

This however sets it so that alarm B does not now wake up, ie I believe all wakups are disabled.

My code is:

 /* Check and handle if the system was resumed from StandBy mode */

 if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)

 { 

   /* Clear standby flag */

   __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);

   /* Clear wakeup flag */

   __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);

   /* Enter standby */

   HAL_PWR_EnterSTANDBYMode();

 }

 else

 {

    MX_RTC_Init();

    HAL_Delay(1000);

    HAL_PWR_EnterSTANDBYMode();

 }

8 REPLIES 8
turboscrew
Senior III

Clear all pending interrupts too.

IDavi.942
Associate II

I have tired to clear every flag that I can find relating to the RTC but must be missing the relevant one

  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc,PWR_FLAG_WU);

  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);

  __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);

  __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRBF);

  __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_CLEAR_ALRAF);

  __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_CLEAR_ALRBF);

  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF2);

  __HAL_PWR_CLEAR_FLAG(PWR_EXTI_LINE_PVD);

  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUFI);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_RECALPF);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_INITF);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_RSF);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_INITS);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_ITSF);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRBF);

  __HAL_RTC_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);

turboscrew
Senior III

Not only RTC related.

The reference manual says "Entering Low-power mode through WFI or WFE will be executed only if no interrupt is pending or no event is pending."

IDavi.942
Associate II

Thanks - I agree, but I am having trouble finiding the offending flag/s that wont allow the MCU to stay in standby.

turboscrew
Senior III

I try to keep as far as I can for as long as I can from CubeIDE, so I don't know about that, but in Atollic, you could put a breakpoint just before the sleep and check the NVIC pending interrupt/event registers. And, of course, the pending interrupt register of EXTI. I just implemented sleep/wakeup for STM32F4xx device two days ago, but that's at work, so I can't share that.

Note that you can reset almost all the peripherals - standby does it anyway. Only backup domain survives.

Waking up sets things up just like after reset. The rough way is to use RRC reset registers.

IDavi.942
Associate II

Using debug I have found I am unable to clear the Wakeup Flag Internal WUFI bit (PWR>SR1).

I have the RTC alarm interrupt through EXTI line 18.

I have tried clearing so many different flags but none allow me to clear the WUFI flag, except HAL_PWREx_DisableInternalWakeUpLine(); which clears the WUFI flag, but then when placing back into standby, a future RTC event does not wakeup the mcu.

turboscrew
Senior III

From the reference manual:

"Bit 0WUF1: Wakeup flag 1This bit is set when a wakeup event is detected on wakeup pin, WKUP1. It is cleared by writing ‘1’ in the CWUF1 bit of the PWR_SCR register."

You should clear all the wakeup flags.

IDavi.942
Associate II

I found the problem.

"PWR_FLAG_WUFI Wake-up Flag Internal. Set when a wakeup is detected on the internal wakeup line."

The WUFI flag only clears when the thing that caused the flag to be set is cleared, in this case Alarm A.

I had tried running:

__HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);

Which did not work.

Ultimately all I needed to do was to clear the CALRAF bit in the RTC->SCR register. So instead I tired:

RTC->SCR = RTC_SCR_CALRAF;

Which cleared both the alarm flag and the WUGI flag. The MCU now can be return to standby without immediately waking up.

Thanks for your help, and hope this helps others.