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

@ -8,7 +8,6 @@ edition = "2018"
std = []
[dependencies]
defmt = { version = "0.3", optional = true }
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.6", git = "https://github.com/embassy-rs/embedded-hal", branch = "embassy" }
embedded-hal-async = { version = "0.0.1", git = "https://github.com/embassy-rs/embedded-hal", branch = "embassy"}

View File

@ -1,13 +0,0 @@
use core::future::Future;
pub trait Delay {
type DelayFuture<'a>: Future<Output = ()> + 'a
where
Self: 'a;
/// Future that completes after now + millis
fn delay_ms(&mut self, millis: u64) -> Self::DelayFuture<'_>;
/// Future that completes after now + micros
fn delay_us(&mut self, micros: u64) -> Self::DelayFuture<'_>;
}

View File

@ -1,57 +0,0 @@
use core::future::Future;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum Error {
Failed,
AddressMisaligned,
BufferMisaligned,
}
pub trait Flash {
type ReadFuture<'a>: Future<Output = Result<(), Error>>
where
Self: 'a;
type WriteFuture<'a>: Future<Output = Result<(), Error>>
where
Self: 'a;
type ErasePageFuture<'a>: Future<Output = Result<(), Error>>
where
Self: 'a;
/// Reads data from the flash device.
///
/// address must be a multiple of self.read_size().
/// buf.len() must be a multiple of self.read_size().
fn read<'a>(&'a mut self, address: usize, buf: &'a mut [u8]) -> Self::ReadFuture<'a>;
/// Writes data to the flash device.
///
/// address must be a multiple of self.write_size().
/// buf.len() must be a multiple of self.write_size().
fn write<'a>(&'a mut self, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>;
/// Erases a single page from the flash device.
///
/// address must be a multiple of self.erase_size().
fn erase(&mut self, address: usize) -> Self::ErasePageFuture<'_>;
/// Returns the total size, in bytes.
/// This is not guaranteed to be a power of 2.
fn size(&self) -> usize;
/// Returns the read size in bytes.
/// This is guaranteed to be a power of 2.
fn read_size(&self) -> usize;
/// Returns the write size in bytes.
/// This is guaranteed to be a power of 2.
fn write_size(&self) -> usize;
/// Returns the erase size in bytes.
/// This is guaranteed to be a power of 2.
fn erase_size(&self) -> usize;
}

View File

@ -3,6 +3,3 @@
#![feature(type_alias_impl_trait)]
pub mod adapter;
pub mod delay;
pub mod flash;
pub mod rng;

View File

@ -1,75 +0,0 @@
use core::future::Future;
/// Random-number Generator
pub trait Rng {
type Error;
type RngFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
/// Completely fill the provided buffer with random bytes.
///
/// May result in delays if entropy is exhausted prior to completely
/// filling the buffer. Upon completion, the buffer will be completely
/// filled or an error will have been reported.
fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>;
}
pub struct Random<T: Rng> {
rng: T,
}
impl<T: Rng> Random<T> {
pub fn new(rng: T) -> Self {
Self { rng }
}
pub async fn next_u8(&mut self, range: u8) -> Result<u8, T::Error> {
// Lemire's method
let t = (-(range as i8) % (range as i8)) as u8;
loop {
let mut buf = [0; 1];
self.rng.fill_bytes(&mut buf).await?;
let x = u8::from_le_bytes(buf);
let m = x as u16 * range as u16;
let l = m as u8;
if l < t {
continue;
}
return Ok((m >> 8) as u8);
}
}
pub async fn next_u16(&mut self, range: u16) -> Result<u16, T::Error> {
// Lemire's method
let t = (-(range as i16) % (range as i16)) as u16;
loop {
let mut buf = [0; 2];
self.rng.fill_bytes(&mut buf).await?;
let x = u16::from_le_bytes(buf);
let m = x as u32 * range as u32;
let l = m as u16;
if l < t {
continue;
}
return Ok((m >> 16) as u16);
}
}
pub async fn next_u32(&mut self, range: u32) -> Result<u32, T::Error> {
// Lemire's method
let t = (-(range as i32) % (range as i32)) as u32;
loop {
let mut buf = [0; 4];
self.rng.fill_bytes(&mut buf).await?;
let x = u32::from_le_bytes(buf);
let m = x as u64 * range as u64;
let l = m as u32;
if l < t {
continue;
}
return Ok((m >> 32) as u32);
}
}
}