1#![macro_use]
7
8use core::hint::unreachable_unchecked;
9
10use embassy_hal_internal::PeripheralType;
11
12use crate::pac::wdt::vals;
13pub use crate::pac::wdt::vals::{Halt as HaltConfig, Sleep as SleepConfig};
14use crate::{interrupt, pac, peripherals, Peri};
15
16const MIN_TICKS: u32 = 15;
17
18#[non_exhaustive]
20pub struct Config {
21 pub timeout_ticks: u32,
26
27 pub action_during_sleep: SleepConfig,
29
30 pub action_during_debug_halt: HaltConfig,
32}
33
34impl Config {
35 pub fn try_new<T: Instance>(_wdt: &Peri<'_, T>) -> Option<Self> {
38 let r = T::REGS;
39
40 #[cfg(not(any(feature = "_nrf91", feature = "_nrf5340")))]
41 let runstatus = r.runstatus().read().runstatus();
42 #[cfg(any(feature = "_nrf91", feature = "_nrf5340"))]
43 let runstatus = r.runstatus().read().runstatuswdt();
44
45 if runstatus {
46 let config = r.config().read();
47 Some(Self {
48 timeout_ticks: r.crv().read(),
49 action_during_sleep: config.sleep(),
50 action_during_debug_halt: config.halt(),
51 })
52 } else {
53 None
54 }
55 }
56}
57
58impl Default for Config {
59 fn default() -> Self {
60 Self {
61 timeout_ticks: 32768, action_during_debug_halt: HaltConfig::RUN,
63 action_during_sleep: SleepConfig::RUN,
64 }
65 }
66}
67
68pub struct Watchdog<T: Instance> {
70 _wdt: Peri<'static, T>,
71}
72
73impl<T: Instance> Watchdog<T> {
74 #[inline]
82 pub fn try_new<const N: usize>(
83 wdt: Peri<'static, T>,
84 config: Config,
85 ) -> Result<(Self, [WatchdogHandle; N]), Peri<'static, T>> {
86 assert!(N >= 1 && N <= 8);
87
88 let r = T::REGS;
89
90 let crv = config.timeout_ticks.max(MIN_TICKS);
91 let rren = crate::pac::wdt::regs::Rren((1u32 << N) - 1);
92
93 #[cfg(not(any(feature = "_nrf91", feature = "_nrf5340")))]
94 let runstatus = r.runstatus().read().runstatus();
95 #[cfg(any(feature = "_nrf91", feature = "_nrf5340"))]
96 let runstatus = r.runstatus().read().runstatuswdt();
97
98 if runstatus {
99 let curr_config = r.config().read();
100 if curr_config.halt() != config.action_during_debug_halt
101 || curr_config.sleep() != config.action_during_sleep
102 || r.crv().read() != crv
103 || r.rren().read() != rren
104 {
105 return Err(wdt);
106 }
107 } else {
108 r.config().write(|w| {
109 w.set_sleep(config.action_during_sleep);
110 w.set_halt(config.action_during_debug_halt);
111 });
112 r.intenset().write(|w| w.set_timeout(true));
113
114 r.crv().write_value(crv);
115 r.rren().write_value(rren);
116 r.tasks_start().write_value(1);
117 }
118
119 let this = Self { _wdt: wdt };
120
121 let mut handles = [const { WatchdogHandle { index: 0 } }; N];
122 for i in 0..N {
123 handles[i] = unsafe { WatchdogHandle::steal::<T>(i as u8) };
124 handles[i].pet();
125 }
126
127 Ok((this, handles))
128 }
129
130 #[inline(always)]
137 pub fn enable_interrupt(&mut self) {
138 T::REGS.intenset().write(|w| w.set_timeout(true));
139 }
140
141 #[inline(always)]
145 pub fn disable_interrupt(&mut self) {
146 T::REGS.intenclr().write(|w| w.set_timeout(true));
147 }
148
149 #[inline(always)]
154 pub fn awaiting_pets(&self) -> bool {
155 let r = T::REGS;
156 let enabled = r.rren().read().0;
157 let status = r.reqstatus().read().0;
158 (status & enabled) == 0
159 }
160}
161
162pub struct WatchdogHandle {
164 index: u8,
165}
166
167impl WatchdogHandle {
168 fn regs(&self) -> pac::wdt::Wdt {
169 match self.index / 8 {
170 #[cfg(not(feature = "_multi_wdt"))]
171 peripherals::WDT::INDEX => peripherals::WDT::REGS,
172 #[cfg(feature = "_multi_wdt")]
173 peripherals::WDT0::INDEX => peripherals::WDT0::REGS,
174 #[cfg(feature = "_multi_wdt")]
175 peripherals::WDT1::INDEX => peripherals::WDT1::REGS,
176 _ => unsafe { unreachable_unchecked() },
177 }
178 }
179
180 fn rr_index(&self) -> usize {
181 usize::from(self.index % 8)
182 }
183
184 #[inline]
191 pub fn pet(&mut self) {
192 let r = self.regs();
193 r.rr(self.rr_index()).write(|w| w.set_rr(vals::Rr::RELOAD));
194 }
195
196 pub fn is_pet(&self) -> bool {
198 let r = self.regs();
199 !r.reqstatus().read().rr(self.rr_index())
200 }
201
202 pub unsafe fn steal<T: Instance>(index: u8) -> Self {
208 Self {
209 index: T::INDEX * 8 + index,
210 }
211 }
212}
213
214pub(crate) trait SealedInstance {
215 const REGS: pac::wdt::Wdt;
216 const INDEX: u8;
217}
218
219#[allow(private_bounds)]
221pub trait Instance: SealedInstance + PeripheralType + 'static + Send {
222 type Interrupt: interrupt::typelevel::Interrupt;
224}
225
226macro_rules! impl_wdt {
227 ($type:ident, $pac_type:ident, $irq:ident, $index:literal) => {
228 impl crate::wdt::SealedInstance for peripherals::$type {
229 const REGS: pac::wdt::Wdt = pac::$pac_type;
230 const INDEX: u8 = $index;
231 }
232 impl crate::wdt::Instance for peripherals::$type {
233 type Interrupt = crate::interrupt::typelevel::$irq;
234 }
235 };
236}