add get_input_interrupt
This commit is contained in:
@ -137,4 +137,9 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
|
|||||||
pub fn get_capture_value(&self, channel: Channel) -> u32 {
|
pub fn get_capture_value(&self, channel: Channel) -> u32 {
|
||||||
self.inner.get_capture_value(channel)
|
self.inner.get_capture_value(channel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get input interrupt.
|
||||||
|
pub fn get_input_interrupt(&self, channel: Channel) -> bool {
|
||||||
|
self.inner.get_input_interrupt(channel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -448,6 +448,11 @@ impl<'d, T: GeneralInstance4Channel> Timer<'d, T> {
|
|||||||
self.regs_gp16().sr().modify(|r| r.set_ccif(channel.index(), false));
|
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.
|
/// Enable input interrupt.
|
||||||
pub fn enable_input_interrupt(&self, channel: Channel, enable: bool) {
|
pub fn enable_input_interrupt(&self, channel: Channel, enable: bool) {
|
||||||
self.regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable));
|
self.regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable));
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
|
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
|
||||||
|
use embassy_stm32::peripherals::PB2;
|
||||||
use embassy_stm32::time::khz;
|
use embassy_stm32::time::khz;
|
||||||
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
|
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
|
||||||
use embassy_stm32::timer::Channel;
|
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
|
/// Connect PB2 and PB10 with a 1k Ohm resistor
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::task]
|
||||||
async fn main(_spawner: Spawner) {
|
async fn blinky(led: PB2) {
|
||||||
let p = embassy_stm32::init(Default::default());
|
let mut led = Output::new(led, Level::High, Speed::Low);
|
||||||
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;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
info!("high");
|
info!("high");
|
||||||
@ -33,12 +25,27 @@ async fn main(_spawner: Spawner) {
|
|||||||
info!("low");
|
info!("low");
|
||||||
led.set_low();
|
led.set_low();
|
||||||
Timer::after_millis(300).await;
|
Timer::after_millis(300).await;
|
||||||
|
}
|
||||||
// Check for input capture
|
}
|
||||||
let cap = ic.get_capture_value(Channel::Ch3);
|
|
||||||
if cap != last {
|
#[embassy_executor::main]
|
||||||
info!("New capture!");
|
async fn main(spawner: Spawner) {
|
||||||
last = cap;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user