fix(HardwareTimer): use ARR+1 in getCaptureCompare percent/resolution#3019
Open
94xhn wants to merge 1 commit into
Open
fix(HardwareTimer): use ARR+1 in getCaptureCompare percent/resolution#301994xhn wants to merge 1 commit into
94xhn wants to merge 1 commit into
Conversation
getCaptureCompare() divided by LL_TIM_GetAutoReload() (ARR) for PERCENT_COMPARE_FORMAT and all RESOLUTION_xB_COMPARE_FORMAT cases, while its counterpart setCaptureCompare() multiplies by ARR+1, per the code's own comment: "PWM reach 100% with CCRx value strictly greater than ARR (So ARR+1 in our case)". This makes get/set inconsistent: reading back a duty cycle written via setCaptureCompare(ch, 100, PERCENT_COMPARE_FORMAT) does not return 100, and overshoots more as ARR gets smaller (e.g. ARR=9 reads back 111, ARR=1 reads back 200). When ARR is 0 (a reachable state, e.g. after setOverflow(1, TICK_FORMAT)), the getter performs a division by zero. Use ARR+1 in the getter too, matching the setter and its own documented rationale. Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com>
This was referenced Jul 11, 2026
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.
Pull Request template
Summary
HardwareTimer::getCaptureCompare()andHardwareTimer::setCaptureCompare()are meant to be inverse operations: one converts a percent/N-bit duty-cycle value to a CCR register tick value, the other converts it back. They currently use inconsistent formulas, so a round trip does not return the original value, and a divide-by-zero is reachable.This PR fixes/implements the following bugs/features
getCaptureCompare()usesARRas the "full scale" divisor forPERCENT_COMPARE_FORMATand everyRESOLUTION_xB_COMPARE_FORMATcase, whilesetCaptureCompare()usesARR + 1for the same conversions (per the code's own comment). This makes get/set round-trips wrong and, whenARR == 0, divides by zero.Motivation
In
libraries/SrcWrapper/src/HardwareTimer.cpp:setCaptureCompare()(correct, and the source of truth for the intended formula):getCaptureCompare()(before this PR, missing the+ 1):Because the getter divides by
ARRinstead ofARR + 1:PERCENT_COMPARE_FORMAT: callingsetCaptureCompare(ch, 100, PERCENT_COMPARE_FORMAT)and then immediatelygetCaptureCompare(ch, PERCENT_COMPARE_FORMAT)should read back100, but reads back111whenARR == 9, and200whenARR == 1. The same discrepancy exists for everyRESOLUTION_xB_COMPARE_FORMATvalue.ARR == 0(a legal, reachable timer state, e.g. right aftersetOverflow(1, TICK_FORMAT)).LL_TIM_GetAutoReload()returns0andgetCaptureCompare(ch, PERCENT_COMPARE_FORMAT)(or any resolution format) computesx / 0.The fix simply adds the missing
+ 1in the getter, matching the setter and the setter's own documented rationale. No public API/signature changes.Validation
Ensure CI build is passed.
Since this is pure integer-arithmetic logic (no MCU register access besides reading
ARR/CCRthrough already-abstracted LL calls), I extracted both formulas verbatim into a standalone host-side C program and ran it on a desktop toolchain (independent of any STM32 board) to confirm the discrepancy before the fix and the correct behavior after:The same overshoot pattern reproduces for the
RESOLUTION_8B_COMPARE_FORMATbranch (full_scale = 255):ARR=9reads back283instead of255,ARR=1reads back510instead of255.I do not own STM32 hardware set up for this project's test rig, so I have not run this on-target; the change only touches a pure integer formula that is otherwise identical in structure/behaviour to the already-existing, already-shipped setter, so the on-target risk is limited to this exact expression change.
Code formatting
Closing issues
N/A – found while reading the timer duty-cycle conversion code, not tied to an existing issue.
Disclosure: I used an AI coding assistant (Claude) to help read through the codebase, extract the reproduction, and draft this PR. All findings were verified by me by compiling and running the standalone reproduction shown above.