Expand description
Pulse width modulation.
Each PWM peripheral, Pwm
, interacts with four submodules, Submodule
.
Each submodule acts as a timer with multiple compare registers, called
ValueRegister
s. A comparison event
- is signaled through a
Status
flag (seeSubmodule::status
). - can generate an interrupt (see
Submodule::interrupts
). - sets a PWM
Output
high or low, depending on the turn on / off values.
Note: PWM outputs can also be manipulated directly with
Submodule
, without usingOutput
.
The PWM driver does not implement any of the embedded-hal PWM traits. You should use these APIs to create your own PWM implementation that satisfies your driver.
Example
The PWM submodule counts over the range of i16
values. The counter runs at
the IPG clock frequency. The PWM outputs produce independent, phase-shifted
outputs.
use imxrt_hal as hal;
use imxrt_ral as ral;
use hal::flexpwm;
let pwm2 = unsafe { ral::pwm::PWM2::instance() };
let (mut pwm, (_, _, mut sm2, _)) = flexpwm::new(pwm2);
// Keep running in wait, debug modes.
sm2.set_debug_enable(true);
sm2.set_wait_enable(true);
// Run on the IPG clock.
sm2.set_clock_select(flexpwm::ClockSelect::Ipg);
// Divide the IPG clock by 1.
sm2.set_prescaler(flexpwm::Prescaler::Prescaler1);
// Allow PWM outputs to operate independently.
sm2.set_pair_operation(flexpwm::PairOperation::Independent);
// Reload every time the full reload value register compares.
sm2.set_load_mode(flexpwm::LoadMode::reload_full());
sm2.set_load_frequency(1);
// Count over the full range of i16 values.
sm2.set_initial_count(&pwm, i16::MIN);
sm2.set_value(flexpwm::FULL_RELOAD_VALUE_REGISTER, i16::MAX);
let gpio_b0_10 = // Handle to the pad
let gpio_b0_11 = // Handle to the pad
let output_a = flexpwm::Output::new_a(gpio_b0_10);
let output_b = flexpwm::Output::new_b(gpio_b0_11);
// Set the turn on / off count values.
output_a.set_turn_on(&sm2, i16::MIN / 2);
output_a.set_turn_off(&sm2, i16::MAX / 2);
// Output B generates the same duty cycle as A
// with a lagging phase shift of 5000 counts.
output_b.set_turn_on(&sm2, output_a.turn_on(&sm2) + 5000);
output_b.set_turn_off(&sm2, output_a.turn_off(&sm2) + 5000);
// Enable the PWM output.
output_a.set_output_enable(&mut pwm, true);
output_b.set_output_enable(&mut pwm, true);
// Load the values into the PWM registers.
sm2.set_load_ok(&mut pwm);
// Start running.
sm2.set_running(&mut pwm, true);
Structs
- Interrupt flags.
- Bitmask for representing submodules.
- A PWM output pin.
- A PWM peripheral.
- Status register flags.
- A PWM submodule.
Enums
- PWM channels.
- PWM input clock selection.
- PWM (re)load mode.
- Describes how PWM channels A and B operate.
- PWM clock prescaler.
- PWM value registers.
Constants
- The full reload value register.
- The half reload value register.
Functions
- Create a PWM peripheral with its submodules.
- Returns the “turn off” value register for an output channel.
- Returns the “turn on” value register for an output channel.
Type Aliases
- Four submodules for the
N
th PWM instance.