Skip to content

fix(timer): use the configured period in capture examples - #73

Open
hp-8472 wants to merge 1 commit into
stm32duino:mainfrom
hp-8472:capture-examples-overflow
Open

hp-8472 wants to merge 1 commit into
stm32duino:mainfrom
hp-8472:capture-examples-overflow

Conversation

@hp-8472

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

Copy link
Copy Markdown

Summary

The two HardwareTimer capture examples hard-code a period of 0x10000
ticks in their wrap arithmetic. They now read the configured period
back from the timer and use it.

On a 32-bit timer they request the full period of 2³² ticks when the
core supports it (getOverflow() returns uint64_t, stm32duino/Arduino_Core_STM32#3138). With older cores they keep 0x10000, so the sketches build
with both.

Validation

Built with Arduino CLI 1.6.0, --warnings all, no warnings:

  • NUCLEO-F446RE and NUCLEO-G474RE against the core with the companion
    PR applied.
  • NUCLEO-F446RE against the unmodified core
    (stm32duino/Arduino_Core_STM32 3ceb5e2).

Run on a NUCLEO-F446RE (90 MHz timer clock). For the test the input pin
was PB10 (TIM2_CH3, 32-bit) instead of D2, and a TIM3_CH2 PWM on PC7,
jumpered to PB10, provided the signal. The measurement code was unchanged.

Signal Core with the companion PR Unmodified core
2 kHz, 25 % 2000 Hz, 25 % 2000 Hz, 25 %
1 Hz, 25 % 1 Hz, 25 % over 113 s, two counter wraps 0 (below the 16-bit limit, as before)

Evidence for this run (plans, test sketches, the diff to the submitted
examples, job reports and UART logs):
https://github.com/agentic-hil/agentic-hil/tree/d32add7d66d9a8fb056b649ebbe24c0fd29be31c/examples/nucleo-f446re_demo/stm32duino-hardwaretimer-32bit/evidence/examples-bench

Both sketches gave the same values. The core change itself was measured
on the same board. The evidence is public:
https://github.com/agentic-hil/agentic-hil/tree/134403e46078e983a43f3513d022727636d76c95/examples/nucleo-f446re_demo/stm32duino-hardwaretimer-32bit

AI assistance: OpenAI Codex (gpt-6.2-sol) wrote the sketch changes.
Claude Code (claude-opus-5-5) ran the builds and the hardware test
and wrote this text.
I reviewed the diff and the bench results before submitting.

Related

Core PR: stm32duino/Arduino_Core_STM32#3138. Refs stm32duino/Arduino_Core_STM32#2071.

InputCapture and Frequency_Dutycycle_measurement assumed a timer period
of 0x10000 ticks when a capture wrapped. Read the period back with
getOverflow() and use it in the wrap arithmetic.

With a core whose getOverflow() returns uint64_t, a 32-bit timer is
set to its full period of 2^32 ticks. With older cores the period
stays 0x10000. The high-state duration is kept in 64 bits so that the
duty-cycle product does not overflow on 32-bit timers.

Refs: stm32duino/Arduino_Core_STM32#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.

1 participant