1
+ //! Watchdog
2
+
1
3
use crate :: hal:: watchdog:: { Watchdog , WatchdogEnable } ;
2
4
3
5
use crate :: stm32:: { DBGMCU , IWDG } ;
@@ -6,19 +8,18 @@ use crate::time::MilliSeconds;
6
8
const LSI_KHZ : u32 = 40 ;
7
9
const MAX_PR : u8 = 8 ;
8
10
const MAX_RL : u16 = 0x1000 ;
9
- const KR_ACCESS : u16 = 0x5555 ;
10
- const KR_RELOAD : u16 = 0xAAAA ;
11
- const KR_START : u16 = 0xCCCC ;
12
11
13
12
pub struct IndependentWatchDog {
14
13
iwdg : IWDG ,
15
14
}
16
15
17
16
impl IndependentWatchDog {
17
+ /// Creates a new `IndependentWatchDog` without starting it. Call `start` to start the watchdog. See `WatchdogEnable` and `Watchdog` for more info.
18
18
pub fn new ( iwdg : IWDG ) -> Self {
19
19
IndependentWatchDog { iwdg }
20
20
}
21
21
22
+ /// Set the watchdog to stop when a breakpoint is hit while debugging
22
23
pub fn stop_on_debug ( & self , dbg : & DBGMCU , stop : bool ) {
23
24
dbg. apb1_fz . modify ( |_, w| w. dbg_iwdg_stop ( ) . bit ( stop) ) ;
24
25
}
@@ -73,11 +74,11 @@ impl IndependentWatchDog {
73
74
74
75
fn access_registers < A , F : FnMut ( & IWDG ) -> A > ( & self , mut f : F ) -> A {
75
76
// 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 ( ) ) ;
77
78
let a = f ( & self . iwdg ) ;
78
79
79
80
// Protect again
80
- self . iwdg . kr . write ( |w| unsafe { w. key ( ) . bits ( KR_RELOAD ) } ) ;
81
+ self . iwdg . kr . write ( |w| w. key ( ) . reset ( ) ) ;
81
82
a
82
83
}
83
84
}
@@ -88,12 +89,12 @@ impl WatchdogEnable for IndependentWatchDog {
88
89
fn start < T : Into < Self :: Time > > ( & mut self , period : T ) {
89
90
self . setup ( period. into ( ) . 0 ) ;
90
91
91
- self . iwdg . kr . write ( |w| unsafe { w. key ( ) . bits ( KR_START ) } ) ;
92
+ self . iwdg . kr . write ( |w| w. key ( ) . start ( ) ) ;
92
93
}
93
94
}
94
95
95
96
impl Watchdog for IndependentWatchDog {
96
97
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 ( ) ) ;
98
99
}
99
100
}
0 commit comments