diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-09-01 22:53:10 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-09-01 22:53:10 +0200 |
| commit | bc68657c2397008087bdd6afcd2088ee649a0058 (patch) | |
| tree | 9681cd2613ea69736c2ffd20a6ecd74336ecfe2a | |
| parent | ea688afe9b32cfdf10e91805d8c1e5668e0e85cd (diff) | |
| parent | aaa4a477d56bdcaeff36723ce7fdaba0dce054d1 (diff) | |
Merge pull request #379 from bobmcwhirter/random_range
Random range
| -rw-r--r-- | embassy-stm32/src/rng.rs | 10 | ||||
| -rw-r--r-- | embassy-traits/src/rng.rs | 63 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/eth.rs | 6 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/rng.rs | 35 |
4 files changed, 104 insertions, 10 deletions
diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 77e408d3d..0afba3ba7 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs | |||
| @@ -19,11 +19,11 @@ pub enum Error { | |||
| 19 | ClockError, | 19 | ClockError, |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | pub struct Random<T: Instance> { | 22 | pub struct Rng<T: Instance> { |
| 23 | _inner: T, | 23 | _inner: T, |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | impl<T: Instance> Random<T> { | 26 | impl<T: Instance> Rng<T> { |
| 27 | pub fn new(inner: impl Unborrow<Target = T>) -> Self { | 27 | pub fn new(inner: impl Unborrow<Target = T>) -> Self { |
| 28 | T::enable(); | 28 | T::enable(); |
| 29 | T::reset(); | 29 | T::reset(); |
| @@ -49,7 +49,7 @@ impl<T: Instance> Random<T> { | |||
| 49 | } | 49 | } |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | impl<T: Instance> RngCore for Random<T> { | 52 | impl<T: Instance> RngCore for Rng<T> { |
| 53 | fn next_u32(&mut self) -> u32 { | 53 | fn next_u32(&mut self) -> u32 { |
| 54 | loop { | 54 | loop { |
| 55 | let bits = unsafe { T::regs().sr().read() }; | 55 | let bits = unsafe { T::regs().sr().read() }; |
| @@ -80,9 +80,9 @@ impl<T: Instance> RngCore for Random<T> { | |||
| 80 | } | 80 | } |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | impl<T: Instance> CryptoRng for Random<T> {} | 83 | impl<T: Instance> CryptoRng for Rng<T> {} |
| 84 | 84 | ||
| 85 | impl<T: Instance> traits::rng::Rng for Random<T> { | 85 | impl<T: Instance> traits::rng::Rng for Rng<T> { |
| 86 | type Error = Error; | 86 | type Error = Error; |
| 87 | #[rustfmt::skip] | 87 | #[rustfmt::skip] |
| 88 | type RngFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + 'a; | 88 | type RngFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + 'a; |
diff --git a/embassy-traits/src/rng.rs b/embassy-traits/src/rng.rs index ddc4c20e0..3cc4b2a0b 100644 --- a/embassy-traits/src/rng.rs +++ b/embassy-traits/src/rng.rs | |||
| @@ -4,9 +4,10 @@ use core::future::Future; | |||
| 4 | pub trait Rng { | 4 | pub trait Rng { |
| 5 | type Error; | 5 | type Error; |
| 6 | 6 | ||
| 7 | type RngFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a | 7 | #[rustfmt::skip] |
| 8 | type RngFuture<'a>: Future<Output = Result<(), Self::Error> > + 'a | ||
| 8 | where | 9 | where |
| 9 | Self: 'a; | 10 | Self: 'a; |
| 10 | 11 | ||
| 11 | /// Completely fill the provided buffer with random bytes. | 12 | /// Completely fill the provided buffer with random bytes. |
| 12 | /// | 13 | /// |
| @@ -15,3 +16,61 @@ pub trait Rng { | |||
| 15 | /// filled or an error will have been reported. | 16 | /// filled or an error will have been reported. |
| 16 | fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>; | 17 | fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>; |
| 17 | } | 18 | } |
| 19 | |||
| 20 | pub struct Random<T: Rng> { | ||
| 21 | rng: T, | ||
| 22 | } | ||
| 23 | |||
| 24 | impl<T: Rng> Random<T> { | ||
| 25 | pub fn new(rng: T) -> Self { | ||
| 26 | Self { rng } | ||
| 27 | } | ||
| 28 | |||
| 29 | pub async fn next_u8<'a>(&'a mut self, range: u8) -> Result<u8, T::Error> { | ||
| 30 | // Lemire's method | ||
| 31 | let t = (-(range as i8) % (range as i8)) as u8; | ||
| 32 | loop { | ||
| 33 | let mut buf = [0; 1]; | ||
| 34 | self.rng.fill_bytes(&mut buf).await?; | ||
| 35 | let x = u8::from_le_bytes(buf); | ||
| 36 | let m = x as u16 * range as u16; | ||
| 37 | let l = m as u8; | ||
| 38 | if l < t { | ||
| 39 | continue; | ||
| 40 | } | ||
| 41 | return Ok((m >> 8) as u8); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | pub async fn next_u16<'a>(&'a mut self, range: u16) -> Result<u16, T::Error> { | ||
| 46 | // Lemire's method | ||
| 47 | let t = (-(range as i16) % (range as i16)) as u16; | ||
| 48 | loop { | ||
| 49 | let mut buf = [0; 2]; | ||
| 50 | self.rng.fill_bytes(&mut buf).await?; | ||
| 51 | let x = u16::from_le_bytes(buf); | ||
| 52 | let m = x as u32 * range as u32; | ||
| 53 | let l = m as u16; | ||
| 54 | if l < t { | ||
| 55 | continue; | ||
| 56 | } | ||
| 57 | return Ok((m >> 16) as u16); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub async fn next_u32<'a>(&'a mut self, range: u32) -> Result<u32, T::Error> { | ||
| 62 | // Lemire's method | ||
| 63 | let t = (-(range as i32) % (range as i32)) as u32; | ||
| 64 | loop { | ||
| 65 | let mut buf = [0; 4]; | ||
| 66 | self.rng.fill_bytes(&mut buf).await?; | ||
| 67 | let x = u32::from_le_bytes(buf); | ||
| 68 | let m = x as u64 * range as u64; | ||
| 69 | let l = m as u32; | ||
| 70 | if l < t { | ||
| 71 | continue; | ||
| 72 | } | ||
| 73 | return Ok((m >> 32) as u32); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs index 4a9f261c2..df4931455 100644 --- a/examples/stm32h7/src/bin/eth.rs +++ b/examples/stm32h7/src/bin/eth.rs | |||
| @@ -21,7 +21,7 @@ use embassy_net::{ | |||
| 21 | }; | 21 | }; |
| 22 | use embassy_stm32::eth::lan8742a::LAN8742A; | 22 | use embassy_stm32::eth::lan8742a::LAN8742A; |
| 23 | use embassy_stm32::eth::{Ethernet, State}; | 23 | use embassy_stm32::eth::{Ethernet, State}; |
| 24 | use embassy_stm32::rng::Random; | 24 | use embassy_stm32::rng::Rng; |
| 25 | use embassy_stm32::{interrupt, peripherals}; | 25 | use embassy_stm32::{interrupt, peripherals}; |
| 26 | use heapless::Vec; | 26 | use heapless::Vec; |
| 27 | use panic_probe as _; | 27 | use panic_probe as _; |
| @@ -81,7 +81,7 @@ fn _embassy_rand(buf: &mut [u8]) { | |||
| 81 | }); | 81 | }); |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | static mut RNG_INST: Option<Random<RNG>> = None; | 84 | static mut RNG_INST: Option<Rng<RNG>> = None; |
| 85 | 85 | ||
| 86 | static EXECUTOR: Forever<Executor> = Forever::new(); | 86 | static EXECUTOR: Forever<Executor> = Forever::new(); |
| 87 | static STATE: Forever<State<'static, 4, 4>> = Forever::new(); | 87 | static STATE: Forever<State<'static, 4, 4>> = Forever::new(); |
| @@ -97,7 +97,7 @@ fn main() -> ! { | |||
| 97 | 97 | ||
| 98 | let p = embassy_stm32::init(config()); | 98 | let p = embassy_stm32::init(config()); |
| 99 | 99 | ||
| 100 | let rng = Random::new(p.RNG); | 100 | let rng = Rng::new(p.RNG); |
| 101 | unsafe { | 101 | unsafe { |
| 102 | RNG_INST.replace(rng); | 102 | RNG_INST.replace(rng); |
| 103 | } | 103 | } |
diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs new file mode 100644 index 000000000..2dc8268b5 --- /dev/null +++ b/examples/stm32h7/src/bin/rng.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | #[path = "../example_common.rs"] | ||
| 8 | mod example_common; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy::time::{Duration, Timer}; | ||
| 11 | use embassy::traits::rng::Random; | ||
| 12 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 13 | use embassy_stm32::rng::Rng; | ||
| 14 | use embassy_stm32::Peripherals; | ||
| 15 | use embedded_hal::digital::v2::OutputPin; | ||
| 16 | use example_common::*; | ||
| 17 | |||
| 18 | #[embassy::main] | ||
| 19 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let mut led = Output::new(p.PB14, Level::High, Speed::Low); | ||
| 23 | |||
| 24 | let mut rng = Random::new(Rng::new(p.RNG)); | ||
| 25 | |||
| 26 | loop { | ||
| 27 | info!("high {}", unwrap!(rng.next_u8(16).await)); | ||
| 28 | unwrap!(led.set_high()); | ||
| 29 | Timer::after(Duration::from_millis(500)).await; | ||
| 30 | |||
| 31 | info!("low {}", unwrap!(rng.next_u8(16).await)); | ||
| 32 | unwrap!(led.set_low()); | ||
| 33 | Timer::after(Duration::from_millis(500)).await; | ||
| 34 | } | ||
| 35 | } | ||
