Skip to content

Commit 1b54ca0

Browse files
committed
stm32/timer: Update examples for new timer API
1 parent 16c82c4 commit 1b54ca0

File tree

23 files changed

+328
-242
lines changed

23 files changed

+328
-242
lines changed

embassy-stm32/src/timer/complementary_pwm.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ impl<'d, T: Advanced4ChInstance> Builder<'d, T> {
4242
/// You may use convenience methods [`ch1_pin()`][Self::ch1_pin()] to `ch4_pin()` to aid type
4343
/// inference.
4444
pub fn pin<C: ChannelMarker>(
45-
&mut self,
45+
mut self,
4646
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
4747
output_type: OutputType,
48-
) -> &mut Self {
48+
) -> Self {
4949
let pin = RawTimerPin::new(pin, AfType::output(output_type, Speed::VeryHigh));
5050
self.channel_pins[C::CHANNEL.index()] = Some(pin);
5151
self
@@ -56,42 +56,43 @@ impl<'d, T: Advanced4ChInstance> Builder<'d, T> {
5656
/// You may use convenience methods [`ch1n_pin()`][Self::ch1n_pin()] to `ch4n_pin()` to aid type
5757
/// inference.
5858
pub fn n_pin<C: NChannelMarker>(
59-
&mut self,
59+
mut self,
6060
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
6161
output_type: OutputType,
62-
) -> &mut Self {
62+
) -> Self {
6363
let pin = RawTimerPin::new(pin, AfType::output(output_type, Speed::VeryHigh));
6464
self.n_channel_pins[C::N_CHANNEL.index()] = Some(pin);
6565
self
6666
}
6767
}
6868

69+
#[rustfmt::skip]
6970
macro_rules! channel_impl {
7071
($chx_pin:ident, $chxn_pin:ident, $channel:ident, $nchannel:ident) => {
7172
impl<'d, T: Advanced4ChInstance> Builder<'d, T> {
7273
#[doc = concat!(
73-
"Attach an output pin for channel ",
74-
stringify!($channel),
75-
" to the complementary PWM driver.\n\nSee [`pin()`][Self::pin()] for details.",
76-
)]
74+
"Attach an output pin for channel ",
75+
stringify!($channel),
76+
" to the complementary PWM driver.\n\nSee [`pin()`][Self::pin()] for details.",
77+
)]
7778
pub fn $chx_pin(
78-
&mut self,
79+
self,
7980
pin: impl Peripheral<P = impl TimerPin<T, $channel>> + 'd,
8081
output_type: OutputType,
81-
) -> &mut Self {
82+
) -> Self {
8283
self.pin::<$channel>(pin, output_type)
8384
}
8485

8586
#[doc = concat!(
86-
"Attach a complementary output pin for channel ",
87-
stringify!($channel),
88-
" to the complementary PWM driver.\n\nSee [`n_pin()`][Self::pin()] for details.",
89-
)]
87+
"Attach a complementary output pin for channel ",
88+
stringify!($channel),
89+
" to the complementary PWM driver.\n\nSee [`n_pin()`][Self::pin()] for details.",
90+
)]
9091
pub fn $chxn_pin(
91-
&mut self,
92+
self,
9293
pin: impl Peripheral<P = impl TimerPin<T, $nchannel>> + 'd,
9394
output_type: OutputType,
94-
) -> &mut Self {
95+
) -> Self {
9596
self.n_pin::<$nchannel>(pin, output_type)
9697
}
9798
}

