An4865 Lowpower Timer Lptim Applicative Use Cases On stm32 Mcus and Mpus Stmicroelectronics
An4865 Lowpower Timer Lptim Applicative Use Cases On stm32 Mcus and Mpus Stmicroelectronics
Application note
Low-power timer (LPTIM) applicative use cases on STM32 MCUs and MPUs
Introduction
This application note describes the various modes and specific features of the low-power timer (LPTIM) embedded in the
STM32 microcontrollers (MCUs) and microprocessors (MPUs) listed in the table below.
This document contains some applicative examples provided with:
• the X-CUBE-LPTIMER Expansion Package which includes:
– asynchronous pulse counter in Stop mode
– PWM (pulse-width modulation) generation in Stop mode
– timeout wakeup mode
• the STM32CubeU5 MCU Package firmware for STM32U5 Series which includes:
– PWM generation in Stop mode with LPBAM (low-power background autonomous mode)
– input capture in Stop mode with Autonomous mode
These use cases demonstrate the importance of the low-power timer and clarify the features that differ from the
general‑purpose timer.
1 General information
This document applies to STM32 MCUs and MPUs based on the Arm® Cortex®-M processor.
Note: Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
2 Overview
Low-power techniques aim to reduce the device current consumption and extend the battery lifetime. This is done
by optimizing the consumption during both run-time and idle-time. The low-power timer (LPTIM) helps to reduce
the power consumption specifically while the system is in low-power mode.
The STM32 LPTIM allows the system to perform simple tasks while the power consumption is kept at an absolute
minimum. The LPTIM main features are listed below:
• a 16-bit auto-reload register to set the period, a 16-bit compare register to set the duty-cycle for a PWM
waveform signal output on the timer
• a repetition counter that allows the adjustment of the counter roll-over
• channels that can be functional both as an input or output
• Input-capture mode
• capability to generate DMA requests to retrieve the input-capture counter values and to reprogram part of
the LPTIMER without the intervention of the CPU
• ability to remain fully functional in Stop mode thanks to the Autonomous mode
The LPTIM can be used for timing and for output generation while the STM32 device is in low-power mode. The
LPTIM also features a wide diversity of clock sources that are used to stay active in most power modes, except in
Standby and Shutdown modes.
Thanks to a flexible clock scheme, the LPTIM can be used as a pulse counter by running with an external clock
source. The LPTIM can also wake up the system from low-power modes, and realize a “timeout function” with
extremely‑low power consumption.
The LPTIM provides the basic functions of the STM32 general-purpose timers with the advantage of a very‑low
power consumption. Additionally, when configured in Asynchronous counting mode, the LPTIM keeps running
even when no internal clock source is active.
The LPTIM remains active both in Sleep and Stop modes, and can wake up the STM32 device from these modes.
Conversely, in Standby or Shutdown mode, the LPTIM is powered down and must be completely reinitialized
when the STM32 device exits any of these modes.
The tables below details the main features of the various LPTIM types and the peripheral implementation
on STM32 devices.
LPTIM
Features
Type 1 Type 2 Type 3
Auto-reload register X X X
Compare register X X X
Repetition counter - X X
Input/output channels - - X
Input-capture mode - - X
DMA requests - - X
The LPTIM presents up to 16 external trigger sources with a configurable polarity. The external trigger inputs
embed digital filters to cancel-out possible faulty triggers in noisy operating environments. The LPTIM can be
configured to run:
• in Continuous mode that is used to generate PWM waveforms
• in One-shot mode that is used to generate pulse waveforms
The LPTIM features an Encoder mode. This function enables the LPTIM to interface with incremental quadrature
encode sensors using the Input1 and Input2 of the peripheral. Both inputs feature a glitch-filtering circuitry.
The LPTIM can output different waveform types even when the STM32 device is in specific low-power modes
whereas almost all internal clock sources are turned off. LPTIM_CMP (or LPTIM_CCRx for LPTIM type 3) and
LPTIM_ARR registers, in conjunction with the WAVE bitfield in LPTIM_CFGR and SNGSTRT in LPTIM_CR, are
used to control the output waveform.
The output waveform can be a typical PWM signal with its period and duty-cycle controlled by LPTIM_ARR and
LPTIM_CMP (or LPTIM_CCRx) registers respectively, or the output waveform can be a single pulse with the last
output state defined by the configured waveform.
If the output waveforms are not equal, the SetOnce mode is configured. The polarity of the LPTIM output
is controlled through the WAVPOL bitfield in LPTIM_CFGR for LPTIM type ½, and through CCxP field in
LPTIM_CCMRx for LPTIM type 3.
The main LPTIM advantages compared to any other timer available in the STM32 MCUs are:
• The LPTIM can be fully functional in Stop mode thanks to the Autonomous mode and LPDMA.
• The LPTIM generates events to wake up the device from Stop mode.
A general-purpose timer (TIM) is active in Run and Sleep mode, while it is frozen in Stop mode: both the TIM
state and the register contents are preserved. The TIM directly resumes operations when the device is woken up.
Depending on the selected clock source, the power consumption can be substantially lowered with LPTIM
compared to a general-purpose timer. The table below details the difference between TIM and LPTIM peripherals
during different power modes.
TIM X X - -
LPTIM X X X X
Run X X X X X X
Sleep X X X X X X
Stop - X X - X(2) X
5 Synchronization block
6 Use cases
Source
Counter value 0 1 2 3 4
External pulses
LPTIM UART
3.3 V
User button
STM32L4 Terminal
LSE
The hardware environment used for this example development is the NUCLEO-L476RG.
To establish the serial communication, the STM32 Nucleo-64 board already embeds the ST-LINK debug and
programming probe that features an auxiliary UART interface. This UART is used in this example to interface with
the PC and to offer a Virtual COM port interface. This application does not require any additional hardware. It only
requires a Mini-B USB cable for data transmission.
In Asynchronous mode, the external pulses injected on the LPTIM external Input1 are also used to clock the
LPTIM that looses the first five active edges. This effect is illustrated in the figure below. The LPTIM counter can
be updated by following either a rising or a falling edge, and not both at the same time (because this mode is not
supported in Asynchronous mode).
0 1 2 3 4 5
LPTIM1
16-bit ARR
Input1
Edge
1
detector 16-bit
External pulses Prescaler
counter
0
CKSEL = 1
16-bit compare
Firmware description
• System clock:
– System clock: HSI
– LPTIM1 source clock: external signal
– UART2 source clock: LSE
• LPTIM1:
– Source clock: external source (LPTIM1_input1)
– Prescaler = 1
– Clock polarity: rising edge
– Trigger source: software
• UART2:
– Source clock: LSE
– Baudrate = 1200
– Stop bits = 1
– Parity: none
– Mode: Tx
– Hardware flow control: none
One of the main advantages of the LPTIM is its ability to work in Stop mode, with the possibility to generate
several different waveforms.
Thanks to the PWM in Stop 2 mode, a signal can be generated:
• with a frequency determined by the value of LPTIM_ARR
• with a duty cycle determined by the value of LPTIM_CMP (or LPTIM_CCRx)
There are two ways to update the values of LPTIM_ARR and LPTIM_CMP (or LPTIM_CCRx) that are controlled
by the PRELOAD bit in LPTIM_CFGR:
• PRELOAD = 0: LPTIM_ARR and LPTIM_CMP (or LPTIM_CCRx) are updated immediately after the write
access.
There is a short delay between the write access to the concerned register and the actual update of its value:
the waveform at the LPTIM output is directly impacted by the selected PRELOAD mode.
• PRELOAD = 1: LPTIM_ARR and LPTIM_CMP (or LPTIM_CCRx) are updated at the end of the running
period of the timer.
The write access to the concerned register must be done before the last clock cycle of the period, otherwise
the update is postponed to the next period.
The LPTIM output depends on the continuous comparison between the LPTIM counter and the LPTIM_CMP
(or LPTIM_CCRx) values. Writing directly to the channel register in the middle of a PWM period may generate
spurious waveforms. The synchronization of the LPTIM clock source with the APB clock domain creates some
latency after the write access in LPTIM_CMP (or LPTIM_CCRx).
Any write access during this latency period must be avoided because it leads to unpredictable results. This
latency is the delay between a write in one of the registers (LPTIM_ARR or LPTIM_CMP/CCRx), and the setting
of the corresponding flag (ARROK or CMPOK).
The resynchronization time of the LPTIM core clock with the ABP clock is part of the delay. The delay includes
2 × APB_CLK + 3 × LPTIM_CLK, with LPTIM_CLK being the kernel clock of the LPTIM.
The user must then add 2 × APB_CLK to set the ARROK flag to 1, knowing that this write access must be made
after the LPTIM enabling.
The change in LPTIM_ARR or LPTIM_CMP (or LPTIM_CCRx) is applied only when their flag is set to inform
the application that the APB bus operation has been successfully completed. When ARROK or CMPOK is set,
an interrupt is generated to the STM32 device without any need for polling to check the completion of the write
operation.
Example:
• system clock = 180 MHz and LPTIM_CLK = LSI = 32 kHz
• system cycle = 5.55 ns and LPTIM_CLK cycle = 31.25 µs
The delay is then given by 2 × APB_CLK + 3 × LPTIM_CLK + 2 × APB_CLK =
2 × 5.55 ns + 3 × 31.25 µs + 2 × 5.55 ns = 93.77 µs = 16 896 system cycles
If these ARROK and CMPOK flags do not exist, the application wastes 16 896 cycles to test the update of the
register status.
The figure below summarizes the preload mechanism for the LPTIM_ARR register.
Update at 18 19 20 21 22 23 24 25 26 27 28 29 30 00 01 02 03
the end of count
30 26
Update
18 19 20 21 22 23 24 25 26 27 28 00 01 02 03 04 05
immediately
30 delay 28
Figure 9. Architecture example of PWM generator in Stop 2 mode with legacy mechanism
LPTIM1
LSI
STM32L476RG
Firmware description
• System clock:
– system clock: MSI
– LPTIM1 source clock: LSI
• LPTIM1:
– source clock: internal source (LSI)
– prescaler = 1
– trigger source: LPTIM_ETR pin
– trigger polarity: rising edge
– output polarity: high
Figure 10. Architecture example of PWM generation in Stop 2 mode with LPBAM mechanism
Software trigger
LPTIM1
LSE LPDMA
STM32U585
Firmware description
• System clock:
– system clock: MSI
– LPTIM1 source clock: LSE
• LPTIM1:
– instance: LPTIM1_Channel1
– source clock: LSE
– prescaler = 1
– trigger source: software trigger
• LPDMA
– instance: LPDMA1_Channel0
– source clock: MSI
– linked-list mode: Circular mode
– transfer-event mode: TC event generated at the end of the last linked-list item
– with the LPBAM task: sequence of DMA transactions conditioned by the LPTIM1 request/trigger
No trigger occurs
Count < CMP (or CCRx)
Wakeup
Timeout duration
First trigger Trigger Trigger Trigger Trigger Trigger
starts the LPTIM occurs occurs occurs occurs occurs
The trigger signal can be selected from several sources by the TRIGSEL bits, such as GPIO, RTC alarm,
RTC_TAMP and COMP_OUT. The TRIGSEL bits are used only when TRIGEN[1:0] is different from 00.
In this example, the LPTIM1 is selected because it is available in the Stop 2 mode. The LSE has been chosen as
source clock to run this LPTIM. The Timeout mode is configured through the TIMOUT bit in the LPTIM1_CFGR
register. The LPTIM1 is started after detection of the first active edge on the LPTIM_ETR.
Before entering the low-power mode, the period and the pulse must be loaded to the LPTIM1_ARR and
LPTIM_CMP (or LPTIM_CCRx) registers to set the timeout duration, then the LPTIM1 must be enabled.
Mux trigger
External trigger LPTIM
LSE
STM32L476RG
Firmware description
• System clock:
– System clock: HSI
– LPTIM1 source clock: LSE
• LPTIM1:
– Source clock: internal source (LSE)
– Prescaler = 1
– Trigger source: LPTIM_ETR pin
– Trigger polarity: rising edge
– Counter source: internal source (LSE)
LPTIM1 configuration
• Enable Autonomous mode: Enable or disable the peripheral bus clock when the SRD domain is in DRUN.
After reset,the peripheral clock is disabled when CPUs are in CSTOP.
• Initialize the LPTIM according to the passed parameters.
• Suspend SysTick.
To minimize the power consumption, after starting Input-capture mode, the device enters Stop 2 mode and
DMA transfers data from the LPTIM_CCR1 register to SRD_DmaCapturedValue buffer . When the transfer
is completed, the DMA generates an interruption to wake up the device.
• Enter in Stop mode.
• Check that the system is resumed from Stop 2 mode, clear the STOP flag, and check this last operation has
been correctly done.
• Compute the expected SysTick value.
• Suspend SysTick.
• Get the input-capture value.
Figure 13. Architecture example of input capture in Stop 2 mode (with Autonomous mode)
LPTIM
Software trigger
LSE LPDMA
STM32U5
Firmware description
• System clock:
– system clock: MSI
– LPTIM1 source clock: LSE
• LPTIM1:
– instance: LPTIM1_Channel1
– source clock: LSE
– prescaler = 1
– trigger source: software trigger
• LPDMA
– instance: LPDMA1_Channel0
– source clock: MSI
– transfer direction: peripheral to memory
– transfer-event mode: TC event generated at the end of each block
Revision history
Table 6. Document revision history
Contents
1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
3 LPTIM versus general-purpose timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 LPTIM clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5 Synchronization block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6 Use cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
6.1 Asynchronous pulse counter in Stop 2 mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
6.2 PWM generation in Stop 2 mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6.2.1 Tasks with legacy mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.2.2 Tasks with LPBAM mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.3 Timeout wakeup mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.4 Autonomous input capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
List of tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
List of figures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
List of tables
Table 1. Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Table 2. Features on various LPTIM types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Table 3. LPTIM types on STM32 devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Table 4. TIM versus LPTIM in working modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Table 5. LPTIM clock source on different power mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Table 6. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
List of figures
Figure 1. Input signal and corresponding counter value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 2. Pulse-counter architecture example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 3. STMicroelectronics Virtual COM port device manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 4. Asynchronous pulse-counter increment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 5. Pulse-counter block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 6. PWM generation in Stop mode with legacy mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 7. PWM generation in Stop mode with LPBAM mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 8. Counter timing diagram with and without update immediately . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 9. Architecture example of PWM generator in Stop 2 mode with legacy mechanism . . . . . . . . . . . . . . . . . . . . . 12
Figure 10. Architecture example of PWM generation in Stop 2 mode with LPBAM mechanism . . . . . . . . . . . . . . . . . . . . 14
Figure 11. Timing of timeout wakeup from Stop 2 mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Figure 12. Architecture example of timeout wakeup mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Figure 13. Architecture example of input capture in Stop 2 mode (with Autonomous mode) . . . . . . . . . . . . . . . . . . . . . . 17