Skip to content

[Feature] HRTIM per-unit switching frequency (leg-frequency) - #165

Draft
luizvilla wants to merge 9 commits into
owntech-foundation:mainfrom
luizvilla:feature/hrtim-per-unit-frequency
Draft

luizvilla wants to merge 9 commits into
owntech-foundation:mainfrom
luizvilla:feature/hrtim-per-unit-frequency

Conversation

@luizvilla

Copy link
Copy Markdown
Member

Summary

Each HRTIM timing unit can run at its own switching frequency, independently of the master. For example, the two legs of an inverter can run at 50 kHz while the DC/DC legs stay at 200 kHz.

The silicon already supports it: each timer has its own period register (RM0440 §27.4.1, "7 independent switching frequencies"). The driver assumed a single frequency, and it had a reset bug that prevented any unit from running at another frequency than the master.

  • HRTIM driver
    • hrtim_tu_frequency_set(): own frequency for a unit, before its init.
    • hrtim_tu_change_frequency(): runtime change of such a unit. CMP1 (duty cycle), CMP3 (ADC trigger) and the user duty limits keep their ratio to the period.
    • hrtim_cnt_reset_sync(): restarts several counters with one register write.
    • hrtim_frequency_set() and hrtim_change_frequency() leave own-frequency units alone.
  • Spin API (PwmHAL): initUnitFrequency(), setFrequency(pwmX, f), getFrequency(pwmX), hasOwnFrequency(), resetCounters().
  • Shield API
    • Optional device-tree property leg-frequency on a leg.
    • PowerAPI::setFrequency(leg, f) and getFrequency(leg).
    • Legs sharing a leg-frequency are started aligned.
    • setFrequency(f) keeps its meaning: master and legs on the common frequency.
  • Docs: shield_introduction.md and spin_pwm.md.

Bugs fixed on the way

  • Units at another frequency were still reset by the master (commit 3). hrtim_tu_init() sets the reset of timer D/E to master CMP3/CMP4. The "not at the master frequency" branch of hrtim_phase_shift_set() only removed the master period reset. Timer D at 50 kHz with a 200 kHz master was therefore cut every 5 µs.
  • hrtim_change_frequency() rounding (commit 1): (f % new_f) * (32 / new_f) is always 0. At 150 kHz the master period was 18128 instead of 18133.
  • hrtim_change_frequency() did not rescale CMP3, so the ADC trigger moved within the period when the frequency changed.

Constraints

  • A unit's own frequency must be ≥ min-frequency, because the clock prescaler is shared. A lower value is refused, and the leg keeps the common frequency (with a message).
  • Own-frequency units are not reset by the master: phase shift and current mode are refused on them.
  • Behaviour without leg-frequency is unchanged: Twist and Ownverter builds behave as before.

Tests

  • Host register-level tests (branch test/hrtim-host-tests, kept out of this PR): hrtim.c built for the PC with the real LL headers and HRTIM1/RCC mapped in RAM. Covers:

    • periods and per-unit frequency;
    • removal of the master reset;
    • runtime change with CMP1/CMP3 rescaling;
    • synchronous counter reset;
    • up-down output conventions.

    The rounding and reset checks fail on main.

  • Builds: Twist 1.4.2 and Ownverter 1.1.0, with and without leg-frequency. O2 1.1.2 fails identically on main (unrelated).

  • On hardware (Spin on a Twist 1.4.2, power off, measured over SWD: pin capture at 100 ns per sample, registers and counters read while running):

    • Spin API: PWMA 200 kHz (common), PWMC 100 kHz, PWMD/PWME 50 kHz. Measured 199 988 / 99 994 / 49 997 / 49 997 Hz, duty cycles as expected, 200 ns dead time, PWMD/PWME in phase.
    • Power API with leg-frequency: LEG1 200 kHz, LEG2 100 kHz, LEG3 50 kHz. Measured 199 990 / 99 995 / 49 997 Hz, 100 ns dead time.
    • Runtime: common and per-leg frequency changes only move their own units; out-of-range frequencies and phase shifts on own-frequency legs are refused.
    • Regression: without leg-frequency, all legs are at 200 kHz and reset by the master, as before.
    • Not yet checked with a scope: precise dead times and edges.