embassy-stm32/src/timer/input_capture.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ impl<'d, T: General1ChInstance> Builder<'d, T> {
4040
///
4141
/// You may use convenience methods [`ch1_pin()`][Self::ch1_pin()] to `ch4_pin()` to aid type
4242
/// inference.
43-
pub fn pin<C: ChannelMarker>(
44-
&mut self,
45-
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
46-
pull: Pull,
47-
) -> &mut Self {
43+
pub fn pin<C: ChannelMarker>(mut self, pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd, pull: Pull) -> Self {
4844
let pin = RawTimerPin::new(pin, AfType::input(pull));
4945
self.channel_pins[C::CHANNEL.index()] = Some(pin);
5046
self
@@ -61,10 +57,10 @@ macro_rules! channel_impl {
6157
" to the input capture driver.\n\nSee [`pin()`][Self::pin()] for details.",
6258
)]
6359
pub fn $chx_pin(
64-
&mut self,
60+
self,
6561
pin: impl Peripheral<P = impl TimerPin<T, $channel>> + 'd,
6662
pull: Pull,
67-
) -> &mut Self {
63+
) -> Self {
6864
self.pin::<$channel>(pin, pull)
6965
}
7066
}

embassy-stm32/src/timer/simple_pwm.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ impl<'d, T: CoreInstance> Builder<'d, T> {
4040
/// You may use convenience methods [`ch1_pin()`][Self::ch1_pin()] to `ch4_pin()` to aid type
4141
/// inference.
4242
pub fn pin<C: ChannelMarker>(
43-
&mut self,
43+
mut self,
4444
pin: impl Peripheral<P = impl TimerPin<T, C>> + 'd,
4545
output_type: OutputType,
46-
) -> &mut Self {
46+
) -> Self {
4747
let pin = RawTimerPin::new(pin, AfType::output(output_type, Speed::VeryHigh));
4848
self.channel_pins[C::CHANNEL.index()] = Some(pin);
4949
self
@@ -52,7 +52,7 @@ impl<'d, T: CoreInstance> Builder<'d, T> {
5252
/// Attach update DMA to the PWM driver.
5353
///
5454
/// This enables you to use [`SimplePwm::waveform_up_dma()`].
55-
pub fn up_dma(&mut self, dma: impl Peripheral<P = impl UpDma<T>> + 'd) -> &mut Self {
55+
pub fn up_dma(mut self, dma: impl Peripheral<P = impl UpDma<T>> + 'd) -> Self {
5656
self.up_dma = Some(raw::up_dma(dma));
5757
self
5858
}
@@ -62,7 +62,7 @@ impl<'d, T: CoreInstance> Builder<'d, T> {
6262
/// This enables you to use [`SimplePwm::waveform_cc_dma()`] with the given channel. You may
6363
/// use convenience methods [`ch1_cc_dma()`][Self::ch1_cc_dma()] to `ch4_cc_dma()`] to aid type
6464
/// inference.
65-
pub fn cc_dma<C: ChannelMarker>(&mut self, dma: impl Peripheral<P = impl CcDma<T, C>> + 'd) -> &mut Self {
65+
pub fn cc_dma<C: ChannelMarker>(mut self, dma: impl Peripheral<P = impl CcDma<T, C>> + 'd) -> Self {
6666
self.cc_dmas[C::CHANNEL.index()] = Some(raw::cc_dma(dma));
6767
self
6868
}
@@ -78,10 +78,10 @@ macro_rules! channel_impl {
7878
" to the PWM driver.\n\nSee [`pin()`][Self::pin()] for details.",
7979
)]
8080
pub fn $chx_pin(
81-
&mut self,
81+
self,
8282
pin: impl Peripheral<P = impl TimerPin<T, $channel>> + 'd,
8383
output_type: OutputType,
84-
) -> &mut Self {
84+
) -> Self {
8585
self.pin::<$channel>(pin, output_type)
8686
}
8787

@@ -90,7 +90,7 @@ macro_rules! channel_impl {
9090
stringify!($channel),
9191
" to the PWM driver.\n\nSee [`cc_dma()`][Self::cc_dma()] for details.",
9292
)]
93-
pub fn $chx_cc_dma(&mut self, dma: impl Peripheral<P = impl CcDma<T, $channel>> + 'd) -> &mut Self {
93+
pub fn $chx_cc_dma(self, dma: impl Peripheral<P = impl CcDma<T, $channel>> + 'd) -> Self {
9494
self.cc_dma::<$channel>(dma)
9595
}
9696
}

examples/stm32f1/src/bin/input_capture.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
77
use embassy_stm32::time::khz;
8-
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
9-
use embassy_stm32::timer::{self, Channel};
8+
use embassy_stm32::timer::{self, input_capture, Channel};
109
use embassy_stm32::{bind_interrupts, peripherals};
1110
use embassy_time::Timer;
1211
use {defmt_rtt as _, panic_probe as _};
@@ -39,14 +38,15 @@ async fn main(spawner: Spawner) {
3938

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

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

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

49-
let capture_value = ic.get_capture_value(Channel::Ch3);
49+
let capture_value = ic.capture_value(Channel::Ch3);
5050
info!("new capture! {}", capture_value);
5151
}
5252
}

examples/stm32f1/src/bin/pwm_input.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ async fn main(spawner: Spawner) {
4343

4444
loop {
4545
Timer::after_millis(500).await;
46-
let period = pwm_input.get_period_ticks();
47-
let width = pwm_input.get_width_ticks();
48-
let duty_cycle = pwm_input.get_duty_cycle();
46+
let period = pwm_input.period_ticks();
47+
let width = pwm_input.width_ticks();
48+
let duty_cycle = pwm_input.duty_cycle();
4949
info!(
5050
"period ticks: {} width ticks: {} duty cycle: {}",
5151
period, width, duty_cycle

examples/stm32f4/src/bin/input_capture.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
77
use embassy_stm32::time::khz;
8-
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
9-
use embassy_stm32::timer::{self, Channel};
8+
use embassy_stm32::timer::{self, input_capture, Channel};
109
use embassy_stm32::{bind_interrupts, peripherals};
1110
use embassy_time::Timer;
1211
use {defmt_rtt as _, panic_probe as _};
@@ -39,14 +38,15 @@ async fn main(spawner: Spawner) {
3938

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

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

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

49-
let capture_value = ic.get_capture_value(Channel::Ch3);
49+
let capture_value = ic.capture_value(Channel::Ch3);
5050
info!("new capture! {}", capture_value);
5151
}
5252
}

examples/stm32f4/src/bin/pwm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_stm32::gpio::OutputType;
77
use embassy_stm32::time::khz;
8-
use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
9-
use embassy_stm32::timer::Channel;
8+
use embassy_stm32::timer::{simple_pwm, Channel};
109
use embassy_time::Timer;
1110
use {defmt_rtt as _, panic_probe as _};
1211

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

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

2323
info!("PWM initialized");

examples/stm32f4/src/bin/pwm_complementary.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_stm32::gpio::OutputType;
77
use embassy_stm32::time::khz;
8-
use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin};
9-
use embassy_stm32::timer::simple_pwm::PwmPin;
10-
use embassy_stm32::timer::Channel;
8+
use embassy_stm32::timer::{complementary_pwm, Channel};
119
use embassy_time::Timer;
1210
use {defmt_rtt as _, panic_probe as _};
1311

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

19-
let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull);
20-
let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull);
21-
let mut pwm = ComplementaryPwm::new(
22-
p.TIM1,
23-
Some(ch1),
24-
Some(ch1n),
25-
None,
26-
None,
27-
None,
28-
None,
29-
None,
30-
None,
31-
khz(10),
32-
Default::default(),
33-
);
17+
let mut pwm = complementary_pwm::Builder::new(p.TIM1)
18+
.ch1_pin(p.PE9, OutputType::PushPull)
19+
.ch1n_pin(p.PA7, OutputType::PushPull)
20+
.build(khz(10), Default::default());
3421

35-
let max = pwm.get_max_duty();
22+
let max = pwm.max_duty();
3623
pwm.set_dead_time(max / 1024);
3724

3825
pwm.enable(Channel::Ch1);

examples/stm32f4/src/bin/pwm_input.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ async fn main(spawner: Spawner) {
4343

4444
loop {
4545
Timer::after_millis(500).await;
46-
let period = pwm_input.get_period_ticks();
47-
let width = pwm_input.get_width_ticks();
48-
let duty_cycle = pwm_input.get_duty_cycle();
46+
let period = pwm_input.period_ticks();
47+
let width = pwm_input.width_ticks();
48+
let duty_cycle = pwm_input.duty_cycle();
4949
info!(
5050
"period ticks: {} width ticks: {} duty cycle: {}",
5151
period, width, duty_cycle

examples/stm32f4/src/bin/ws2812_pwm.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
use embassy_executor::Spawner;
1616
use embassy_stm32::gpio::OutputType;
1717
use embassy_stm32::time::khz;
18-
use embassy_stm32::timer::low_level::CountingMode;
19-
use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
20-
use embassy_stm32::timer::Channel;
18+
use embassy_stm32::timer::{simple_pwm, Channel};
2119
use embassy_time::{Duration, Ticker, Timer};
2220
use {defmt_rtt as _, panic_probe as _};
2321

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

49-
let mut dp = embassy_stm32::init(device_config);
47+
let dp = embassy_stm32::init(device_config);
5048

51-
let mut ws2812_pwm = SimplePwm::new(
52-
dp.TIM3,
53-
Some(PwmPin::new_ch1(dp.PB4, OutputType::PushPull)),
54-
None,
55-
None,
56-
None,
57-
khz(800), // data rate of ws2812
58-
CountingMode::EdgeAlignedUp,
59-
);
49+
let mut ws2812_pwm = simple_pwm::Builder::new(dp.TIM3)
50+
.ch1_pin(dp.PB4, OutputType::PushPull)
51+
.up_dma(dp.DMA1_CH2)
52+
.build_4ch(khz(800));
6053

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

64-
let max_duty = ws2812_pwm.get_max_duty() as u16;
57+
let max_duty = ws2812_pwm.max_duty() as u16;
6558
let n0 = 8 * max_duty / 25; // ws2812 Bit 0 high level timing
6659
let n1 = 2 * n0; // ws2812 Bit 1 high level timing
6760

@@ -92,7 +85,7 @@ async fn main(_spawner: Spawner) {
9285
loop {
9386
for &color in color_list {
9487
// with &mut, we can easily reuse same DMA channel multiple times
95-
ws2812_pwm.waveform_up(&mut dp.DMA1_CH2, pwm_channel, color).await;
88+
ws2812_pwm.waveform_up_dma(pwm_channel, color).await;
9689
// ws2812 need at least 50 us low level input to confirm the input data and change it's state
9790
Timer::after_micros(50).await;
9891
// wait until ticker tick

examples/stm32g0/src/bin/hf_timer.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use defmt::info;
55
use embassy_executor::Spawner;
66
use embassy_stm32::gpio::OutputType;
77
use embassy_stm32::time::khz;
8-
use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin};
9-
use embassy_stm32::timer::simple_pwm::PwmPin;
10-
use embassy_stm32::timer::Channel;
8+
use embassy_stm32::timer::{complementary_pwm, Channel};
119
use embassy_stm32::Config as PeripheralConfig;
1210
use {defmt_rtt as _, panic_probe as _};
1311

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

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

41-
let mut pwm = ComplementaryPwm::new(
42-
p.TIM1,
43-
Some(ch1),
44-
Some(ch1n),
45-
None,
46-
None,
47-
None,
48-
None,
49-
None,
50-
None,
51-
khz(512),
52-
Default::default(),
53-
);
54-
55-
let max = pwm.get_max_duty();
41+
let max = pwm.max_duty();
5642
info!("Max duty: {}", max);
5743

5844
pwm.set_duty(Channel::Ch1, max / 2);

0 commit comments

Comments
 (0)