| 1 | //! Quadrature decoder using a timer. |
| 2 | |
| 3 | use core::marker::PhantomData; |
| 4 | |
| 5 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
| 6 | use stm32_metapac::timer::vals; |
| 7 | |
| 8 | use super::low_level::Timer; |
| 9 | use super::{Channel1Pin, Channel2Pin, GeneralInstance4Channel}; |
| 10 | use crate::gpio::{AfType, AnyPin, Pull}; |
| 11 | use crate::Peripheral; |
| 12 | |
| 13 | /// Counting direction |
| 14 | pub enum Direction { |
| 15 | /// Counting up. |
| 16 | Upcounting, |
| 17 | /// Counting down. |
| 18 | Downcounting, |
| 19 | } |
| 20 | |
| 21 | /// Channel 1 marker type. |
| 22 | pub enum Ch1 {} |
| 23 | /// Channel 2 marker type. |
| 24 | pub enum Ch2 {} |
| 25 | |
| 26 | /// Wrapper for using a pin with QEI. |
| 27 | pub struct QeiPin<'d, T, Channel> { |
| 28 | _pin: PeripheralRef<'d, AnyPin>, |
| 29 | phantom: PhantomData<(T, Channel)>, |
| 30 | } |
| 31 | |
| 32 | macro_rules! channel_impl { |
| 33 | ($new_chx:ident, $channel:ident, $pin_trait:ident) => { |
| 34 | impl<'d, T: GeneralInstance4Channel> QeiPin<'d, T, $channel> { |
| 35 | #[doc = concat!("Create a new " , stringify!($channel), " QEI pin instance." )] |
| 36 | pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<T>> + 'd) -> Self { |
| 37 | into_ref!(pin); |
| 38 | critical_section::with(|_| { |
| 39 | pin.set_low(); |
| 40 | pin.set_as_af(pin.af_num(), AfType::input(Pull::None)); |
| 41 | }); |
| 42 | QeiPin { |
| 43 | _pin: pin.map_into(), |
| 44 | phantom: PhantomData, |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | channel_impl!(new_ch1, Ch1, Channel1Pin); |
| 52 | channel_impl!(new_ch2, Ch2, Channel2Pin); |
| 53 | |
| 54 | /// Quadrature decoder driver. |
| 55 | pub struct Qei<'d, T: GeneralInstance4Channel> { |
| 56 | inner: Timer<'d, T>, |
| 57 | } |
| 58 | |
| 59 | impl<'d, T: GeneralInstance4Channel> Qei<'d, T> { |
| 60 | /// Create a new quadrature decoder driver. |
| 61 | pub fn new(tim: impl Peripheral<P = T> + 'd, _ch1: QeiPin<'d, T, Ch1>, _ch2: QeiPin<'d, T, Ch2>) -> Self { |
| 62 | Self::new_inner(tim) |
| 63 | } |
| 64 | |
| 65 | fn new_inner(tim: impl Peripheral<P = T> + 'd) -> Self { |
| 66 | let inner = Timer::new(tim); |
| 67 | let r = inner.regs_gp16(); |
| 68 | |
| 69 | // Configure TxC1 and TxC2 as captures |
| 70 | r.ccmr_input(0).modify(|w| { |
| 71 | w.set_ccs(0, vals::CcmrInputCcs::TI4); |
| 72 | w.set_ccs(1, vals::CcmrInputCcs::TI4); |
| 73 | }); |
| 74 | |
| 75 | // enable and configure to capture on rising edge |
| 76 | r.ccer().modify(|w| { |
| 77 | w.set_cce(0, true); |
| 78 | w.set_cce(1, true); |
| 79 | |
| 80 | w.set_ccp(0, false); |
| 81 | w.set_ccp(1, false); |
| 82 | }); |
| 83 | |
| 84 | r.smcr().modify(|w| { |
| 85 | w.set_sms(vals::Sms::ENCODER_MODE_3); |
| 86 | }); |
| 87 | |
| 88 | r.arr().modify(|w| w.set_arr(u16::MAX)); |
| 89 | r.cr1().modify(|w| w.set_cen(true)); |
| 90 | |
| 91 | Self { inner } |
| 92 | } |
| 93 | |
| 94 | /// Get direction. |
| 95 | pub fn read_direction(&self) -> Direction { |
| 96 | match self.inner.regs_gp16().cr1().read().dir() { |
| 97 | vals::Dir::DOWN => Direction::Downcounting, |
| 98 | vals::Dir::UP => Direction::Upcounting, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// Get count. |
| 103 | pub fn count(&self) -> u16 { |
| 104 | self.inner.regs_gp16().cnt().read().cnt() |
| 105 | } |
| 106 | } |
| 107 | |