diff options
| author | Bruno Bousquet <[email protected]> | 2024-05-05 23:00:48 -0400 |
|---|---|---|
| committer | Bruno Bousquet <[email protected]> | 2024-05-05 23:00:48 -0400 |
| commit | 29d6fa0a4aa3203e95cf81ada366cb0ccf593af4 (patch) | |
| tree | 9c94a5a69b04a099dd50449b6ab932424c146749 /examples/stm32f4/src/bin/input_capture.rs | |
| parent | 431a60ca6384a77243d33f5b1bbef878267bea49 (diff) | |
add get_input_interrupt
Diffstat (limited to 'examples/stm32f4/src/bin/input_capture.rs')
| -rw-r--r-- | examples/stm32f4/src/bin/input_capture.rs | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/examples/stm32f4/src/bin/input_capture.rs b/examples/stm32f4/src/bin/input_capture.rs index 714f043b6..17f65c6b9 100644 --- a/examples/stm32f4/src/bin/input_capture.rs +++ b/examples/stm32f4/src/bin/input_capture.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | use defmt::*; | 4 | use defmt::*; |
| 5 | use embassy_executor::Spawner; | 5 | use embassy_executor::Spawner; |
| 6 | use embassy_stm32::gpio::{Level, Output, Pull, Speed}; | 6 | use embassy_stm32::gpio::{Level, Output, Pull, Speed}; |
| 7 | use embassy_stm32::peripherals::PB2; | ||
| 7 | use embassy_stm32::time::khz; | 8 | use embassy_stm32::time::khz; |
| 8 | use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; | 9 | use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; |
| 9 | use embassy_stm32::timer::Channel; | 10 | use embassy_stm32::timer::Channel; |
| @@ -12,18 +13,9 @@ use {defmt_rtt as _, panic_probe as _}; | |||
| 12 | 13 | ||
| 13 | /// Connect PB2 and PB10 with a 1k Ohm resistor | 14 | /// Connect PB2 and PB10 with a 1k Ohm resistor |
| 14 | 15 | ||
| 15 | #[embassy_executor::main] | 16 | #[embassy_executor::task] |
| 16 | async fn main(_spawner: Spawner) { | 17 | async fn blinky(led: PB2) { |
| 17 | let p = embassy_stm32::init(Default::default()); | 18 | let mut led = Output::new(led, Level::High, Speed::Low); |
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let mut led = Output::new(p.PB2, Level::High, Speed::Low); | ||
| 21 | |||
| 22 | let ch3 = CapturePin::new_ch3(p.PB10, Pull::None); | ||
| 23 | let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, khz(1000), Default::default()); | ||
| 24 | ic.enable(Channel::Ch3); | ||
| 25 | |||
| 26 | let mut last = 0; | ||
| 27 | 19 | ||
| 28 | loop { | 20 | loop { |
| 29 | info!("high"); | 21 | info!("high"); |
| @@ -33,12 +25,27 @@ async fn main(_spawner: Spawner) { | |||
| 33 | info!("low"); | 25 | info!("low"); |
| 34 | led.set_low(); | 26 | led.set_low(); |
| 35 | Timer::after_millis(300).await; | 27 | Timer::after_millis(300).await; |
| 28 | } | ||
| 29 | } | ||
| 36 | 30 | ||
| 31 | #[embassy_executor::main] | ||
| 32 | async fn main(spawner: Spawner) { | ||
| 33 | let p = embassy_stm32::init(Default::default()); | ||
| 34 | info!("Hello World!"); | ||
| 35 | |||
| 36 | unwrap!(spawner.spawn(blinky(p.PB2))); | ||
| 37 | |||
| 38 | let ch3 = CapturePin::new_ch3(p.PB10, Pull::None); | ||
| 39 | let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, khz(1000), Default::default()); | ||
| 40 | ic.enable(Channel::Ch3); | ||
| 41 | |||
| 42 | loop { | ||
| 37 | // Check for input capture | 43 | // Check for input capture |
| 38 | let cap = ic.get_capture_value(Channel::Ch3); | 44 | if ic.get_input_interrupt(Channel::Ch3) { |
| 39 | if cap != last { | 45 | let capture_value = ic.get_capture_value(Channel::Ch3); |
| 40 | info!("New capture!"); | 46 | info!("New capture! {}", capture_value); |
| 41 | last = cap; | ||
| 42 | } | 47 | } |
| 48 | // Wait a little bit | ||
| 49 | Timer::after_millis(1).await; | ||
| 43 | } | 50 | } |
| 44 | } | 51 | } |
