@@ -23,14 +23,16 @@ impl fmt::Debug for IndependentWatchdog {
23
23
}
24
24
}
25
25
26
- const MAX_PR : u8 = 0b110 ;
26
+ const LSI_KHZ : u32 = 32 ;
27
+ const MAX_PR : u32 = 0b110 ;
27
28
const MAX_RL : u16 = 0xFFF ;
28
29
const KR_ACCESS : u16 = 0x5555 ;
29
30
const KR_RELOAD : u16 = 0xAAAA ;
30
31
const KR_START : u16 = 0xCCCC ;
31
32
32
33
impl IndependentWatchdog {
33
- /// Wrap and start the watchdog
34
+ /// Creates a new `IndependentWatchDog` without starting it. Call `start` to start the watchdog.
35
+ /// See `WatchdogEnable` and `Watchdog` for more info.
34
36
pub fn new ( iwdg : IWDG ) -> Self {
35
37
IndependentWatchdog { iwdg }
36
38
}
@@ -40,15 +42,22 @@ impl IndependentWatchdog {
40
42
dbgmcu. apb1_fz . modify ( |_, w| w. dbg_iwdg_stop ( ) . bit ( stop) ) ;
41
43
}
42
44
43
- fn setup ( & self , timeout_ms : u32 ) {
44
- let mut pr = 0 ;
45
- while pr < MAX_PR && Self :: timeout_period ( pr, MAX_RL ) < timeout_ms {
46
- pr += 1 ;
47
- }
45
+ /// Sets the watchdog timer timout period. Max: 32768 ms
46
+ fn setup ( & self , timeout_ms : MilliSeconds ) {
47
+ assert ! ( timeout_ms. ticks( ) < ( 1 << 15 ) , "Watchdog timeout to high" ) ;
48
+ let pr = match timeout_ms. ticks ( ) {
49
+ t if t == 0 => 0b000 , // <= (MAX_PR + 1) * 4 / LSI_KHZ => 0b000,
50
+ t if t <= ( MAX_PR + 1 ) * 8 / LSI_KHZ => 0b001 ,
51
+ t if t <= ( MAX_PR + 1 ) * 16 / LSI_KHZ => 0b010 ,
52
+ t if t <= ( MAX_PR + 1 ) * 32 / LSI_KHZ => 0b011 ,
53
+ t if t <= ( MAX_PR + 1 ) * 64 / LSI_KHZ => 0b100 ,
54
+ t if t <= ( MAX_PR + 1 ) * 128 / LSI_KHZ => 0b101 ,
55
+ _ => 0b110 ,
56
+ } ;
48
57
49
58
let max_period = Self :: timeout_period ( pr, MAX_RL ) ;
50
59
let max_rl = u32:: from ( MAX_RL ) ;
51
- let rl = ( timeout_ms * max_rl / max_period) . min ( max_rl) as u16 ;
60
+ let rl = ( timeout_ms. ticks ( ) * max_rl / max_period) . min ( max_rl) as u16 ;
52
61
53
62
self . access_registers ( |iwdg| {
54
63
iwdg. pr . modify ( |_, w| w. pr ( ) . bits ( pr) ) ;
@@ -72,7 +81,7 @@ impl IndependentWatchdog {
72
81
73
82
/// pr: Prescaler divider bits, rl: reload value
74
83
///
75
- /// Returns ms
84
+ /// Returns timeout period in ms
76
85
fn timeout_period ( pr : u8 , rl : u16 ) -> u32 {
77
86
let divider: u32 = match pr {
78
87
0b000 => 4 ,
@@ -85,7 +94,7 @@ impl IndependentWatchdog {
85
94
0b111 => 256 ,
86
95
_ => unreachable ! ( ) ,
87
96
} ;
88
- ( u32:: from ( rl) + 1 ) * divider / 32
97
+ ( u32:: from ( rl) + 1 ) * divider / LSI_KHZ
89
98
}
90
99
91
100
fn access_registers < A , F : FnMut ( & IWDG ) -> A > ( & self , mut f : F ) -> A {
@@ -99,7 +108,7 @@ impl IndependentWatchdog {
99
108
}
100
109
101
110
pub fn start ( & mut self , period : MilliSeconds ) {
102
- self . setup ( period. ticks ( ) ) ;
111
+ self . setup ( period) ;
103
112
104
113
self . iwdg . kr . write ( |w| unsafe { w. key ( ) . bits ( KR_START ) } ) ;
105
114
}
0 commit comments