From d6bf28eb15e7d7d8f0e21c7adad093667c2593b8 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Wed, 29 Nov 2023 20:53:23 +0100 Subject: [PATCH] Fixes for updated embedded hal --- rtic-monotonics/src/imxrt.rs | 14 ++++++++++++-- rtic-monotonics/src/nrf/rtc.rs | 14 ++++++++++++-- rtic-monotonics/src/nrf/timer.rs | 14 ++++++++++++-- rtic-monotonics/src/rp2040.rs | 13 +++++++++++-- rtic-monotonics/src/stm32.rs | 14 ++++++++++++-- rtic-monotonics/src/systick.rs | 17 +++++++++++++++-- 6 files changed, 74 insertions(+), 12 deletions(-) diff --git a/rtic-monotonics/src/imxrt.rs b/rtic-monotonics/src/imxrt.rs index 39448291a0db..41d1fd2fbf95 100644 --- a/rtic-monotonics/src/imxrt.rs +++ b/rtic-monotonics/src/imxrt.rs @@ -217,7 +217,12 @@ macro_rules! make_timer { } #[cfg(feature = "embedded-hal-async")] - impl embedded_hal_async::delay::DelayUs for $mono_name { + impl embedded_hal_async::delay::DelayNs for $mono_name { + #[inline] + async fn delay_ns(&mut self, ns: u32) { + Self::delay((ns as u64).nanos()).await; + } + #[inline] async fn delay_us(&mut self, us: u32) { Self::delay((us as u64).micros()).await; @@ -229,7 +234,12 @@ macro_rules! make_timer { } } - impl embedded_hal::delay::DelayUs for $mono_name { + impl embedded_hal::delay::DelayNs for $mono_name { + fn delay_ns(&mut self, ns: u32) { + let done = Self::now() + u64::from(ns).nanos(); + while Self::now() < done {} + } + fn delay_us(&mut self, us: u32) { let done = Self::now() + (us as u64).micros(); while Self::now() < done {} diff --git a/rtic-monotonics/src/nrf/rtc.rs b/rtic-monotonics/src/nrf/rtc.rs index 94913071d8ef..e42dd1bd6447 100644 --- a/rtic-monotonics/src/nrf/rtc.rs +++ b/rtic-monotonics/src/nrf/rtc.rs @@ -168,7 +168,12 @@ macro_rules! make_rtc { } #[cfg(feature = "embedded-hal-async")] - impl embedded_hal_async::delay::DelayUs for $mono_name { + impl embedded_hal_async::delay::DelayNs for $mono_name { + #[inline] + async fn delay_ns(&mut self, ns: u32) { + Self::delay((ns as u64).nanos()).await; + } + #[inline] async fn delay_us(&mut self, us: u32) { Self::delay((us as u64).micros()).await; @@ -180,7 +185,12 @@ macro_rules! make_rtc { } } - impl embedded_hal::delay::DelayUs for $mono_name { + impl embedded_hal::delay::DelayNs for $mono_name { + fn delay_ns(&mut self, ns: u32) { + let done = Self::now() + u64::from(ns).nanos(); + while Self::now() < done {} + } + fn delay_us(&mut self, us: u32) { let done = Self::now() + u64::from(us).micros(); while Self::now() < done {} diff --git a/rtic-monotonics/src/nrf/timer.rs b/rtic-monotonics/src/nrf/timer.rs index 589cc6fdfbae..94dbc2929afc 100644 --- a/rtic-monotonics/src/nrf/timer.rs +++ b/rtic-monotonics/src/nrf/timer.rs @@ -204,7 +204,12 @@ macro_rules! make_timer { } #[cfg(feature = "embedded-hal-async")] - impl embedded_hal_async::delay::DelayUs for $mono_name { + impl embedded_hal_async::delay::DelayNs for $mono_name { + #[inline] + async fn delay_ns(&mut self, ns: u32) { + Self::delay((ns as u64).nanos()).await; + } + #[inline] async fn delay_us(&mut self, us: u32) { Self::delay((us as u64).micros()).await; @@ -216,7 +221,12 @@ macro_rules! make_timer { } } - impl embedded_hal::delay::DelayUs for $mono_name { + impl embedded_hal::delay::DelayNs for $mono_name { + fn delay_ns(&mut self, ns: u32) { + let done = Self::now() + u64::from(ns).nanos(); + while Self::now() < done {} + } + fn delay_us(&mut self, us: u32) { let done = Self::now() + (us as u64).micros(); while Self::now() < done {} diff --git a/rtic-monotonics/src/rp2040.rs b/rtic-monotonics/src/rp2040.rs index 130c7d3e60ba..2062558f16cb 100644 --- a/rtic-monotonics/src/rp2040.rs +++ b/rtic-monotonics/src/rp2040.rs @@ -152,7 +152,11 @@ impl Monotonic for Timer { } #[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::delay::DelayUs for Timer { +impl embedded_hal_async::delay::DelayNs for Timer { + async fn delay_ns(&mut self, ns: u32) { + Self::delay((ns as u64).nanos()).await; + } + async fn delay_us(&mut self, us: u32) { Self::delay((us as u64).micros()).await; } @@ -162,7 +166,12 @@ impl embedded_hal_async::delay::DelayUs for Timer { } } -impl embedded_hal::delay::DelayUs for Timer { +impl embedded_hal::delay::DelayNs for Timer { + fn delay_ns(&mut self, ns: u32) { + let done = Self::now() + u64::from(ns).nanos(); + while Self::now() < done {} + } + fn delay_us(&mut self, us: u32) { let done = Self::now() + u64::from(us).micros(); while Self::now() < done {} diff --git a/rtic-monotonics/src/stm32.rs b/rtic-monotonics/src/stm32.rs index 2676a3462786..bec84a304dc1 100644 --- a/rtic-monotonics/src/stm32.rs +++ b/rtic-monotonics/src/stm32.rs @@ -219,7 +219,12 @@ macro_rules! make_timer { } #[cfg(feature = "embedded-hal-async")] - impl embedded_hal_async::delay::DelayUs for $mono_name { + impl embedded_hal_async::delay::DelayNs for $mono_name { + #[inline] + async fn delay_ns(&mut self, ns: u32) { + Self::delay((ns as u64).nanos()).await; + } + #[inline] async fn delay_us(&mut self, us: u32) { Self::delay((us as u64).micros()).await; @@ -231,7 +236,12 @@ macro_rules! make_timer { } } - impl embedded_hal::delay::DelayUs for $mono_name { + impl embedded_hal::delay::DelayNs for $mono_name { + fn delay_ns(&mut self, ns: u32) { + let done = Self::now() + u64::from(ns).nanos(); + while Self::now() < done {} + } + fn delay_us(&mut self, us: u32) { let done = Self::now() + (us as u64).micros(); while Self::now() < done {} diff --git a/rtic-monotonics/src/systick.rs b/rtic-monotonics/src/systick.rs index 265ca9a0ecf4..0eb24468e2df 100644 --- a/rtic-monotonics/src/systick.rs +++ b/rtic-monotonics/src/systick.rs @@ -189,7 +189,13 @@ impl Monotonic for Systick { } #[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::delay::DelayUs for Systick { +impl embedded_hal_async::delay::DelayNs for Systick { + async fn delay_ns(&mut self, ns: u32) { + #[cfg(feature = "systick-64bit")] + let ns = u64::from(ns); + Self::delay(ns.nanos()).await; + } + async fn delay_us(&mut self, us: u32) { #[cfg(feature = "systick-64bit")] let us = u64::from(us); @@ -203,7 +209,14 @@ impl embedded_hal_async::delay::DelayUs for Systick { } } -impl embedded_hal::delay::DelayUs for Systick { +impl embedded_hal::delay::DelayNs for Systick { + fn delay_ns(&mut self, ns: u32) { + #[cfg(feature = "systick-64bit")] + let ns = u64::from(ns); + let done = Self::now() + ns.nanos(); + while Self::now() < done {} + } + fn delay_us(&mut self, us: u32) { #[cfg(feature = "systick-64bit")] let us = u64::from(us);