diff --git a/src/ztimer/mod.rs b/src/ztimer/mod.rs index 39f52a7..0daaa6b 100644 --- a/src/ztimer/mod.rs +++ b/src/ztimer/mod.rs @@ -209,6 +209,29 @@ impl embedded_hal_0_2::blocking::delay::DelayUs for Clock<1000000> { } } +impl embedded_hal::delay::DelayNs for Clock { + // FIXME: Provide delay_us and delay_ms, at least for the clocks where those fit, to avoid the + // loops where the provided function wakes up every 4.3s + + #[inline(always)] + fn delay_ns(&mut self, ns: u32) { + if F > NANOS_PER_SEC { + // On really fast ZTimers, we may need to loop (but let's implement this when anyone + // ever implements a faster-than-nanosecond timer) + todo!("Test for whether this needs to loop") + } else { + // No need to loop, but we need to take care not to overflow -- and we can't + // pre-calculate (F / NANOS_PER_SEC) because that's rounded to 0 + + // FIXME: There has to be a more efficient way -- for now we're relying on inlining and + // hope that constant propagation takes care of things + + let ticks = (ns as u64) * (F as u64) / (NANOS_PER_SEC as u64); + self.sleep_ticks(ticks as u32); + } + } +} + /// The error type of fallible conversions to ticks. /// /// Overflow is the only ever indicated error type; lack of accuracy in the timer does not