Skip to content

Commit

Permalink
stm32/timer: Update examples for new timer API
Browse files Browse the repository at this point in the history
  • Loading branch information
honzasp committed Aug 4, 2024
1 parent 16c82c4 commit 1b54ca0
Show file tree
Hide file tree
Showing 23 changed files with 328 additions and 242 deletions.
33 changes: 17 additions & 16 deletions embassy-stm32/src/timer/complementary_pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ impl<'d, T: Advanced4ChInstance> Builder<'d, T> {
/// You may use convenience methods [`ch1_pin()`][Self::ch1_pin()] to `ch4_pin()` to aid type
/// inference.
pub fn pin<C: ChannelMarker>(
&mut self,
mut self,
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
output_type: OutputType,
) -> &mut Self {
) -> Self {
let pin = RawTimerPin::new(pin, AfType::output(output_type, Speed::VeryHigh));
self.channel_pins[C::CHANNEL.index()] = Some(pin);
self
Expand All @@ -56,42 +56,43 @@ impl<'d, T: Advanced4ChInstance> Builder<'d, T> {
/// You may use convenience methods [`ch1n_pin()`][Self::ch1n_pin()] to `ch4n_pin()` to aid type
/// inference.
pub fn n_pin<C: NChannelMarker>(
&mut self,
mut self,
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
output_type: OutputType,
) -> &mut Self {
) -> Self {
let pin = RawTimerPin::new(pin, AfType::output(output_type, Speed::VeryHigh));
self.n_channel_pins[C::N_CHANNEL.index()] = Some(pin);
self
}
}

#[rustfmt::skip]
macro_rules! channel_impl {
($chx_pin:ident, $chxn_pin:ident, $channel:ident, $nchannel:ident) => {
impl<'d, T: Advanced4ChInstance> Builder<'d, T> {
#[doc = concat!(
"Attach an output pin for channel ",
stringify!($channel),
" to the complementary PWM driver.\n\nSee [`pin()`][Self::pin()] for details.",
)]
"Attach an output pin for channel ",
stringify!($channel),
" to the complementary PWM driver.\n\nSee [`pin()`][Self::pin()] for details.",
)]
pub fn $chx_pin(
&mut self,
self,
pin: impl Peripheral<P = impl TimerPin<T, $channel>> + 'd,
output_type: OutputType,
) -> &mut Self {
) -> Self {
self.pin::<$channel>(pin, output_type)
}

#[doc = concat!(
"Attach a complementary output pin for channel ",
stringify!($channel),
" to the complementary PWM driver.\n\nSee [`n_pin()`][Self::pin()] for details.",
)]
"Attach a complementary output pin for channel ",
stringify!($channel),
" to the complementary PWM driver.\n\nSee [`n_pin()`][Self::pin()] for details.",
)]
pub fn $chxn_pin(
&mut self,
self,
pin: impl Peripheral<P = impl TimerPin<T, $nchannel>> + 'd,
output_type: OutputType,
) -> &mut Self {
) -> Self {
self.n_pin::<$nchannel>(pin, output_type)
}
}
Expand Down
10 changes: 3 additions & 7 deletions embassy-stm32/src/timer/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ impl<'d, T: General1ChInstance> Builder<'d, T> {
///
/// You may use convenience methods [`ch1_pin()`][Self::ch1_pin()] to `ch4_pin()` to aid type
/// inference.
pub fn pin<C: ChannelMarker>(
&mut self,
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
pull: Pull,
) -> &mut Self {
pub fn pin<C: ChannelMarker>(mut self, pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd, pull: Pull) -> Self {
let pin = RawTimerPin::new(pin, AfType::input(pull));
self.channel_pins[C::CHANNEL.index()] = Some(pin);
self
Expand All @@ -61,10 +57,10 @@ macro_rules! channel_impl {
" to the input capture driver.\n\nSee [`pin()`][Self::pin()] for details.",
)]
pub fn $chx_pin(
&mut self,
self,
pin: impl Peripheral<P = impl TimerPin<T, $channel>> + 'd,
pull: Pull,
) -> &mut Self {
) -> Self {
self.pin::<$channel>(pin, pull)
}
}
Expand Down
14 changes: 7 additions & 7 deletions embassy-stm32/src/timer/simple_pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ impl<'d, T: CoreInstance> Builder<'d, T> {
/// You may use convenience methods [`ch1_pin()`][Self::ch1_pin()] to `ch4_pin()` to aid type
/// inference.
pub fn pin<C: ChannelMarker>(
&mut self,
mut self,
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
output_type: OutputType,
) -> &mut Self {
) -> Self {
let pin = RawTimerPin::new(pin, AfType::output(output_type, Speed::VeryHigh));
self.channel_pins[C::CHANNEL.index()] = Some(pin);
self
Expand All @@ -52,7 +52,7 @@ impl<'d, T: CoreInstance> Builder<'d, T> {
/// Attach update DMA to the PWM driver.
///
/// This enables you to use [`SimplePwm::waveform_up_dma()`].
pub fn up_dma(&mut self, dma: impl Peripheral<P = impl UpDma<T>> + 'd) -> &mut Self {
pub fn up_dma(mut self, dma: impl Peripheral<P = impl UpDma<T>> + 'd) -> Self {
self.up_dma = Some(raw::up_dma(dma));
self
}
Expand All @@ -62,7 +62,7 @@ impl<'d, T: CoreInstance> Builder<'d, T> {
/// This enables you to use [`SimplePwm::waveform_cc_dma()`] with the given channel. You may
/// use convenience methods [`ch1_cc_dma()`][Self::ch1_cc_dma()] to `ch4_cc_dma()`] to aid type
/// inference.
pub fn cc_dma<C: ChannelMarker>(&mut self, dma: impl Peripheral<P = impl CcDma<T, C>> + 'd) -> &mut Self {
pub fn cc_dma<C: ChannelMarker>(mut self, dma: impl Peripheral<P = impl CcDma<T, C>> + 'd) -> Self {
self.cc_dmas[C::CHANNEL.index()] = Some(raw::cc_dma(dma));
self
}
Expand All @@ -78,10 +78,10 @@ macro_rules! channel_impl {
" to the PWM driver.\n\nSee [`pin()`][Self::pin()] for details.",
)]
pub fn $chx_pin(
&mut self,
self,
pin: impl Peripheral<P = impl TimerPin<T, $channel>> + 'd,
output_type: OutputType,
) -> &mut Self {
) -> Self {
self.pin::<$channel>(pin, output_type)
}

Expand All @@ -90,7 +90,7 @@ macro_rules! channel_impl {
stringify!($channel),
" to the PWM driver.\n\nSee [`cc_dma()`][Self::cc_dma()] for details.",
)]
pub fn $chx_cc_dma(&mut self, dma: impl Peripheral<P = impl CcDma<T, $channel>> + 'd) -> &mut Self {
pub fn $chx_cc_dma(self, dma: impl Peripheral<P = impl CcDma<T, $channel>> + 'd) -> Self {
self.cc_dma::<$channel>(dma)
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/stm32f1/src/bin/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::time::khz;
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
use embassy_stm32::timer::{self, Channel};
use embassy_stm32::timer::{self, input_capture, Channel};
use embassy_stm32::{bind_interrupts, peripherals};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
Expand Down Expand Up @@ -39,14 +38,15 @@ async fn main(spawner: Spawner) {

unwrap!(spawner.spawn(blinky(p.PC13)));

let ch3 = CapturePin::new_ch3(p.PA2, Pull::None);
let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());
let mut ic = input_capture::Builder::new(p.TIM2, Irqs)
.ch3_pin(p.PA2, Pull::None)
.build(khz(1000));

loop {
info!("wait for rising edge");
ic.wait_for_rising_edge(Channel::Ch3).await;

let capture_value = ic.get_capture_value(Channel::Ch3);
let capture_value = ic.capture_value(Channel::Ch3);
info!("new capture! {}", capture_value);
}
}
6 changes: 3 additions & 3 deletions examples/stm32f1/src/bin/pwm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ async fn main(spawner: Spawner) {

loop {
Timer::after_millis(500).await;
let period = pwm_input.get_period_ticks();
let width = pwm_input.get_width_ticks();
let duty_cycle = pwm_input.get_duty_cycle();
let period = pwm_input.period_ticks();
let width = pwm_input.width_ticks();
let duty_cycle = pwm_input.duty_cycle();
info!(
"period ticks: {} width ticks: {} duty cycle: {}",
period, width, duty_cycle
Expand Down
10 changes: 5 additions & 5 deletions examples/stm32f4/src/bin/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::time::khz;
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
use embassy_stm32::timer::{self, Channel};
use embassy_stm32::timer::{self, input_capture, Channel};
use embassy_stm32::{bind_interrupts, peripherals};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
Expand Down Expand Up @@ -39,14 +38,15 @@ async fn main(spawner: Spawner) {

unwrap!(spawner.spawn(blinky(p.PB2)));

let ch3 = CapturePin::new_ch3(p.PB10, Pull::None);
let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());
let mut ic = input_capture::Builder::new(p.TIM2, Irqs)
.ch3_pin(p.PB10, Pull::None)
.build(khz(1000));

loop {
info!("wait for risign edge");
ic.wait_for_rising_edge(Channel::Ch3).await;

let capture_value = ic.get_capture_value(Channel::Ch3);
let capture_value = ic.capture_value(Channel::Ch3);
info!("new capture! {}", capture_value);
}
}
10 changes: 5 additions & 5 deletions examples/stm32f4/src/bin/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::OutputType;
use embassy_stm32::time::khz;
use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
use embassy_stm32::timer::Channel;
use embassy_stm32::timer::{simple_pwm, Channel};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};

Expand All @@ -15,9 +14,10 @@ async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");

let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull);
let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10), Default::default());
let max = pwm.get_max_duty();
let mut pwm = simple_pwm::Builder::new(p.TIM1)
.ch1_pin(p.PE9, OutputType::PushPull)
.build(khz(10));
let max = pwm.max_duty();
pwm.enable(Channel::Ch1);

info!("PWM initialized");
Expand Down
25 changes: 6 additions & 19 deletions examples/stm32f4/src/bin/pwm_complementary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::OutputType;
use embassy_stm32::time::khz;
use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin};
use embassy_stm32::timer::simple_pwm::PwmPin;
use embassy_stm32::timer::Channel;
use embassy_stm32::timer::{complementary_pwm, Channel};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};

Expand All @@ -16,23 +14,12 @@ async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");

let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull);
let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull);
let mut pwm = ComplementaryPwm::new(
p.TIM1,
Some(ch1),
Some(ch1n),
None,
None,
None,
None,
None,
None,
khz(10),
Default::default(),
);
let mut pwm = complementary_pwm::Builder::new(p.TIM1)
.ch1_pin(p.PE9, OutputType::PushPull)
.ch1n_pin(p.PA7, OutputType::PushPull)
.build(khz(10), Default::default());

