aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBruno Bousquet <[email protected]>2024-05-06 02:47:42 -0400
committerBruno Bousquet <[email protected]>2024-05-06 02:47:42 -0400
commit55c8d3f4743bc653c1659dc3e961df86afe7d3db (patch)
tree3ec6a5afaf16d4a7aa38fd532a661e597a832acd /examples
parentb662dfb1838a72d243e939da9349c68c8fef5bdc (diff)
add async capture
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f4/src/bin/input_capture.rs26
1 files changed, 14 insertions, 12 deletions
diff --git a/examples/stm32f4/src/bin/input_capture.rs b/examples/stm32f4/src/bin/input_capture.rs
index 17f65c6b9..862a3103b 100644
--- a/examples/stm32f4/src/bin/input_capture.rs
+++ b/examples/stm32f4/src/bin/input_capture.rs
@@ -3,18 +3,19 @@
3 3
4use defmt::*; 4use defmt::*;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_stm32::bind_interrupts;
6use embassy_stm32::gpio::{Level, Output, Pull, Speed}; 7use embassy_stm32::gpio::{Level, Output, Pull, Speed};
7use embassy_stm32::peripherals::PB2; 8use embassy_stm32::peripherals;
8use embassy_stm32::time::khz; 9use embassy_stm32::time::khz;
9use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; 10use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
10use embassy_stm32::timer::Channel; 11use embassy_stm32::timer::{self, Channel};
11use embassy_time::Timer; 12use embassy_time::Timer;
12use {defmt_rtt as _, panic_probe as _}; 13use {defmt_rtt as _, panic_probe as _};
13 14
14/// Connect PB2 and PB10 with a 1k Ohm resistor 15/// Connect PB2 and PB10 with a 1k Ohm resistor
15 16
16#[embassy_executor::task] 17#[embassy_executor::task]
17async fn blinky(led: PB2) { 18async fn blinky(led: peripherals::PB2) {
18 let mut led = Output::new(led, Level::High, Speed::Low); 19 let mut led = Output::new(led, Level::High, Speed::Low);
19 20
20 loop { 21 loop {
@@ -28,6 +29,10 @@ async fn blinky(led: PB2) {
28 } 29 }
29} 30}
30 31
32bind_interrupts!(struct Irqs {
33 TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
34});
35
31#[embassy_executor::main] 36#[embassy_executor::main]
32async fn main(spawner: Spawner) { 37async fn main(spawner: Spawner) {
33 let p = embassy_stm32::init(Default::default()); 38 let p = embassy_stm32::init(Default::default());
@@ -36,16 +41,13 @@ async fn main(spawner: Spawner) {
36 unwrap!(spawner.spawn(blinky(p.PB2))); 41 unwrap!(spawner.spawn(blinky(p.PB2)));
37 42
38 let ch3 = CapturePin::new_ch3(p.PB10, Pull::None); 43 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()); 44 let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());
40 ic.enable(Channel::Ch3);
41 45
42 loop { 46 loop {
43 // Check for input capture 47 info!("wait for risign edge");
44 if ic.get_input_interrupt(Channel::Ch3) { 48 ic.wait_for_rising_edge(Channel::Ch3).await;
45 let capture_value = ic.get_capture_value(Channel::Ch3); 49
46 info!("New capture! {}", capture_value); 50 let capture_value = ic.get_capture_value(Channel::Ch3);
47 } 51 info!("new capture! {}", capture_value);
48 // Wait a little bit
49 Timer::after_millis(1).await;
50 } 52 }
51} 53}