How to test

The test apps are on separate branches of luizvilla/Core, not part of this PR. Each one is this branch plus one commit that puts the app in src/, so src/main.cpp of main is untouched. Check out a branch and build; there are no files to copy. The README in src/ gives the setup and the checklist, and preliminary_test_results.md gives the results of the run described above.

Branch Board Instrument What it checks
test/hrtim-freq-spin Spin alone scope Spin API: PWMA 200 kHz (common), PWMC 100 kHz, PWMD/PWME 50 kHz aligned; menu to change the common / PWMD frequency, re-align, outputs off/on
test/hrtim-freq-ownverter Ownverter 1.1.0, no DC bus scope Power API + leg-frequency: LEG1 200 kHz, LEG2 100 kHz, LEG3 50 kHz; gate drivers disabled; per-leg frequency changes, phase shift refused, regression without leg-frequency
test/hrtim-freq-measure-spin Spin + ST-Link none Same as test/hrtim-freq-spin, measured over SWD (registers, counters, 100 ns logic capture); tools/swd_measure/test_spin.py runs the whole sequence
test/hrtim-freq-measure-twist Spin on a Twist 1.4.2 (power off) + ST-Link none The Ownverter app on a Twist (same timers A, C, E), measured over SWD; tools/swd_measure/test_twist.py

Still open for a tester with a scope: precise dead times and edge shapes; the Ownverter board itself.

🤖 Generated with Claude Code

Luiz Villa and others added 9 commits October 21, 2026 00:29
The remainder term multiplied by (32 / new_frequency), which is always 0
in integer arithmetic. Use the same formula as _period_ckpsc(): scale the
remainder by 32 before dividing. At 150 kHz the master period was 18128
instead of 18133.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hrtim_tu_frequency_set(tu, f) gives a timing unit its own switching
frequency, to call before hrtim_tu_init(). hrtim_frequency_set() and
hrtim_change_frequency() then leave the unit alone, so that, for
instance, two inverter legs run at 50 kHz while the master and the boost
legs stay at 200 kHz.

- pwm_conf.own_frequency stores the requested switching frequency
  (0 = follows the master). pwm_conf.frequency cannot be used for this:
  the init of an up-down unit doubles it, and a second init (initBuck then
  initBoost) would double it again. hrtim_frequency_set() restores it from
  own_frequency.
- The frequency must be at least the minimum frequency: the clock
  prescaler is shared with the master.
- hrtim_tu_has_own_frequency() and hrtim_tu_frequency_get() (switching
  frequency, not the doubled counting frequency of up-down units).
- hrtim_period_get_us() uses the prescaler of the unit, not the master's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hrtim_tu_init() sets the reset of a unit to its phase shift compare
(master CMP3 for timer D, CMP4 for timer E), then hrtim_phase_shift_set(0)
handles a unit that does not run at the master frequency by removing only
the master period reset. The phase shift compare stayed, so timer D was
still reset every master period: at 50 kHz with a 200 kHz master its
waveform was cut every 5 us (RSTDR = MSTR_CMP3 on a host test with the
driver registers mapped in RAM).

- Units with their own frequency: remove the master period and the phase
  shift compare from the reset sources; a non-zero phase shift is
  refused (hrtim_phase_shift_set() now returns -1, callers that ignore
  the result are unchanged).
- Same fix in the existing 'not at the master frequency' branch.
- hrtim_tu_init() refuses current mode on a unit with its own frequency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hrtim_tu_change_frequency(tu, f) changes the frequency of a unit that has
its own frequency, without touching the master and the other units.
CMP1 (duty cycle), CMP3 (ADC trigger) and the user duty limits keep their
ratio to the period; the values are preloaded and applied together on the
next period. The new period is checked against the HRTIM limits for the
shared prescaler.

