aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Bousquet <[email protected]>2024-05-05 23:00:48 -0400
committerBruno Bousquet <[email protected]>2024-05-05 23:00:48 -0400
commit29d6fa0a4aa3203e95cf81ada366cb0ccf593af4 (patch)
tree9c94a5a69b04a099dd50449b6ab932424c146749
parent431a60ca6384a77243d33f5b1bbef878267bea49 (diff)
add get_input_interrupt
-rw-r--r--embassy-stm32/src/timer/input_capture.rs5
-rw-r--r--embassy-stm32/src/timer/low_level.rs5
-rw-r--r--examples/stm32f4/src/bin/input_capture.rs39
3 files changed, 33 insertions, 16 deletions
diff --git a/embassy-stm32/src/timer/input_capture.rs b/embassy-stm32/src/timer/input_capture.rs
index bf26cabc6..bc7614cda 100644
--- a/embassy-stm32/src/timer/input_capture.rs
+++ b/embassy-stm32/src/timer/input_capture.rs
@@ -137,4 +137,9 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
137 pub fn get_capture_value(&self, channel: Channel) -> u32 { 137 pub fn get_capture_value(&self, channel: Channel) -> u32 {
138 self.inner.get_capture_value(channel) 138 self.inner.get_capture_value(channel)
139 } 139 }
140
141 /// Get input interrupt.
142 pub fn get_input_interrupt(&self, channel: Channel) -> bool {
143 self.inner.get_input_interrupt(channel)
144 }
140} 145}
diff --git a/embassy-stm32/src/timer/low_level.rs b/embassy-stm32/src/timer/low_level.rs
index aa73986ea..7f533b75c 100644
--- a/embassy-stm32/src/timer/low_level.rs
+++ b/embassy-stm32/src/timer/low_level.rs
@@ -448,6 +448,11 @@ impl<'d, T: GeneralInstance4Channel> Timer<'d, T> {
448 self.regs_gp16().sr().modify(|r| r.set_ccif(channel.index(), false)); 448 self.regs_gp16().sr().modify(|r| r.set_ccif(channel.index(), false));
449 } 449 }
450 450
451 /// Get input interrupt.
452 pub fn get_input_interrupt(&self, channel: Channel) -> bool {
453 self.regs_gp16().sr().read().ccif(channel.index())
454 }
455
451 /// Enable input interrupt. 456 /// Enable input interrupt.
452 pub fn enable_input_interrupt(&self, channel: Channel, enable: bool) { 457 pub fn enable_input_interrupt(&self, channel: Channel, enable: bool) {
453 self.regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable)); 458 self.regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable));
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 @@
4use defmt::*; 4use defmt::*;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_stm32::gpio::{Level, Output, Pull, Speed}; 6use embassy_stm32::gpio::{Level, Output, Pull, Speed};
7use embassy_stm32::peripherals::PB2;
7use embassy_stm32::time::khz; 8use embassy_stm32::time::khz;
8use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; 9use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
9use embassy_stm32::timer::Channel; 10use 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]
16async fn main(_spawner: Spawner) { 17async 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]
32async 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}