Skip to content

Commit feceab4

Browse files
committed
use safe methods, add docs
1 parent cc6652e commit feceab4

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/watchdog.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Watchdog
2+
13
use crate::hal::watchdog::{Watchdog, WatchdogEnable};
24

35
use crate::stm32::{DBGMCU, IWDG};
@@ -6,19 +8,18 @@ use crate::time::MilliSeconds;
68
const LSI_KHZ: u32 = 40;
79
const MAX_PR: u8 = 8;
810
const MAX_RL: u16 = 0x1000;
9-
const KR_ACCESS: u16 = 0x5555;
10-
const KR_RELOAD: u16 = 0xAAAA;
11-
const KR_START: u16 = 0xCCCC;
1211

1312
pub struct IndependentWatchDog {
1413
iwdg: IWDG,
1514
}
1615

1716
impl IndependentWatchDog {
17+
/// Creates a new `IndependentWatchDog` without starting it. Call `start` to start the watchdog. See `WatchdogEnable` and `Watchdog` for more info.
1818
pub fn new(iwdg: IWDG) -> Self {
1919
IndependentWatchDog { iwdg }
2020
}
2121

22+
/// Set the watchdog to stop when a breakpoint is hit while debugging
2223
pub fn stop_on_debug(&self, dbg: &DBGMCU, stop: bool) {
2324
dbg.apb1_fz.modify(|_, w| w.dbg_iwdg_stop().bit(stop));
2425
}
@@ -73,11 +74,11 @@ impl IndependentWatchDog {
7374

7475
fn access_registers<A, F: FnMut(&IWDG) -> A>(&self, mut f: F) -> A {
7576
// Unprotect write access to registers
76-
self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_ACCESS) });
77+
self.iwdg.kr.write(|w| w.key().enable());
7778
let a = f(&self.iwdg);
7879

7980
// Protect again
80-
self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_RELOAD) });
81+
self.iwdg.kr.write(|w| w.key().reset());
8182
a
8283
}
8384
}
@@ -88,12 +89,12 @@ impl WatchdogEnable for IndependentWatchDog {
8889
fn start<T: Into<Self::Time>>(&mut self, period: T) {
8990
self.setup(period.into().0);
9091

91-
self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_START) });
92+
self.iwdg.kr.write(|w| w.key().start());
9293
}
9394
}
9495

9596
impl Watchdog for IndependentWatchDog {
9697
fn feed(&mut self) {
97-
self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_RELOAD) });
98+
self.iwdg.kr.write(|w| w.key().reset());
9899
}
99100
}

0 commit comments

Comments
 (0)