Skip to content

Commit a2f7254

Browse files
committed
Fix calculation for DelayNs.
Use u64 for intermediate value to avoid overflow.
1 parent 4f15555 commit a2f7254

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

nrf-hal-common/src/delay.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Delays.
2-
use cortex_m::peripheral::syst::SystClkSource;
3-
use cortex_m::peripheral::SYST;
42
53
use crate::clocks::HFCLK_FREQ;
4+
use core::convert::TryInto;
5+
use cortex_m::peripheral::syst::SystClkSource;
6+
use cortex_m::peripheral::SYST;
67
use embedded_hal::delay::DelayNs;
78
use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};
89

@@ -66,7 +67,9 @@ impl DelayNs for Delay {
6667
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
6768
const MAX_RVR: u32 = 0x00FF_FFFF;
6869

69-
let mut total_rvr = ns * (HFCLK_FREQ / 1_000_000_000);
70+
let mut total_rvr: u32 = (u64::from(ns) * u64::from(HFCLK_FREQ) / 1_000_000_000)
71+
.try_into()
72+
.unwrap();
7073

7174
while total_rvr != 0 {
7275
let current_rvr = if total_rvr <= MAX_RVR {

0 commit comments

Comments
 (0)