From 1b54ca0866752eadbc92c708a6eab797ce762834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0pa=C4=8Dek?= Date: Sat, 29 Jun 2024 16:19:23 +0200 Subject: [PATCH] stm32/timer: Update examples for new timer API --- embassy-stm32/src/timer/complementary_pwm.rs | 33 +++---- embassy-stm32/src/timer/input_capture.rs | 10 +-- embassy-stm32/src/timer/simple_pwm.rs | 14 +-- examples/stm32f1/src/bin/input_capture.rs | 10 +-- examples/stm32f1/src/bin/pwm_input.rs | 6 +- examples/stm32f4/src/bin/input_capture.rs | 10 +-- examples/stm32f4/src/bin/pwm.rs | 10 +-- examples/stm32f4/src/bin/pwm_complementary.rs | 25 ++---- examples/stm32f4/src/bin/pwm_input.rs | 6 +- examples/stm32f4/src/bin/ws2812_pwm.rs | 23 ++--- examples/stm32g0/src/bin/hf_timer.rs | 26 ++---- examples/stm32g0/src/bin/input_capture.rs | 18 ++-- examples/stm32g0/src/bin/pwm_complementary.rs | 30 ++----- examples/stm32g0/src/bin/pwm_input.rs | 18 ++-- examples/stm32g4/src/bin/pwm.rs | 10 +-- examples/stm32h7/Cargo.toml | 2 +- examples/stm32h7/src/bin/dac_dma.rs | 33 +++---- examples/stm32h7/src/bin/input_capture.rs | 58 +++++++++++++ .../stm32h7/src/bin/low_level_timer_api.rs | 85 +++++++------------ examples/stm32h7/src/bin/pwm.rs | 12 +-- examples/stm32h7/src/bin/pwm_complementary.rs | 44 ++++++++++ examples/stm32h7/src/bin/pwm_input.rs | 54 ++++++++++++ examples/stm32l4/src/bin/dac_dma.rs | 33 +++---- 23 files changed, 328 insertions(+), 242 deletions(-) create mode 100644 examples/stm32h7/src/bin/input_capture.rs create mode 100644 examples/stm32h7/src/bin/pwm_complementary.rs create mode 100644 examples/stm32h7/src/bin/pwm_input.rs diff --git a/embassy-stm32/src/timer/complementary_pwm.rs b/embassy-stm32/src/timer/complementary_pwm.rs index 51b74d39e5..4ccde191ae 100644 --- a/embassy-stm32/src/timer/complementary_pwm.rs +++ b/embassy-stm32/src/timer/complementary_pwm.rs @@ -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( - &mut self, + mut self, pin: impl Peripheral

> + '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 @@ -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( - &mut self, + mut self, pin: impl Peripheral

> + '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

> + '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

> + 'd, output_type: OutputType, - ) -> &mut Self { + ) -> Self { self.n_pin::<$nchannel>(pin, output_type) } } diff --git a/embassy-stm32/src/timer/input_capture.rs b/embassy-stm32/src/timer/input_capture.rs index 452a98bd85..b842626ca3 100644 --- a/embassy-stm32/src/timer/input_capture.rs +++ b/embassy-stm32/src/timer/input_capture.rs @@ -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( - &mut self, - pin: impl Peripheral

> + 'd, - pull: Pull, - ) -> &mut Self { + pub fn pin(mut self, pin: impl Peripheral

> + 'd, pull: Pull) -> Self { let pin = RawTimerPin::new(pin, AfType::input(pull)); self.channel_pins[C::CHANNEL.index()] = Some(pin); self @@ -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

> + 'd, pull: Pull, - ) -> &mut Self { + ) -> Self { self.pin::<$channel>(pin, pull) } } diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index be2c436fab..0a8c757c7d 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs @@ -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( - &mut self, + mut self, pin: impl Peripheral

> + '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 @@ -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

> + 'd) -> &mut Self { + pub fn up_dma(mut self, dma: impl Peripheral

> + 'd) -> Self { self.up_dma = Some(raw::up_dma(dma)); self } @@ -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(&mut self, dma: impl Peripheral

> + 'd) -> &mut Self { + pub fn cc_dma(mut self, dma: impl Peripheral

> + 'd) -> Self { self.cc_dmas[C::CHANNEL.index()] = Some(raw::cc_dma(dma)); self } @@ -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

> + 'd, output_type: OutputType, - ) -> &mut Self { + ) -> Self { self.pin::<$channel>(pin, output_type) } @@ -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

> + 'd) -> &mut Self { + pub fn $chx_cc_dma(self, dma: impl Peripheral

> + 'd) -> Self { self.cc_dma::<$channel>(dma) } } diff --git a/examples/stm32f1/src/bin/input_capture.rs b/examples/stm32f1/src/bin/input_capture.rs index 5e2dab9e6c..3e894977b9 100644 --- a/examples/stm32f1/src/bin/input_capture.rs +++ b/examples/stm32f1/src/bin/input_capture.rs @@ -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 _}; @@ -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); } } diff --git a/examples/stm32f1/src/bin/pwm_input.rs b/examples/stm32f1/src/bin/pwm_input.rs index f74853d4ea..31dd0a1766 100644 --- a/examples/stm32f1/src/bin/pwm_input.rs +++ b/examples/stm32f1/src/bin/pwm_input.rs @@ -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 diff --git a/examples/stm32f4/src/bin/input_capture.rs b/examples/stm32f4/src/bin/input_capture.rs index 49de33d2b5..e821754e0b 100644 --- a/examples/stm32f4/src/bin/input_capture.rs +++ b/examples/stm32f4/src/bin/input_capture.rs @@ -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 _}; @@ -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); } } diff --git a/examples/stm32f4/src/bin/pwm.rs b/examples/stm32f4/src/bin/pwm.rs index 8844a9f0e3..e61c49b6c1 100644 --- a/examples/stm32f4/src/bin/pwm.rs +++ b/examples/stm32f4/src/bin/pwm.rs @@ -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 _}; @@ -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"); diff --git a/examples/stm32f4/src/bin/pwm_complementary.rs b/examples/stm32f4/src/bin/pwm_complementary.rs index 161f43c48d..9050aea0c2 100644 --- a/examples/stm32f4/src/bin/pwm_complementary.rs +++ b/examples/stm32f4/src/bin/pwm_complementary.rs @@ -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 _}; @@ -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); diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs index ce200549d5..5ff95102ae 100644 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -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 diff --git a/examples/stm32f4/src/bin/ws2812_pwm.rs b/examples/stm32f4/src/bin/ws2812_pwm.rs index cbaff75fcb..300b1b823f 100644 --- a/examples/stm32f4/src/bin/ws2812_pwm.rs +++ b/examples/stm32f4/src/bin/ws2812_pwm.rs @@ -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 _}; @@ -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 @@ -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 diff --git a/examples/stm32g0/src/bin/hf_timer.rs b/examples/stm32g0/src/bin/hf_timer.rs index 3ea06cdee1..106b34bdfb 100644 --- a/examples/stm32g0/src/bin/hf_timer.rs +++ b/examples/stm32g0/src/bin/hf_timer.rs @@ -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 _}; @@ -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); diff --git a/examples/stm32g0/src/bin/input_capture.rs b/examples/stm32g0/src/bin/input_capture.rs index 69fdae96db..2ed4523580 100644 --- a/examples/stm32g0/src/bin/input_capture.rs +++ b/examples/stm32g0/src/bin/input_capture.rs @@ -13,9 +13,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed}; use embassy_stm32::time::khz; -use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; -use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; -use embassy_stm32::timer::Channel; +use embassy_stm32::timer::{input_capture, simple_pwm, Channel}; use embassy_stm32::{bind_interrupts, peripherals, timer}; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; @@ -46,21 +44,23 @@ async fn main(spawner: Spawner) { unwrap!(spawner.spawn(blinky(p.PB1))); - // Connect PB1 and PA8 with a 1k Ohm resistor - let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); - let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default()); + // Connect PA0 and PA8 with a 1k Ohm resistor + let mut pwm = simple_pwm::Builder::new(p.TIM1) + .ch1_pin(p.PA8, OutputType::PushPull) + .build(khz(1)); pwm.enable(Channel::Ch1); pwm.set_duty(Channel::Ch1, 50); - let ch1 = CapturePin::new_ch1(p.PA0, Pull::None); - let mut ic = InputCapture::new(p.TIM2, Some(ch1), None, None, None, Irqs, khz(1000), Default::default()); + let mut ic = input_capture::Builder::new(p.TIM2, Irqs) + .ch1_pin(p.PA0, Pull::None) + .build(khz(1000)); let mut old_capture = 0; loop { ic.wait_for_rising_edge(Channel::Ch1).await; - let capture_value = ic.get_capture_value(Channel::Ch1); + let capture_value = ic.capture_value(Channel::Ch1); info!("{}", capture_value - old_capture); old_capture = capture_value; } diff --git a/examples/stm32g0/src/bin/pwm_complementary.rs b/examples/stm32g0/src/bin/pwm_complementary.rs index 97b163c408..7f1c6a0403 100644 --- a/examples/stm32g0/src/bin/pwm_complementary.rs +++ b/examples/stm32g0/src/bin/pwm_complementary.rs @@ -17,35 +17,21 @@ 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 {defmt_rtt as _, panic_probe as _}; #[embassy_executor::main] async fn main(_spawner: Spawner) { let p = embassy_stm32::init(Default::default()); - let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); - let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull); - let ch2 = PwmPin::new_ch2(p.PB3, OutputType::PushPull); - let ch2n = ComplementaryPwmPin::new_ch2(p.PB0, OutputType::PushPull); + let mut pwm = complementary_pwm::Builder::new(p.TIM1) + .ch1_pin(p.PA8, OutputType::PushPull) + .ch1n_pin(p.PA7, OutputType::PushPull) + .ch2_pin(p.PB3, OutputType::PushPull) + .ch2n_pin(p.PB0, OutputType::PushPull) + .build(khz(100), Default::default()); - let mut pwm = ComplementaryPwm::new( - p.TIM1, - Some(ch1), - Some(ch1n), - Some(ch2), - Some(ch2n), - None, - None, - None, - None, - khz(100), - Default::default(), - ); - - let max = pwm.get_max_duty(); + let max = pwm.max_duty(); info!("Max duty: {}", max); pwm.set_duty(Channel::Ch1, max / 4); diff --git a/examples/stm32g0/src/bin/pwm_input.rs b/examples/stm32g0/src/bin/pwm_input.rs index 152ecda860..1a21c3569d 100644 --- a/examples/stm32g0/src/bin/pwm_input.rs +++ b/examples/stm32g0/src/bin/pwm_input.rs @@ -13,8 +13,7 @@ use embassy_executor::Spawner; use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed}; use embassy_stm32::time::khz; use embassy_stm32::timer::pwm_input::PwmInput; -use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; -use embassy_stm32::timer::Channel; +use embassy_stm32::timer::{simple_pwm, Channel}; use embassy_stm32::{bind_interrupts, peripherals, timer}; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; @@ -42,10 +41,11 @@ async fn main(spawner: Spawner) { let p = embassy_stm32::init(Default::default()); unwrap!(spawner.spawn(blinky(p.PB1))); - // Connect PA8 and PA6 with a 1k Ohm resistor - let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); - let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default()); - let max = pwm.get_max_duty(); + // Connect PA8 and PA0 with a 1k Ohm resistor + let mut pwm = simple_pwm::Builder::new(p.TIM1) + .ch1_pin(p.PA8, OutputType::PushPull) + .build(khz(1)); + let max = pwm.max_duty(); pwm.set_duty(Channel::Ch1, max / 4); pwm.enable(Channel::Ch1); @@ -54,9 +54,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 diff --git a/examples/stm32g4/src/bin/pwm.rs b/examples/stm32g4/src/bin/pwm.rs index d4809a481f..1ede88750c 100644 --- a/examples/stm32g4/src/bin/pwm.rs +++ b/examples/stm32g4/src/bin/pwm.rs @@ -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 _}; @@ -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.PC0, 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.PC0, OutputType::PushPull) + .build(khz(10)); + let max = pwm.max_duty(); pwm.enable(Channel::Ch1); info!("PWM initialized"); diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 78343b74f1..4337a41aeb 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" [dependencies] # Change stm32h743bi to your chip name, if necessary. -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32h743bi", "time-driver-tim2", "exti", "memory-x", "unstable-pac", "chrono"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32h743bi", "time-driver-tim4", "exti", "memory-x", "unstable-pac", "chrono"] } embassy-sync = { version = "0.6.0", path = "../../embassy-sync", features = ["defmt"] } embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } diff --git a/examples/stm32h7/src/bin/dac_dma.rs b/examples/stm32h7/src/bin/dac_dma.rs index 3a9887e3c6..d97ac56ba7 100644 --- a/examples/stm32h7/src/bin/dac_dma.rs +++ b/examples/stm32h7/src/bin/dac_dma.rs @@ -6,9 +6,8 @@ use embassy_executor::Spawner; use embassy_stm32::dac::{DacCh1, DacCh2, ValueArray}; use embassy_stm32::pac::timer::vals::Mms; use embassy_stm32::peripherals::{DAC1, DMA1_CH3, DMA1_CH4, TIM6, TIM7}; -use embassy_stm32::rcc::frequency; use embassy_stm32::time::Hertz; -use embassy_stm32::timer::low_level::Timer; +use embassy_stm32::timer::raw::RawTimer; use micromath::F32Ext; use {defmt_rtt as _, panic_probe as _}; @@ -58,12 +57,14 @@ async fn main(spawner: Spawner) { #[embassy_executor::task] async fn dac_task1(tim: TIM6, mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { let data: &[u8; 256] = &calculate_array::<256>(); + let tim = RawTimer::new_basic(tim); - info!("TIM6 frequency is {}", frequency::()); + let tim_frequency = tim.clock_frequency(); + info!("TIM6 frequency is {}", tim_frequency); const FREQUENCY: Hertz = Hertz::hz(200); // Compute the reload value such that we obtain the FREQUENCY for the sine - let reload: u32 = (frequency::().0 / FREQUENCY.0) / data.len() as u32; + let reload: u32 = (tim_frequency.0 / FREQUENCY.0) / data.len() as u32; // Depends on your clock and on the specific chip used, you may need higher or lower values here if reload < 10 { @@ -74,17 +75,16 @@ async fn dac_task1(tim: TIM6, mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { dac.set_triggering(true); dac.enable(); - let tim = Timer::new(tim); - tim.regs_basic().arr().modify(|w| w.set_arr(reload as u16 - 1)); - tim.regs_basic().cr2().modify(|w| w.set_mms(Mms::UPDATE)); - tim.regs_basic().cr1().modify(|w| { + tim.arr().modify(|w| w.set_arr(reload as u16 - 1)); + tim.cr2_mms().modify(|w| w.set_mms(Mms::UPDATE)); + tim.cr1_core().modify(|w| { w.set_opm(false); w.set_cen(true); }); debug!( "TIM6 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", - frequency::(), + tim_frequency, FREQUENCY, reload, reload as u16, @@ -101,20 +101,21 @@ async fn dac_task1(tim: TIM6, mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { #[embassy_executor::task] async fn dac_task2(tim: TIM7, mut dac: DacCh2<'static, DAC1, DMA1_CH4>) { let data: &[u8; 256] = &calculate_array::<256>(); + let tim = RawTimer::new_basic(tim); - info!("TIM7 frequency is {}", frequency::()); + let tim_frequency = tim.clock_frequency(); + info!("TIM7 frequency is {}", tim_frequency); const FREQUENCY: Hertz = Hertz::hz(600); - let reload: u32 = (frequency::().0 / FREQUENCY.0) / data.len() as u32; + let reload: u32 = (tim_frequency.0 / FREQUENCY.0) / data.len() as u32; if reload < 10 { error!("Reload value {} below threshold!", reload); } - let tim = Timer::new(tim); - tim.regs_basic().arr().modify(|w| w.set_arr(reload as u16 - 1)); - tim.regs_basic().cr2().modify(|w| w.set_mms(Mms::UPDATE)); - tim.regs_basic().cr1().modify(|w| { + tim.arr().modify(|w| w.set_arr(reload as u16 - 1)); + tim.cr2_mms().modify(|w| w.set_mms(Mms::UPDATE)); + tim.cr1_core().modify(|w| { w.set_opm(false); w.set_cen(true); }); @@ -125,7 +126,7 @@ async fn dac_task2(tim: TIM7, mut dac: DacCh2<'static, DAC1, DMA1_CH4>) { debug!( "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", - frequency::(), + tim_frequency, FREQUENCY, reload, reload as u16, diff --git a/examples/stm32h7/src/bin/input_capture.rs b/examples/stm32h7/src/bin/input_capture.rs new file mode 100644 index 0000000000..87798240e8 --- /dev/null +++ b/examples/stm32h7/src/bin/input_capture.rs @@ -0,0 +1,58 @@ +#![no_std] +#![no_main] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::gpio::{Level, Output, Pull, Speed}; +use embassy_stm32::time::khz; +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 _}; + +/// Connect PA5 and PB4 with a 1k Ohm resistor + +#[embassy_executor::task] +async fn blinky(led: peripherals::PB4) { + let mut led = Output::new(led, Level::High, Speed::Low); + + loop { + info!("high"); + led.set_high(); + Timer::after_millis(300).await; + + info!("low"); + led.set_low(); + Timer::after_millis(300).await; + } +} + +bind_interrupts!(struct Irqs { + TIM2 => timer::CaptureCompareInterruptHandler; +}); + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let p = embassy_stm32::init(Default::default()); + info!("Hello World!"); + + unwrap!(spawner.spawn(blinky(p.PB4))); + + let mut ic = input_capture::Builder::new(p.TIM2, Irqs) + .ch1_pin(p.PA5, Pull::None) + .build(khz(1000)); + + let mut last_capture_value = 0; + loop { + info!("wait for rising edge"); + ic.wait_for_rising_edge(Channel::Ch1).await; + + let capture_value = ic.capture_value(Channel::Ch1); + info!( + "new capture! {}, delta {}", + capture_value, + capture_value.wrapping_sub(last_capture_value) + ); + last_capture_value = capture_value; + } +} diff --git a/examples/stm32h7/src/bin/low_level_timer_api.rs b/examples/stm32h7/src/bin/low_level_timer_api.rs index b796996eac..6908f9846f 100644 --- a/examples/stm32h7/src/bin/low_level_timer_api.rs +++ b/examples/stm32h7/src/bin/low_level_timer_api.rs @@ -3,11 +3,12 @@ use defmt::*; use embassy_executor::Spawner; -use embassy_stm32::gpio::{AfType, Flex, OutputType, Speed}; +use embassy_stm32::gpio::{AfType, OutputType, Speed}; use embassy_stm32::time::{khz, Hertz}; use embassy_stm32::timer::low_level::{OutputCompareMode, Timer as LLTimer}; -use embassy_stm32::timer::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance32bit4Channel}; -use embassy_stm32::{into_ref, Config, Peripheral}; +use embassy_stm32::timer::raw::{RawTimer, RawTimerPin}; +use embassy_stm32::timer::{Ch1, Ch2, Ch3, Ch4, Channel, General32BitInstance, General32BitTim, TimerPin}; +use embassy_stm32::{Config, Peripheral}; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; @@ -39,7 +40,7 @@ async fn main(_spawner: Spawner) { info!("Hello World!"); let mut pwm = SimplePwm32::new(p.TIM5, p.PA0, p.PA1, p.PA2, p.PA3, khz(10)); - let max = pwm.get_max_duty(); + let max = pwm.max_duty(); pwm.enable(Channel::Ch1); info!("PWM initialized"); @@ -56,83 +57,61 @@ async fn main(_spawner: Spawner) { Timer::after_millis(300).await; } } -pub struct SimplePwm32<'d, T: GeneralInstance32bit4Channel> { - tim: LLTimer<'d, T>, - _ch1: Flex<'d>, - _ch2: Flex<'d>, - _ch3: Flex<'d>, - _ch4: Flex<'d>, +pub struct SimplePwm32<'d> { + tim: LLTimer<'d, General32BitTim>, + _pins: [RawTimerPin<'d>; 4], } -impl<'d, T: GeneralInstance32bit4Channel> SimplePwm32<'d, T> { - pub fn new( +impl<'d> SimplePwm32<'d> { + pub fn new( tim: impl Peripheral

+ 'd, - ch1: impl Peripheral

> + 'd, - ch2: impl Peripheral

> + 'd, - ch3: impl Peripheral

> + 'd, - ch4: impl Peripheral

> + 'd, + ch1: impl Peripheral

> + 'd, + ch2: impl Peripheral

> + 'd, + ch3: impl Peripheral

> + 'd, + ch4: impl Peripheral

> + 'd, freq: Hertz, ) -> Self { - into_ref!(ch1, ch2, ch3, ch4); - - let af1 = ch1.af_num(); - let af2 = ch2.af_num(); - let af3 = ch3.af_num(); - let af4 = ch4.af_num(); - let mut ch1 = Flex::new(ch1); - let mut ch2 = Flex::new(ch2); - let mut ch3 = Flex::new(ch3); - let mut ch4 = Flex::new(ch4); - ch1.set_as_af_unchecked(af1, AfType::output(OutputType::PushPull, Speed::VeryHigh)); - ch2.set_as_af_unchecked(af2, AfType::output(OutputType::PushPull, Speed::VeryHigh)); - ch3.set_as_af_unchecked(af3, AfType::output(OutputType::PushPull, Speed::VeryHigh)); - ch4.set_as_af_unchecked(af4, AfType::output(OutputType::PushPull, Speed::VeryHigh)); + let tim = RawTimer::new_general_32bit(tim); + let ch1 = RawTimerPin::new(ch1, AfType::output(OutputType::PushPull, Speed::VeryHigh)); + let ch2 = RawTimerPin::new(ch2, AfType::output(OutputType::PushPull, Speed::VeryHigh)); + let ch3 = RawTimerPin::new(ch3, AfType::output(OutputType::PushPull, Speed::VeryHigh)); + let ch4 = RawTimerPin::new(ch4, AfType::output(OutputType::PushPull, Speed::VeryHigh)); let mut this = Self { tim: LLTimer::new(tim), - _ch1: ch1, - _ch2: ch2, - _ch3: ch3, - _ch4: ch4, + _pins: [ch1, ch2, ch3, ch4], }; this.set_frequency(freq); this.tim.start(); - - let r = this.tim.regs_gp32(); - r.ccmr_output(0) - .modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into())); - r.ccmr_output(0) - .modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into())); - r.ccmr_output(1) - .modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into())); - r.ccmr_output(1) - .modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into())); + for n in 0..4 { + this.tim + .raw + .ccmr_output_1ch(n / 2) + .modify(|w| w.set_ocm(n % 2, OutputCompareMode::PwmMode1.into())); + } this } pub fn enable(&mut self, channel: Channel) { - self.tim.regs_gp32().ccer().modify(|w| w.set_cce(channel.index(), true)); + self.tim.raw.ccer_1ch().modify(|w| w.set_cce(channel.index(), true)); } pub fn disable(&mut self, channel: Channel) { - self.tim - .regs_gp32() - .ccer() - .modify(|w| w.set_cce(channel.index(), false)); + self.tim.raw.ccer_1ch().modify(|w| w.set_cce(channel.index(), false)); } pub fn set_frequency(&mut self, freq: Hertz) { self.tim.set_frequency(freq); } - pub fn get_max_duty(&self) -> u32 { - self.tim.regs_gp32().arr().read() + pub fn max_duty(&self) -> u32 { + self.tim.raw.arr_32bit().read() } pub fn set_duty(&mut self, channel: Channel, duty: u32) { - defmt::assert!(duty < self.get_max_duty()); - self.tim.regs_gp32().ccr(channel.index()).write_value(duty) + defmt::assert!(duty < self.max_duty()); + self.tim.raw.ccr_32bit(channel.index()).write_value(duty) } } diff --git a/examples/stm32h7/src/bin/pwm.rs b/examples/stm32h7/src/bin/pwm.rs index 1e48ba67b5..f6974fb310 100644 --- a/examples/stm32h7/src/bin/pwm.rs +++ b/examples/stm32h7/src/bin/pwm.rs @@ -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_stm32::Config; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; @@ -37,9 +36,10 @@ async fn main(_spawner: Spawner) { let p = embassy_stm32::init(config); info!("Hello World!"); - let ch1 = PwmPin::new_ch1(p.PA6, OutputType::PushPull); - let mut pwm = SimplePwm::new(p.TIM3, Some(ch1), None, None, None, khz(10), Default::default()); - let max = pwm.get_max_duty(); + let mut pwm = simple_pwm::Builder::new(p.TIM3) + .ch1_pin(p.PA6, OutputType::PushPull) + .build(khz(10)); + let max = pwm.max_duty(); pwm.enable(Channel::Ch1); info!("PWM initialized"); @@ -52,7 +52,7 @@ async fn main(_spawner: Spawner) { Timer::after_millis(300).await; pwm.set_duty(Channel::Ch1, max / 2); Timer::after_millis(300).await; - pwm.set_duty(Channel::Ch1, max - 1); + pwm.set_duty(Channel::Ch1, max); Timer::after_millis(300).await; } } diff --git a/examples/stm32h7/src/bin/pwm_complementary.rs b/examples/stm32h7/src/bin/pwm_complementary.rs new file mode 100644 index 0000000000..2a68720f1f --- /dev/null +++ b/examples/stm32h7/src/bin/pwm_complementary.rs @@ -0,0 +1,44 @@ +//! PWM complementary example +//! +//! This example uses two complementary pwm outputs from TIM1 with different duty cycles +//! ___ ___ +//! |_________| |_________| PE9 +//! _________ _________ +//! ___| |___| | PE8 +//! _________ _________ +//! |___| |___| PE11 +//! ___ ___ +//! _________| |_________| | PE10 + +#![no_std] +#![no_main] + +use defmt::info; +use embassy_executor::Spawner; +use embassy_stm32::gpio::OutputType; +use embassy_stm32::time::khz; +use embassy_stm32::timer::{complementary_pwm, Channel}; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_stm32::init(Default::default()); + + let mut pwm = complementary_pwm::Builder::new(p.TIM1) + .ch1_pin(p.PE9, OutputType::PushPull) + .ch1n_pin(p.PE8, OutputType::PushPull) + .ch2_pin(p.PE11, OutputType::PushPull) + .ch2n_pin(p.PE10, OutputType::PushPull) + .build(khz(5), Default::default()); + + let max = pwm.max_duty(); + info!("Max duty: {}", max); + + pwm.set_duty(Channel::Ch1, max / 4); + pwm.enable(Channel::Ch1); + pwm.set_duty(Channel::Ch2, max * 3 / 4); + pwm.enable(Channel::Ch2); + pwm.set_dead_time(500); + + loop {} +} diff --git a/examples/stm32h7/src/bin/pwm_input.rs b/examples/stm32h7/src/bin/pwm_input.rs new file mode 100644 index 0000000000..e197d955eb --- /dev/null +++ b/examples/stm32h7/src/bin/pwm_input.rs @@ -0,0 +1,54 @@ +#![no_std] +#![no_main] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::gpio::{Level, Output, Pull, Speed}; +use embassy_stm32::time::khz; +use embassy_stm32::timer::pwm_input::PwmInput; +use embassy_stm32::{bind_interrupts, peripherals, timer}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +/// Connect PA5 and PB4 with a 1k Ohm resistor + +#[embassy_executor::task] +async fn blinky(led: peripherals::PB4) { + let mut led = Output::new(led, Level::High, Speed::Low); + + loop { + info!("high"); + led.set_high(); + Timer::after_millis(400).await; + + info!("low"); + led.set_low(); + Timer::after_millis(200).await; + } +} + +bind_interrupts!(struct Irqs { + TIM2 => timer::CaptureCompareInterruptHandler; +}); + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let p = embassy_stm32::init(Default::default()); + info!("Hello World!"); + + unwrap!(spawner.spawn(blinky(p.PB4))); + + let mut pwm_input = PwmInput::new(p.TIM2, p.PA5, Pull::None, khz(10)); + pwm_input.enable(); + + loop { + Timer::after_millis(500).await; + 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 + ); + } +} diff --git a/examples/stm32l4/src/bin/dac_dma.rs b/examples/stm32l4/src/bin/dac_dma.rs index d01b016c0c..8b467f8338 100644 --- a/examples/stm32l4/src/bin/dac_dma.rs +++ b/examples/stm32l4/src/bin/dac_dma.rs @@ -6,9 +6,8 @@ use embassy_executor::Spawner; use embassy_stm32::dac::{DacCh1, DacCh2, ValueArray}; use embassy_stm32::pac::timer::vals::Mms; use embassy_stm32::peripherals::{DAC1, DMA1_CH3, DMA1_CH4, TIM6, TIM7}; -use embassy_stm32::rcc::frequency; use embassy_stm32::time::Hertz; -use embassy_stm32::timer::low_level::Timer; +use embassy_stm32::timer::raw::RawTimer; use micromath::F32Ext; use {defmt_rtt as _, panic_probe as _}; @@ -29,12 +28,14 @@ async fn main(spawner: Spawner) { #[embassy_executor::task] async fn dac_task1(tim: TIM6, mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { let data: &[u8; 256] = &calculate_array::<256>(); + let tim = RawTimer::new_basic(tim); - info!("TIM6 frequency is {}", frequency::()); + let tim_frequency = tim.clock_frequency(); + info!("TIM6 frequency is {}", tim_frequency); const FREQUENCY: Hertz = Hertz::hz(200); // Compute the reload value such that we obtain the FREQUENCY for the sine - let reload: u32 = (frequency::().0 / FREQUENCY.0) / data.len() as u32; + let reload: u32 = (tim_frequency.0 / FREQUENCY.0) / data.len() as u32; // Depends on your clock and on the specific chip used, you may need higher or lower values here if reload < 10 { @@ -45,17 +46,16 @@ async fn dac_task1(tim: TIM6, mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { dac.set_triggering(true); dac.enable(); - let tim = Timer::new(tim); - tim.regs_basic().arr().modify(|w| w.set_arr(reload as u16 - 1)); - tim.regs_basic().cr2().modify(|w| w.set_mms(Mms::UPDATE)); - tim.regs_basic().cr1().modify(|w| { + tim.arr().modify(|w| w.set_arr(reload as u16 - 1)); + tim.cr2_mms().modify(|w| w.set_mms(Mms::UPDATE)); + tim.cr1_core().modify(|w| { w.set_opm(false); w.set_cen(true); }); debug!( "TIM6 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", - frequency::(), + tim_frequency, FREQUENCY, reload, reload as u16, @@ -72,20 +72,21 @@ async fn dac_task1(tim: TIM6, mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { #[embassy_executor::task] async fn dac_task2(tim: TIM7, mut dac: DacCh2<'static, DAC1, DMA1_CH4>) { let data: &[u8; 256] = &calculate_array::<256>(); + let tim = RawTimer::new_basic(tim); - info!("TIM7 frequency is {}", frequency::()); + let tim_frequency = tim.clock_frequency(); + info!("TIM7 frequency is {}", tim_frequency); const FREQUENCY: Hertz = Hertz::hz(600); - let reload: u32 = (frequency::().0 / FREQUENCY.0) / data.len() as u32; + let reload: u32 = (tim_frequency.0 / FREQUENCY.0) / data.len() as u32; if reload < 10 { error!("Reload value {} below threshold!", reload); } - let tim = Timer::new(tim); - tim.regs_basic().arr().modify(|w| w.set_arr(reload as u16 - 1)); - tim.regs_basic().cr2().modify(|w| w.set_mms(Mms::UPDATE)); - tim.regs_basic().cr1().modify(|w| { + tim.arr().modify(|w| w.set_arr(reload as u16 - 1)); + tim.cr2_mms().modify(|w| w.set_mms(Mms::UPDATE)); + tim.cr1_core().modify(|w| { w.set_opm(false); w.set_cen(true); }); @@ -96,7 +97,7 @@ async fn dac_task2(tim: TIM7, mut dac: DacCh2<'static, DAC1, DMA1_CH4>) { debug!( "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", - frequency::(), + tim_frequency, FREQUENCY, reload, reload as u16,