Expand description
Provides basic Pulse-width modulation (PWM) capabilities
There are 2 main structures [Pwm
] and [PwmHz
]. Both structures implement embedded_hal_02::Pwm
and have some additional API.
First one is based on FTimer
with fixed prescaler
and easy to use with fugit::TimerDurationU32
for setting pulse width and period without advanced calculations.
Second one is based on Timer
with dynamic internally calculated prescaler and require fugit::Hertz
to set period.
The main way to run PWM is calling FTimer::pwm
with initial period
/frequency
corresponding PWM period.
This returns PwmManager
and a tuple of all PwmChannel
s supported by timer.
Also there is PwmExt
trait implemented on pac::TIMx
to simplify creating new structure.
let (pwm_manager, (pwm_ch1, pwm_ch2, ..)) = dp.TIM1.pwm_us(100.micros(), &clocks);
Each PwmChannel
implements embedded_hal::pwm::SetDutyCycle
.
They are disabled.
To enable PwmChannel
you need to pass one or more regular pins allowed by channel
using with
or with_open_drain
.
Also you can pass complementary pins by .with_complementary(other_complementary_pin)
.
After connecting pins you can dynamically enable main or complementary channels with enable
and enable_complementary
and change their polarity with set_polarity
and set_complementary_polarity
.
let mut pwm_c1 = pwm_c1.with(gpioa.pa8).with_complementary(gpioa.pa7);
pwm_c1.enable();
pwm_c1.enable_complementary();
By default PwmChannel
contains information about connected pins to be possible to release
them.
But you can erase
this information to constuct ErasedChannel
which can be collected to array.
Note that this operation is irreversible.
PwmManager
allows you to change PWM period
/frequency
and also has methods for advanced PWM control.