AN4759 · Application noteIntroduction to using the hardware real-time clock (RTC) and the tamper management unit (TAMP) for STM32 MCUs
Download .pdfOpen in the full document

RTC periodic wakeup unit

RTC periodic wakeup unit

The STM32 MCUs provide several low-power modes to reduce the power consumption. The RTC features a periodic timebase and a wakeup unit that is able to wake up the system from low-power modes. This unit is a programmable down-counting auto-reload timer. When this counter reaches zero, a flag and an interrupt (if enabled) are generated.

The wakeup unit has the following features:
  • Programmable down-counting auto-reload timer
  • Specific flag and interrupt capable of waking up the device from low-power modes
  • Wakeup alternate function output that can be routed to the RTC_ALARM output for RTC2 and the TAMPALRM output for RTC3 (unique pad for alarm A, alarm B or wakeup events) with configurable polarity
  • Full set of prescalers to select the desired waiting period

Program the auto-wakeup unit

The table below describes the steps required to configure the auto-wakeup unit.

Table 11. Steps to configure the auto-wakeup unit
StepWhatHowComment
1Disable write protection on RTC registers. Write 0xCA and then 0x53 into RTC_WPR.The RTC registers can be modified.
2Disable the wakeup timer.Clear WUTE bit in RTC_CR.-
3Ensure access to wakeup auto-reload counter and WUCKSEL[2:0] are allowed.Poll WUTWF until it is set in RTC_ISR (RTC2)/RTC_ICSR (RTC3)
  • For RTC2: It takes around two RTCCLK clock cycles due to clock synchronization.
  • For RTC31:
    • If WUCKSEL[2] = 0, WUTWF is set. It takes around 1 ck_wut + 1 RTCCLK cycles after WUTE is cleared.
    • If WUCKSEL[2] = 1, WUTWF is set. It takes up to 1 ck_apre + 1 RTCCLK cycles after WUTE is cleared.
4Program the value into the wakeup timer.Set WUT[15:0] in RTC_WUTR.

For RTC3, the user must also program WUTOCLR[15:0]2in RTC_WUCR

See Maximum and minimum RTC wakeup period.
5Select the desired clock source.Program WUCKSEL[2:0] in RTC_CR.
6Re-enable the wakeup timer.Set WUTE in RTC_CR.The wakeup timer restarts counting down.
7Enable write protection on RTC registers.Write 0xFF into the RTC_WPR.The RTC registers cannot be modified.
1 ck_wut is the wakeup timer clock input, and ck_spre the clock output by the RTC synchronous prescaler (usually 1 Hz).
2 WUTOCLR bits are only available on RTC3 type. They are used to choose if the WUTF flag is cleared by software (WUTOCLR = 0x0000) or automatically cleared by hardware when the auto-reload down counter reaches WUTOCLR value (0x0000 < WUTOCLR ≤ WUT). For RTC2 type, WUTF must always be cleared by software.

Maximum and minimum RTC wakeup period

The wakeup unit clock is configured through WUCKSEL[2:0] in RTC_CR. Three different configurations are possible:
Periodic timebase/wakeup configuration for clock configuration 1

The figure below shows the prescaler connection to the timebase/wakeup unit and the table below gives the timebase/wakeup clock resolutions corresponding to configuration 1.

The prescaler depends on the wakeup clock selection as follows:
  • WUCKSEL[2:0] =000: RTCCLK/16 clock selected
  • WUCKSEL[2:0] =001: RTCCLK/8 clock selected
  • WUCKSEL[2:0] =010: RTCCLK/4 clock selected
  • WUCKSEL[2:0] =011: RTCCLK/2 clock selected
Figure 6. Prescalers connected to the timebase/wakeup unit for configuration 1
Table 12. Timebase/wakeup unit period resolution with clock configuration 1
Clock sourceWakeup period resolution
WUCKSEL[2:0] = 000 (div16)WUCKSEL[2:0] = 011 (div2)
LSE = 32 768 Hz488.28 µs61.035 µs
When RTCCLK= 32768 Hz, the minimum timebase/wakeup resolution is 61.035 µs, and the maximum resolution is 488.28 µs. As a result:
  • The minimum timebase/wakeup period is (0x0001 + 1) x 61.035 µs = 122.07 µs.

    The timebase/wakeup timer counter WUT[15:0] cannot be set to 0x0000 with WUCKSEL[2:0] = 011 (fRTCCLK/2) because this configuration is prohibited. Refer to the product reference manuals for more details.

  • The maximum timebase/wakeup period is (0xFFFF+ 1) x 488.28 µs = 32 s.
Periodic timebase/wakeup configuration for clock configuration 2
The figure below shows the prescaler connection to the timebase/wakeup unit corresponding to configuration 2 and 3.
Figure 7. Prescalers connected to the wakeup unit for configurations 2 and 3
If ck_spre (synchronous prescaler output clock) is adjusted to 1 Hz, then:
  • The minimum timebase/wakeup period is (0x0000 + 1) x 1 s = 1 s.
  • The maximum timebase/wakeup period is (0xFFFF+ 1) x 1 s = 65536 s (18 hours).
Periodic timebase/wakeup configuration for clock configuration 3

For this configuration, the resolution is the same as for configuration 2. However, the timebase/wakeup counter down counts starting from 0x1FFFF to 0x00000 (instead of 0xFFFF to 0x0000 for configuration 2).

If ck_spre is adjusted to 1 Hz, then:
  • The minimum timebase/wakeup period is (0x10000 + 1) x 1 s = 65537 s (18 hours + 1 s).
  • The maximum timebase/wakeup period is (0x1FFFF+ 1) x 1 s = 131072 s (36 hours).
Note:

In binary or mixed modes, ck_spre is replaced by the clock used to update the calendar (as defined by BCDU bits in RTC_ICSR). See Binary and mixed modes (RTC3 only) for more details on BCDU).

Summary of timebase/wakeup period extrema
When RTCCLK= 32768 Hz and ck_spre (synchronous prescaler output clock) is adjusted to 1 Hz, the minimum and maximum period values are listed in the table below.
Table 13. Min. and max. timebase/wakeup period when RTCCLK= 32768
ConfigurationMinimum periodMaximum period
1122.07 µs32 s
21 s18 hours
318 hours +1 s36 hours

Wakeup firmware examples

The RTC comes with a set of example projects so that the user can quickly become familiar with this peripheral. Refer to the X-CUBE-RTC Expansion Package for a complete projects list.

For example, the user can find the following projects concerning wakeup:
  • For the NUCLEO-L412RB-P equipped with an RTC3:

    STM32Cube_FW_L4_Vx.y.z\Projects\NUCLEO-L412RB-P\Examples_LL\RTC\RTC_ExitStandbyWithWakeUpTimer

  • For the NUCLEO-G071RB equipped with an RTC3:

    STM32Cube_FW_G0_Vx.y.z\Projects\NUCLEO-G071RB\Examples_LL\RTC\RTC_ExitStandbyWithWakeUpTimer_Init

  • For the NUCLEO-G431RB equipped with an RTC3:

    STM32Cube_FW_G4_Vx.y.z\Projects\NUCLEO-G431RB\Examples_LL\RTC\RTC_ProgrammingTheWakeUpTime

If one example is not available in the X-CUBE-RTC for a given STM32 MCU, the user can adapt it.