-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathwdt.rs
450 lines (418 loc) · 12.8 KB
/
wdt.rs
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! HAL interface to the WDT peripheral
//!
//! This HAL implements a basic watchdog timer with 1..=8 handles.
//! Once the watchdog has been started, it cannot be stopped.
use crate::pac::WDT;
use handles::*;
/// A type state representing a watchdog that has not been started
pub struct Inactive;
/// A type state representing a watchdog that has been started and cannot be stopped
pub struct Active;
/// An interface to the Watchdog
pub struct Watchdog<T: sealed::WdMode> {
wdt: WDT,
_state: T,
}
/// A structure containing the active watchdog and all requested
/// Watchdog handles
pub struct Parts<T> {
pub watchdog: Watchdog<Active>,
pub handles: T,
}
/// An interface to feed the Watchdog
pub struct WatchdogHandle<T: sealed::HandleId>(T);
impl<T> WatchdogHandle<T>
where
T: sealed::HandleId,
{
/// Pet the watchdog
///
/// This function pets the given watchdog handle.
///
/// NOTE: All active handles must be pet within the time interval to
/// prevent a reset from occuring.
#[inline]
pub fn pet(&mut self) {
let hdl = unsafe { &*WDT::ptr() };
hdl.rr[self.0.index()].write(|w| w.rr().reload());
}
/// Has this handle been pet within the current window?
pub fn is_pet(&self) -> bool {
let hdl = unsafe { &*WDT::ptr() };
let rd = hdl.reqstatus.read().bits();
let idx = self.0.index();
debug_assert!(idx < 8, "Bad Index!");
((rd >> idx) & 0x1) == 0
}
/// Convert the handle into a generic handle
///
/// This is useful if you need to place handles into an array
pub fn degrade(self) -> WatchdogHandle<HdlN> {
WatchdogHandle(HdlN {
idx: self.0.index() as u8,
})
}
}
impl Watchdog<Inactive> {
/// Try to create a new watchdog instance from the peripheral
///
/// This function will return an error if the watchdog has already
/// been activated, which may happen on a (non-watchdog) soft reset.
/// In this case, it may be possible to still obtain the handles with
/// the `Watchdog::try_recover()` method.
///
/// If the watchdog has already started, configuration is no longer possible.
pub fn try_new(wdt: WDT) -> Result<Watchdog<Inactive>, WDT> {
let watchdog = Watchdog {
wdt,
_state: Inactive,
};
if watchdog.is_active() {
Err(watchdog.wdt)
} else {
Ok(watchdog)
}
}
/// Release the peripheral
///
/// Note: The peripheral cannot be released after activation
pub fn release(self) -> WDT {
self.wdt
}
/// Activate the watchdog with the given number of handles
///
/// The watchdog cannot be deactivated after starting.
///
/// NOTE: All activated handles must be pet within the configured time interval to
/// prevent a reset from occuring.
pub fn activate<H: sealed::Handles>(self) -> Parts<H::Handles> {
self.wdt.rren.write(|w| unsafe { w.bits(H::ENABLE) });
self.wdt.tasks_start.write(|w| unsafe { w.bits(1) });
Parts {
watchdog: Watchdog {
wdt: self.wdt,
_state: Active,
},
handles: H::create_handle(),
}
}
/// Enable the watchdog interrupt
///
/// NOTE: Although the interrupt will occur, there is no way to prevent
/// the reset from occuring. From the time the event was fired, the
/// system will reset two LFCLK ticks later (61 microseconds) if the
/// interrupt has been enabled.
#[inline(always)]
pub fn enable_interrupt(&mut self) {
self.wdt.intenset.write(|w| w.timeout().set_bit());
}
/// Disable the watchdog interrupt
///
/// NOTE: This has no effect on the reset caused by the Watchdog
#[inline(always)]
pub fn disable_interrupt(&mut self) {
self.wdt.intenclr.write(|w| w.timeout().set_bit());
}
/// Set the number of 32.768kHz ticks in each watchdog period
///
/// This value defaults to 0xFFFF_FFFF (1.5 days) on reset.
///
/// Note: there is a minimum of 15 ticks (458 microseconds). If a lower
/// number is provided, 15 ticks will be used as the configured value
#[inline(always)]
pub fn set_lfosc_ticks(&mut self, ticks: u32) {
self.wdt
.crv
.write(|w| unsafe { w.bits(ticks.max(0x0000_000F)) });
}
/// Should the watchdog continue to count during sleep modes?
///
/// This value defaults to ENABLED on reset.
pub fn run_during_sleep(&self, setting: bool) {
self.wdt.config.modify(|_r, w| w.sleep().bit(setting));
}
/// Should the watchdog continue to count when the CPU is halted for debug?
///
/// This value defaults to DISABLED on reset.
pub fn run_during_debug_halt(&self, setting: bool) {
self.wdt.config.modify(|_r, w| w.halt().bit(setting));
}
}
impl Watchdog<Active> {
/// Is the watchdog still awaiting pets from any handle?
///
/// This reports whether sufficient pets have been received from all
/// handles to prevent a reset this time period
#[inline(always)]
pub fn awaiting_pets(&self) -> bool {
let enabled = self.wdt.rren.read().bits();
let status = self.wdt.reqstatus.read().bits();
(status & enabled) == 0
}
/// Try to recover a handle to an already running watchdog. If the
/// number of requested handles matches the activated number of handles,
/// an activated handle will be returned. Otherwise the peripheral will
/// be returned
///
/// NOTE: Since the watchdog is already counting, you want to pet these dogs
/// as soon as possible!
pub fn try_recover<H: sealed::Handles>(wdt: WDT) -> Result<Parts<H::Handles>, WDT> {
// Do we have the same number of handles at least?
if wdt.rren.read().bits() == H::ENABLE {
Ok(Parts {
watchdog: Watchdog {
wdt,
_state: Active,
},
handles: H::create_handle(),
})
} else {
Err(wdt)
}
}
}
impl<T> Watchdog<T>
where
T: sealed::WdMode,
{
/// Is the watchdog active?
#[inline(always)]
pub fn is_active(&self) -> bool {
self.wdt.runstatus.read().runstatus().bit_is_set()
}
}
mod sealed {
pub trait HandleId {
fn index(&self) -> usize;
}
pub trait Handles {
type Handles;
const ENABLE: u32;
fn create_handle() -> Self::Handles;
}
pub trait WdMode {}
}
impl sealed::WdMode for Inactive {}
impl sealed::WdMode for Active {}
impl sealed::HandleId for Hdl0 {
fn index(&self) -> usize {
0
}
}
impl sealed::HandleId for Hdl1 {
fn index(&self) -> usize {
1
}
}
impl sealed::HandleId for Hdl2 {
fn index(&self) -> usize {
2
}
}
impl sealed::HandleId for Hdl3 {
fn index(&self) -> usize {
3
}
}
impl sealed::HandleId for Hdl4 {
fn index(&self) -> usize {
4
}
}
impl sealed::HandleId for Hdl5 {
fn index(&self) -> usize {
5
}
}
impl sealed::HandleId for Hdl6 {
fn index(&self) -> usize {
6
}
}
impl sealed::HandleId for Hdl7 {
fn index(&self) -> usize {
7
}
}
impl sealed::HandleId for HdlN {
fn index(&self) -> usize {
self.idx.into()
}
}
pub mod handles {
//! Type states representing individual watchdog handles
/// A type state representing Watchdog Handle 0
pub struct Hdl0;
/// A type state representing Watchdog Handle 1
pub struct Hdl1;
/// A type state representing Watchdog Handle 2
pub struct Hdl2;
/// A type state representing Watchdog Handle 3
pub struct Hdl3;
/// A type state representing Watchdog Handle 4
pub struct Hdl4;
/// A type state representing Watchdog Handle 5
pub struct Hdl5;
/// A type state representing Watchdog Handle 6
pub struct Hdl6;
/// A type state representing Watchdog Handle 7
pub struct Hdl7;
/// A structure that represents a runtime stored Watchdog Handle
pub struct HdlN {
pub(super) idx: u8,
}
}
pub mod count {
//! Type states representing the number of requested handles
use super::{sealed::Handles, Hdl0, Hdl1, Hdl2, Hdl3, Hdl4, Hdl5, Hdl6, Hdl7, WatchdogHandle};
/// A type state representing the request for One handles
pub struct One;
/// A type state representing the request for Two handles
pub struct Two;
/// A type state representing the request for Three handles
pub struct Three;
/// A type state representing the request for Four handles
pub struct Four;
/// A type state representing the request for Five handles
pub struct Five;
/// A type state representing the request for Six handles
pub struct Six;
/// A type state representing the request for Seven handles
pub struct Seven;
/// A type state representing the request for Eight handles
pub struct Eight;
impl Handles for One {
type Handles = (WatchdogHandle<Hdl0>,);
const ENABLE: u32 = 0b0000_0001;
fn create_handle() -> Self::Handles {
(WatchdogHandle(Hdl0),)
}
}
impl Handles for Two {
type Handles = (WatchdogHandle<Hdl0>, WatchdogHandle<Hdl1>);
const ENABLE: u32 = 0b0000_0011;
fn create_handle() -> Self::Handles {
(WatchdogHandle(Hdl0), WatchdogHandle(Hdl1))
}
}
impl Handles for Three {
type Handles = (
WatchdogHandle<Hdl0>,
WatchdogHandle<Hdl1>,
WatchdogHandle<Hdl2>,
);
const ENABLE: u32 = 0b0000_0111;
fn create_handle() -> Self::Handles {
(
WatchdogHandle(Hdl0),
WatchdogHandle(Hdl1),
WatchdogHandle(Hdl2),
)
}
}
impl Handles for Four {
type Handles = (
WatchdogHandle<Hdl0>,
WatchdogHandle<Hdl1>,
WatchdogHandle<Hdl2>,
WatchdogHandle<Hdl3>,
);
const ENABLE: u32 = 0b0000_1111;
fn create_handle() -> Self::Handles {
(
WatchdogHandle(Hdl0),
WatchdogHandle(Hdl1),
WatchdogHandle(Hdl2),
WatchdogHandle(Hdl3),
)
}
}
impl Handles for Five {
type Handles = (
WatchdogHandle<Hdl0>,
WatchdogHandle<Hdl1>,
WatchdogHandle<Hdl2>,
WatchdogHandle<Hdl3>,
WatchdogHandle<Hdl4>,
);
const ENABLE: u32 = 0b0001_1111;
fn create_handle() -> Self::Handles {
(
WatchdogHandle(Hdl0),
WatchdogHandle(Hdl1),
WatchdogHandle(Hdl2),
WatchdogHandle(Hdl3),
WatchdogHandle(Hdl4),
)
}
}
impl Handles for Six {
type Handles = (
WatchdogHandle<Hdl0>,
WatchdogHandle<Hdl1>,
WatchdogHandle<Hdl2>,
WatchdogHandle<Hdl3>,
WatchdogHandle<Hdl4>,
WatchdogHandle<Hdl5>,
);
const ENABLE: u32 = 0b0011_1111;
fn create_handle() -> Self::Handles {
(
WatchdogHandle(Hdl0),
WatchdogHandle(Hdl1),
WatchdogHandle(Hdl2),
WatchdogHandle(Hdl3),
WatchdogHandle(Hdl4),
WatchdogHandle(Hdl5),
)
}
}
impl Handles for Seven {
type Handles = (
WatchdogHandle<Hdl0>,
WatchdogHandle<Hdl1>,
WatchdogHandle<Hdl2>,
WatchdogHandle<Hdl3>,
WatchdogHandle<Hdl4>,
WatchdogHandle<Hdl5>,
WatchdogHandle<Hdl6>,
);
const ENABLE: u32 = 0b0111_1111;
fn create_handle() -> Self::Handles {
(
WatchdogHandle(Hdl0),
WatchdogHandle(Hdl1),
WatchdogHandle(Hdl2),
WatchdogHandle(Hdl3),
WatchdogHandle(Hdl4),
WatchdogHandle(Hdl5),
WatchdogHandle(Hdl6),
)
}
}
impl Handles for Eight {
type Handles = (
WatchdogHandle<Hdl0>,
WatchdogHandle<Hdl1>,
WatchdogHandle<Hdl2>,
WatchdogHandle<Hdl3>,
WatchdogHandle<Hdl4>,
WatchdogHandle<Hdl5>,
WatchdogHandle<Hdl6>,
WatchdogHandle<Hdl7>,
);
const ENABLE: u32 = 0b1111_1111;
fn create_handle() -> Self::Handles {
(
WatchdogHandle(Hdl0),
WatchdogHandle(Hdl1),
WatchdogHandle(Hdl2),
WatchdogHandle(Hdl3),
WatchdogHandle(Hdl4),
WatchdogHandle(Hdl5),
WatchdogHandle(Hdl6),
WatchdogHandle(Hdl7),
)
}
}
}