aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-08-30 23:16:33 +0000
committerGitHub <[email protected]>2023-08-30 23:16:33 +0000
commit0823d9dc938bc1289adeb67c081ff679d9ddfc85 (patch)
treeaf3abdaccc396e0b82609280398d1f07cb06400b
parenta2ce3aa1a1ea8bf67e9e29a2bf0e3e5fc86ccb7d (diff)
parentc10fb7c1c4ca40749494f4873c2144906c2f1a17 (diff)
Merge pull request #1845 from xoviat/qei
stm32: add qei
-rw-r--r--embassy-stm32/src/timer/mod.rs1
-rw-r--r--embassy-stm32/src/timer/qei.rs96
2 files changed, 97 insertions, 0 deletions
diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs
index b5ced45fe..839548a53 100644
--- a/embassy-stm32/src/timer/mod.rs
+++ b/embassy-stm32/src/timer/mod.rs
@@ -1,4 +1,5 @@
1pub mod complementary_pwm; 1pub mod complementary_pwm;
2pub mod qei;
2pub mod simple_pwm; 3pub mod simple_pwm;
3 4
4use stm32_metapac::timer::vals; 5use stm32_metapac::timer::vals;
diff --git a/embassy-stm32/src/timer/qei.rs b/embassy-stm32/src/timer/qei.rs
new file mode 100644
index 000000000..c0f9288a5
--- /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 SimplePwm<'d, T> {
47 _inner: PeripheralRef<'d, T>,
48}
49
50impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'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}