Skip to content

Commit

Permalink
Use overflow-workaround on delay_ms
Browse files Browse the repository at this point in the history
  • Loading branch information
David-OConnor committed Jun 12, 2021
1 parent cf5dbcd commit 1a2b6ea
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl Delay {
}

/// Delay using the Cortex-M systick for a certain duration, µs.
#[inline]
pub fn delay_us(&mut self, us: u32) {
let ticks = (us as u64) * (self.ahb_frequency as u64) / 1_000_000;

Expand Down Expand Up @@ -57,14 +58,8 @@ impl Delay {
}

/// Delay using the Cortex-M systick for a certain duration, ms.
pub fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1_000);
}
}

impl DelayMs<u32> for Delay {
#[inline]
fn delay_ms(&mut self, mut ms: u32) {
pub fn delay_ms(&mut self, mut ms: u32) {
// 4294967 is the highest u32 value which you can multiply by 1000 without overflow
while ms > 4294967 {
Delay::delay_us(self, 4294967000u32);
Expand All @@ -74,6 +69,13 @@ impl DelayMs<u32> for Delay {
}
}

impl DelayMs<u32> for Delay {
#[inline]
fn delay_ms(&mut self, ms: u32) {
Delay::delay_ms(self, ms);
}
}

// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
impl DelayMs<i32> for Delay {
#[inline(always)]
Expand Down

0 comments on commit 1a2b6ea

Please sign in to comment.