Repository navigation
Conversation
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>
|
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 readsWith 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 :
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. |
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_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()andhrtim_change_frequency()leave own-frequency units alone.initUnitFrequency(),setFrequency(pwmX, f),getFrequency(pwmX),hasOwnFrequency(),resetCounters().leg-frequencyon a leg.PowerAPI::setFrequency(leg, f)andgetFrequency(leg).leg-frequencyare started aligned.setFrequency(f)keeps its meaning: master and legs on the common frequency.shield_introduction.mdandspin_pwm.md.Bugs fixed on the way
hrtim_tu_init()sets the reset of timer D/E to master CMP3/CMP4. The "not at the master frequency" branch ofhrtim_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
min-frequency, because the clock prescaler is shared. A lower value is refused, and the leg keeps the common frequency (with a message).leg-frequencyis unchanged: Twist and Ownverter builds behave as before.Tests
Host register-level tests (branch
test/hrtim-host-tests, kept out of this PR):hrtim.cbuilt for the PC with the real LL headers and HRTIM1/RCC mapped in RAM. Covers: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 onmain(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):
leg-frequency: LEG1 200 kHz, LEG2 100 kHz, LEG3 50 kHz. Measured 199 990 / 99 995 / 49 997 Hz, 100 ns dead time.leg-frequency, all legs are at 200 kHz and reset by the master, as before.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 insrc/, sosrc/main.cppofmainis untouched. Check out a branch and build; there are no files to copy. The README insrc/gives the setup and the checklist, andpreliminary_test_results.mdgives the results of the run described above.test/hrtim-freq-spintest/hrtim-freq-ownverterleg-frequency: LEG1 200 kHz, LEG2 100 kHz, LEG3 50 kHz; gate drivers disabled; per-leg frequency changes, phase shift refused, regression withoutleg-frequencytest/hrtim-freq-measure-spintest/hrtim-freq-spin, measured over SWD (registers, counters, 100 ns logic capture);tools/swd_measure/test_spin.pyruns the whole sequencetest/hrtim-freq-measure-twisttools/swd_measure/test_twist.pyStill open for a tester with a scope: precise dead times and edge shapes; the Ownverter board itself.
🤖 Generated with Claude Code