From 84707af5d7f3e62caafed06c416cf12fa82e4664 Mon Sep 17 00:00:00 2001
From: Bruno Bousquet <21108660+brunob45@users.noreply.github.com>
Date: Thu, 30 May 2024 17:43:38 -0400
Subject: [PATCH] create functions in inner to handle register modification
---
embassy-stm32/src/timer/input_capture.rs | 21 ++++--------
embassy-stm32/src/timer/low_level.rs | 14 ++++++++
embassy-stm32/src/timer/pwm_input.rs | 42 +++++++-----------------
3 files changed, 31 insertions(+), 46 deletions(-)
diff --git a/embassy-stm32/src/timer/input_capture.rs b/embassy-stm32/src/timer/input_capture.rs
index b3434ae63..8d1a77867 100644
--- a/embassy-stm32/src/timer/input_capture.rs
+++ b/embassy-stm32/src/timer/input_capture.rs
@@ -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
> + '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 {
- 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 {
channel: Channel,
@@ -198,7 +189,7 @@ struct InputCaptureFuture {
impl Drop for InputCaptureFuture {
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 Future for InputCaptureFuture {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll {
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()) {
diff --git a/embassy-stm32/src/timer/low_level.rs b/embassy-stm32/src/timer/low_level.rs
index 141e96894..8482fad7b 100644
--- a/embassy-stm32/src/timer/low_level.rs
+++ b/embassy-stm32/src/timer/low_level.rs
@@ -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))]
diff --git a/embassy-stm32/src/timer/pwm_input.rs b/embassy-stm32/src/timer/pwm_input.rs
index 7bcb7802a..dcf098a78 100644
--- a/embassy-stm32/src/timer/pwm_input.rs
+++ b/embassy-stm32/src/timer/pwm_input.rs
@@ -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 + '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 }