Repository navigation
Conversation
Map unsupported nonzero compares to zero using the prescaler-specific hardware minimum, while retaining CMP1 = 0 and normal preload timing. Ensure zero takes precedence over near-full duty at small periods. Centralize full-duty output swapping in the driver and delegate both SPIN and SHIELD duty updates to it. Route frequency rescaling through the same setter so it cannot bypass the compare boundary handling. Validation: STLink build; 512 software register/state checks across CKPSC 0..7; 1,608 MHO984 center-aligned PWMC settings across all eight prescalers, both APIs and buck/boost, including 880 zero-duty checks. Hardware checks cover settled states; single-cycle transitions remain uncharacterized.
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
At very low duty cycle, when no duty cycle bound are set, it is possible to get PWM duty cycle reversal. This PR is about changing the low level driver so it enforces correctly the minimal duty cycle bound so you cannot get undesired reversal from the user application no matter what.
Map unsupported nonzero compares to zero using the prescaler-specific hardware minimum, while retaining CMP1 = 0 and normal preload timing. Ensure zero takes precedence over near-full duty at small periods.
Centralize full-duty output swapping in the driver and delegate both SPIN and SHIELD duty updates to it. Route frequency rescaling through the same setter so it cannot bypass the compare boundary handling.
Validation: STLink build; 512 software register/state checks across CKPSC 0..7; 1,608 MHO984 center-aligned PWMC settings across all eight prescalers, both APIs and buck/boost, including 880 zero-duty checks. Hardware checks cover settled states; single-cycle transitions remain uncharacterized.
Proposed fix: enforce HRTIM minimum compares while retaining zero duty
Branch: fix/hrtim-low-duty-reversal
Commit: 9178dfa — Fix low-duty HRTIM PWM reversal
Base: main / origin/main at 8111a0d (fetched before branch creation).
The branch contains exactly one commit above main. The commit changes four
files: hrtim.c, hrtim.h, PwmHAL.cpp and Power.cpp. Bench firmware, measurement
scripts, raw results and unrelated working-tree changes remain uncommitted.
Problem and proposed behavior
At 100 kHz, center-aligned PWMC showed a real output reversal: CMP1=32 and
smaller tested nonzero values held the controlled output high near 3.3 V.
CMP1=33 and CMP1=0 returned low. Both SPIN/SHIELD and buck/boost reproduced it.
This was confirmed using voltage levels, not a misleading duty reading on DC.
The affected nonzero compares are outside RM0440's supported compare range;
the exact internal mechanism behind the observed 32/33 boundary is unproven.
The shared setter now suppresses unsupported nonzero compares to CMP1=0,
using the prescaler-dependent minimum already defined in the driver:
CKPSC 0..7 -> 96, 48, 24, 12, 6, 3, 3, 3 counts.
It preserves zero in both alignments and the normal compare/preload timing.
Lower-limit handling precedes near-full handling, so zero cannot become full
duty at small legal periods. Near-full output swapping is centralized in the
setter; both APIs delegate to it, with SHIELD user limits applied first.
Frequency rescaling also uses this setter to enforce the same bounds and
preserve full-duty state. No forced output levels, event-mask reconfiguration,
update-gating changes, forced update or interrupt lock is added to the setter.
Timing regression and the design decision
An earlier forced-level implementation disconnected timer output events at
zero and restored them on PWM recovery. Its transition medians were about
2.2–3.7 us, with an initial observed sample of 5.43 us. That design was removed
because of its execution cost. A same-function experiment with the benchmark
outer lock disabled found that removing the setter lock saved only 10 cycles
(0.059 us); locking did not explain the multi-microsecond transition cost.
The benchmark's outer lock also affected measured timing, despite being outside
the timed window, so final timings use no outer lock.
Final driver-only timing, Cortex-M4 DWT at 170 MHz, same-binary comparison:
Original two-write setter replica: 10 cycles = 0.059 us median.
Current ordinary PWM update: 37 cycles = 0.218 us median.
Current zero-to-PWM update: 37 cycles = 0.218 us median.
Current repeated zero/PWM-to-zero: 31 cycles = 0.182 us median.
Current entry/exit of full duty: 85–96 cycles = 0.500–0.565 us median.
The special zero-to-PWM penalty is eliminated. Ordinary updates retain a
27-cycle (0.159 us) overhead versus the original two-write replica; the fix is
not execution-time neutral. Timings cover the shared driver, not whole API
calls. Each case used 32 warmups and 512 saved samples, with final DSB included
and empty-call median overhead subtracted. Physical outputs were disabled.
These sampled timings are not worst-case execution-time guarantees, and the
baseline is a compiled replica rather than a separate old firmware image.
Why center-aligned zero remains a documented uncertainty
RM0440 Rev 9, p.893 Table 232, specifies the prescaler-dependent nonzero compare
limits. Its p.901 null-duty exception permits zero in CMP1/CMP3 under specific
conditions: the output is set by PERIOD, reset by the local compare, and that
compare is not used to control another timer unit.
The left-aligned configuration matches that pattern. The center-aligned setup
uses SET=NONE and RESET=CMP1, with direction-dependent output actions described
on p.915. It therefore does not meet the stated conditions of that exception.
The manual alone does not guarantee CMP1=0 for our center-aligned configuration.
This does not demonstrate a failure at zero; it leaves a specification gap.
The chosen tradeoff is to retain the previously observed compare-zero behavior
and validate it experimentally, avoiding the costly forced-level transition.
Passing the tests below does not expand the reference manual's guarantee.
The earlier ES0430 Rev 9 review found no matching erratum for the retained
configuration; greater-than compare mode was disabled on this board.
Validation of the final implementation
Hardware: SPIN 1.2.0 / STM32G474 (recorded silicon revision X), TWIST 1.4.2
logical shield, PWMC/LEG2, Rigol MHO984. Center-aligned voltage mode, DTS
100 ns rising/falling complementary dead time, user minimum zero. PWMC1 ->
CH2, PWMC2 -> CH1; trigger CH1 buck / CH2 boost. Measurements used native scope
duty/voltage queries and frequency at PWM points; no waveform downloads.
Build and software checks
STLink build passed. 512 register/state checks compiled the actual setter
with mocked register/swap operations: all CKPSC values, cached/fallback
minima, below/at minimum, zero/full recovery, and minimum legal periods.
Focused center-aligned hardware matrix
292/292 settings passed, 73 per API/convention, including 148 zero checks.
Covered cold startup without a duty write, repeated zero, raw/float duty,
32/33 and 95/96/97 boundaries, ascending/descending duty, PWM/full-to-zero,
recovery, hot swap at zero, and 100 -> 200 -> 100 kHz rescaling.
At zero: active VMAX <= 0.19493 V, companion VMIN >= 3.12680 V.
All-prescaler frequency sweeps
1,608/1,608 settings passed across 32 API/convention/prescaler configurations,
including 880 zero checks. Each CKPSC was selected before initialization and
verified in both master/TIMC hardware registers on every point. Frequency
changes used the public API and retained that prescaler; automatic selection
of prescalers was not tested.
CKPSC Minimum CMP Requested frequency range Points per direction*
0 96 100000–1000000 Hz 4 ascending, 3 descending
1 48 50000–1000000 Hz 5 ascending, 4 descending
2 24 25000–1000000 Hz 4 ascending, 3 descending
3 12 12500–1000000 Hz 5 ascending, 4 descending
4 6 6250–1000000 Hz 5 ascending, 4 descending
5 3 3125–1000000 Hz 5 ascending, 4 descending
6 3 1563–1000000 Hz 5 ascending, 4 descending
7 3 782–1000000 Hz 5 ascending, 4 descending
*Distinct sampled frequencies; these are not continuous/exhaustive sweeps.
At each ascending point: zero held through rescaling, 50% PWM and frequency,
return to zero, minimum-1 suppression, minimum/minimum+1, full duty and zero.
Descending points check zero, PWM recovery and zero again. Checked dead-time
registers and complementary duty sums. No reversal was observed.
At zero: active VMAX <= 0.26160 V, companion VMIN >= 3.10090 V.
Frequency quantization is recorded: CKPSC 7 produced up to about 1.012 MHz
for a 1 MHz request, consistent with its hardware timer periods.
The final revision therefore passed 1,900 hardware settings across the two
campaigns, including 1,028 zero checks. These counts include repeated conditions
and should not be interpreted as that many unique timer configurations.
All outputs were stopped at the end.
Earlier characterization and excluded evidence
Original characterization comprised 504 left-aligned and 568 center-aligned
points, plus 56 repeated boundary/recovery settings with three readings each.
The left-aligned original sweep did not show reversal. Another 584 checks
validated the superseded forced-level design; they are not counted as final-fix
validation. Initial scope-gain mismatch and stale-measurement attempts were
retained as diagnostics and excluded from the successful final campaigns.
The validated all-prescaler run required zero measurement rechecks.
Remaining limits
Measurements were taken after 350–400 ms settling. They support steady-state
behavior and recovery, but do not rule out a single-cycle wrong-level pulse
while entering/leaving zero. No phase-locked transition/glitch captures were
performed. Final-fix electrical testing covered one PWMC pair on one board,
center alignment, voltage mode, the listed frequencies and DTS dead time.
Left alignment was not electrically retested for the final revision; neither
were other timer units, current mode, other dead times or other silicon samples.
RM0440's conditional zero-duty wording remains the key specification caveat.