Repository navigation
Conversation
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>
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. |
Member
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
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.
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
hrtim_dma.c, which addshrtim_dma_cmp1_init/start/stop/...and the period-wait helpershrtim_wait_period_start/middle/end.PwmHALwrappers (raw CMP1 values).setDutyCycledoesn't touch a running sequence,setFrequencyrescales it, andstopends it. PlusCONFIG_OWNTECH_POWER_DUTY_SEQ_MAX_LEN.start/stopsynchronised on the PWM period,prepareStart/startPreparedandwaitPeriod*. This is separate from commit 3 so it can be discussed on its own.shield_introduction.mdandspin_pwm.md.Requirements
&dma2 { status = "okay"; };inapp.overlay. Timing unitsPWMAtoPWMFuse DMA2 channels 1 to 6.Lft_aligned; init returns-1otherwise.Bench results (Spin + ownverter)
Points for discussion
startDutySequencewaits 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.startDutySequenceand 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.dma_stm32behaviour may have changed.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.
setFrequencywhile running,getDutySequenceErrors() == 0🤖 Generated with Claude Code