hrtim_change_frequency() now also rescales CMP3: the ADC trigger moved
within the period when the master frequency changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hrtim_cnt_reset_sync(tus, count) resets the counters of several timing
units with one CR2 write. Units with their own frequency are not reset by
the master and are enabled one at a time by hrtim_tu_init(), so two legs
of an H-bridge at the same frequency start with an arbitrary offset; one
synchronous reset aligns them, and they cannot drift afterwards (same
clock, same period).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Wrappers for the HRTIM per-unit frequency:
- initUnitFrequency(pwmX, f): own frequency, between
  initVariableFrequency() and initUnit()
- setFrequency(pwmX, f): runtime change of such a unit (the existing
  setFrequency(f) is unchanged and leaves it alone)
- getFrequency(pwmX), hasOwnFrequency(pwmX)
- resetCounters(pwmX[], count): restart several units together

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A leg can have its own switching frequency with the optional
leg-frequency property (0 or absent: common default-frequency, as
before), for instance two inverter legs at 50 kHz on a board whose other
legs run at 200 kHz.

- power-leg.yaml: leg-frequency, and the default/min-frequency texts no
  longer say that the frequency must be the same for all the timers.
- power_init: dt_leg_frequency[].
- initMode(): gives the leg its frequency before initUnit(). Prints a
  message if it is below min-frequency (the common frequency is then
  used), if it does not divide the common frequency (the critical task
  runs on the common frequency), or if a phase shift is set.
- alignOwnFrequencyLegs(): after every init, restarts together the legs
  that share a leg-frequency, so that the two legs of an H-bridge
  initialised one after the other are in phase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- setFrequency(leg, f): changes the frequency of a leg that has its own
  (leg-frequency); duty cycle, duty limits and ADC trigger keep their
  ratio to the period. ALL is setFrequency(f). Returns -1 for a leg on
  the shared frequency or a frequency out of range.
- getFrequency(leg): own or shared frequency of the leg.
- setFrequency(f) no longer recomputes the duty limits of legs with their
  own frequency (their period does not change).
- setPhaseShift() documented as without effect on such legs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- shield_introduction.md: leg-frequency in the overlay, PowerAPI
  setFrequency(leg, f) / getFrequency(leg), constraints (min-frequency,
  no phase shift, no current mode, critical task on the shared frequency)
  and the alignment of legs sharing a frequency.
- spin_pwm.md: PwmHAL initUnitFrequency / setFrequency(pwmX, f) /
  resetCounters snippet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@luizvilla
luizvilla requested a review from Ayoub-Farah October 21, 2026 10:31
@luizvilla luizvilla self-assigned this Oct 21, 2026
@Ayoub-Farah

Copy link
Copy Markdown
Member

I've tested this PR on SPIN + TWIST 1.4.2.

The independent PWM outputs work correctly, I swept timers A and C separately from 10 kHz to 250 kHz and checked the correct results in the oscilloscope.

ADC samples are lost before reaching the control task when the DMA buffer wraps between reads

With the master at 200 kHz, the control task runs at 10 kHz. I configured LEG2 as an independent PWM, with three ADC2 channels, discontinuous mode set to one conversion per trigger, and no ADC decimation.

shield.power.setFrequency(200000);
shield.power.setAdcDecim(ALL, 1);

task.createCritical(critical, 100, source_hrtim);
task.startCritical();

shield.power.setFrequency(LEG2, 250000);

I've counted the number of samples we're receiving in the control task, here is the result :

Timer C Acquistion per control interval Expected samples/s Received samples/s
100 kHz 10 100,000 ≈100,000
200 kHz 20 200,000 ≈200,000
225 kHz 22.5 on average ≈225,000 ≈15,000
250 kHz 25 250,000 ≈40,000

If the control task is 100µs and the PWMC has a frequency of 100kHz, then it triggers the acquisition of a sample every 10µs thus we have 10 acquisition per control interval and 100,000 sample per second. We are indeed receiving these 100k samples for this configurations. However, the moment we are increasing the frequency to more than 225kHz, we are not receiving the expected number of samples.

This is due to the fact that the buffer dma is limitted to 21-word. So it fits for 200kHz but a higher frequency seems to be problematic.

There is a need to add a warning about this, if the size of the DMA is not increased there is a need to increase the decimation to avoid increasing the number of acquisitions. shield.power.setAdcDecim();

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.

2 participants