aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/f4/mod.rs1
-rw-r--r--embassy-stm32/src/f4/qei.rs91
-rw-r--r--embassy-stm32/src/lib.rs2
-rw-r--r--embassy-traits/src/lib.rs1
-rw-r--r--embassy-traits/src/qei.rs22
5 files changed, 1 insertions, 116 deletions
diff --git a/embassy-stm32/src/f4/mod.rs b/embassy-stm32/src/f4/mod.rs
index a80d640dc..b1fc0cf1d 100644
--- a/embassy-stm32/src/f4/mod.rs
+++ b/embassy-stm32/src/f4/mod.rs
@@ -1,2 +1 @@
1pub mod qei;
2pub mod serial; pub mod serial;
diff --git a/embassy-stm32/src/f4/qei.rs b/embassy-stm32/src/f4/qei.rs
deleted file mode 100644
index 5973e62d9..000000000
--- a/embassy-stm32/src/f4/qei.rs
+++ /dev/null
@@ -1,91 +0,0 @@
1use crate::interrupt;
2use core::future::Future;
3use core::pin::Pin;
4use embassy::traits::qei::WaitForRotate;
5use embedded_hal::Direction;
6use stm32f4xx_hal::pac::TIM2;
7use stm32f4xx_hal::{qei, qei::Pins};
8
9pub struct Qei<T: Instance, PINS> {
10 _qei: qei::Qei<T, PINS>,
11 int: T::Interrupt,
12}
13
14impl<PINS: Pins<TIM2>> Qei<TIM2, PINS> {
15 pub fn tim2(tim: TIM2, pins: PINS, interrupt: interrupt::TIM2) -> Self {
16 let qei = qei::Qei::tim2(tim, pins);
17
18 let tim = unsafe {
19 &mut *(stm32f4xx_hal::stm32::TIM2::ptr()
20 as *mut stm32f4xx_hal::stm32::tim2::RegisterBlock)
21 };
22 /*
23 enable qei interrupt
24 */
25 tim.dier.write(|w| w.uie().set_bit());
26
27 Qei {
28 _qei: qei,
29 int: interrupt,
30 }
31 }
32}
33
34impl<PINS: Pins<TIM2> + 'static> WaitForRotate for Qei<TIM2, PINS> {
35 type RotateFuture<'a> = impl Future<Output = Direction> + 'a;
36
37 fn wait_for_rotate<'a>(
38 self: Pin<&'a mut Self>,
39 count_down: u16,
40 count_up: u16,
41 ) -> Self::RotateFuture<'a> {
42 let s = unsafe { self.get_unchecked_mut() };
43
44 let tim = unsafe {
45 &mut *(stm32f4xx_hal::stm32::TIM2::ptr()
46 as *mut stm32f4xx_hal::stm32::tim2::RegisterBlock)
47 };
48
49 /*
50 the interrupt will be reached at zero or the max count
51 write the total range to the qei.
52 */
53 tim.arr
54 .write(|w| unsafe { w.bits((count_down + count_up) as u32) });
55
56 /*
57 set timer to the correct value in the range
58 */
59 tim.cnt.write(|w| unsafe { w.bits(count_down as u32) });
60
61 /*
62 clear interrupt flag
63 */
64 tim.sr.write(|w| w.uif().clear_bit());
65
66 async move {
67 embassy::util::InterruptFuture::new(&mut s.int).await;
68
69 if tim.cnt.read().bits() == 0 {
70 Direction::Downcounting
71 } else if tim.cnt.read() == count_down + count_up {
72 Direction::Upcounting
73 } else {
74 panic!("unexpected value")
75 }
76 }
77 }
78}
79
80mod sealed {
81 pub trait Sealed {}
82}
83
84pub trait Instance: sealed::Sealed {
85 type Interrupt: interrupt::Interrupt;
86}
87
88impl sealed::Sealed for TIM2 {}
89impl Instance for TIM2 {
90 type Interrupt = interrupt::TIM2;
91}
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index a60c3192b..3b8b36faf 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -109,7 +109,7 @@ pub mod rtc;
109 feature = "stm32f469", 109 feature = "stm32f469",
110 feature = "stm32f479", 110 feature = "stm32f479",
111))] 111))]
112pub use f4::{qei, serial}; 112pub use f4::serial;
113 113
114#[cfg(any( 114#[cfg(any(
115 feature = "stm32f401", 115 feature = "stm32f401",
diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs
index 81a847ef6..ea59444c1 100644
--- a/embassy-traits/src/lib.rs
+++ b/embassy-traits/src/lib.rs
@@ -12,6 +12,5 @@ pub mod delay;
12pub mod flash; 12pub mod flash;
13pub mod gpio; 13pub mod gpio;
14pub mod i2c; 14pub mod i2c;
15pub mod qei;
16pub mod spi; 15pub mod spi;
17pub mod uart; 16pub mod uart;
diff --git a/embassy-traits/src/qei.rs b/embassy-traits/src/qei.rs
deleted file mode 100644
index 73581256d..000000000
--- a/embassy-traits/src/qei.rs
+++ /dev/null
@@ -1,22 +0,0 @@
1use core::future::Future;
2use core::pin::Pin;
3use embedded_hal::Direction;
4
5// Wait for a specified number of rotations either up or down
6pub trait WaitForRotate {
7 type RotateFuture<'a>: Future<Output = Direction> + 'a;
8
9 /// Wait for a specified number of rotations, in ticks, either up or down.
10 ///
11 /// Return Direction::Upcounting if the high bound is reached.
12 /// Return Direction::Downcounting if the low bound is reached.
13 ///
14 /// Number of ticks is encoder dependent. As an example, if we connect
15 /// the Bourns PEC11H-4120F-S0020, we have 20 ticks per full rotation.
16 /// Other encoders may vary.
17 fn wait_for_rotate<'a>(
18 self: Pin<&'a mut Self>,
19 count_down: u16,
20 count_up: u16,
21 ) -> Self::RotateFuture<'a>;
22}