Skip to content

fix(hrtim): up-down unit keeps its frequency on re-init - #172

Open
luizvilla wants to merge 1 commit into
owntech-foundation:mainfrom
luizvilla:fix/hrtim-updown-reinit
Open

luizvilla wants to merge 1 commit into
owntech-foundation:mainfrom
luizvilla:fix/hrtim-updown-reinit

Conversation

@luizvilla

Copy link
Copy Markdown
Member

Summary

In up-down mode, each further initUnit() on the same timing unit doubles its PWM frequency. hrtim_tu_init() stores the counter frequency, twice the switching frequency, in pwm_conf.frequency, and the next init starts from that value.

 hrtim_tu_init(tu)
   freq_mult = (UpDwn && VOLTAGE_MODE) ? 2 : 1
-  pwm_conf.frequency = _period_ckpsc(freq_mult * pwm_conf.frequency)       # counter frequency
+  pwm_conf.frequency = _period_ckpsc(freq_mult * pwm_conf.frequency) / freq_mult   # switching frequency

After a few re-inits, the period goes below the minimum and the assert in _period_ckpsc() aborts (kernel panic). Left-aligned units aren't affected, because freq_mult is 1. Most applications init each unit once and never see it. We found it in a test firmware that re-initialised the legs at each PWM start.

pwm_conf.frequency is now the switching frequency for up-down units too, as hrtim_change_frequency() already stores it. hrtim_phase_shift_set() already accepts a timing unit at the master frequency or at twice it, so phase shifts still work.

Evidence

Test firmware on a Spin (env STLink): initFixedFrequency(200000), setModulation(PWMA, UpDwn), then initUnit(PWMA) three times. After each init, the firmware reads TIMA's period register (PERxR); the values were then read over SWD.

Init main This branch
1 13600 (200 kHz) 13600 (200 kHz)
2 6800 (400 kHz) 13600 (200 kHz)
3 3400 (800 kHz) 13600 (200 kHz)

Merge Danger

Door: two-way

One line in the driver. A revert restores the old behaviour.

Blast Radius: up-down re-init

  • Changes: only units in up-down voltage mode that are initialised more than once. Their frequency now stays as set.
  • Unchanged: a single init writes the same registers as before.
  • One more difference: code that reads pwm_conf.frequency of an up-down unit after init now gets the switching frequency, not twice it. In this repository, only hrtim_phase_shift_set() reads it, and it accepts both.

🤖 Generated with Claude Code

hrtim_tu_init() stored the up-down counter frequency (twice the switching
frequency) in pwm_conf.frequency, which is the switching frequency. Each
further init of the same unit doubled the PWM frequency again, until the
period fell below the minimum and the driver's assert aborted. Store the
switching frequency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant