Skip to content

[Feature] DMA-driven HRTIM duty sequences - #164

Draft
luizvilla wants to merge 5 commits into
owntech-foundation:mainfrom
luizvilla:feature/hrtim-dma-sequences
Draft

luizvilla wants to merge 5 commits into
owntech-foundation:mainfrom
luizvilla:feature/hrtim-dma-sequences

Conversation

@luizvilla

Copy link
Copy Markdown
Member

Summary

A leg can now play a precomputed sequence of duty cycles, one value per PWM period. DMA2 copies each value into CMP1 on the repetition event of the timing unit, so the CPU does no work per period. On top of this, the PowerAPI can fire single shots: a known number of periods on several legs, with clean first and last periods.

float32_t pattern[10] = {1, 1, 1, 1, 1, 0, 0, 0, 0, 0};
shield.power.setDutySequence(LEG1, pattern, 10);
shield.power.startDutySequence(LEG1);
shield.power.start(LEG1);

Use cases: excitation patterns (0/1 vectors) for component characterisation (for example RDSon or inductance), and waveforms that are slow compared with a fast PWM.

Commits

  1. hrtim: hrtim_dma.c, which adds hrtim_dma_cmp1_init/start/stop/... and the period-wait helpers hrtim_wait_period_start/middle/end.
  2. spin: PwmHAL wrappers (raw CMP1 values).
  3. shield: PowerAPI duty sequences (0..1 values, double-buffered updates, 100 % handling). Also guards: setDutyCycle doesn't touch a running sequence, setFrequency rescales it, and stop ends it. Plus CONFIG_OWNTECH_POWER_DUTY_SEQ_MAX_LEN.
  4. shield: multi-leg start/stop synchronised on the PWM period, prepareStart/startPrepared and waitPeriod*. This is separate from commit 3 so it can be discussed on its own.
  5. doc: shield_introduction.md and spin_pwm.md.

Requirements

  • &dma2 { status = "okay"; }; in app.overlay. Timing units PWMA to PWMF use DMA2 channels 1 to 6.
  • The leg must be Lft_aligned; init returns -1 otherwise.
  • When DMA2 is disabled, every function is a stub that returns an error, so existing applications build and behave as before.

Bench results (Spin + ownverter)

  • 50 kHz and 200 kHz, sequences up to 40 entries on LEG1 (PWMA) and LEG3 (PWME), no DMA transfer errors.
  • Single shots synchronised on LEG1 + LEG3, re-armable, up to 10 A in the inductor.
  • Use case: RDSon measurement by sampling Vds and iL in phase during shots. The fit gives about 9.9 mΩ from the ADC samples and 9.4 mΩ from the scope.

Points for discussion

  1. Critical task timing. startDutySequence waits for the middle of a PWM period with interrupts locked, for up to about two periods. That can delay the critical task. [bug] data_dispatch: size ADC DMA buffers for several dispatch periods #162 makes the ADC dispatch robust to this delay, so it should be merged first.
  2. Shot offsets. How many periods pass between startDutySequence and the first value depends on the frequency (measured: 2 at 50 kHz, 1 at 200 kHz). Applications compensate for it today. It could move into the PowerAPI later.
  3. Zephyr DMA driver. It is used in HAL override mode, which leaves the half/complete flags set; the callback clears them. Bump zephyr to 4.4.2 #160 (Zephyr 4.4.2) should be checked against this, since the dma_stm32 behaviour may have changed.
  4. RAM. Static buffers take 16 bytes per value and per leg, for every leg enabled in the device tree: 2 kB for 2 legs (twist) or 3 kB for 3 legs at the default length of 64. They are allocated even without DMA2 and could be compiled only when DMA2 is enabled.
  5. Coverage. Only PWMA (LEG1) and PWME (LEG3) were bench-tested. Long runs at 100 % were not tested; bootstrap supplies may discharge (see the warning in the docs).

Related

Before marking ready

The bench results above come from the development branch. This branch has the same code, regrouped into 5 commits, with only comment and doc changes. It builds at every commit, with and without DMA2.

  • Smoke test of this branch on Spin + scope (planned next week): cyclic sequence, single shot on LEG1 + LEG3, setFrequency while running, getDutySequenceErrors() == 0
  • Check against Bump zephyr to 4.4.2 #160 (Zephyr 4.4.2)

🤖 Generated with Claude Code

Luiz Villa and others added 5 commits October 16, 2026 19:57
Adds hrtim_dma.c: DMA2 copies one CMP1 value per PWM period from a
circular buffer into a timing unit, on the repetition event of that timing
unit. This lets a leg follow a precomputed duty-cycle vector (for instance
0/1 excitation patterns, or one value per period at 200 kHz) without any
CPU work per period.

API (hrtim.h):
- hrtim_dma_cmp1_init / start / stop / is_running / get_next_index /
  get_error_count
- hrtim_wait_period_start / middle / end: busy-waits on the timer counter,
  used to start several timing units on the same period and to switch
  outputs at a period boundary.

Timing unit x uses DMA2 channel x+1 (DMAMUX request HRTIM1_A + x). Only
up-counting (Lft_aligned) timing units are accepted, since one period must
equal one repetition event. When dma2 is not enabled in the device tree,
every function is a stub returning an error, so existing applications
build and behave as before.

Details from bench testing:
- Zephyr's dma_stm32 in HAL override mode leaves the half/complete flags
  set; the callback clears them, otherwise the interrupt fires forever.
- The channel IRQ is routed with dma_config() once only; a second call is
  refused with "dma stream busy".
- Stopping also clears the NVIC pending bit; a stale request otherwise ran
  the DMA interrupt with no flag set and Zephyr logged a transfer error.
- Period boundaries are detected by counter wrap: the DMA acknowledge can
  clear the REP flag before it is polled.
- The wait loops only read the counter; calling k_cycle_get_32() in them
  made the sampling too coarse at 200 kHz.
- The stop margin is computed from the HRTIM clock (f_HRTIM x 32 >> ckpsc).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Exposes the HRTIM DMA duty sequences through spin.pwm:
initDutySequence, startDutySequence (one unit or several on the same
period), stopDutySequence, isDutySequenceRunning, getDutySequenceIndex and
getDutySequenceErrors. Values are raw CMP1 counts; the shield PowerAPI
provides the duty-cycle (0..1) interface on top of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds duty-cycle sequences to the PowerAPI, on top of the HRTIM DMA
sequences: one duty value (0..1) per PWM period, applied by DMA and
repeated until stopped.

- setDutySequence(leg, duties, length, double_buffered)
- updateDutySequence(leg, duties): with double buffering, the new values
  are staged and each half of the DMA buffer picks them up once it has
  been played, so updates never land in the middle of a sequence.
- startDutySequence / stopDutySequence (LEG1..LEG5 or ALL, ALL starts all
  legs on the same period), isDutySequenceRunning, getDutySequenceIndex,
  getDutySequenceErrors.

Values follow the setDutyCycleMin/Max limits. 100 % is encoded as CMP1
above the period (period + 32, capped at 0xFFDF): CMP1 equal to the
period resets the output on the same instant it is set, which gives 0 %.

Interactions with the existing API:
- setDutyCycle/setDutyCycleRaw leave legs with a running sequence alone.
- setFrequency stops running sequences, rescales their raw values to the
  new period and restarts them.
- stop(leg) also stops the sequence of the leg.

Buffers are static, sized by CONFIG_OWNTECH_POWER_DUTY_SEQ_MAX_LEN
(default 64, 16 bytes per value for each leg in the device tree).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds PowerAPI functions to switch several legs at a precise point of the
PWM period, used to fire single shots of a duty sequence (a known number
of periods, with clean first and last periods):

- start(legs, count, at_period_start) / stop(legs, count, at_period_end):
  all outputs are enabled or disabled with a single register write. With
  the flag set, start waits for a period boundary and stop for
  POWER_STOP_MARGIN_NS (2 us) before the end of the period, so that no
  partial period appears on the outputs. stop also ends the sequences.
