aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/rng.rs32
-rw-r--r--embassy-traits/src/rng.rs61
-rw-r--r--examples/stm32h7/src/bin/eth.rs6
-rw-r--r--examples/stm32h7/src/bin/rng.rs10
4 files changed, 68 insertions, 41 deletions
diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs
index 305e26771..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
22pub struct Random<T: Instance> { 22pub struct Rng<T: Instance> {
23 _inner: T, 23 _inner: T,
24} 24}
25 25
26impl<T: Instance> Random<T> { 26impl<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
52impl<T: Instance> RngCore for Random<T> { 52impl<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
83impl<T: Instance> CryptoRng for Random<T> {} 83impl<T: Instance> CryptoRng for Rng<T> {}
84 84
85impl<T: Instance> traits::rng::Rng for Random<T> { 85impl<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;
@@ -128,28 +128,6 @@ impl<T: Instance> traits::rng::Rng for Random<T> {
128 } 128 }
129} 129}
130 130
131impl<T: Instance> traits::rng::Random for Random<T> {
132 #[rustfmt::skip]
133 type NextFuture<'a> where Self: 'a = impl Future<Output=Result<u32, Self::Error>> + 'a;
134
135 fn next<'a>(&'a mut self, range: u32) -> Self::NextFuture<'a> {
136 async move {
137 let t = (-(range as i32) % (range as i32)) as u32;
138 loop {
139 let mut buf = [0; 4];
140 traits::rng::Rng::fill_bytes(self, &mut buf).await?;
141 let x = u32::from_le_bytes(buf);
142 let m = x as u64 * range as u64;
143 let l = m as u32;
144 if l < t {
145 continue;
146 }
147 return Ok((m >> 32) as u32);
148 }
149 }
150 }
151}
152
153pub(crate) mod sealed { 131pub(crate) mod sealed {
154 use super::*; 132 use super::*;
155 133
diff --git a/embassy-traits/src/rng.rs b/embassy-traits/src/rng.rs
index 181faa515..3cc4b2a0b 100644
--- a/embassy-traits/src/rng.rs
+++ b/embassy-traits/src/rng.rs
@@ -17,11 +17,60 @@ pub trait Rng {
17 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>;
18} 18}
19 19
20pub trait Random: Rng { 20pub struct Random<T: Rng> {
21 #[rustfmt::skip] 21 rng: T,
22 type NextFuture<'a>: Future<Output = Result<u32, <Self as Rng>::Error>> + 'a 22}
23 where 23
24 Self: 'a; 24impl<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 }
25 60
26 fn next<'a>(&'a mut self, range: u32) -> Self::NextFuture<'a>; 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 }
27} 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};
22use embassy_stm32::eth::lan8742a::LAN8742A; 22use embassy_stm32::eth::lan8742a::LAN8742A;
23use embassy_stm32::eth::{Ethernet, State}; 23use embassy_stm32::eth::{Ethernet, State};
24use embassy_stm32::rng::Random; 24use embassy_stm32::rng::Rng;
25use embassy_stm32::{interrupt, peripherals}; 25use embassy_stm32::{interrupt, peripherals};
26use heapless::Vec; 26use heapless::Vec;
27use panic_probe as _; 27use panic_probe as _;
@@ -81,7 +81,7 @@ fn _embassy_rand(buf: &mut [u8]) {
81 }); 81 });
82} 82}
83 83
84static mut RNG_INST: Option<Random<RNG>> = None; 84static mut RNG_INST: Option<Rng<RNG>> = None;
85 85
86static EXECUTOR: Forever<Executor> = Forever::new(); 86static EXECUTOR: Forever<Executor> = Forever::new();
87static STATE: Forever<State<'static, 4, 4>> = Forever::new(); 87static 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
index fea88c410..cf1b14ea5 100644
--- a/examples/stm32h7/src/bin/rng.rs
+++ b/examples/stm32h7/src/bin/rng.rs
@@ -8,9 +8,9 @@
8mod example_common; 8mod example_common;
9use embassy::executor::Spawner; 9use embassy::executor::Spawner;
10use embassy::time::{Duration, Timer}; 10use embassy::time::{Duration, Timer};
11use embassy::traits::rng::Random as _;
12use embassy_stm32::gpio::{Level, Output, Speed}; 11use embassy_stm32::gpio::{Level, Output, Speed};
13use embassy_stm32::rng::Random; 12use embassy_stm32::rng::Rng;
13use embassy::traits::rng::Random;
14use embassy_stm32::Peripherals; 14use embassy_stm32::Peripherals;
15use embedded_hal::digital::v2::OutputPin; 15use embedded_hal::digital::v2::OutputPin;
16use example_common::*; 16use example_common::*;
@@ -21,14 +21,14 @@ async fn main(_spawner: Spawner, p: Peripherals) {
21 21
22 let mut led = Output::new(p.PB14, Level::High, Speed::Low); 22 let mut led = Output::new(p.PB14, Level::High, Speed::Low);
23 23
24 let mut rng = Random::new(p.RNG); 24 let mut rng = Random::new(Rng::new(p.RNG));
25 25
26 loop { 26 loop {
27 info!("high {}", unwrap!(rng.next(16).await)); 27 info!("high {}", unwrap!(rng.next_u8(16).await));
28 unwrap!(led.set_high()); 28 unwrap!(led.set_high());
29 Timer::after(Duration::from_millis(500)).await; 29 Timer::after(Duration::from_millis(500)).await;
30 30
31 info!("low {}", unwrap!(rng.next(16).await)); 31 info!("low {}", unwrap!(rng.next_u8(16).await));
32 unwrap!(led.set_low()); 32 unwrap!(led.set_low());
33 Timer::after(Duration::from_millis(500)).await; 33 Timer::after(Duration::from_millis(500)).await;
34 } 34 }