Module stm32f4xx_hal::timer::pwm

source ·
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.

You can split any of those structures on independent PwmChannels if you need that implement embedded_hal_02::PwmPin but can’t change PWM period.

Also there is PwmExt trait implemented on pac::TIMx to simplify creating new structure.

You need to pass one or tuple of channels with pins you plan to use and initial time/frequency corresponding PWM period. Pins can be collected with ChannelBuilders in sequence corresponding to the channel number. Smaller channel number first. Each channel group can contain 1 or several main pins and 0, 1 or several complementary pins. Start constructing channel with new(first_main_pin). Then use .with(other_main_pin) and .with_complementary(other_complementary_pin) accordingly to add advanced pins on same channel.

For example:

let channels = (
    Channel1::new(gpioa.pa8),
    Channel2::new(gpioa.pa9), // use Channel2OD` for `OpenDrain` pin
);

or

let channels = Channel1::new(gpioa.pa8).with_complementary(gpioa.pa7); // (CH1, CHN1)

where CHx and CHx_n are main pins of PWM channel x and CHNx are complementary pins of PWM channel x.

After creating structures you can dynamically enable main or complementary channels with enable and enable_complementary and change their polarity with set_polarity and set_complementary_polarity.

Re-exports

  • pub use super::Ch;
  • pub use super::C1;
  • pub use super::C2;
  • pub use super::C3;
  • pub use super::C4;

Structs

Traits

Type Aliases