Skip to content

feat(timer)!: support full 32-bit timer periods - #3138

Open
hp-8472 wants to merge 1 commit into
stm32duino:mainfrom
hp-8472:hardwaretimer-32bit
Open

hp-8472 wants to merge 1 commit into
stm32duino:mainfrom
hp-8472:hardwaretimer-32bit

Conversation

@hp-8472

@hp-8472 hp-8472 commented Dec 4, 2026 •

Copy link
Copy Markdown

Summary

This PR lets HardwareTimer use the full counter width of 32-bit timers.

This PR fixes/implements the following bugs/features

  • Feature: full 32-bit period, count and compare values on 32-bit timers
  • Breaking changes: setOverflow() takes and getOverflow() returns uint64_t

Motivation

setOverflow() took a uint32_t and writes ARR = value - 1. The largest
period it could set was 2³² - 1 ticks, one tick short of the counter
width. Input capture code that subtracts 32-bit captures across the wrap
was then off by one tick per wrap (#2071).

This revisits #2072. The points raised there are handled as follows:

  • Check above UINT32_MAX: the period is now a uint64_t. Tick
    requests above the native limit (2¹⁶ or 2³² ticks) saturate. Time and
    frequency requests are bounded before multiplication.
  • getOverflow() as uint64_t: done. It returns 2³² ticks when ARR is
    0xFFFFFFFF.
  • Examples: the two capture examples that hard-code a period of
    0x10000 are updated in a companion PR to STM32Examples (fix(timer): use the configured period in capture examples STM32Examples#73). The other callers in the core and its libraries (analog.cpp,
    Tone.cpp, Servo, SoftwareSerial) pass 32-bit values and need no change.
  • "HardwareTimer uses all timers like 16-bit timers": the limit is now
    taken per instance. 16-bit timers keep the 0xFFFF limits, the
    microsecond conversion and the prescaler selection, including the
    headroom for CCR = ARR + 1 at 100% PWM. The initial period stays
    0xFFFF on every timer. CCR saturates at the native maximum, so at
    maximum ARR a 100% duty cycle leaves one tick low, as before for
    16-bit timers. The documentation is updated accordingly.

Behaviour changes beyond the types: on 32-bit timers the time and
frequency formats can choose a smaller prescaler and a larger ARR than
before. setPrescaleFactor() limits the factor to 1–65536.
setOverflow(), setCount() and setCaptureCompare() with 0 Hz leave
the register unchanged instead of dividing by zero.

Validation

Hardware test on a NUCLEO-F446RE (TIM2 32-bit, TIM3 16-bit, 90 MHz timer
clock). Two boots per image, 20 wrap pairs per case and boot, expected
interval 20,000 ticks:

Core Case ARR Capture delta modulo counter width
unmodified TIM2, largest period the API accepts 0xFFFFFFFE 20,001 (all 40 pairs)
unmodified TIM2, ARR written directly 0xFFFFFFFF 20,000
unmodified TIM3, 16-bit control 0xFFFF 20,000
this PR TIM2, setOverflow(1ULL << 32) 0xFFFFFFFF 20,000 (all 40 pairs)
this PR TIM3, 16-bit control 0xFFFF 20,000

With this PR getOverflow() returned 0x100000000. The TIM3 control
gave the same result on both cores. Firmware, test plan, all 280 pairs,
console logs and the analyzer are public:
https://github.com/agentic-hil/agentic-hil/tree/134403e46078e983a43f3513d022727636d76c95/examples/nucleo-f446re_demo/stm32duino-hardwaretimer-32bit

The board was flashed, reset and read through ST-LINK/OpenOCD by
Agentic Hardware-in-the-Loop,
which we maintain. A jumper PB10–PC7 connects the stimulus and capture
timers, which share one clock.

AI assistance: OpenAI Codex (gpt-6.2-sol) wrote the patch, the test
firmware, the test plan and the analyzer. Claude Code (claude-opus-5-5)
ran the formatting and documentation checks, removed one Markdown line
and wrote parts of this text. I reviewed the diff and the bench results before submitting.

Compile checks with Arduino CLI 1.6.0 and the toolchain of package 3.1.0:

  • NUCLEO-F446RE: the test firmware, Servo Sweep, SoftwareSerial
    SoftwareSerialExample.
  • NUCLEO-G474RE: a sketch that calls setOverflow() and getOverflow()
    in all three formats on TIM2.
  • Both boards: the two updated STM32Examples capture sketches with
    --warnings all, no warnings.

The measured images were built from the published patch, before AStyle
formatting and before one Markdown fix in the documentation. Formatting
changed only the indentation of one continuation line in
HardwareTimer.cpp (diff -w of the sources is empty). Rebuilding with
this PR's patch gives byte-identical ELF files for both boards.

The 16-bit prescaler selection was checked by calculation, not on the
bench: for 32,768 µs at 90 MHz it still gives PSC = 45 and ARR = 64,110,
and 100% PWM gives CCR = 64,111 (above ARR), as on the unmodified core.

Code formatting

AStyle 3.1, built from the official astyle_3.1_linux.tar.gz, run on
this branch with main at 3ceb5e2:

$ astyle --version
Artistic Style Version 3.1
$ python3 CI/astyle/astyle.py -g
Unchanged  cores/arduino/stm32/HardwareTimer.cpp
Unchanged  libraries/SrcWrapper/inc/HardwareTimer.h
$ git diff --check
$

The documentation change passes the strict MkDocs build, with the
packages from requirements-docs.txt (MkDocs 1.6.1, mkdocs-material
9.7.7):

$ "$HOME/.venvs/stm32duino-docs/bin/mkdocs" build --strict
INFO    -  Documentation built in 2.42 seconds

Closing issues

Fixes #2071

HardwareTimer used every timer as a 16-bit timer. setOverflow() took a
uint32_t, so a 32-bit timer could not be set to its full period of
2^32 ticks (ARR = 0xFFFFFFFF). Captures subtracted across the wrap
then differed by one tick per wrap.

Use the native counter width of the instance
(IS_TIM_32B_COUNTER_INSTANCE) as the limit for ARR, CNT and CCR. Keep
the prescaler limited to its 16-bit register. Widen the period, count
and compare arithmetic to 64 bits and saturate results at the hardware
or return-type limit.

16-bit timers keep their limits, their microsecond conversion and
their prescaler selection. The initial period stays 0xFFFF on all
timers, so a 32-bit timer only uses the wider range when a sketch
asks for it.

BREAKING CHANGE: setOverflow() now takes a uint64_t and getOverflow()
returns a uint64_t. Calls with 32-bit arguments still compile. Code
that names the exact member-function types must be updated, and
objects built against the old header must be rebuilt. A getOverflow()
result above UINT32_MAX (32-bit timers only) is truncated when stored
in a uint32_t. On 32-bit timers, MICROSEC_FORMAT and HERTZ_FORMAT now
select the smallest prescale factor that fits, so the prescaler and
ARR can differ from earlier releases for the same request.

Fixes: stm32duino#2071
Assisted-by: OpenAI Codex: gpt-6.2-sol
Assisted-by: Claude Code: claude-opus-5-5
Assisted-by: agentic-hil: https://github.com/agentic-hil/agentic-hil
Signed-off-by: Hannes Pauli <mail@hannes-pauli.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HardwareTimer: support 32 bit timers

1 participant