mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
Merge pull request #3014 from brunob45/pwm_input
Add PWM Input for STM32
This commit is contained in:
commit
ba940017ee
@ -7,7 +7,7 @@ use core::task::{Context, Poll};
|
||||
|
||||
use embassy_hal_internal::{into_ref, PeripheralRef};
|
||||
|
||||
use super::low_level::{CountingMode, InputCaptureMode, InputTISelection, Timer};
|
||||
use super::low_level::{CountingMode, FilterValue, InputCaptureMode, InputTISelection, Timer};
|
||||
use super::{
|
||||
CaptureCompareInterruptHandler, Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin,
|
||||
GeneralInstance4Channel,
|
||||
@ -40,11 +40,9 @@ macro_rules! channel_impl {
|
||||
#[doc = concat!("Create a new ", stringify!($channel), " capture pin instance.")]
|
||||
pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<T>> + 'd, pull_type: Pull) -> Self {
|
||||
into_ref!(pin);
|
||||
critical_section::with(|_| {
|
||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||
#[cfg(gpio_v2)]
|
||||
pin.set_speed(crate::gpio::Speed::VeryHigh);
|
||||
});
|
||||
|
||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||
|
||||
CapturePin {
|
||||
_pin: pin.map_into(),
|
||||
phantom: PhantomData,
|
||||
@ -83,7 +81,7 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
|
||||
let mut this = Self { inner: Timer::new(tim) };
|
||||
|
||||
this.inner.set_counting_mode(counting_mode);
|
||||
this.set_tick_freq(freq);
|
||||
this.inner.set_tick_freq(freq);
|
||||
this.inner.enable_outputs(); // Required for advanced timers, see GeneralInstance4Channel for details
|
||||
this.inner.start();
|
||||
|
||||
@ -109,24 +107,6 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
|
||||
self.inner.get_channel_enable_state(channel)
|
||||
}
|
||||
|
||||
/// Set tick frequency.
|
||||
///
|
||||
/// Note: when you call this, the max period value changes
|
||||
pub fn set_tick_freq(&mut self, freq: Hertz) {
|
||||
let f = freq;
|
||||
assert!(f.0 > 0);
|
||||
let timer_f = self.inner.get_clock_frequency();
|
||||
|
||||
let pclk_ticks_per_timer_period = timer_f / f;
|
||||
let psc: u16 = unwrap!((pclk_ticks_per_timer_period - 1).try_into());
|
||||
|
||||
let regs = self.inner.regs_core();
|
||||
regs.psc().write_value(psc);
|
||||
|
||||
// Generate an Update Request
|
||||
regs.egr().write(|r| r.set_ug(true));
|
||||
}
|
||||
|
||||
/// Set the input capture mode for a given channel.
|
||||
pub fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode) {
|
||||
self.inner.set_input_capture_mode(channel, mode);
|
||||
@ -148,10 +128,13 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
|
||||
}
|
||||
|
||||
fn new_future(&self, channel: Channel, mode: InputCaptureMode, tisel: InputTISelection) -> InputCaptureFuture<T> {
|
||||
self.inner.enable_channel(channel, true);
|
||||
self.inner.set_input_capture_mode(channel, mode);
|
||||
// Configuration steps from ST RM0390 (STM32F446) chapter 17.3.5
|
||||
// or ST RM0008 (STM32F103) chapter 15.3.5 Input capture mode
|
||||
self.inner.set_input_ti_selection(channel, tisel);
|
||||
self.inner.clear_input_interrupt(channel);
|
||||
self.inner.set_input_capture_filter(channel, FilterValue::NOFILTER);
|
||||
self.inner.set_input_capture_mode(channel, mode);
|
||||
self.inner.set_input_capture_prescaler(channel, 0);
|
||||
self.inner.enable_channel(channel, true);
|
||||
self.inner.enable_input_interrupt(channel, true);
|
||||
|
||||
InputCaptureFuture {
|
||||
|
@ -7,6 +7,8 @@
|
||||
//! The available functionality depends on the timer type.
|
||||
|
||||
use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
|
||||
// Re-export useful enums
|
||||
pub use stm32_metapac::timer::vals::{FilterValue, Sms as SlaveMode, Ts as TriggerSource};
|
||||
|
||||
use super::*;
|
||||
use crate::pac::timer::vals;
|
||||
@ -274,6 +276,22 @@ impl<'d, T: CoreInstance> Timer<'d, T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set tick frequency.
|
||||
pub fn set_tick_freq(&mut self, freq: Hertz) {
|
||||
let f = freq;
|
||||
assert!(f.0 > 0);
|
||||
let timer_f = self.get_clock_frequency();
|
||||
|
||||
let pclk_ticks_per_timer_period = timer_f / f;
|
||||
let psc: u16 = unwrap!((pclk_ticks_per_timer_period - 1).try_into());
|
||||
|
||||
let regs = self.regs_core();
|
||||
regs.psc().write_value(psc);
|
||||
|
||||
// Generate an Update Request
|
||||
regs.egr().write(|r| r.set_ug(true));
|
||||
}
|
||||
|
||||
/// Clear update interrupt.
|
||||
///
|
||||
/// Returns whether the update interrupt flag was set.
|
||||
@ -573,6 +591,16 @@ impl<'d, T: GeneralInstance4Channel> Timer<'d, T> {
|
||||
pub fn set_cc_dma_enable_state(&self, channel: Channel, ccde: bool) {
|
||||
self.regs_gp16().dier().modify(|w| w.set_ccde(channel.index(), ccde))
|
||||
}
|
||||
|
||||
/// Set Timer Slave Mode
|
||||
pub fn set_slave_mode(&self, sms: SlaveMode) {
|
||||
self.regs_gp16().smcr().modify(|r| r.set_sms(sms));
|
||||
}
|
||||
|
||||
/// Set Timer Trigger Source
|
||||
pub fn set_trigger_source(&self, ts: TriggerSource) {
|
||||
self.regs_gp16().smcr().modify(|r| r.set_ts(ts));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(stm32l0))]
|
||||
|
@ -8,6 +8,7 @@ use embassy_sync::waitqueue::AtomicWaker;
|
||||
pub mod complementary_pwm;
|
||||
pub mod input_capture;
|
||||
pub mod low_level;
|
||||
pub mod pwm_input;
|
||||
pub mod qei;
|
||||
pub mod simple_pwm;
|
||||
|
||||
|
114
embassy-stm32/src/timer/pwm_input.rs
Normal file
114
embassy-stm32/src/timer/pwm_input.rs
Normal file
@ -0,0 +1,114 @@
|
||||
//! PWM Input driver.
|
||||
|
||||
use embassy_hal_internal::into_ref;
|
||||
|
||||
use super::low_level::{CountingMode, InputCaptureMode, InputTISelection, SlaveMode, Timer, TriggerSource};
|
||||
use super::{Channel, Channel1Pin, Channel2Pin, GeneralInstance4Channel};
|
||||
use crate::gpio::{AFType, Pull};
|
||||
use crate::time::Hertz;
|
||||
use crate::Peripheral;
|
||||
|
||||
/// PWM Input driver.
|
||||
pub struct PwmInput<'d, T: GeneralInstance4Channel> {
|
||||
channel: Channel,
|
||||
inner: Timer<'d, T>,
|
||||
}
|
||||
|
||||
impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
|
||||
/// Create a new PWM input driver.
|
||||
pub fn new(
|
||||
tim: impl Peripheral<P = T> + 'd,
|
||||
pin: impl Peripheral<P = impl Channel1Pin<T>> + 'd,
|
||||
pull_type: Pull,
|
||||
freq: Hertz,
|
||||
) -> Self {
|
||||
into_ref!(pin);
|
||||
|
||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||
|
||||
Self::new_inner(tim, freq, Channel::Ch1, Channel::Ch2)
|
||||
}
|
||||
|
||||
/// Create a new PWM input driver.
|
||||
pub fn new_alt(
|
||||
tim: impl Peripheral<P = T> + 'd,
|
||||
pin: impl Peripheral<P = impl Channel2Pin<T>> + 'd,
|
||||
pull_type: Pull,
|
||||
freq: Hertz,
|
||||
) -> Self {
|
||||
into_ref!(pin);
|
||||
|
||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||
|
||||
Self::new_inner(tim, freq, Channel::Ch2, Channel::Ch1)
|
||||
}
|
||||
|
||||
fn new_inner(tim: impl Peripheral<P = T> + 'd, freq: Hertz, ch1: Channel, ch2: Channel) -> Self {
|
||||
let mut inner = Timer::new(tim);
|
||||
|
||||
inner.set_counting_mode(CountingMode::EdgeAlignedUp);
|
||||
inner.set_tick_freq(freq);
|
||||
inner.enable_outputs(); // Required for advanced timers, see GeneralInstance4Channel for details
|
||||
inner.start();
|
||||
|
||||
// Configuration steps from ST RM0390 (STM32F446) chapter 17.3.6
|
||||
// or ST RM0008 (STM32F103) chapter 15.3.6 Input capture mode
|
||||
inner.set_input_ti_selection(ch1, InputTISelection::Normal);
|
||||
inner.set_input_capture_mode(ch1, InputCaptureMode::Rising);
|
||||
|
||||
inner.set_input_ti_selection(ch2, InputTISelection::Alternate);
|
||||
inner.set_input_capture_mode(ch2, InputCaptureMode::Falling);
|
||||
|
||||
inner.set_trigger_source(match ch1 {
|
||||
Channel::Ch1 => TriggerSource::TI1FP1,
|
||||
Channel::Ch2 => TriggerSource::TI2FP2,
|
||||
_ => panic!("Invalid channel for PWM input"),
|
||||
});
|
||||
|
||||
inner.set_slave_mode(SlaveMode::RESET_MODE);
|
||||
|
||||
// Must call the `enable` function after
|
||||
|
||||
Self { channel: ch1, inner }
|
||||
}
|
||||
|
||||
/// Enable the given channel.
|
||||
pub fn enable(&mut self) {
|
||||
self.inner.enable_channel(Channel::Ch1, true);
|
||||
self.inner.enable_channel(Channel::Ch2, true);
|
||||
}
|
||||
|
||||
/// Disable the given channel.
|
||||
pub fn disable(&mut self) {
|
||||
self.inner.enable_channel(Channel::Ch1, false);
|
||||
self.inner.enable_channel(Channel::Ch2, false);
|
||||
}
|
||||
|
||||
/// Check whether given channel is enabled
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
self.inner.get_channel_enable_state(Channel::Ch1)
|
||||
}
|
||||
|
||||
/// Get the period tick count
|
||||
pub fn get_period_ticks(&self) -> u32 {
|
||||
self.inner.get_capture_value(self.channel)
|
||||
}
|
||||
|
||||
/// Get the pulse width tick count
|
||||
pub fn get_width_ticks(&self) -> u32 {
|
||||
self.inner.get_capture_value(match self.channel {
|
||||
Channel::Ch1 => Channel::Ch2,
|
||||
Channel::Ch2 => Channel::Ch1,
|
||||
_ => panic!("Invalid channel for PWM input"),
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the duty cycle in 100%
|
||||
pub fn get_duty_cycle(&self) -> f32 {
|
||||
let period = self.get_period_ticks();
|
||||
if period == 0 {
|
||||
return 0.;
|
||||
}
|
||||
100. * (self.get_width_ticks() as f32) / (period as f32)
|
||||
}
|
||||
}
|
52
examples/stm32f1/src/bin/input_capture.rs
Normal file
52
examples/stm32f1/src/bin/input_capture.rs
Normal file
@ -0,0 +1,52 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
|
||||
use embassy_stm32::time::khz;
|
||||
use embassy_stm32::timer::input_capture::{CapturePin, InputCapture};
|
||||
use embassy_stm32::timer::{self, Channel};
|
||||
use embassy_stm32::{bind_interrupts, peripherals};
|
||||
use embassy_time::Timer;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
/// Connect PA2 and PC13 with a 1k Ohm resistor
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn blinky(led: peripherals::PC13) {
|
||||
let mut led = Output::new(led, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
led.set_high();
|
||||
Timer::after_millis(300).await;
|
||||
|
||||
info!("low");
|
||||
led.set_low();
|
||||
Timer::after_millis(300).await;
|
||||
}
|
||||
}
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
info!("Hello World!");
|
||||
|
||||
unwrap!(spawner.spawn(blinky(p.PC13)));
|
||||
|
||||
let ch3 = CapturePin::new_ch3(p.PA2, Pull::None);
|
||||
let mut ic = InputCapture::new(p.TIM2, None, None, Some(ch3), None, Irqs, khz(1000), Default::default());
|
||||
|
||||
loop {
|
||||
info!("wait for rising edge");
|
||||
ic.wait_for_rising_edge(Channel::Ch3).await;
|
||||
|
||||
let capture_value = ic.get_capture_value(Channel::Ch3);
|
||||
info!("new capture! {}", capture_value);
|
||||
}
|
||||
}
|
54
examples/stm32f1/src/bin/pwm_input.rs
Normal file
54
examples/stm32f1/src/bin/pwm_input.rs
Normal file
@ -0,0 +1,54 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
|
||||
use embassy_stm32::time::khz;
|
||||
use embassy_stm32::timer::pwm_input::PwmInput;
|
||||
use embassy_stm32::{bind_interrupts, peripherals, timer};
|
||||
use embassy_time::Timer;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
/// Connect PA0 and PC13 with a 1k Ohm resistor
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn blinky(led: peripherals::PC13) {
|
||||
let mut led = Output::new(led, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
led.set_high();
|
||||
Timer::after_millis(300).await;
|
||||
|
||||
info!("low");
|
||||
led.set_low();
|
||||
Timer::after_millis(300).await;
|
||||
}
|
||||
}
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
info!("Hello World!");
|
||||
|
||||
unwrap!(spawner.spawn(blinky(p.PC13)));
|
||||
|
||||
let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, Pull::None, khz(10));
|
||||
pwm_input.enable();
|
||||
|
||||
loop {
|
||||
Timer::after_millis(500).await;
|
||||
let period = pwm_input.get_period_ticks();
|
||||
let width = pwm_input.get_width_ticks();
|
||||
let duty_cycle = pwm_input.get_duty_cycle();
|
||||
info!(
|
||||
"period ticks: {} width ticks: {} duty cycle: {}",
|
||||
period, width, duty_cycle
|
||||
);
|
||||
}
|
||||
}
|
54
examples/stm32f4/src/bin/pwm_input.rs
Normal file
54
examples/stm32f4/src/bin/pwm_input.rs
Normal file
@ -0,0 +1,54 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
|
||||
use embassy_stm32::time::khz;
|
||||
use embassy_stm32::timer::pwm_input::PwmInput;
|
||||
use embassy_stm32::{bind_interrupts, peripherals, timer};
|
||||
use embassy_time::Timer;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
/// Connect PB2 and PA6 with a 1k Ohm resistor
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn blinky(led: peripherals::PB2) {
|
||||
let mut led = Output::new(led, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
led.set_high();
|
||||
Timer::after_millis(300).await;
|
||||
|
||||
info!("low");
|
||||
led.set_low();
|
||||
Timer::after_millis(300).await;
|
||||
}
|
||||
}
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>;
|
||||
});
|
||||
|
||||
#[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 mut pwm_input = PwmInput::new(p.TIM3, p.PA6, Pull::None, khz(10));
|
||||
pwm_input.enable();
|
||||
|
||||
loop {
|
||||
Timer::after_millis(500).await;
|
||||
let period = pwm_input.get_period_ticks();
|
||||
let width = pwm_input.get_width_ticks();
|
||||
let duty_cycle = pwm_input.get_duty_cycle();
|
||||
info!(
|
||||
"period ticks: {} width ticks: {} duty cycle: {}",
|
||||
period, width, duty_cycle
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user