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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use crate::pac::{
dbg::ctl0::TIMER0_HOLD_A, dbg::ctl1::TIMER14_HOLD_A, DBG, TIMER0, TIMER1, TIMER13, TIMER14,
TIMER15, TIMER16, TIMER2, TIMER5,
};
use crate::rcu::{sealed::RcuBus, Clocks, Enable, GetBusFreq, Reset, APB1, APB2};
use crate::time::Hertz;
use cast::{u16, u32, u64};
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use embedded_hal::timer::{Cancel, CountDown, Periodic};
use void::Void;
pub enum Event {
Update,
}
#[derive(Debug, PartialEq)]
pub enum Error {
Canceled,
}
pub struct Timer<TIMER> {
pub(crate) timer: TIMER,
pub(crate) clock: Hertz,
}
pub struct CountDownTimer<TIMER> {
timer: TIMER,
clock: Hertz,
}
pub enum DebugHold {
Continue,
Stop,
}
impl From<DebugHold> for TIMER0_HOLD_A {
fn from(hold: DebugHold) -> Self {
match hold {
DebugHold::Continue => TIMER0_HOLD_A::CONTINUE,
DebugHold::Stop => TIMER0_HOLD_A::STOP,
}
}
}
impl From<DebugHold> for TIMER14_HOLD_A {
fn from(hold: DebugHold) -> Self {
match hold {
DebugHold::Continue => TIMER14_HOLD_A::CONTINUE,
DebugHold::Stop => TIMER14_HOLD_A::STOP,
}
}
}
impl Timer<SYST> {
pub fn syst(mut syst: SYST, clocks: &Clocks) -> Self {
syst.set_clock_source(SystClkSource::Core);
Self {
timer: syst,
clock: clocks.hclk(),
}
}
pub fn start_count_down<T>(self, timeout: T) -> CountDownTimer<SYST>
where
T: Into<Hertz>,
{
let Self { timer, clock } = self;
let mut timer = CountDownTimer { timer, clock };
timer.start(timeout);
timer
}
pub fn release(self) -> SYST {
self.timer
}
}
impl CountDownTimer<SYST> {
pub fn listen(&mut self, event: Event) {
match event {
Event::Update => self.timer.enable_interrupt(),
}
}
pub fn unlisten(&mut self, event: Event) {
match event {
Event::Update => self.timer.disable_interrupt(),
}
}
pub fn reset(&mut self) {
self.timer.clear_current();
}
pub fn micros_since(&self) -> u32 {
let reload_value = SYST::get_reload();
let timer_clock = u64(self.clock.0);
let ticks = u64(reload_value - SYST::get_current());
u32(1_000_000 * ticks / timer_clock).unwrap()
}
pub fn stop(mut self) -> Timer<SYST> {
self.timer.disable_counter();
let Self { timer, clock } = self;
Timer { timer, clock }
}
pub fn release(self) -> SYST {
self.stop().release()
}
}
impl CountDown for CountDownTimer<SYST> {
type Time = Hertz;
fn start<T>(&mut self, timeout: T)
where
T: Into<Hertz>,
{
let rvr = self.clock.0 / timeout.into().0 - 1;
assert!(rvr < (1 << 24));
self.timer.set_reload(rvr);
self.timer.clear_current();
self.timer.enable_counter();
}
fn wait(&mut self) -> nb::Result<(), Void> {
if self.timer.has_wrapped() {
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}
impl Cancel for CountDownTimer<SYST> {
type Error = Error;
fn cancel(&mut self) -> Result<(), Self::Error> {
if !self.timer.is_counter_enabled() {
return Err(Self::Error::Canceled);
}
self.timer.disable_counter();
Ok(())
}
}
impl Periodic for CountDownTimer<SYST> {}
pub(crate) trait TimerExt {
fn reset_counter(&mut self);
fn configure_prescaler_reload(&mut self, timeout: Hertz, clock: Hertz);
}
macro_rules! hal {
($TIMERX:ident: ($timerX:ident, $APBx:ident, $dbg_ctlX:ident, $timerX_hold:ident$(,$master_timerbase:ident)*)) => {
impl Timer<$TIMERX> {
pub fn $timerX(timer: $TIMERX, clocks: &Clocks, apb: &mut $APBx) -> Self {
$TIMERX::enable(apb);
$TIMERX::reset(apb);
Self {
timer,
clock: <$TIMERX as RcuBus>::Bus::get_timer_frequency(clocks),
}
}
pub fn start_count_down<T>(self, timeout: T) -> CountDownTimer<$TIMERX>
where
T: Into<Hertz>,
{
let Self { timer, clock } = self;
let mut timer = CountDownTimer { timer, clock };
timer.start(timeout);
timer
}
$(
pub fn start_master<T>(
self,
timeout: T,
mode: crate::pac::$master_timerbase::ctl1::MMC_A,
) -> CountDownTimer<$TIMERX>
where
T: Into<Hertz>,
{
let Self { timer, clock } = self;
let mut timer = CountDownTimer { timer, clock };
timer.timer.ctl1.modify(|_, w| w.mmc().variant(mode));
timer.start(timeout);
timer
}
)?
#[inline(always)]
pub fn clocking_reset(&mut self, apb: &mut <$TIMERX as RcuBus>::Bus) {
$TIMERX::reset(apb);
}
#[inline(always)]
pub fn stop_in_debug(&mut self, dbg: &mut DBG, hold: DebugHold) {
dbg.$dbg_ctlX.modify(|_, w| w.$timerX_hold().variant(hold.into()));
}
pub fn release(self) -> $TIMERX {
self.timer
}
}
impl CountDownTimer<$TIMERX> {
pub fn listen(&mut self, event: Event) {
match event {
Event::Update => self.timer.dmainten.modify(|_, w| w.upie().enabled()),
}
}
pub fn unlisten(&mut self, event: Event) {
match event {
Event::Update => self.timer.dmainten.modify(|_, w| w.upie().disabled()),
}
}
pub fn stop(self) -> Timer<$TIMERX> {
self.timer.ctl0.modify(|_, w| w.cen().disabled());
let Self { timer, clock } = self;
Timer { timer, clock }
}
pub fn clear_update_interrupt_flag(&mut self) {
self.timer.intf.modify(|_, w| w.upif().clear());
}
pub fn release(self) -> $TIMERX {
self.stop().release()
}
pub fn micros_since(&self) -> u32 {
let timer_clock = self.clock.0;
let psc = u32(self.timer.psc.read().psc().bits());
let freq_divider = u64(timer_clock / (psc + 1));
let cnt = u64(self.timer.cnt.read().cnt().bits());
u32(1_000_000 * cnt / freq_divider).unwrap()
}
pub fn reset(&mut self) {
self.timer.reset_counter();
}
}
impl TimerExt for $TIMERX {
fn reset_counter(&mut self) {
self.ctl0.modify(|_, w| w.ups().counter_only());
self.swevg.write(|w| w.upg().update());
self.ctl0.modify(|_, w| w.ups().any_event());
}
fn configure_prescaler_reload(&mut self, timeout: Hertz, clock: Hertz) {
let (prescaler, auto_reload_value) = compute_prescaler_reload(timeout, clock);
self.psc.write(|w| w.psc().bits(prescaler));
self.car.write(|w| w.car().bits(auto_reload_value.into()));
}
}
impl CountDown for CountDownTimer<$TIMERX> {
type Time = Hertz;
fn start<T>(&mut self, timeout: T)
where
T: Into<Hertz>,
{
self.timer.ctl0.modify(|_, w| w.cen().disabled());
self.timer.configure_prescaler_reload(timeout.into(), self.clock);
self.timer.reset_counter();
self.timer.ctl0.modify(|_, w| w.cen().enabled());
}
fn wait(&mut self) -> nb::Result<(), Void> {
if self.timer.intf.read().upif().is_clear() {
Err(nb::Error::WouldBlock)
} else {
self.clear_update_interrupt_flag();
Ok(())
}
}
}
impl Cancel for CountDownTimer<$TIMERX> {
type Error = Error;
fn cancel(&mut self) -> Result<(), Self::Error> {
let is_counter_enabled = self.timer.ctl0.read().cen().is_enabled();
if !is_counter_enabled {
return Err(Self::Error::Canceled);
}
self.timer.ctl0.modify(|_, w| w.cen().disabled());
Ok(())
}
}
impl Periodic for CountDownTimer<$TIMERX> {}
};
}
#[inline(always)]
fn compute_prescaler_reload(freq: Hertz, clock: Hertz) -> (u16, u16) {
let ticks = clock.0 / freq.0;
let psc = u16((ticks - 1) / (1 << 16)).unwrap();
let car = u16(ticks / u32(psc + 1)).unwrap();
(psc, car)
}
hal!(TIMER0: (timer0, APB2, ctl0, timer0_hold, timer0));
hal!(TIMER1: (timer1, APB1, ctl0, timer1_hold, timer1));
hal!(TIMER2: (timer2, APB1, ctl0, timer2_hold, timer1));
hal!(TIMER5: (timer5, APB1, ctl0, timer5_hold, timer5));
hal!(TIMER13: (timer13, APB1, ctl0, timer13_hold));
hal!(TIMER14: (timer14, APB2, ctl1, timer14_hold, timer14));
hal!(TIMER15: (timer15, APB2, ctl1, timer15_hold));
hal!(TIMER16: (timer16, APB2, ctl1, timer16_hold));