diff --git a/rtic-monotonics/CHANGELOG.md b/rtic-monotonics/CHANGELOG.md index 4341658811..9f35757eab 100644 --- a/rtic-monotonics/CHANGELOG.md +++ b/rtic-monotonics/CHANGELOG.md @@ -10,6 +10,7 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top! ### Added - STM32 support. +- `embedded-hal` 1.0.0-rc.1 `DelayUs` support ## v1.1.0 - 2023-08-29 diff --git a/rtic-monotonics/Cargo.toml b/rtic-monotonics/Cargo.toml index c4396ad244..5c0e7588ed 100644 --- a/rtic-monotonics/Cargo.toml +++ b/rtic-monotonics/Cargo.toml @@ -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" diff --git a/rtic-monotonics/src/nrf/rtc.rs b/rtic-monotonics/src/nrf/rtc.rs index c74c9861c9..94913071d8 100644 --- a/rtic-monotonics/src/nrf/rtc.rs +++ b/rtic-monotonics/src/nrf/rtc.rs @@ -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); diff --git a/rtic-monotonics/src/nrf/timer.rs b/rtic-monotonics/src/nrf/timer.rs index 1b90f16fcb..bd544d0b38 100644 --- a/rtic-monotonics/src/nrf/timer.rs +++ b/rtic-monotonics/src/nrf/timer.rs @@ -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); diff --git a/rtic-monotonics/src/rp2040.rs b/rtic-monotonics/src/rp2040.rs index 61bd1ff76f..43e6770274 100644 --- a/rtic-monotonics/src/rp2040.rs +++ b/rtic-monotonics/src/rp2040.rs @@ -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 { diff --git a/rtic-monotonics/src/stm32.rs b/rtic-monotonics/src/stm32.rs index 3232f4ae04..77d36e4c40 100644 --- a/rtic-monotonics/src/stm32.rs +++ b/rtic-monotonics/src/stm32.rs @@ -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; type Duration = fugit::TimerDurationU64; diff --git a/rtic-monotonics/src/systick.rs b/rtic-monotonics/src/systick.rs index 9b8fca8ac2..fb4f8832a1 100644 --- a/rtic-monotonics/src/systick.rs +++ b/rtic-monotonics/src/systick.rs @@ -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 {