Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR lets HardwareTimer use the full counter width of 32-bit timers.
This PR fixes/implements the following bugs/features
setOverflow()takes andgetOverflow()returnsuint64_tMotivation
setOverflow()took auint32_tand writes ARR = value - 1. The largestperiod 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:
UINT32_MAX: the period is now auint64_t. Tickrequests above the native limit (2¹⁶ or 2³² ticks) saturate. Time and
frequency requests are bounded before multiplication.
getOverflow()asuint64_t: done. It returns 2³² ticks when ARR is0xFFFFFFFF.0x10000are 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.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()andsetCaptureCompare()with 0 Hz leavethe 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:
0xFFFFFFFE0xFFFFFFFF0xFFFFsetOverflow(1ULL << 32)0xFFFFFFFF0xFFFFWith this PR
getOverflow()returned0x100000000. The TIM3 controlgave 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:
Sweep, SoftwareSerialSoftwareSerialExample.setOverflow()andgetOverflow()in all three formats on TIM2.
--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 -wof the sources is empty). Rebuilding withthis 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 onthis branch with
mainat3ceb5e2:The documentation change passes the strict MkDocs build, with the
packages from
requirements-docs.txt(MkDocs 1.6.1, mkdocs-material9.7.7):
Closing issues
Fixes #2071