aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/timer/qei.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32/src/timer/qei.rs')
-rw-r--r--embassy-stm32/src/timer/qei.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/embassy-stm32/src/timer/qei.rs b/embassy-stm32/src/timer/qei.rs
new file mode 100644
index 000000000..15f2c3a79
--- /dev/null
+++ b/embassy-stm32/src/timer/qei.rs
@@ -0,0 +1,96 @@
1use core::marker::PhantomData;
2
3use embassy_hal_internal::{into_ref, PeripheralRef};
4
5use super::*;
6use crate::gpio::sealed::AFType;
7use crate::gpio::AnyPin;
8use crate::Peripheral;
9
10pub enum Direction {
11 Upcounting,
12 Downcounting,
13}
14
15pub struct Ch1;
16pub struct Ch2;
17
18pub struct QeiPin<'d, Perip, Channel> {
19 _pin: PeripheralRef<'d, AnyPin>,
20 phantom: PhantomData<(Perip, Channel)>,
21}
22
23macro_rules! channel_impl {
24 ($new_chx:ident, $channel:ident, $pin_trait:ident) => {
25 impl<'d, Perip: CaptureCompare16bitInstance> QeiPin<'d, Perip, $channel> {
26 pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<Perip>> + 'd) -> Self {
27 into_ref!(pin);
28 critical_section::with(|_| {
29 pin.set_low();
30 pin.set_as_af(pin.af_num(), AFType::Input);
31 #[cfg(gpio_v2)]
32 pin.set_speed(crate::gpio::Speed::VeryHigh);
33 });
34 QeiPin {
35 _pin: pin.map_into(),
36 phantom: PhantomData,
37 }
38 }
39 }
40 };
41}
42
43channel_impl!(new_ch1, Ch1, Channel1Pin);
44channel_impl!(new_ch2, Ch2, Channel2Pin);
45
46pub struct Qei<'d, T> {
47 _inner: PeripheralRef<'d, T>,
48}
49
50impl<'d, T: CaptureCompare16bitInstance> Qei<'d, T> {
51 pub fn new(tim: impl Peripheral<P = T> + 'd, _ch1: QeiPin<'d, T, Ch1>, _ch2: QeiPin<'d, T, Ch2>) -> Self {
52 Self::new_inner(tim)
53 }
54
55 fn new_inner(tim: impl Peripheral<P = T> + 'd) -> Self {
56 into_ref!(tim);
57
58 T::enable();
59 <T as crate::rcc::sealed::RccPeripheral>::reset();
60
61 // Configure TxC1 and TxC2 as captures
62 T::regs_gp16().ccmr_input(0).modify(|w| {
63 w.set_ccs(0, vals::CcmrInputCcs::TI4);
64 w.set_ccs(1, vals::CcmrInputCcs::TI4);
65 });
66
67 // enable and configure to capture on rising edge
68 T::regs_gp16().ccer().modify(|w| {
69 w.set_cce(0, true);
70 w.set_cce(1, true);
71
72 w.set_ccp(0, false);
73 w.set_ccp(1, false);
74 });
75
76 T::regs_gp16().smcr().modify(|w| {
77 w.set_sms(vals::Sms::ENCODER_MODE_3);
78 });
79
80 T::regs_gp16().arr().modify(|w| w.set_arr(u16::MAX));
81 T::regs_gp16().cr1().modify(|w| w.set_cen(true));
82
83 Self { _inner: tim }
84 }
85
86 pub fn read_direction(&self) -> Direction {
87 match T::regs_gp16().cr1().read().dir() {
88 vals::Dir::DOWN => Direction::Downcounting,
89 vals::Dir::UP => Direction::Upcounting,
90 }
91 }
92
93 pub fn count(&self) -> u16 {
94 T::regs_gp16().cnt().read().cnt()
95 }
96}