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
use crate::ccm::{perclk, ticks, Divider, Frequency, Ticks, TicksError};
use core::marker::PhantomData;
use embedded_hal::timer::{CountDown, Periodic};
use imxrt1062_pac as pac;
pub struct UnclockedPIT(pac::PIT);
impl UnclockedPIT {
pub(crate) fn new(base: pac::PIT) -> Self {
UnclockedPIT(base)
}
pub fn clock(
self,
configured: perclk::Configured,
) -> (
PIT<channel::_0>,
PIT<channel::_1>,
PIT<channel::_2>,
PIT<channel::_3>,
) {
let (clock_hz, divider) = configured.enable();
self.0.mcr.write(|w| w.mdis().mdis_0());
(
PIT::new(clock_hz, divider),
PIT::new(clock_hz, divider),
PIT::new(clock_hz, divider),
PIT::new(clock_hz, divider),
)
}
}
pub mod channel {
pub struct _X;
pub struct _0;
pub struct _1;
pub struct _2;
pub struct _3;
pub trait Channel {
const IDX: usize;
type ChainedTo: Channel;
}
impl Channel for _X {
const IDX: usize = core::usize::MAX;
type ChainedTo = _X;
}
impl Channel for _0 {
const IDX: usize = 0;
type ChainedTo = _X;
}
impl Channel for _1 {
const IDX: usize = 1;
type ChainedTo = _0;
}
impl Channel for _2 {
const IDX: usize = 2;
type ChainedTo = _1;
}
impl Channel for _3 {
const IDX: usize = 3;
type ChainedTo = _2;
}
}
pub struct PIT<Chan> {
timer: &'static pac::pit::TIMER,
clock_hz: Frequency,
divider: Divider,
_chan: PhantomData<Chan>,
}
impl<Chan: channel::Channel> PIT<Chan> {
fn new(clock_hz: Frequency, divider: Divider) -> PIT<Chan> {
PIT {
timer: unsafe { &(*pac::PIT::ptr()).timer[Chan::IDX] },
clock_hz,
divider,
_chan: PhantomData,
}
}
#[inline(always)]
fn disabled<F: FnMut(&Self) -> R, R>(&self, mut act: F) -> R {
let enabled = self.timer.tctrl.read().ten().bit_is_set();
self.timer.tctrl.modify(|_, w| w.ten().clear_bit());
let tsv = self.timer.ldval.read().tsv().bits();
let res = act(self);
self.ldval(tsv);
self.timer.tctrl.modify(|_, w| w.ten().bit(enabled));
res
}
fn ldval(&self, val: u32) {
self.timer.ldval.write(|w| unsafe { w.tsv().bits(val) });
}
fn tif(&self) -> bool {
self.timer.tflg.read().tif().bit_is_set()
}
fn clear_tif(&self) {
self.timer.tflg.write(|w| w.tif().set_bit());
}
pub fn clock_period(&self) -> core::time::Duration {
(self.clock_hz / self.divider).into()
}
pub fn time<F: FnMut() -> R, R>(&mut self, mut act: F) -> (R, Option<core::time::Duration>) {
const STARTING_LDVAL: u32 = u32::max_value();
self.with_interrupts_disabled(|this| {
this.disabled(|this| {
this.clear_tif();
this.ldval(STARTING_LDVAL);
self.timer.tctrl.modify(|_, w| w.ten().set_bit());
let res = act();
let counter = this.timer.cval.read().tvl().bits();
if this.tif() {
(res, None)
} else {
let ticks = STARTING_LDVAL - counter;
let clock_period: core::time::Duration = (this.clock_hz / this.divider).into();
(res, Some(ticks * clock_period))
}
})
})
}
pub fn set_interrupt_enable(&mut self, interrupt: bool) {
self.timer.tctrl.modify(|_, w| w.tie().bit(interrupt));
}
pub fn interrupt_enable(&self) -> bool {
self.timer.tctrl.read().tie().bit_is_set()
}
#[inline(always)]
fn with_interrupts_disabled<F: FnMut(&Self) -> R, R>(&self, mut act: F) -> R {
let interrupt_enabled = self.interrupt_enable();
self.timer.tctrl.modify(|_, w| w.tie().clear_bit());
let res = act(self);
self.timer
.tctrl
.modify(|_, w| w.tie().bit(interrupt_enabled));
res
}
}
impl<Chan: channel::Channel> CountDown for PIT<Chan> {
type Time = core::time::Duration;
fn start<T: Into<Self::Time>>(&mut self, ms: T) {
let ticks: Ticks<u32> = match ticks(ms.into(), self.clock_hz, self.divider) {
Ok(ticks) => ticks,
Err(TicksError::TicksOverflow) | Err(TicksError::DurationOverflow) => {
Ticks(u32::max_value())
}
Err(TicksError::DivideByZero) => Ticks(0),
};
self.timer.tctrl.modify(|_, w| w.ten().clear_bit());
self.clear_tif();
self.ldval(ticks.0);
self.timer.tctrl.modify(|_, w| w.ten().set_bit());
}
fn wait(&mut self) -> nb::Result<(), void::Void> {
if self.tif() {
self.clear_tif();
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}
impl<Chan: channel::Channel> Periodic for PIT<Chan> {}
pub struct ChainedPIT<C0, C1> {
lower: PIT<C0>,
upper: PIT<C1>,
}
impl<C0, C1> ChainedPIT<C0, C1>
where
C1: channel::Channel,
{
pub fn set_interrupt_enable(&mut self, interrupt: bool) {
self.upper.set_interrupt_enable(interrupt);
}
pub fn interrupt_enable(&self) -> bool {
self.upper.interrupt_enable()
}
}
pub fn chain<C1: channel::Channel>(
mut lower: PIT<<C1 as channel::Channel>::ChainedTo>,
upper: PIT<C1>,
) -> ChainedPIT<<C1 as channel::Channel>::ChainedTo, C1> {
lower.set_interrupt_enable(false);
ChainedPIT { lower, upper }
}
impl<C0, C1> CountDown for ChainedPIT<C0, C1>
where
C0: channel::Channel,
C1: channel::Channel,
{
type Time = core::time::Duration;
fn start<T: Into<Self::Time>>(&mut self, time: T) {
let ticks: Ticks<u64> = match ticks(time.into(), self.lower.clock_hz, self.lower.divider) {
Ok(ticks) => ticks,
Err(TicksError::TicksOverflow) | Err(TicksError::DurationOverflow) => {
Ticks(u64::max_value())
}
Err(TicksError::DivideByZero) => Ticks(0),
};
self.lower.timer.tctrl.modify(|_, w| w.ten().clear_bit());
self.upper.timer.tctrl.modify(|_, w| w.ten().clear_bit());
self.upper.clear_tif();
self.upper.timer.tctrl.modify(|_, w| w.chn().set_bit());
self.upper.ldval((ticks.0 >> 32) as u32);
self.lower.ldval((ticks.0 & 0xFFFF_FFFF) as u32);
self.lower.timer.tctrl.modify(|_, w| w.ten().set_bit());
self.upper.timer.tctrl.modify(|_, w| w.ten().set_bit());
}
fn wait(&mut self) -> nb::Result<(), void::Void> {
if self.upper.tif() {
self.upper.clear_tif();
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}
impl ChainedPIT<channel::_0, channel::_1> {
pub fn time<F: FnMut() -> R, R>(&mut self, mut act: F) -> (R, Option<core::time::Duration>) {
const STARTING_LDVAL: u32 = u32::max_value();
self.upper.with_interrupts_disabled(|upper| {
self.lower.disabled(|lower| {
upper.disabled(|upper| {
upper.clear_tif();
upper.ldval(STARTING_LDVAL);
lower.ldval(STARTING_LDVAL);
upper.timer.tctrl.modify(|_, w| w.chn().set_bit());
upper.timer.tctrl.modify(|_, w| w.ten().set_bit());
lower.timer.tctrl.modify(|_, w| w.ten().set_bit());
let res = act();
let lifetime = {
let pit = unsafe { &*pac::PIT::ptr() };
let lifetime: u64 = (pit.ltmr64h.read().bits() as u64) << 32;
lifetime | (pit.ltmr64l.read().bits() as u64)
};
if upper.tif() {
(res, None)
} else {
let ticks = u64::max_value() - lifetime;
let clock_period: u64 =
core::time::Duration::from(upper.clock_hz / upper.divider).as_nanos()
as u64;
(
res,
Some(core::time::Duration::from_nanos(ticks * clock_period)),
)
}
})
})
})
}
}