pwmgen9
Software pwm/pdm generation
Usage
loadrt pwmgen output_type=type0[,type1...]
Overview
pwmgen is used to generate PWM (pulse width modulation) or PDM (pulse density modulation) signals. The maximum PWM frequency and the resolution is quite limited compared to hardware-based approaches, but in many cases software PWM can be very useful. If better performance is needed, a hardware PWM generator is a better choice.
pwmgen supports a maximum of eight channels. The number of channels actually loaded depends on the number of type values given. The value of each type determines the outputs for that channel.
- type 0: single output A single output pin,
pwm, whose duty cycle is determined by the input value for positive inputs, and which is off (or atmin-dc) for negative inputs. -
Suitable for single ended circuits.
- type 1: pwm/direction
-
Two output pins,
pwmanddir. The duty cycle onpwmvaries as a function of the input value.diris low for positive inputs and high for negative inputs.
- type 2: up/down
-
Two output pins,
upanddown. For positive inputs, the PWM/PDM waveform appears onup, whiledownis low. For negative inputs, the waveform appears ondown, whileupis low. Suitable for driving the two sides of an H-bridge to generate a bipolar output.
Functions
pwmgen.make-pulses(no floating-point)-
Generates the actual PWM waveforms, using information computed by
update. Must be called as frequently as possible, to maximize the attainable PWM frequency and resolution, and minimize jitter. Operates on all channels at once.
pwmgen.update(uses floating point)-
Accepts an input value, performs scaling and limit checks, and converts it into a form usable by
make-pulsesfor PWM/PDM generation. Can (and should) be called less frequently thanmake-pulses. Operates on all channels at once.
Pins
pwmgen.N.enablebit in-
Enables PWM generator N - when false, all
pwmgen.N output pins are low.
pwmgen.N.valuefloat in-
Commanded value. When
value= 0.0, duty cycle is 0%, and whenvalue= ±scale, duty cycle is ± 100% (subject tomin-dcandmax-dclimitations).
pwmgen.N.pwmbit out (output types 0 and 1 only)-
PWM/PDM waveform.
pwmgen.N.dirbit out (output type 1 only)-
Direction output: low for forward, high for reverse.
pwmgen.N.upbit out (output type 2 only)-
PWM/PDM waveform for positive input values, low for negative inputs.
pwmgen.N.downbit out (output type 2 only)-
PWM/PDM waveform for negative input values, low for positive inputs.
pwmgen.N.curr-dcfloat out-
The current duty cycle, after all scaling and limits have been applied. Range is from -1.0 to +1.0.
pwmgen.N.max-dcfloat in/out-
The maximum duty cycle. A value of 1.0 corresponds to 100%. This can be useful when using transistor drivers with bootstrapped power supplies, since the supply requires some low time to recharge.
pwmgen.N.min-dcfloat in/out-
The minimum duty cycle. A value of 1.0 corresponds to 100%. Note that when the pwm generator is disabled, the outputs are constantly low, regardless of the setting of
min-dc.
pwmgen.N.scalefloat in/out
pwmgen.N.offsetfloat in/out-
These parameters provide a scale and offset from the
valuepin to the actual duty cycle. The duty cycle is calculated according to dc = (value/scale) + offset, with 1.0 meaning 100%.
pwmgen.N.pwm-freqfloat in/out-
PWM frequency in Hz. The upper limit is half of the frequency at which
make-pulsesis invoked, and values above that limit will be changed to the limit. Ifdither-pwmis false, the value will be changed to the nearest integer submultiple of themake-pulsesfrequency. A value of zero produces Pulse Density Modulation instead of Pulse Width Modulation.
pwmgen.N.dither-pwmbit in/out-
Because software-generated PWM uses a fairly slow timebase (several to many microseconds), it has limited resolution. For example, if
make-pulsesis called at a 20 kHz rate, andpwm-freqis 2 kHz, there are only 10 possible duty cycles. Ifdither-pwmis false, the commanded duty cycle will be rounded to the nearest of those values. Assumingvalueremains constant, the same output will repeat every PWM cycle. Ifdither-pwmis true, the output duty cycle will be dithered between the two closest values, so that the long-term average is closer to the desired level.dither-pwmhas no effect ifpwm-freqis zero (PDM mode), since PDM is an inherently dithered process.
Releases
2 releases since Debian 12 (Bookworm); the text changed 1 time.