atsamx7x_hal/
watchdog.rs

1//! Watchdog timer configuration.
2use crate::{generics, pac::WDT};
3
4use core::convert::From;
5use core::marker::PhantomData;
6
7/// The state of the [`Watchdog`].
8pub trait WatchdogState: generics::Sealed {}
9
10/// The [`Watchdog`] is disabled.
11pub enum Disabled {}
12/// The [`Watchdog`] is in reset and has yet to be configured. It will
13/// trigger a system reset ~15 seconds from system start.
14pub enum Reset {}
15
16impl generics::Sealed for Disabled {}
17impl WatchdogState for Disabled {}
18impl generics::Sealed for Reset {}
19impl WatchdogState for Reset {}
20
21/// [`WDT`] abstraction.
22pub struct Watchdog<S: WatchdogState> {
23    watchdog: WDT,
24    state: PhantomData<S>,
25}
26
27impl Watchdog<Reset> {
28    /// Creates a [`Watchdog`] in [`Reset`] from the device's [`WDT`].
29    pub fn new(watchdog: WDT) -> Self {
30        Self {
31            watchdog,
32            state: PhantomData,
33        }
34    }
35
36    /// Disables the watchdog completely.
37    pub fn disable(self) -> Watchdog<Disabled> {
38        self.watchdog.mr().write(|w| w.wddis().set_bit());
39        Watchdog {
40            watchdog: self.watchdog,
41            state: PhantomData,
42        }
43    }
44}
45
46impl From<WDT> for Watchdog<Disabled> {
47    fn from(wd: WDT) -> Self {
48        Watchdog::new(wd).disable()
49    }
50}
51
52impl crate::ehal::watchdog::Watchdog for Watchdog<Disabled> {
53    /// Stub implementation. Does nothing and immediately returns.
54    fn feed(&mut self) {}
55}