1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use crate::gpio::{gpioa, gpiob, AltMode, Analog};
use crate::pac::{tim2, tim21, TIM2, TIM21};
use crate::rcc::Rcc;
use core::marker::PhantomData;
pub trait Pins<TIM> {
fn into_alt_mode(self);
}
pub trait PinCh1<TIM> {
fn into_alt_mode(self);
}
pub trait PinCh2<TIM> {
fn into_alt_mode(self);
}
impl PinCh1<TIM21> for gpiob::PB13<Analog> {
fn into_alt_mode(self) {
self.set_alt_mode(AltMode::AF6);
}
}
impl PinCh2<TIM21> for gpiob::PB14<Analog> {
fn into_alt_mode(self) {
self.set_alt_mode(AltMode::AF6);
}
}
impl PinCh1<TIM2> for gpioa::PA0<Analog> {
fn into_alt_mode(self) {
self.set_alt_mode(AltMode::AF2);
}
}
impl PinCh2<TIM2> for gpioa::PA1<Analog> {
fn into_alt_mode(self) {
self.set_alt_mode(AltMode::AF2);
}
}
impl<TIM, CH1, CH2> Pins<TIM> for (CH1, CH2)
where
CH1: PinCh1<TIM>,
CH2: PinCh2<TIM>,
{
fn into_alt_mode(self) {
self.0.into_alt_mode();
self.1.into_alt_mode();
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Direction {
Up,
Down,
}
pub struct Status {
pub direction: Direction,
pub did_overflow: bool,
pub count: u16,
}
pub enum Mode {
CountTi2 = 0b001,
CountTi1 = 0b010,
Qei = 0b011,
}
pub trait EncoderExt<TIM> {
fn encoder<PINS>(self, pins: PINS, mode: Mode, arr: u16, rcc: &mut Rcc) -> Encoder<TIM, PINS>
where
PINS: Pins<TIM>;
}
pub struct Encoder<T, PINS> {
timer: T,
_pins: PhantomData<PINS>,
}
macro_rules! encoders {
($($TIM:ident: ($timXen:ident, $timXrst:ident, $apbXenr:ident, $apbXrstr:ident, $sms:ty),)+) => {
$(
impl EncoderExt<$TIM> for $TIM {
fn encoder<PINS>(
self,
pins: PINS,
mode: Mode,
arr: u16,
rcc: &mut Rcc,
) -> Encoder<$TIM, PINS>
where
PINS: Pins<$TIM>,
{
Encoder::<$TIM, PINS>::new(self, pins, mode, arr, rcc)
}
}
impl<PINS> Encoder<$TIM, PINS>
where
PINS: Pins<$TIM>,
{
fn new(timer: $TIM, pins: PINS, mode: Mode, arr: u16, rcc: &mut Rcc) -> Self {
rcc.rb.$apbXenr.modify(|_, w| w.$timXen().set_bit());
rcc.rb.$apbXrstr.modify(|_, w| w.$timXrst().set_bit());
rcc.rb.$apbXrstr.modify(|_, w| w.$timXrst().clear_bit());
timer.cr1.write(|w| w.cen().clear_bit());
pins.into_alt_mode();
timer.smcr.write(|w| {
w
.ts()
.ti1f_ed()
.sms()
.variant(match mode {
Mode::CountTi1 => <$sms>::ENCODER_MODE_1,
Mode::CountTi2 => <$sms>::ENCODER_MODE_2,
Mode::Qei => <$sms>::ENCODER_MODE_3,
})
});
timer.cr1.write(|w| {
w
.urs()
.set_bit()
.cen()
.set_bit()
});
cortex_m::asm::delay(5000);
let mut self_ = Self {
timer,
_pins: PhantomData,
};
self_.set_arr(arr);
self_
}
pub fn arr(&mut self) -> u16 {
self.timer.arr.read().bits() as u16
}
pub fn set_arr(&mut self, arr: u16) {
#[allow(unused_unsafe)]
self.timer.arr.write(|w| unsafe { w.arr().bits(arr) });
}
pub fn listen(&mut self) {
self.timer.dier.write(|w| w.uie().enabled());
}
pub fn listen_all(&mut self) {
self.listen();
self.timer.dier.write(|w| w.tie().enabled());
}
pub fn status(&mut self) -> Status {
let is_dir_up = self.timer.cr1.read().dir().bit_is_clear();
let did_overflow = self.timer.sr.read().bits() & 0x01 == 1;
let count = self.timer.cnt.read().bits() as u16;
let direction = match is_dir_up {
true => Direction::Up,
false => Direction::Down,
};
Status {
did_overflow,
count,
direction,
}
}
pub fn clear_irq(&mut self) {
self.timer.sr.reset();
}
}
)+
}
}
encoders! {
TIM2: (tim2en, tim2rst, apb1enr, apb1rstr, tim2::smcr::SMS_A),
TIM21: (tim21en, tim21rst, apb2enr, apb2rstr, tim21::smcr::SMS_A),
}