Skip to content

Commit b954de7

Browse files
authored
Merge pull request #143 from jamesmunns/fix-systick
Use the systick implementation from the nrf-hal which handles rollovers
2 parents b27ec08 + 9a67c74 commit b954de7

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/delay.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl Delay {
2727
}
2828
}
2929

30+
/// System timer (SysTick) as a delay provider.
3031
impl DelayMs<u32> for Delay {
3132
fn delay_ms(&mut self, ms: u32) {
3233
self.delay_us(ms * 1_000);
@@ -47,17 +48,29 @@ impl DelayMs<u8> for Delay {
4748

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

52-
// assert!(rvr < (1 << 24)); //TODO fix this assertion
54+
let mut total_rvr = us * (self.clocks.sysclk().0 / 1_000_000);
5355

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

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

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

0 commit comments

Comments
 (0)