- prepareStart(legs, count) / startPrepared(outputs): split start into its
  slow part (driver pins, output mask, several us) and a single register
  write, for use after a time-critical wait at 200 kHz.
- waitPeriodMiddle(leg), waitPeriodStart(leg, boundaries),
  getPeriodCounter(leg): period reference points for the application.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- shield_introduction.md: PowerAPI duty sequences (device tree
  requirements, API, interactions with setDutyCycle/setFrequency/stop,
  DMA2 channels, Kconfig length) and single shots with the
  period-synchronised start/stop functions.
- spin_pwm.md: PwmHAL raw duty sequence snippet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@luizvilla

Copy link
Copy Markdown
Member Author

Here are the test codes for this pull request.

There are two files, one for the spin and one for the shield, and a readme.

pr164_dma_test.zip

@luizvilla
luizvilla requested a review from Ayoub-Farah October 20, 2026 07:50
@luizvilla luizvilla self-assigned this Oct 20, 2026
@Ayoub-Farah

Copy link
Copy Markdown
Member

I've tested this code and I found the following problems :

A 25% sequence becomes 12.5% after changing carrier frequency from 200 kHz to 100 kHz

I've set PWM to 200 kHz, loaded a single-value sequence commanding 25% and Changed PWM to **100 kHz before starting the sequence.

static float32_t duty[] = {0.25F};
shield.power.setFrequency(200000);
int load = shield.power.setDutySequence(LEG2, duty, 1);
shield.power.setFrequency(100000);
int start = shield.power.startDutySequence(LEG2);

I was expecting a 25% duty cycle with a frequency of 100kHz, but what I got instead was a 11% duty cycle :

image

It seems that the sequence retains the raw compare value calculated at 200 kHz, and this value is not updated for a sequence that is loaded but not yet started.

A requested 20-period shot produces extra pulses

At 200 kHz, I loaded 25% duty sequences on both legs and generated a 20-period shot. I've called the following lines from the background task :

leg_t legs[] = {LEG1, LEG2};

uint32_t outputs = shield.power.prepareStart(legs, 2);
shield.power.waitPeriodStart(LEG1);
shield.power.startPrepared(outputs);

shield.power.waitPeriodStart(LEG1, 20);
shield.power.stop(legs, 2, true);

I expected 20 complete PWM periods, corresponding to a 100 µs. Instead, three oscilloscope captures on Time C channel 1/PB12 showed more than that (27 on the capture).

image

waitPeriodStart() is doing software polling. If an interrupt preempts the polling loop, the timer continues running and some periods can go uncounted thus resulting in having more shot than 20.

A direct duty command changes the output of a running DMA sequence

At 200 kHz carrier frequency, I started a constant 25% duty sequence on LEG2, then called spin.pwm.setDutyCycle() on the same timer without stopping the sequence:

static float32_t duty[] = {0.25F};

shield.power.setFrequency(200000);
shield.power.setDutySequence(LEG2, duty, 1);
shield.power.startDutySequence(LEG2);
shield.power.start(LEG2);

// While the sequence is still running:
spin.pwm.setDutyCycle(PWMC, 1.0F);

The duty changed from approximately 23% to 73% :

image

While the 25% DMA sequence was running on LEG2, I called spin.pwm.setDutyCycle(PWMC, 1.0F);

The call spin.pwm.setDutyCycle(PWMC, 1.0F) uses an implementation for 100% duty that swaps the timer's two complementary outputs, TC1 and TC2. Then the running DMA sequence restores the compare value corresponding to 25% duty, but leaves the two outputs swapped. TC1 therefore carries the complementary waveform: its measured positive duty changes from approximately 23% to 73%, including dead time.

The sequence is still running, but its waveform is now routed to the wrong output. Direct PWM commands should not change this output routing while a DMA sequence is active.

A DMA duty sequence is accepted in current mode despite sharing CMP1

However, current mode uses the timer's CMP1 register to define the maximum duty boundary. The DMA sequence also writes to CMP1, replacing that boundary with its sequence values. We need to avoid this combination, use only either DMA sequence or current mode.

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