From 3420604e2e7c41f9a4894e3c87d3e9ad4038f22d Mon Sep 17 00:00:00 2001 From: Hannes Pauli Date: Fri, 4 Dec 2026 12:56:46 +0000 Subject: [PATCH] fix(timer): use the configured period in capture examples 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 --- .../Frequency_Dutycycle_measurement.ino | 49 +++++++++---------- .../InputCapture/InputCapture.ino | 29 ++++++----- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino b/examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino index 397639f..6f5b8b6 100644 --- a/examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino +++ b/examples/Peripherals/HardwareTimer/Frequency_Dutycycle_measurement/Frequency_Dutycycle_measurement.ino @@ -20,8 +20,10 @@ #define pin D2 uint32_t channelRising, channelFalling; -volatile uint32_t FrequencyMeasured, DutycycleMeasured, LastPeriodCapture = 0, CurrentCapture, HighStateMeasured; +volatile uint32_t FrequencyMeasured, DutycycleMeasured, LastPeriodCapture = 0, CurrentCapture; +volatile uint64_t HighStateMeasured; uint32_t input_freq = 0; +uint64_t capturePeriod; volatile uint32_t rolloverCompareCount = 0; HardwareTimer *MyTim; @@ -32,18 +34,11 @@ HardwareTimer *MyTim; void TIMINPUT_Capture_Rising_IT_callback(void) { CurrentCapture = MyTim->getCaptureCompare(channelRising); - /* frequency computation */ - if (CurrentCapture > LastPeriodCapture) - { - FrequencyMeasured = input_freq / (CurrentCapture - LastPeriodCapture); - DutycycleMeasured = (HighStateMeasured * 100) / (CurrentCapture - LastPeriodCapture); - } - else if (CurrentCapture <= LastPeriodCapture) - { - /* 0x1000 is max overflow value */ - FrequencyMeasured = input_freq / (0x10000 + CurrentCapture - LastPeriodCapture); - DutycycleMeasured = (HighStateMeasured * 100) / (0x10000 + CurrentCapture - LastPeriodCapture); - } + const uint64_t ticks = CurrentCapture > LastPeriodCapture ? + uint64_t(CurrentCapture) - LastPeriodCapture : + capturePeriod + CurrentCapture - LastPeriodCapture; + FrequencyMeasured = input_freq / ticks; + DutycycleMeasured = (HighStateMeasured * 100) / ticks; LastPeriodCapture = CurrentCapture; rolloverCompareCount = 0; @@ -71,15 +66,9 @@ void TIMINPUT_Capture_Falling_IT_callback(void) /* prepare DutyCycle computation */ CurrentCapture = MyTim->getCaptureCompare(channelFalling); - if (CurrentCapture > LastPeriodCapture) - { - HighStateMeasured = CurrentCapture - LastPeriodCapture; - } - else if (CurrentCapture <= LastPeriodCapture) - { - /* 0x1000 is max overflow value */ - HighStateMeasured = 0x10000 + CurrentCapture - LastPeriodCapture; - } + HighStateMeasured = CurrentCapture > LastPeriodCapture ? + uint64_t(CurrentCapture) - LastPeriodCapture : + capturePeriod + CurrentCapture - LastPeriodCapture; } @@ -115,15 +104,21 @@ void setup() // Configure rising edge detection to measure frequency MyTim->setMode(channelRising, TIMER_INPUT_FREQ_DUTY_MEASUREMENT, pin); - // With a PrescalerFactor = 1, the minimum frequency value to measure is : TIM counter clock / CCR MAX - // = (SystemCoreClock) / 65535 - // Example on Nucleo_L476RG with systemClock at 80MHz, the minimum frequency is around 1,2 khz - // To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. + // At prescale factor 1, the minimum measurable frequency is the timer + // clock divided by its full period. A larger prescale factor lowers that + // frequency at the cost of precision. // The maximum frequency depends on processing of both interruptions and thus depend on board used // Example on Nucleo_L476RG with systemClock at 80MHz the interruptions processing is around 10 microseconds and thus Max frequency is around 100kHz uint32_t PrescalerFactor = 1; MyTim->setPrescaleFactor(PrescalerFactor); - MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover + uint64_t maxPeriod = 0x10000; +#if defined(IS_TIM_32B_COUNTER_INSTANCE) + if (sizeof(MyTim->getOverflow()) == sizeof(uint64_t) && IS_TIM_32B_COUNTER_INSTANCE(Instance)) { + maxPeriod = uint64_t{1} << 32; + } +#endif + MyTim->setOverflow(maxPeriod); + capturePeriod = MyTim->getOverflow(); MyTim->attachInterrupt(channelRising, TIMINPUT_Capture_Rising_IT_callback); MyTim->attachInterrupt(channelFalling, TIMINPUT_Capture_Falling_IT_callback); MyTim->attachInterrupt(Rollover_IT_callback); diff --git a/examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino b/examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino index 561c4f3..c2134d9 100644 --- a/examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino +++ b/examples/Peripherals/HardwareTimer/InputCapture/InputCapture.ino @@ -20,20 +20,17 @@ uint32_t channel; volatile uint32_t FrequencyMeasured, LastCapture = 0, CurrentCapture; uint32_t input_freq = 0; +uint64_t capturePeriod; volatile uint32_t rolloverCompareCount = 0; HardwareTimer *MyTim; void InputCapture_IT_callback(void) { CurrentCapture = MyTim->getCaptureCompare(channel); - /* frequency computation */ - if (CurrentCapture > LastCapture) { - FrequencyMeasured = input_freq / (CurrentCapture - LastCapture); - } - else if (CurrentCapture <= LastCapture) { - /* 0x1000 is max overflow value */ - FrequencyMeasured = input_freq / (0x10000 + CurrentCapture - LastCapture); - } + const uint64_t ticks = CurrentCapture > LastCapture ? + uint64_t(CurrentCapture) - LastCapture : + capturePeriod + CurrentCapture - LastCapture; + FrequencyMeasured = input_freq / ticks; LastCapture = CurrentCapture; rolloverCompareCount = 0; } @@ -66,15 +63,21 @@ void setup() // Configure rising edge detection to measure frequency MyTim->setMode(channel, TIMER_INPUT_CAPTURE_RISING, pin); - // With a PrescalerFactor = 1, the minimum frequency value to measure is : TIM counter clock / CCR MAX - // = (SystemCoreClock) / 65535 - // Example on Nucleo_L476RG with systemClock at 80MHz, the minimum frequency is around 1,2 khz - // To reduce minimum frequency, it is possible to increase prescaler. But this is at a cost of precision. + // At prescale factor 1, the minimum measurable frequency is the timer + // clock divided by its full period. A larger prescale factor lowers that + // frequency at the cost of precision. // The maximum frequency depends on processing of the interruption and thus depend on board used // Example on Nucleo_L476RG with systemClock at 80MHz the interruption processing is around 4,5 microseconds and thus Max frequency is around 220kHz uint32_t PrescalerFactor = 1; MyTim->setPrescaleFactor(PrescalerFactor); - MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover + uint64_t maxPeriod = 0x10000; +#if defined(IS_TIM_32B_COUNTER_INSTANCE) + if (sizeof(MyTim->getOverflow()) == sizeof(uint64_t) && IS_TIM_32B_COUNTER_INSTANCE(Instance)) { + maxPeriod = uint64_t{1} << 32; + } +#endif + MyTim->setOverflow(maxPeriod); + capturePeriod = MyTim->getOverflow(); MyTim->attachInterrupt(channel, InputCapture_IT_callback); MyTim->attachInterrupt(Rollover_IT_callback); MyTim->resume();