From 41b9a12574f9ad992ca986b87926838fbe1f1775 Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Tue, 28 May 2024 20:36:23 -0400 Subject: compile pwm_input example --- examples/stm32f4/src/bin/pwm_input.rs | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/stm32f4/src/bin/pwm_input.rs (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs new file mode 100644 index 000000000..49de33d2b --- /dev/null +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -0,0 +1,52 @@ +#![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::input_capture::{CapturePin, InputCapture}; +use embassy_stm32::timer::{self, Channel}; +use embassy_stm32::{bind_interrupts, peripherals}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +/// Connect PB2 and PB10 with a 1k Ohm resistor + +#[embassy_executor::task] +async fn blinky(led: peripherals::PB2) { + 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.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()); + + loop { + info!("wait for risign edge"); + ic.wait_for_rising_edge(Channel::Ch3).await; + + let capture_value = ic.get_capture_value(Channel::Ch3); + info!("new capture! {}", capture_value); + } +} -- cgit From a6c419d096bf2a4d14243fe90a7e2c1881b33fdf Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Tue, 28 May 2024 23:12:08 -0400 Subject: add f103 example for input_capture --- examples/stm32f4/src/bin/pwm_input.rs | 52 ----------------------------------- 1 file changed, 52 deletions(-) delete mode 100644 examples/stm32f4/src/bin/pwm_input.rs (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs deleted file mode 100644 index 49de33d2b..000000000 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ /dev/null @@ -1,52 +0,0 @@ -#![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::input_capture::{CapturePin, InputCapture}; -use embassy_stm32::timer::{self, Channel}; -use embassy_stm32::{bind_interrupts, peripherals}; -use embassy_time::Timer; -use {defmt_rtt as _, panic_probe as _}; - -/// Connect PB2 and PB10 with a 1k Ohm resistor - -#[embassy_executor::task] -async fn blinky(led: peripherals::PB2) { - 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.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()); - - loop { - info!("wait for risign edge"); - ic.wait_for_rising_edge(Channel::Ch3).await; - - let capture_value = ic.get_capture_value(Channel::Ch3); - info!("new capture! {}", capture_value); - } -} -- cgit From 521332bdd1d682b1d43ab7896b54e280c17b9771 Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Wed, 29 May 2024 00:28:26 -0400 Subject: pwm_input is working on F446 --- examples/stm32f4/src/bin/pwm_input.rs | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/stm32f4/src/bin/pwm_input.rs (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs new file mode 100644 index 000000000..e57e58c22 --- /dev/null +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -0,0 +1,52 @@ +#![no_std] +#![no_main] + +use cortex_m::asm; +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::gpio::{Level, Output, Pull, Speed}; +use embassy_stm32::time::khz; +use embassy_stm32::timer::{self, pwm_input::PwmInput}; +use embassy_stm32::{bind_interrupts, peripherals}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +/// Connect PB2 and PB10 with a 1k Ohm resistor + +#[embassy_executor::task] +async fn blinky(led: peripherals::PB2) { + 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.PB2))); + + let mut pwm_input = PwmInput::new(p.TIM3, p.PA6, Pull::None, khz(10)); + pwm_input.enable(); + + loop { + Timer::after_millis(500).await; + let _per = pwm_input.get_period_ticks(); + let _dc = pwm_input.get_duty_ticks(); + let _pc = pwm_input.get_duty_cycle(); + asm::nop(); + } +} -- cgit From 61f1f80e90c3fd8ec505936f3bab0a50d2ba9b79 Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Wed, 29 May 2024 00:52:55 -0400 Subject: fix fmt --- examples/stm32f4/src/bin/pwm_input.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs index e57e58c22..98ea50df4 100644 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -6,7 +6,8 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::gpio::{Level, Output, Pull, Speed}; use embassy_stm32::time::khz; -use embassy_stm32::timer::{self, pwm_input::PwmInput}; +use embassy_stm32::timer; +use embassy_stm32::timer::pwm_input::PwmInput; use embassy_stm32::{bind_interrupts, peripherals}; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; -- cgit From 7f4803ddaf8f6eeeec45418f23b092bff95222d2 Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Wed, 29 May 2024 00:55:49 -0400 Subject: fix fmt again --- examples/stm32f4/src/bin/pwm_input.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs index 98ea50df4..eb1e7cb87 100644 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -6,9 +6,8 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::gpio::{Level, Output, Pull, Speed}; use embassy_stm32::time::khz; -use embassy_stm32::timer; use embassy_stm32::timer::pwm_input::PwmInput; -use embassy_stm32::{bind_interrupts, peripherals}; +use embassy_stm32::{bind_interrupts, peripherals, timer}; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; -- cgit From a23fa8dcb2e22ed2d8bac15fd875dfe53af43eda Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Wed, 29 May 2024 09:14:05 -0400 Subject: Apply suggestions from code review Co-authored-by: Romain Reignier --- examples/stm32f4/src/bin/pwm_input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs index eb1e7cb87..30cefac3a 100644 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -11,7 +11,7 @@ use embassy_stm32::{bind_interrupts, peripherals, timer}; use embassy_time::Timer; use {defmt_rtt as _, panic_probe as _}; -/// Connect PB2 and PB10 with a 1k Ohm resistor +/// Connect PB2 and PA6 with a 1k Ohm resistor #[embassy_executor::task] async fn blinky(led: peripherals::PB2) { -- cgit From 292c1dd0b858fe8dde74edd2f60a96f4c9e588da Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Wed, 29 May 2024 09:58:46 -0400 Subject: rename get_width_ticks and add info!() in examples --- examples/stm32f4/src/bin/pwm_input.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs index 30cefac3a..8fe1fdb5b 100644 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] -use cortex_m::asm; use defmt::*; use embassy_executor::Spawner; use embassy_stm32::gpio::{Level, Output, Pull, Speed}; @@ -44,9 +43,9 @@ async fn main(spawner: Spawner) { loop { Timer::after_millis(500).await; - let _per = pwm_input.get_period_ticks(); - let _dc = pwm_input.get_duty_ticks(); - let _pc = pwm_input.get_duty_cycle(); - asm::nop(); + let period = pwm_input.get_period_ticks(); + let width = pwm_input.get_width_ticks(); + let duty_cycle = pwm_input.get_duty_cycle(); + info!("period ticks: {} width ticks: {} duty cycle: {}", period, width, duty_cycle); } } -- cgit From a87b33303403ba3601d0c631b9efe1cb3853c73b Mon Sep 17 00:00:00 2001 From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com> Date: Wed, 29 May 2024 10:02:54 -0400 Subject: fix fmt --- examples/stm32f4/src/bin/pwm_input.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'examples/stm32f4/src') diff --git a/examples/stm32f4/src/bin/pwm_input.rs b/examples/stm32f4/src/bin/pwm_input.rs index 8fe1fdb5b..ce200549d 100644 --- a/examples/stm32f4/src/bin/pwm_input.rs +++ b/examples/stm32f4/src/bin/pwm_input.rs @@ -46,6 +46,9 @@ async fn main(spawner: Spawner) { let period = pwm_input.get_period_ticks(); let width = pwm_input.get_width_ticks(); let duty_cycle = pwm_input.get_duty_cycle(); - info!("period ticks: {} width ticks: {} duty cycle: {}", period, width, duty_cycle); + info!( + "period ticks: {} width ticks: {} duty cycle: {}", + period, width, duty_cycle + ); } } -- cgit