let max = pwm.get_max_duty();
let max = pwm.max_duty();
pwm.set_dead_time(max / 1024);

pwm.enable(Channel::Ch1);
Expand Down
6 changes: 3 additions & 3 deletions examples/stm32f4/src/bin/pwm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ async fn main(spawner: Spawner) {

loop {
Timer::after_millis(500).await;
let period = pwm_input.get_period_ticks();
let width = pwm_input.get_width_ticks();
let duty_cycle = pwm_input.get_duty_cycle();
let period = pwm_input.period_ticks();
let width = pwm_input.width_ticks();
let duty_cycle = pwm_input.duty_cycle();
info!(
"period ticks: {} width ticks: {} duty cycle: {}",
period, width, duty_cycle
Expand Down
23 changes: 8 additions & 15 deletions examples/stm32f4/src/bin/ws2812_pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use embassy_executor::Spawner;
use embassy_stm32::gpio::OutputType;
use embassy_stm32::time::khz;
use embassy_stm32::timer::low_level::CountingMode;
use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
use embassy_stm32::timer::Channel;
use embassy_stm32::timer::{simple_pwm, Channel};
use embassy_time::{Duration, Ticker, Timer};
use {defmt_rtt as _, panic_probe as _};

Expand Down Expand Up @@ -46,22 +44,17 @@ async fn main(_spawner: Spawner) {
device_config.rcc.sys = Sysclk::PLL1_P;
}

let mut dp = embassy_stm32::init(device_config);
let dp = embassy_stm32::init(device_config);

let mut ws2812_pwm = SimplePwm::new(
dp.TIM3,
Some(PwmPin::new_ch1(dp.PB4, OutputType::PushPull)),
None,
None,
None,
khz(800), // data rate of ws2812
CountingMode::EdgeAlignedUp,
);
let mut ws2812_pwm = simple_pwm::Builder::new(dp.TIM3)
.ch1_pin(dp.PB4, OutputType::PushPull)
.up_dma(dp.DMA1_CH2)
.build_4ch(khz(800));

// construct ws2812 non-return-to-zero (NRZ) code bit by bit
// ws2812 only need 24 bits for each LED, but we add one bit more to keep PWM output low

let max_duty = ws2812_pwm.get_max_duty() as u16;
let max_duty = ws2812_pwm.max_duty() as u16;
let n0 = 8 * max_duty / 25; // ws2812 Bit 0 high level timing
let n1 = 2 * n0; // ws2812 Bit 1 high level timing

Expand Down Expand Up @@ -92,7 +85,7 @@ async fn main(_spawner: Spawner) {
loop {
for &color in color_list {
// with &mut, we can easily reuse same DMA channel multiple times
ws2812_pwm.waveform_up(&mut dp.DMA1_CH2, pwm_channel, color).await;
ws2812_pwm.waveform_up_dma(pwm_channel, color).await;
// ws2812 need at least 50 us low level input to confirm the input data and change it's state
Timer::after_micros(50).await;
// wait until ticker tick
Expand Down
26 changes: 6 additions & 20 deletions examples/stm32g0/src/bin/hf_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use defmt::info;
use embassy_executor::Spawner;
use embassy_stm32::gpio::OutputType;
use embassy_stm32::time::khz;
use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin};
use embassy_stm32::timer::simple_pwm::PwmPin;
use embassy_stm32::timer::Channel;
use embassy_stm32::timer::{complementary_pwm, Channel};
use embassy_stm32::Config as PeripheralConfig;
use {defmt_rtt as _, panic_probe as _};

Expand Down Expand Up @@ -35,24 +33,12 @@ async fn main(_spawner: Spawner) {
}
let p = embassy_stm32::init(config);

let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull);
let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull);
let mut pwm = complementary_pwm::Builder::new(p.TIM1)
.ch1_pin(p.PA8, OutputType::PushPull)
.ch1n_pin(p.PA7, OutputType::PushPull)
.build(khz(512), Default::default());

let mut pwm = ComplementaryPwm::new(
p.TIM1,
Some(ch1),
Some(ch1n),
None,
None,
None,
None,
None,
None,
khz(512),
Default::default(),
);

let max = pwm.get_max_duty();
let max = pwm.max_duty();
info!("Max duty: {}", max);

pwm.set_duty(Channel::Ch1, max / 2);
Expand Down
Loading

0 comments on commit 1b54ca0

Please sign in to comment.