aboutsummaryrefslogtreecommitdiff
path: root/embassy-imxrt
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-imxrt')
-rw-r--r--embassy-imxrt/Cargo.toml4
-rw-r--r--embassy-imxrt/src/rng.rs48
2 files changed, 42 insertions, 10 deletions
diff --git a/embassy-imxrt/Cargo.toml b/embassy-imxrt/Cargo.toml
index f16002a8d..d8cf3aaba 100644
--- a/embassy-imxrt/Cargo.toml
+++ b/embassy-imxrt/Cargo.toml
@@ -80,9 +80,11 @@ cortex-m = "0.7.6"
80critical-section = "1.1" 80critical-section = "1.1"
81embedded-io = { version = "0.6.1" } 81embedded-io = { version = "0.6.1" }
82embedded-io-async = { version = "0.6.1" } 82embedded-io-async = { version = "0.6.1" }
83rand_core = "0.6.4"
84fixed = "1.23.1" 83fixed = "1.23.1"
85 84
85rand-core-06 = { package = "rand_core", version = "0.6" }
86rand-core-09 = { package = "rand_core", version = "0.9" }
87
86embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [ 88embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
87 "unproven", 89 "unproven",
88] } 90] }
diff --git a/embassy-imxrt/src/rng.rs b/embassy-imxrt/src/rng.rs
index 67e7ab65d..75f243df9 100644
--- a/embassy-imxrt/src/rng.rs
+++ b/embassy-imxrt/src/rng.rs
@@ -6,7 +6,6 @@ use core::task::Poll;
6 6
7use embassy_futures::block_on; 7use embassy_futures::block_on;
8use embassy_sync::waitqueue::AtomicWaker; 8use embassy_sync::waitqueue::AtomicWaker;
9use rand_core::{CryptoRng, RngCore};
10 9
11use crate::clocks::{enable_and_reset, SysconPeripheral}; 10use crate::clocks::{enable_and_reset, SysconPeripheral};
12use crate::interrupt::typelevel::Interrupt; 11use crate::interrupt::typelevel::Interrupt;
@@ -201,32 +200,63 @@ impl<'d> Rng<'d> {
201 .mctl() 200 .mctl()
202 .modify(|_, w| w.trng_acc().set_bit().prgm().clear_bit()); 201 .modify(|_, w| w.trng_acc().set_bit().prgm().clear_bit());
203 } 202 }
204}
205 203
206impl RngCore for Rng<'_> { 204 /// Generate a random u32
207 fn next_u32(&mut self) -> u32 { 205 pub fn blocking_next_u32(&mut self) -> u32 {
208 let mut bytes = [0u8; 4]; 206 let mut bytes = [0u8; 4];
209 block_on(self.async_fill_bytes(&mut bytes)).unwrap(); 207 block_on(self.async_fill_bytes(&mut bytes)).unwrap();
210 u32::from_ne_bytes(bytes) 208 u32::from_ne_bytes(bytes)
211 } 209 }
212 210
213 fn next_u64(&mut self) -> u64 { 211 /// Generate a random u64
212 pub fn blocking_next_u64(&mut self) -> u64 {
214 let mut bytes = [0u8; 8]; 213 let mut bytes = [0u8; 8];
215 block_on(self.async_fill_bytes(&mut bytes)).unwrap(); 214 block_on(self.async_fill_bytes(&mut bytes)).unwrap();
216 u64::from_ne_bytes(bytes) 215 u64::from_ne_bytes(bytes)
217 } 216 }
218 217
219 fn fill_bytes(&mut self, dest: &mut [u8]) { 218 /// Fill a slice with random bytes.
219 pub fn blocking_fill_bytes(&mut self, dest: &mut [u8]) {
220 block_on(self.async_fill_bytes(dest)).unwrap(); 220 block_on(self.async_fill_bytes(dest)).unwrap();
221 } 221 }
222}
222 223
223 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { 224impl<'d> rand_core_06::RngCore for Rng<'d> {
224 self.fill_bytes(dest); 225 fn next_u32(&mut self) -> u32 {
226 self.blocking_next_u32()
227 }
228
229 fn next_u64(&mut self) -> u64 {
230 self.blocking_next_u64()
231 }
232
233 fn fill_bytes(&mut self, dest: &mut [u8]) {
234 self.blocking_fill_bytes(dest);
235 }
236
237 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
238 self.blocking_fill_bytes(dest);
225 Ok(()) 239 Ok(())
226 } 240 }
227} 241}
228 242
229impl CryptoRng for Rng<'_> {} 243impl<'d> rand_core_06::CryptoRng for Rng<'d> {}
244
245impl<'d> rand_core_09::RngCore for Rng<'d> {
246 fn next_u32(&mut self) -> u32 {
247 self.blocking_next_u32()
248 }
249
250 fn next_u64(&mut self) -> u64 {
251 self.blocking_next_u64()
252 }
253
254 fn fill_bytes(&mut self, dest: &mut [u8]) {
255 self.blocking_fill_bytes(dest);
256 }
257}
258
259impl<'d> rand_core_09::CryptoRng for Rng<'d> {}
230 260
231struct Info { 261struct Info {
232 regs: crate::pac::Trng, 262 regs: crate::pac::Trng,