Skip to content

Commit

Permalink
ztimer: Implement embedded-hal 1.0 trait
Browse files Browse the repository at this point in the history
This is not a high quality implementation, but it satisfies the trait
requirements.
  • Loading branch information
chrysn committed Jan 26, 2024
1 parent 0849036 commit 641df2e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ztimer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,29 @@ impl embedded_hal_0_2::blocking::delay::DelayUs<u32> for Clock<1000000> {
}
}

impl<const F: u32> embedded_hal::delay::DelayNs for Clock<F> {
// 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
Expand Down

0 comments on commit 641df2e

Please sign in to comment.