diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-01-27 00:08:02 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-01-27 00:08:02 +0100 |
| commit | 0719b05d63a1d80d3b8ea39a411545a6e8e22ec2 (patch) | |
| tree | 9abf789ef5213c191433466b0b201edde967eaed /embassy-traits/src | |
| parent | d76cd5ceaf5140c48ef97180beae156c0c0e07c8 (diff) | |
traits: migrate Delay to embedded-hal 1.0+async, remove Rng and Flash.
Diffstat (limited to 'embassy-traits/src')
| -rw-r--r-- | embassy-traits/src/delay.rs | 13 | ||||
| -rw-r--r-- | embassy-traits/src/flash.rs | 57 | ||||
| -rw-r--r-- | embassy-traits/src/lib.rs | 3 | ||||
| -rw-r--r-- | embassy-traits/src/rng.rs | 75 |
4 files changed, 0 insertions, 148 deletions
diff --git a/embassy-traits/src/delay.rs b/embassy-traits/src/delay.rs deleted file mode 100644 index c4ef155ee..000000000 --- a/embassy-traits/src/delay.rs +++ /dev/null | |||
| @@ -1,13 +0,0 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | |||
| 3 | pub trait Delay { | ||
| 4 | type DelayFuture<'a>: Future<Output = ()> + 'a | ||
| 5 | where | ||
| 6 | Self: 'a; | ||
| 7 | |||
| 8 | /// Future that completes after now + millis | ||
| 9 | fn delay_ms(&mut self, millis: u64) -> Self::DelayFuture<'_>; | ||
| 10 | |||
| 11 | /// Future that completes after now + micros | ||
| 12 | fn delay_us(&mut self, micros: u64) -> Self::DelayFuture<'_>; | ||
| 13 | } | ||
diff --git a/embassy-traits/src/flash.rs b/embassy-traits/src/flash.rs deleted file mode 100644 index 94e11dbc5..000000000 --- a/embassy-traits/src/flash.rs +++ /dev/null | |||
| @@ -1,57 +0,0 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | |||
| 3 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
| 4 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 5 | #[non_exhaustive] | ||
| 6 | pub enum Error { | ||
| 7 | Failed, | ||
| 8 | AddressMisaligned, | ||
| 9 | BufferMisaligned, | ||
| 10 | } | ||
| 11 | |||
| 12 | pub trait Flash { | ||
| 13 | type ReadFuture<'a>: Future<Output = Result<(), Error>> | ||
| 14 | where | ||
| 15 | Self: 'a; | ||
| 16 | |||
| 17 | type WriteFuture<'a>: Future<Output = Result<(), Error>> | ||
| 18 | where | ||
| 19 | Self: 'a; | ||
| 20 | |||
| 21 | type ErasePageFuture<'a>: Future<Output = Result<(), Error>> | ||
| 22 | where | ||
| 23 | Self: 'a; | ||
| 24 | |||
| 25 | /// Reads data from the flash device. | ||
| 26 | /// | ||
| 27 | /// address must be a multiple of self.read_size(). | ||
| 28 | /// buf.len() must be a multiple of self.read_size(). | ||
| 29 | fn read<'a>(&'a mut self, address: usize, buf: &'a mut [u8]) -> Self::ReadFuture<'a>; | ||
| 30 | |||
| 31 | /// Writes data to the flash device. | ||
| 32 | /// | ||
| 33 | /// address must be a multiple of self.write_size(). | ||
| 34 | /// buf.len() must be a multiple of self.write_size(). | ||
| 35 | fn write<'a>(&'a mut self, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>; | ||
| 36 | |||
| 37 | /// Erases a single page from the flash device. | ||
| 38 | /// | ||
| 39 | /// address must be a multiple of self.erase_size(). | ||
| 40 | fn erase(&mut self, address: usize) -> Self::ErasePageFuture<'_>; | ||
| 41 | |||
| 42 | /// Returns the total size, in bytes. | ||
| 43 | /// This is not guaranteed to be a power of 2. | ||
| 44 | fn size(&self) -> usize; | ||
| 45 | |||
| 46 | /// Returns the read size in bytes. | ||
| 47 | /// This is guaranteed to be a power of 2. | ||
| 48 | fn read_size(&self) -> usize; | ||
| 49 | |||
| 50 | /// Returns the write size in bytes. | ||
| 51 | /// This is guaranteed to be a power of 2. | ||
| 52 | fn write_size(&self) -> usize; | ||
| 53 | |||
| 54 | /// Returns the erase size in bytes. | ||
| 55 | /// This is guaranteed to be a power of 2. | ||
| 56 | fn erase_size(&self) -> usize; | ||
| 57 | } | ||
diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs index a41d0106f..9c5c367a8 100644 --- a/embassy-traits/src/lib.rs +++ b/embassy-traits/src/lib.rs | |||
| @@ -3,6 +3,3 @@ | |||
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | pub mod adapter; | 5 | pub mod adapter; |
| 6 | pub mod delay; | ||
| 7 | pub mod flash; | ||
| 8 | pub mod rng; | ||
diff --git a/embassy-traits/src/rng.rs b/embassy-traits/src/rng.rs deleted file mode 100644 index ec97406b0..000000000 --- a/embassy-traits/src/rng.rs +++ /dev/null | |||
| @@ -1,75 +0,0 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | |||
| 3 | /// Random-number Generator | ||
| 4 | pub trait Rng { | ||
| 5 | type Error; | ||
| 6 | |||
| 7 | type RngFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a | ||
| 8 | where | ||
| 9 | Self: 'a; | ||
| 10 | |||
| 11 | /// Completely fill the provided buffer with random bytes. | ||
| 12 | /// | ||
| 13 | /// May result in delays if entropy is exhausted prior to completely | ||
| 14 | /// filling the buffer. Upon completion, the buffer will be completely | ||
| 15 | /// filled or an error will have been reported. | ||
| 16 | fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>; | ||
| 17 | } | ||
| 18 | |||
| 19 | pub struct Random<T: Rng> { | ||
| 20 | rng: T, | ||
| 21 | } | ||
| 22 | |||
| 23 | impl<T: Rng> Random<T> { | ||
| 24 | pub fn new(rng: T) -> Self { | ||
| 25 | Self { rng } | ||
| 26 | } | ||
| 27 | |||
| 28 | pub async fn next_u8(&mut self, range: u8) -> Result<u8, T::Error> { | ||
| 29 | // Lemire's method | ||
| 30 | let t = (-(range as i8) % (range as i8)) as u8; | ||
| 31 | loop { | ||
| 32 | let mut buf = [0; 1]; | ||
| 33 | self.rng.fill_bytes(&mut buf).await?; | ||
| 34 | let x = u8::from_le_bytes(buf); | ||
| 35 | let m = x as u16 * range as u16; | ||
| 36 | let l = m as u8; | ||
| 37 | if l < t { | ||
| 38 | continue; | ||
| 39 | } | ||
| 40 | return Ok((m >> 8) as u8); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | pub async fn next_u16(&mut self, range: u16) -> Result<u16, T::Error> { | ||
| 45 | // Lemire's method | ||
| 46 | let t = (-(range as i16) % (range as i16)) as u16; | ||
| 47 | loop { | ||
| 48 | let mut buf = [0; 2]; | ||
| 49 | self.rng.fill_bytes(&mut buf).await?; | ||
| 50 | let x = u16::from_le_bytes(buf); | ||
| 51 | let m = x as u32 * range as u32; | ||
| 52 | let l = m as u16; | ||
| 53 | if l < t { | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | return Ok((m >> 16) as u16); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | pub async fn next_u32(&mut self, range: u32) -> Result<u32, T::Error> { | ||
| 61 | // Lemire's method | ||
| 62 | let t = (-(range as i32) % (range as i32)) as u32; | ||
| 63 | loop { | ||
| 64 | let mut buf = [0; 4]; | ||
| 65 | self.rng.fill_bytes(&mut buf).await?; | ||
| 66 | let x = u32::from_le_bytes(buf); | ||
| 67 | let m = x as u64 * range as u64; | ||
| 68 | let l = m as u32; | ||
| 69 | if l < t { | ||
| 70 | continue; | ||
| 71 | } | ||
| 72 | return Ok((m >> 32) as u32); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
