add get_input_interrupt

This commit is contained in:
Bruno Bousquet 2024-05-05 23:00:48 -04:00
parent 431a60ca63
commit 29d6fa0a4a
3 changed files with 36 additions and 19 deletions

View File

@ -137,4 +137,9 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
pub fn get_capture_value(&self, channel: Channel) -> u32 {
self.inner.get_capture_value(channel)
}
/// Get input interrupt.
pub fn get_input_interrupt(&self, channel: Channel) -> bool {
self.inner.get_input_interrupt(channel)
}
}

View File

@ -448,6 +448,11 @@ impl<'d, T: GeneralInstance4Channel> Timer<'d, T> {
self.regs_gp16().sr().modify(|r| r.set_ccif(channel.index(), false));
}
/// Get input interrupt.
pub fn get_input_interrupt(&self, channel: Channel) -> bool {
self.regs_gp16().sr().read().ccif(channel.index())
}
/// Enable input interrupt.
pub fn enable_input_interrupt(&self, channel: Channel, enable: bool) {
self.regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable));

View File

@ -4,6 +4,7 @@
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use embassy_stm32::peripherals::PB2;
use embassy_stm32::time::khz;
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
use embassy_stm32::timer::Channel;
@ -12,18 +13,9 @@ use {defmt_rtt as _, panic_probe as _};
/// Connect PB2 and PB10 with a 1k Ohm resistor
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
let mut led = Output::new(p.PB2, Level::High, Speed::Low);
let ch3 = CapturePin::new_ch3(p.PB10, Pull::None);
let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, khz(1000), Default::default());
ic.enable(Channel::Ch3);
let mut last = 0;
#[embassy_executor::task]
async fn blinky(led: PB2) {
let mut led = Output::new(led, Level::High, Speed::Low);
loop {
info!("high");
@ -33,12 +25,27 @@ async fn main(_spawner: Spawner) {
info!("low");
led.set_low();
Timer::after_millis(300).await;
// Check for input capture
let cap = ic.get_capture_value(Channel::Ch3);
if cap != last {
info!("New capture!");
last = cap;
}
}
}
#[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, khz(1000), Default::default());
ic.enable(Channel::Ch3);
loop {
// Check for input capture
if ic.get_input_interrupt(Channel::Ch3) {
let capture_value = ic.get_capture_value(Channel::Ch3);
info!("New capture! {}", capture_value);
}
// Wait a little bit
Timer::after_millis(1).await;
}
}