Skip to content

arch/stm32: unify LPTIM drivers - #21138

Merged
xiaoxiang781216 merged 11 commits into
apache:masterfrom
raiden00pl:stm32_refactor_all-lptim
Oct 31, 2026
Merged

xiaoxiang781216 merged 11 commits into
apache:masterfrom
raiden00pl:stm32_refactor_all-lptim

Conversation

@raiden00pl

Copy link
Copy Markdown
Member

Summary

unify LPTIM drivers for STM32

Impact

remove duplicated drivers

Testing

Verified on HW:

  • NUCLEO-H743ZI: LPTIM1-5
  • NUCLEO-H7S3L8: LPTIM1-5
  • NUCLEO-N657X0-Q: LPTIM1-5
  • NUCLEO-H533RE: LPTIM1-2
  • NUCLEO-L552ZE: LPTIM1-3
  • NUCLEO-U5A5ZJ-Q: LPTIM1-4
  • NUCLEO-U3C5ZI-Q: LPTIM1-4
  • NUCLEO-U083RC: LPTIM1-3
  • NUCLEO-C562RE: LPTIM1
  • NUCLEO-G071RB: LPTIM1-2
  • NUCLEO-L073RZ: LPTIM1
  • NUCLEO-L476RG: LPTIM1-2,
  • NUCLEO-L496ZG: LPTIM1-2

acassis
acassis previously approved these changes Oct 29, 2026
@raiden00pl
raiden00pl requested a review from btashton as a code owner October 29, 2026 19:59
@github-actions github-actions Bot added Area: CI Arch: arm Issues related to ARM (32-bit) architecture Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Board: arm labels Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

qemu-armv8a

  • Code: .text.nxtask_setup_fork +20 B (+0.0%, 353,788 B)

qemu-intel64

  • Code: .text +16 B (+0.0%, 8,687,025 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Only the STM32L4 PWM driver supports LPTIM PWM and it is the only user
of the LPTIM clock source choice.  Offer both only where they are used.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
Add the missing blank lines after declarations before the file moves
to common/stm32.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
Move the STM32H5 LPTIM driver and its register header to common/stm32
and use it on STM32H7 instead of the local copy.  The RCC clock enable
and reset bits of each instance come from stm32_lptim_clk.h, detected
from the family RCC definitions like stm32_tim_clk.h does for TIM.

The ARR and CMP write wait of the STM32H5 driver is kept for
STM32_HAVE_IP_LPTIM_V2 only, the STM32H7 driver had none.  deinit()
clears the RCC clock enable bit, the STM32H7 driver cleared the bit in
the sleep mode clock enable register.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
Add the timer operations of the STM32L4 LPTIM API to the common driver:
setmode, setclock, setchannel, setclocksource, setpolarity, setcountmode
and getperiod.  init() resets the timer and fails for an instance in use,
start() takes stm32_lptim_mode_t.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
Use the common LPTIM driver for all STM32 families and remove the
STM32L4 copy and its register header.

The LPTIM register layout is described by two IP versions taken from
the reference SVD files:

- STM32_HAVE_IP_LPTIM_V1: IER and CMP, optional OR, CFGR2 and RCR
  (F4, F7, G0, G4, H7, L0, L4, L5, WB, WL5)
- STM32_HAVE_IP_LPTIM_V2: DIER and capture/compare channels
  (C5, H5, H7RS, N6, U0, U3, U5)

The interrupt and the base address of each instance come from the family
headers, the RCC bits from stm32_lptim_clk.h.

Changes compared to the old drivers:

- setclocksource() selects the external or the internal clock only, the
  kernel clock is selected by the RCC configuration as on STM32F7. The
  STM32L4 driver also wrote RCC_CCIPR.
- setchannel() configures the board GPIO_LPTIMn_CHkOUT pins like the
  STM32L4 PWM driver, instead of the GPIO_LPTIMn_OUT_k pinmap
  alternatives.
- STM32H5 LPTIM2 builds, STM32_LPTIM2_BASE was missing the _BASE suffix.

The STM32F7 LPTIM1 interrupt is renamed to STM32_IRQ_LPTIM1 as on the
other families and the STM32U5 APB3RSTR LPTIM reset bits are added.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
The common LPTIM driver works on every STM32 with an LPTIM, so select
the LPTIM instances available on each chip. The instances are taken
from the reference SVD files and checked against the ST CMSIS device
headers (the STM32G070 SVD file lists LPTIM1/2, but the device has no
LPTIM):

- STM32C5: LPTIM1
- STM32F410: LPTIM1
- STM32G0: LPTIM1-2, except G030, G050, G070 and G0B0
- STM32H5: LPTIM1-2, LPTIM3-6 on H56x/H57x only
- STM32H7: LPTIM1-5, LPTIM1-3 on H7A3/H7B0/H7B3
- STM32L0: LPTIM1
- STM32L5: LPTIM1-3
- STM32N6: LPTIM1-5
- STM32U3: LPTIM1-4
- STM32U5: LPTIM2 added
- STM32WB: LPTIM2 added
- STM32WL5: LPTIM1-3

STM32_LPTIM2 and STM32_LPTIM5 depend on the STM32_HAVE_LPTIMn flags
instead of chip lists and STM32_LPTIM6 is added.

The missing STM32F410 LPTIM1 base address, interrupt and RCC bits and
the STM32N6 RCC APB4L registers and LPTIM bits are added.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
ackint() wrote ICR only.  On STM32H7 LPTIM2-5 the write had not taken
effect when the interrupt returned, so every event was taken twice.
Read ISR back after the write.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
LPTIM1 is clocked from MSIK (CCIPR3 LPTIM1SEL reset value), not HCLK,
and LPTIM1 cannot select HCLK at all.  MSIK runs at 4 MHz on this board.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
With -finstrument-functions (ARMV7M_STACKCHECK) each CNT read took longer
than one count, so the getcounter() double read never matched and hung.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
LPTIM1 runs on PCLK1 and LPTIM2-5 on PCLK4, the reset values of the
RCC_D2CCIP2R and RCC_D3CCIPR LPTIM source selections.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
@raiden00pl
raiden00pl force-pushed the stm32_refactor_all-lptim branch from 555b23b to a17acbf Compare October 30, 2026 08:01
jerpelea
jerpelea previously approved these changes Oct 30, 2026
@jerpelea

Copy link
Copy Markdown
Contributor

@raiden00pl thanks for the cleanup

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@raiden00pl please fix style issue.

LPTIM V2 DIER must only be modified while the timer is enabled and the
write completes with DIEROK.  Enable the timer for the write if needed,
which fixes a restarted LPTIM timer on STM32C5 never firing.

Signed-off-by: raiden00pl <[email protected]>
Assisted-by: Claude Code
@raiden00pl
raiden00pl dismissed stale reviews from jerpelea and xiaoxiang781216 via 3afb875 October 30, 2026 12:10
@raiden00pl
raiden00pl force-pushed the stm32_refactor_all-lptim branch from a17acbf to 3afb875 Compare October 30, 2026 12:10
@xiaoxiang781216
xiaoxiang781216 merged commit 10c3f04 into apache:master Oct 31, 2026
59 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Area: CI Board: arm Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants