Skip to content

Commit 460c09b

Browse files
rfuestmvertescher
authored andcommitted
Use delay_us from stm32f4xx-hal crate
The new implementation doesn't panic if the delay requires a reload value that is larger than the maximum reload value for the SysTick timer. Instead the timer is started multiple times, if required.
1 parent e8b2084 commit 460c09b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/delay.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,29 @@ impl DelayMs<u8> for Delay {
4747

4848
impl DelayUs<u32> for Delay {
4949
fn delay_us(&mut self, us: u32) {
50-
let rvr = us * (self.clocks.hclk().0 / 8_000_000);
50+
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
51+
const MAX_RVR: u32 = 0x00FF_FFFF;
5152

52-
assert!(rvr < (1 << 24));
53+
let mut total_rvr = us * (self.clocks.hclk().0 / 8_000_000);
5354

54-
self.syst.set_reload(rvr);
55-
self.syst.clear_current();
56-
self.syst.enable_counter();
55+
while total_rvr != 0 {
56+
let current_rvr = if total_rvr <= MAX_RVR {
57+
total_rvr
58+
} else {
59+
MAX_RVR
60+
};
5761

58-
while !self.syst.has_wrapped() {}
62+
self.syst.set_reload(current_rvr);
63+
self.syst.clear_current();
64+
self.syst.enable_counter();
5965

60-
self.syst.disable_counter();
66+
// Update the tracking variable while we are waiting...
67+
total_rvr -= current_rvr;
68+
69+
while !self.syst.has_wrapped() {}
70+
71+
self.syst.disable_counter();
72+
}
6173
}
6274
}
6375

0 commit comments

Comments
 (0)