mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-25 08:12:30 +00:00
create functions in inner to handle register modification
This commit is contained in:
parent
a87b333034
commit
84707af5d7
@ -7,7 +7,7 @@ use core::task::{Context, Poll};
|
|||||||
|
|
||||||
use embassy_hal_internal::{into_ref, PeripheralRef};
|
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::{
|
use super::{
|
||||||
CaptureCompareInterruptHandler, Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin,
|
CaptureCompareInterruptHandler, Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin,
|
||||||
GeneralInstance4Channel,
|
GeneralInstance4Channel,
|
||||||
@ -40,11 +40,9 @@ macro_rules! channel_impl {
|
|||||||
#[doc = concat!("Create a new ", stringify!($channel), " capture pin instance.")]
|
#[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 {
|
pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<T>> + 'd, pull_type: Pull) -> Self {
|
||||||
into_ref!(pin);
|
into_ref!(pin);
|
||||||
critical_section::with(|_| {
|
|
||||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||||
#[cfg(gpio_v2)]
|
|
||||||
pin.set_speed(crate::gpio::Speed::VeryHigh);
|
|
||||||
});
|
|
||||||
CapturePin {
|
CapturePin {
|
||||||
_pin: pin.map_into(),
|
_pin: pin.map_into(),
|
||||||
phantom: PhantomData,
|
phantom: PhantomData,
|
||||||
@ -130,8 +128,6 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn new_future(&self, channel: Channel, mode: InputCaptureMode, tisel: InputTISelection) -> InputCaptureFuture<T> {
|
fn new_future(&self, channel: Channel, mode: InputCaptureMode, tisel: InputTISelection) -> InputCaptureFuture<T> {
|
||||||
use stm32_metapac::timer::vals::FilterValue;
|
|
||||||
|
|
||||||
// Configuration steps from ST RM0390 (STM32F446) chapter 17.3.5
|
// Configuration steps from ST RM0390 (STM32F446) chapter 17.3.5
|
||||||
// or ST RM0008 (STM32F103) chapter 15.3.5 Input capture mode
|
// or ST RM0008 (STM32F103) chapter 15.3.5 Input capture mode
|
||||||
self.inner.set_input_ti_selection(channel, tisel);
|
self.inner.set_input_ti_selection(channel, tisel);
|
||||||
@ -184,11 +180,6 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert pointer to TIM instance to TimGp16 object
|
|
||||||
fn regs_gp16(ptr: *mut ()) -> crate::pac::timer::TimGp16 {
|
|
||||||
unsafe { crate::pac::timer::TimGp16::from_ptr(ptr) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||||
struct InputCaptureFuture<T: GeneralInstance4Channel> {
|
struct InputCaptureFuture<T: GeneralInstance4Channel> {
|
||||||
channel: Channel,
|
channel: Channel,
|
||||||
@ -198,7 +189,7 @@ struct InputCaptureFuture<T: GeneralInstance4Channel> {
|
|||||||
impl<T: GeneralInstance4Channel> Drop for InputCaptureFuture<T> {
|
impl<T: GeneralInstance4Channel> Drop for InputCaptureFuture<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
critical_section::with(|_| {
|
critical_section::with(|_| {
|
||||||
let regs = regs_gp16(T::regs());
|
let regs = unsafe { crate::pac::timer::TimGp16::from_ptr(T::regs()) };
|
||||||
|
|
||||||
// disable interrupt enable
|
// disable interrupt enable
|
||||||
regs.dier().modify(|w| w.set_ccie(self.channel.index(), false));
|
regs.dier().modify(|w| w.set_ccie(self.channel.index(), false));
|
||||||
@ -212,7 +203,7 @@ impl<T: GeneralInstance4Channel> Future for InputCaptureFuture<T> {
|
|||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
T::state().cc_waker[self.channel.index()].register(cx.waker());
|
T::state().cc_waker[self.channel.index()].register(cx.waker());
|
||||||
|
|
||||||
let regs = regs_gp16(T::regs());
|
let regs = unsafe { crate::pac::timer::TimGp16::from_ptr(T::regs()) };
|
||||||
|
|
||||||
let dier = regs.dier().read();
|
let dier = regs.dier().read();
|
||||||
if !dier.ccie(self.channel.index()) {
|
if !dier.ccie(self.channel.index()) {
|
||||||
|
@ -12,6 +12,10 @@ use super::*;
|
|||||||
use crate::pac::timer::vals;
|
use crate::pac::timer::vals;
|
||||||
use crate::time::Hertz;
|
use crate::time::Hertz;
|
||||||
|
|
||||||
|
pub use stm32_metapac::timer::vals::FilterValue;
|
||||||
|
pub use stm32_metapac::timer::vals::Sms as SlaveMode;
|
||||||
|
pub use stm32_metapac::timer::vals::Ts as TriggerSource;
|
||||||
|
|
||||||
/// Input capture mode.
|
/// Input capture mode.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum InputCaptureMode {
|
pub enum InputCaptureMode {
|
||||||
@ -588,6 +592,16 @@ impl<'d, T: GeneralInstance4Channel> Timer<'d, T> {
|
|||||||
pub fn set_cc_dma_enable_state(&self, channel: Channel, ccde: bool) {
|
pub fn set_cc_dma_enable_state(&self, channel: Channel, ccde: bool) {
|
||||||
self.regs_gp16().dier().modify(|w| w.set_ccde(channel.index(), ccde))
|
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))]
|
#[cfg(not(stm32l0))]
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use embassy_hal_internal::into_ref;
|
use embassy_hal_internal::into_ref;
|
||||||
|
|
||||||
use super::low_level::{CountingMode, InputCaptureMode, InputTISelection, Timer};
|
use super::low_level::{CountingMode, InputCaptureMode, InputTISelection, SlaveMode, Timer, TriggerSource};
|
||||||
use super::{Channel, Channel1Pin, Channel2Pin, GeneralInstance4Channel};
|
use super::{Channel, Channel1Pin, Channel2Pin, GeneralInstance4Channel};
|
||||||
use crate::gpio::{AFType, Pull};
|
use crate::gpio::{AFType, Pull};
|
||||||
use crate::time::Hertz;
|
use crate::time::Hertz;
|
||||||
@ -14,11 +14,6 @@ pub struct PwmInput<'d, T: GeneralInstance4Channel> {
|
|||||||
inner: Timer<'d, T>,
|
inner: Timer<'d, T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert pointer to TIM instance to TimGp16 object
|
|
||||||
fn regs_gp16(ptr: *mut ()) -> crate::pac::timer::TimGp16 {
|
|
||||||
unsafe { crate::pac::timer::TimGp16::from_ptr(ptr) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
|
impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
|
||||||
/// Create a new PWM input driver.
|
/// Create a new PWM input driver.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
@ -28,11 +23,8 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
|
|||||||
freq: Hertz,
|
freq: Hertz,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
into_ref!(pin);
|
into_ref!(pin);
|
||||||
critical_section::with(|_| {
|
|
||||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||||
#[cfg(gpio_v2)]
|
|
||||||
pin.set_speed(crate::gpio::Speed::VeryHigh);
|
|
||||||
});
|
|
||||||
|
|
||||||
Self::new_inner(tim, freq, Channel::Ch1, Channel::Ch2)
|
Self::new_inner(tim, freq, Channel::Ch1, Channel::Ch2)
|
||||||
}
|
}
|
||||||
@ -45,18 +37,13 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
|
|||||||
freq: Hertz,
|
freq: Hertz,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
into_ref!(pin);
|
into_ref!(pin);
|
||||||
critical_section::with(|_| {
|
|
||||||
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
pin.set_as_af_pull(pin.af_num(), AFType::Input, pull_type);
|
||||||
#[cfg(gpio_v2)]
|
|
||||||
pin.set_speed(crate::gpio::Speed::VeryHigh);
|
|
||||||
});
|
|
||||||
|
|
||||||
Self::new_inner(tim, freq, Channel::Ch2, Channel::Ch1)
|
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 {
|
fn new_inner(tim: impl Peripheral<P = T> + 'd, freq: Hertz, ch1: Channel, ch2: Channel) -> Self {
|
||||||
use stm32_metapac::timer::vals::{Sms, Ts};
|
|
||||||
|
|
||||||
let mut inner = Timer::new(tim);
|
let mut inner = Timer::new(tim);
|
||||||
|
|
||||||
inner.set_counting_mode(CountingMode::EdgeAlignedUp);
|
inner.set_counting_mode(CountingMode::EdgeAlignedUp);
|
||||||
@ -72,21 +59,14 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
|
|||||||
inner.set_input_ti_selection(ch2, InputTISelection::Alternate);
|
inner.set_input_ti_selection(ch2, InputTISelection::Alternate);
|
||||||
inner.set_input_capture_mode(ch2, InputCaptureMode::Falling);
|
inner.set_input_capture_mode(ch2, InputCaptureMode::Falling);
|
||||||
|
|
||||||
let regs = regs_gp16(T::regs());
|
inner.set_trigger_source(match ch1 {
|
||||||
regs.smcr().modify(|r| {
|
Channel::Ch1 => TriggerSource::TI1FP1,
|
||||||
// Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register
|
Channel::Ch2 => TriggerSource::TI2FP2,
|
||||||
// (TI1FP1 selected).
|
_ => panic!("Invalid channel for PWM input"),
|
||||||
r.set_ts(match ch1 {
|
|
||||||
Channel::Ch1 => Ts::TI1FP1,
|
|
||||||
Channel::Ch2 => Ts::TI2FP2,
|
|
||||||
_ => panic!("Invalid channel for PWM input"),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Configure the slave mode controller in reset mode: write the SMS bits to 100 in the
|
|
||||||
// TIMx_SMCR register.
|
|
||||||
r.set_sms(Sms::RESET_MODE);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
inner.set_slave_mode(SlaveMode::RESET_MODE);
|
||||||
|
|
||||||
// Must call the `enable` function after
|
// Must call the `enable` function after
|
||||||
|
|
||||||
Self { channel: ch1, inner }
|
Self { channel: ch1, inner }
|
||||||
|
Loading…
Reference in New Issue
Block a user