STM32 wake up from standby by RTC

luden picture luden · Feb 13, 2017 · Viewed 10.8k times · Source

I am programming STM32L051R8 and have next problem. I'm trying use standby mode in most part of time, and sometimes wake up by RTC, it is an auto wake-up. If I work without sleep - all works perfectly, I got an RTC interrupt every time, but when I use standby - don't.

If I use standby, I have a good first cycle:

  1. reset
  2. set RTC
  3. enter standby
  4. waiting for interrupt
  5. wake-up

But second and next cycles wake up immediately after entering standby (3).

Answer

Bence Kaulics picture Bence Kaulics · Feb 14, 2017

When the microcontroller is in Standby mode and an RTC interrupt occurs the WUF: Wakeup flag will be set by the hardware in the PWR control/status register (Page 162).

Bit 0 WUF: Wakeup flag

This bit is set by hardware and cleared by a system reset or by setting the CWUF bit in the PWR power control register (PWR_CR)

0: No wakeup event occurred

1: A wakeup event was received from the WKUP pin or from the RTC alarm (Alarm A or Alarm B), RTC Tamper event, RTC TimeStamp event or RTC Wakeup).

Initially this is cleared by a system reset so that is why your first cycle is OK. But after a wake-up from standby you have to clear it manually using the CWUF bit in the PWR control register. If you do not do this then the controller will wake up at once as the this bit signals an occurred wake-up event.

You can access the register directly to set this bit or with HAL library the following macro can be used:

__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);