Skip to content

Commit

Permalink
rtic-monotonics: Implement blocking DelayUs from embedded-hal 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nilfit committed Sep 14, 2023
1 parent 54aec9b commit e173c26
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions rtic-monotonics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustdoc-flags = ["--cfg", "docsrs"]

[dependencies]
rtic-time = { version = "1.0.0", path = "../rtic-time" }
embedded-hal = { version = "1.0.0-rc.1" }
embedded-hal-async = { version = "1.0.0-rc.1", optional = true }
fugit = { version = "0.3.6" }
atomic-polyfill = "1"
Expand Down
7 changes: 7 additions & 0 deletions rtic-monotonics/src/nrf/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ macro_rules! make_rtc {
}
}

impl embedded_hal::delay::DelayUs for $mono_name {
fn delay_us(&mut self, us: u32) {
let done = Self::now() + u64::from(us).micros();
while Self::now() < done {}
}
}

impl Monotonic for $mono_name {
const ZERO: Self::Instant = Self::Instant::from_ticks(0);

Expand Down
7 changes: 7 additions & 0 deletions rtic-monotonics/src/nrf/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ macro_rules! make_timer {
}
}

impl embedded_hal::delay::DelayUs for $mono_name {
fn delay_us(&mut self, us: u32) {
let done = Self::now() + (us as u64).micros();
while Self::now() < done {}
}
}

impl Monotonic for $mono_name {
const ZERO: Self::Instant = Self::Instant::from_ticks(0);

Expand Down
7 changes: 7 additions & 0 deletions rtic-monotonics/src/rp2040.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ impl embedded_hal_async::delay::DelayUs for Timer {
}
}

impl embedded_hal::delay::DelayUs for Timer {
fn delay_us(&mut self, us: u32) {
let done = Self::now() + u64::from(us).micros();
while Self::now() < done {}
}
}

/// Register the Timer interrupt for the monotonic.
#[macro_export]
macro_rules! create_rp2040_monotonic_token {
Expand Down
7 changes: 7 additions & 0 deletions rtic-monotonics/src/stm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ macro_rules! make_timer {
}
}

impl embedded_hal::delay::DelayUs for $mono_name {
fn delay_us(&mut self, us: u32) {
let done = Self::now() + (us as u64).micros();
while Self::now() < done {}
}
}

impl Monotonic for $mono_name {
type Instant = fugit::TimerInstantU64<TIMER_HZ>;
type Duration = fugit::TimerDurationU64<TIMER_HZ>;
Expand Down
9 changes: 9 additions & 0 deletions rtic-monotonics/src/systick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ impl embedded_hal_async::delay::DelayUs for Systick {
}
}

impl embedded_hal::delay::DelayUs for Systick {
fn delay_us(&mut self, us: u32) {
#[cfg(feature = "systick-64bit")]
let us = u64::from(us);
let done = Self::now() + us.micros();
while Self::now() < done {}
}
}

/// Register the Systick interrupt for the monotonic.
#[macro_export]
macro_rules! create_systick_token {
Expand Down

0 comments on commit e173c26

Please sign in to comment.