create functions in inner to handle register modification

This commit is contained in:
Bruno Bousquet 2024-05-30 17:43:38 -04:00
parent a87b333034
commit 84707af5d7
3 changed files with 31 additions and 46 deletions

View File

@ -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,
@ -130,8 +128,6 @@ impl<'d, T: GeneralInstance4Channel> InputCapture<'d, 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
// or ST RM0008 (STM32F103) chapter 15.3.5 Input capture mode
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"]
struct InputCaptureFuture<T: GeneralInstance4Channel> {
channel: Channel,
@ -198,7 +189,7 @@ struct InputCaptureFuture<T: GeneralInstance4Channel> {
impl<T: GeneralInstance4Channel> Drop for InputCaptureFuture<T> {
fn drop(&mut self) {
critical_section::with(|_| {
let regs = regs_gp16(T::regs());
let regs = unsafe { crate::pac::timer::TimGp16::from_ptr(T::regs()) };
// disable interrupt enable
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> {
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();
if !dier.ccie(self.channel.index()) {

View File

@ -12,6 +12,10 @@ use super::*;
use crate::pac::timer::vals;
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.
#[derive(Clone, Copy)]
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) {
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))]

View File

@ -2,7 +2,7 @@
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 crate::gpio::{AFType, Pull};
use crate::time::Hertz;
@ -14,11 +14,6 @@ pub struct PwmInput<'d, T: GeneralInstance4Channel> {
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> {
/// Create a new PWM input driver.
pub fn new(
@ -28,11 +23,8 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
freq: Hertz,
) -> 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);
Self::new_inner(tim, freq, Channel::Ch1, Channel::Ch2)
}
@ -45,18 +37,13 @@ impl<'d, T: GeneralInstance4Channel> PwmInput<'d, T> {
freq: Hertz,
) -> 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);
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 {
use stm32_metapac::timer::vals::{Sms, Ts};
let mut inner = Timer::new(tim);
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_capture_mode(ch2, InputCaptureMode::Falling);
let regs = regs_gp16(T::regs());
regs.smcr().modify(|r| {
// Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register
// (TI1FP1 selected).
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_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 }