diff options
| author | xoviat <[email protected]> | 2023-08-30 23:16:33 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-08-30 23:16:33 +0000 |
| commit | 0823d9dc938bc1289adeb67c081ff679d9ddfc85 (patch) | |
| tree | af3abdaccc396e0b82609280398d1f07cb06400b | |
| parent | a2ce3aa1a1ea8bf67e9e29a2bf0e3e5fc86ccb7d (diff) | |
| parent | c10fb7c1c4ca40749494f4873c2144906c2f1a17 (diff) | |
Merge pull request #1845 from xoviat/qei
stm32: add qei
| -rw-r--r-- | embassy-stm32/src/timer/mod.rs | 1 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/qei.rs | 96 |
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 @@ | |||
| 1 | pub mod complementary_pwm; | 1 | pub mod complementary_pwm; |
| 2 | pub mod qei; | ||
| 2 | pub mod simple_pwm; | 3 | pub mod simple_pwm; |
| 3 | 4 | ||
| 4 | use stm32_metapac::timer::vals; | 5 | use 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 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | |||
| 3 | use embassy_hal_internal::{into_ref, PeripheralRef}; | ||
| 4 | |||
| 5 | use super::*; | ||
| 6 | use crate::gpio::sealed::AFType; | ||
| 7 | use crate::gpio::AnyPin; | ||
| 8 | use crate::Peripheral; | ||
| 9 | |||
| 10 | pub enum Direction { | ||
| 11 | Upcounting, | ||
| 12 | Downcounting, | ||
| 13 | } | ||
| 14 | |||
| 15 | pub struct Ch1; | ||
| 16 | pub struct Ch2; | ||
| 17 | |||
| 18 | pub struct QeiPin<'d, Perip, Channel> { | ||
| 19 | _pin: PeripheralRef<'d, AnyPin>, | ||
| 20 | phantom: PhantomData<(Perip, Channel)>, | ||
| 21 | } | ||
| 22 | |||
| 23 | macro_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 | |||
| 43 | channel_impl!(new_ch1, Ch1, Channel1Pin); | ||
| 44 | channel_impl!(new_ch2, Ch2, Channel2Pin); | ||
| 45 | |||
| 46 | pub struct SimplePwm<'d, T> { | ||
| 47 | _inner: PeripheralRef<'d, T>, | ||
| 48 | } | ||
| 49 | |||
| 50 | impl<'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 | } | ||
