Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependence on embedded-hal for Delay #344

Merged
merged 6 commits into from Jun 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/delay.rs
Expand Up @@ -29,7 +29,8 @@ impl Delay {
self.syst
}

fn _delay_us(&mut self, us: u32) {
/// Delay using the Cortex-M systick for a certain duration, µs.
pub fn delay_us(&mut self, us: u32) {
David-OConnor marked this conversation as resolved.
Show resolved Hide resolved
let ticks = (us as u64) * (self.ahb_frequency as u64) / 1_000_000;

let full_cycles = ticks >> 24;
Expand All @@ -54,17 +55,22 @@ impl Delay {

self.syst.disable_counter();
}

/// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is correct not for all argument values. Please see the delay_ms implementation below.

}
}

impl DelayMs<u32> for Delay {
#[inline]
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 {
self.delay_us(4294967000u32);
Delay::delay_us(self, 4294967000u32);
ms -= 4294967;
}
self.delay_us(ms * 1_000);
Delay::delay_us(self, ms * 1_000);
}
}

Expand All @@ -73,28 +79,28 @@ impl DelayMs<i32> for Delay {
#[inline(always)]
fn delay_ms(&mut self, ms: i32) {
assert!(ms >= 0);
self.delay_ms(ms as u32);
Delay::delay_ms(self, ms as u32);
}
}

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

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

impl DelayUs<u32> for Delay {
#[inline]
fn delay_us(&mut self, us: u32) {
self._delay_us(us);
Delay::delay_us(self, us);
}
}

Expand All @@ -103,20 +109,20 @@ impl DelayUs<i32> for Delay {
#[inline(always)]
fn delay_us(&mut self, us: i32) {
assert!(us >= 0);
self.delay_us(us as u32);
Delay::delay_us(self, us as u32);
}
}

impl DelayUs<u16> for Delay {
#[inline(always)]
fn delay_us(&mut self, us: u16) {
self.delay_us(u32::from(us))
Delay::delay_us(self, u32::from(us))
}
}

impl DelayUs<u8> for Delay {
#[inline(always)]
fn delay_us(&mut self, us: u8) {
self.delay_us(u32::from(us))
Delay::delay_us(self, u32::from(us))
}
}