From 1fe05cfedbcfb35dba3bee3ed6b7f4f293e9bb78 Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Mon, 29 Sep 2025 21:11:09 +0900 Subject: Make the Qei struct own the channel 1 and 2 pins --- embassy-stm32/src/timer/qei.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'embassy-stm32/src/timer') diff --git a/embassy-stm32/src/timer/qei.rs b/embassy-stm32/src/timer/qei.rs index 82b5968b0..d63a2b45d 100644 --- a/embassy-stm32/src/timer/qei.rs +++ b/embassy-stm32/src/timer/qei.rs @@ -55,20 +55,27 @@ impl SealedQeiChannel for Ch2 {} /// Quadrature decoder driver. pub struct Qei<'d, T: GeneralInstance4Channel> { inner: Timer<'d, T>, + _ch1: Peri<'d, AnyPin>, + _ch2: Peri<'d, AnyPin>, } impl<'d, T: GeneralInstance4Channel> Qei<'d, T> { /// Create a new quadrature decoder driver. #[allow(unused)] - pub fn new<#[cfg(afio)] A>( + pub fn new( tim: Peri<'d, T>, - ch1: if_afio!(QeiPin<'d, T, Ch1, A>), - ch2: if_afio!(QeiPin<'d, T, Ch2, A>), + ch1: Peri<'d, if_afio!(impl TimerPin)>, + ch2: Peri<'d, if_afio!(impl TimerPin)>, ) -> Self { - Self::new_inner(tim) - } + // Configure the pins to be used for the QEI peripheral. + critical_section::with(|_| { + ch1.set_low(); + set_as_af!(ch1, AfType::input(Pull::None)); + + ch2.set_low(); + set_as_af!(ch2, AfType::input(Pull::None)); + }); - fn new_inner(tim: Peri<'d, T>) -> Self { let inner = Timer::new(tim); let r = inner.regs_gp16(); @@ -94,7 +101,11 @@ impl<'d, T: GeneralInstance4Channel> Qei<'d, T> { r.arr().modify(|w| w.set_arr(u16::MAX)); r.cr1().modify(|w| w.set_cen(true)); - Self { inner } + Self { + inner, + _ch1: ch1.into(), + _ch2: ch2.into(), + } } /// Get direction. -- cgit From a791e9f1fc17eda2b05df97a72b94264ee6a4c12 Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Mon, 29 Sep 2025 21:17:15 +0900 Subject: Remove the QeiPin struct --- embassy-stm32/src/timer/qei.rs | 23 ----------------------- 1 file changed, 23 deletions(-) (limited to 'embassy-stm32/src/timer') diff --git a/embassy-stm32/src/timer/qei.rs b/embassy-stm32/src/timer/qei.rs index d63a2b45d..25e8c3705 100644 --- a/embassy-stm32/src/timer/qei.rs +++ b/embassy-stm32/src/timer/qei.rs @@ -1,7 +1,5 @@ //! Quadrature decoder using a timer. -use core::marker::PhantomData; - use stm32_metapac::timer::vals; use super::low_level::Timer; @@ -19,27 +17,6 @@ pub enum Direction { Downcounting, } -/// Wrapper for using a pin with QEI. -pub struct QeiPin<'d, T, Channel, #[cfg(afio)] A> { - #[allow(unused)] - pin: Peri<'d, AnyPin>, - phantom: PhantomData, -} - -impl<'d, T: GeneralInstance4Channel, C: QeiChannel, #[cfg(afio)] A> if_afio!(QeiPin<'d, T, C, A>) { - /// Create a new QEI pin instance. - pub fn new(pin: Peri<'d, if_afio!(impl TimerPin)>) -> Self { - critical_section::with(|_| { - pin.set_low(); - set_as_af!(pin, AfType::input(Pull::None)); - }); - QeiPin { - pin: pin.into(), - phantom: PhantomData, - } - } -} - trait SealedQeiChannel: TimerChannel {} /// Marker trait for a timer channel eligible for use with QEI. -- cgit From 165fa1debd7f8aa6131c467825477a25e1862539 Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Mon, 29 Sep 2025 22:05:27 +0900 Subject: Add a Config struct for the Qei peripheral --- embassy-stm32/src/timer/qei.rs | 65 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) (limited to 'embassy-stm32/src/timer') diff --git a/embassy-stm32/src/timer/qei.rs b/embassy-stm32/src/timer/qei.rs index 25e8c3705..2e438bc74 100644 --- a/embassy-stm32/src/timer/qei.rs +++ b/embassy-stm32/src/timer/qei.rs @@ -1,6 +1,6 @@ //! Quadrature decoder using a timer. -use stm32_metapac::timer::vals; +use stm32_metapac::timer::vals::{self, Sms}; use super::low_level::Timer; pub use super::{Ch1, Ch2}; @@ -9,6 +9,51 @@ use crate::gpio::{AfType, AnyPin, Pull}; use crate::timer::TimerChannel; use crate::Peri; +/// Qei driver config. +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[derive(Clone, Copy)] +pub struct Config { + /// Configures the internal pull up/down resistor for Qei's channel 1 pin. + pub ch1_pull: Pull, + /// Configures the internal pull up/down resistor for Qei's channel 2 pin. + pub ch2_pull: Pull, + /// Specifies the encoder mode to use for the Qei peripheral. + pub mode: QeiMode, +} + +impl Default for Config { + /// Arbitrary defaults to preserve backwards compatibility + fn default() -> Self { + Self { + ch1_pull: Pull::None, + ch2_pull: Pull::None, + mode: QeiMode::Mode3, + } + } +} + +/// See STMicro AN4013 for ยง2.3 for more information +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[derive(Clone, Copy)] +pub enum QeiMode { + /// Direct alias for [`Sms::ENCODER_MODE_1`] + Mode1, + /// Direct alias for [`Sms::ENCODER_MODE_2`] + Mode2, + /// Direct alias for [`Sms::ENCODER_MODE_3`] + Mode3, +} + +impl From for Sms { + fn from(mode: QeiMode) -> Self { + match mode { + QeiMode::Mode1 => Sms::ENCODER_MODE_1, + QeiMode::Mode2 => Sms::ENCODER_MODE_2, + QeiMode::Mode3 => Sms::ENCODER_MODE_3, + } + } +} + /// Counting direction pub enum Direction { /// Counting up. @@ -37,20 +82,30 @@ pub struct Qei<'d, T: GeneralInstance4Channel> { } impl<'d, T: GeneralInstance4Channel> Qei<'d, T> { - /// Create a new quadrature decoder driver. + /// Create a new quadrature decoder driver, with a given [`Config`]. #[allow(unused)] pub fn new( tim: Peri<'d, T>, ch1: Peri<'d, if_afio!(impl TimerPin)>, ch2: Peri<'d, if_afio!(impl TimerPin)>, + ) -> Self { + Self::new_with_config(tim, ch1, ch2, Default::default()) + } + /// Create a new quadrature decoder driver, with a given [`Config`]. + #[allow(unused)] + pub fn new_with_config( + tim: Peri<'d, T>, + ch1: Peri<'d, if_afio!(impl TimerPin)>, + ch2: Peri<'d, if_afio!(impl TimerPin)>, + config: Config, ) -> Self { // Configure the pins to be used for the QEI peripheral. critical_section::with(|_| { ch1.set_low(); - set_as_af!(ch1, AfType::input(Pull::None)); + set_as_af!(ch1, AfType::input(config.ch1_pull)); ch2.set_low(); - set_as_af!(ch2, AfType::input(Pull::None)); + set_as_af!(ch2, AfType::input(config.ch2_pull)); }); let inner = Timer::new(tim); @@ -72,7 +127,7 @@ impl<'d, T: GeneralInstance4Channel> Qei<'d, T> { }); r.smcr().modify(|w| { - w.set_sms(vals::Sms::ENCODER_MODE_3); + w.set_sms(config.mode.into()); }); r.arr().modify(|w| w.set_arr(u16::MAX)); -- cgit From 61bc254879b328a944a36a81080abbadc6431ccf Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Mon, 29 Sep 2025 22:24:19 +0900 Subject: Remove 'new_with_config()', just use 'new()' --- embassy-stm32/src/timer/qei.rs | 9 --------- 1 file changed, 9 deletions(-) (limited to 'embassy-stm32/src/timer') diff --git a/embassy-stm32/src/timer/qei.rs b/embassy-stm32/src/timer/qei.rs index 2e438bc74..bb152731c 100644 --- a/embassy-stm32/src/timer/qei.rs +++ b/embassy-stm32/src/timer/qei.rs @@ -88,15 +88,6 @@ impl<'d, T: GeneralInstance4Channel> Qei<'d, T> { tim: Peri<'d, T>, ch1: Peri<'d, if_afio!(impl TimerPin)>, ch2: Peri<'d, if_afio!(impl TimerPin)>, - ) -> Self { - Self::new_with_config(tim, ch1, ch2, Default::default()) - } - /// Create a new quadrature decoder driver, with a given [`Config`]. - #[allow(unused)] - pub fn new_with_config( - tim: Peri<'d, T>, - ch1: Peri<'d, if_afio!(impl TimerPin)>, - ch2: Peri<'d, if_afio!(impl TimerPin)>, config: Config, ) -> Self { // Configure the pins to be used for the QEI peripheral. -- cgit