traits: migrate Delay to embedded-hal 1.0+async, remove Rng and Flash.

This commit is contained in:
Dario Nieuwenhuis
2022-01-27 00:08:02 +01:00
parent d76cd5ceaf
commit 0719b05d63
32 changed files with 145 additions and 304 deletions

View File

@ -7,9 +7,12 @@ resolver = "2"
[features]
default = []
std = ["futures/std", "embassy-traits/std", "time", "time-tick-1mhz", "embassy-macros/std"]
std = ["futures/std", "time", "time-tick-1mhz", "embassy-macros/std"]
wasm = ["wasm-bindgen", "js-sys", "embassy-macros/wasm", "wasm-timer", "time", "time-tick-1mhz"]
# Implement embedded-hal 1.0 alpha and embedded-hal-async traits.
unstable-traits = ["embedded-hal-1", "embedded-hal-async"]
# Enable `embassy::time` module.
# NOTE: This feature is only intended to be enabled by crates providing the time driver implementation.
# Enabling it directly without supplying a time driver will fail to link.
@ -29,13 +32,15 @@ executor-agnostic = []
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-alpha.6", git = "https://github.com/embassy-rs/embedded-hal", branch = "embassy", optional = true}
embedded-hal-async = { version = "0.0.1", git = "https://github.com/embassy-rs/embedded-hal", branch = "embassy", optional = true}
futures = { version = "0.3.17", default-features = false, features = [ "cfg-target-has-atomic", "unstable" ] }
pin-project = { version = "1.0.8", default-features = false }
embassy-macros = { version = "0.1.0", path = "../embassy-macros"}
embassy-traits = { version = "0.1.0", path = "../embassy-traits"}
atomic-polyfill = "0.1.5"
critical-section = "0.2.5"
embedded-hal = "0.2.6"
heapless = "0.7.5"
cfg-if = "1.0.0"

View File

@ -21,7 +21,6 @@ pub mod time;
pub mod util;
pub use embassy_macros::*;
pub use embassy_traits as traits;
#[doc(hidden)]
/// Implementation details for embassy macros. DO NOT USE.

View File

@ -1,6 +1,10 @@
use core::future::Future;
use super::{Duration, Instant};
use super::{Duration, Instant, Timer};
/// Blocks for at least `duration`.
pub fn block_for(duration: Duration) {
let expires_at = Instant::now() + duration;
while Instant::now() < expires_at {}
}
/// Type implementing async delays and blocking `embedded-hal` delays.
///
@ -10,55 +14,86 @@ use super::{Duration, Instant, Timer};
/// active driver.
pub struct Delay;
impl crate::traits::delay::Delay for Delay {
type DelayFuture<'a> = impl Future<Output = ()> + 'a;
#[cfg(feature = "unstable-traits")]
mod eh1 {
use core::future::Future;
use futures::FutureExt;
fn delay_ms(&mut self, millis: u64) -> Self::DelayFuture<'_> {
Timer::after(Duration::from_millis(millis))
use super::*;
use crate::time::Timer;
impl embedded_hal_1::delay::blocking::DelayUs for Delay {
type Error = core::convert::Infallible;
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
Ok(block_for(Duration::from_micros(us as u64)))
}
fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
Ok(block_for(Duration::from_millis(ms as u64)))
}
}
fn delay_us(&mut self, micros: u64) -> Self::DelayFuture<'_> {
Timer::after(Duration::from_micros(micros))
impl embedded_hal_async::delay::DelayUs for Delay {
type Error = core::convert::Infallible;
type DelayUsFuture<'a>
where
Self: 'a,
= impl Future<Output = Result<(), Self::Error>> + 'a;
fn delay_us(&mut self, micros: u32) -> Self::DelayUsFuture<'_> {
Timer::after(Duration::from_micros(micros as _)).map(Ok)
}
type DelayMsFuture<'a>
where
Self: 'a,
= impl Future<Output = Result<(), Self::Error>> + 'a;
fn delay_ms(&mut self, millis: u32) -> Self::DelayMsFuture<'_> {
Timer::after(Duration::from_millis(millis as _)).map(Ok)
}
}
}
impl embedded_hal::blocking::delay::DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
block_for(Duration::from_millis(ms as u64))
mod eh02 {
use super::*;
use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};
impl DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
block_for(Duration::from_millis(ms as u64))
}
}
impl DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
block_for(Duration::from_millis(ms as u64))
}
}
impl DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
block_for(Duration::from_millis(ms as u64))
}
}
impl DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
block_for(Duration::from_micros(us as u64))
}
}
impl DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
block_for(Duration::from_micros(us as u64))
}
}
impl DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
block_for(Duration::from_micros(us as u64))
}
}
}
impl embedded_hal::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
block_for(Duration::from_micros(us as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
block_for(Duration::from_micros(us as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
block_for(Duration::from_micros(us as u64))
}
}
/// Blocks for at least `duration`.
pub fn block_for(duration: Duration) {
let expires_at = Instant::now() + duration;
while Instant::now() < expires_at {}
}