py32_hal/timer/
qei.rs

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