Skip to content

Commit 8cd9275

Browse files
James MunnsSh3Rm4n
authored andcommitted
Fix off-by-one error in Delay impl
This makes two changes: * Updates the MAX_RVR to a 24-bit (not 25-bit) number * Moves tracking variable update for better potential precision This brings the stm32f3 delay impl up to date with the [nrf52 delay impl](https://github.com/nrf-rs/nrf-hal/blob/master/nrf-hal-common/src/delay.rs#L46-L72).
1 parent 052411d commit 8cd9275

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/delay.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl DelayMs<u8> for Delay {
6464

6565
impl DelayUs<u32> for Delay {
6666
fn delay_us(&mut self, us: u32) {
67-
// The RVR register is 24 bits wide, as SysTick is based on a 24 bit counter
68-
const MAX_RVR: u32 = 1 << 24;
67+
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
68+
const MAX_RVR: u32 = 0x00FF_FFFF;
6969

7070
// Depending on hclk (core clock), this 32 bit value allows
7171
// delays between 1 min to 9 min.
@@ -79,17 +79,19 @@ impl DelayUs<u32> for Delay {
7979
// Like dividing total_rvr / MAX_RVR
8080
// and delaying by MAX_RVR * (fraction).
8181
while total_rvr != 0 {
82-
let current_rvr = if total_rvr < MAX_RVR {
82+
let current_rvr = if total_rvr <= MAX_RVR {
8383
total_rvr
8484
} else {
8585
MAX_RVR
8686
};
87-
total_rvr -= current_rvr;
8887

8988
self.syst.set_reload(current_rvr);
9089
self.syst.clear_current();
9190
self.syst.enable_counter();
9291

92+
// Update the tracking variable while we are waiting...
93+
total_rvr -= current_rvr;
94+
9395
while !self.syst.has_wrapped() {}
9496

9597
self.syst.disable_counter();

0 commit comments

Comments
 (0)