Update embedded-(hal,io,nal).

This commit is contained in:
Dario Nieuwenhuis
2023-11-29 16:37:07 +01:00
parent 1b9925e3da
commit 4634316749
36 changed files with 134 additions and 95 deletions

View File

@ -22,8 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 0.1.3 - 2023-08-28
- Update `embedded-hal-async` to `1.0.0-rc.1`
- Update `embedded-hal v1` to `1.0.0-rc.1`
- Update `embedded-hal-async` to `1.0.0-rc.2`
- Update `embedded-hal v1` to `1.0.0-rc.2`
## 0.1.2 - 2023-07-05

View File

@ -242,8 +242,8 @@ defmt = { version = "0.3", optional = true }
log = { version = "0.4.14", optional = true }
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6" }
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-rc.1", optional = true}
embedded-hal-async = { version = "=1.0.0-rc.1", optional = true}
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-rc.2", optional = true}
embedded-hal-async = { version = "=1.0.0-rc.2", optional = true}
futures-util = { version = "0.3.17", default-features = false }
critical-section = "1.1"

View File

@ -18,7 +18,11 @@ pub struct Delay;
mod eh1 {
use super::*;
impl embedded_hal_1::delay::DelayUs for Delay {
impl embedded_hal_1::delay::DelayNs for Delay {
fn delay_ns(&mut self, ns: u32) {
block_for(Duration::from_nanos(ns as u64))
}
fn delay_us(&mut self, us: u32) {
block_for(Duration::from_micros(us as u64))
}
@ -34,13 +38,17 @@ mod eha {
use super::*;
use crate::Timer;
impl embedded_hal_async::delay::DelayUs for Delay {
async fn delay_us(&mut self, micros: u32) {
Timer::after_micros(micros as _).await
impl embedded_hal_async::delay::DelayNs for Delay {
async fn delay_ns(&mut self, ns: u32) {
Timer::after_nanos(ns as _).await
}
async fn delay_ms(&mut self, millis: u32) {
Timer::after_millis(millis as _).await
async fn delay_us(&mut self, us: u32) {
Timer::after_micros(us as _).await
}
async fn delay_ms(&mut self, ms: u32) {
Timer::after_millis(ms as _).await
}
}
}

View File

@ -2,6 +2,7 @@ use core::fmt;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use super::{GCD_1K, GCD_1M, TICK_HZ};
use crate::GCD_1G;
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@ -61,6 +62,14 @@ impl Duration {
}
}
/// Creates a duration from the specified number of nanoseconds, rounding up.
/// NOTE: Delays this small may be inaccurate.
pub const fn from_nanos(micros: u64) -> Duration {
Duration {
ticks: div_ceil(micros * (TICK_HZ / GCD_1G), 1_000_000_000 / GCD_1G),
}
}
/// Creates a duration from the specified number of seconds, rounding down.
pub const fn from_secs_floor(secs: u64) -> Duration {
Duration { ticks: secs * TICK_HZ }

View File

@ -52,6 +52,7 @@ const fn gcd(a: u64, b: u64) -> u64 {
pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000);
pub(crate) const GCD_1M: u64 = gcd(TICK_HZ, 1_000_000);
pub(crate) const GCD_1G: u64 = gcd(TICK_HZ, 1_000_000_000);
#[cfg(feature = "defmt-timestamp-uptime")]
defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() }

View File

@ -74,6 +74,15 @@ impl Timer {
Self::after(Duration::from_ticks(ticks))
}
/// Expire after the specified number of nanoseconds.
///
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_nanos())`.
/// For more details, refer to [`Timer::after()`] and [`Duration::from_nanos()`].
#[inline]
pub fn after_nanos(nanos: u64) -> Self {
Self::after(Duration::from_nanos(nanos))
}
/// Expire after the specified number of microseconds.
///
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_micros())`.