aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/rng.rs35
1 files changed, 16 insertions, 19 deletions
diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs
index 9eedcb17d..af527cd53 100644
--- a/embassy-stm32/src/rng.rs
+++ b/embassy-stm32/src/rng.rs
@@ -1,15 +1,16 @@
1#![macro_use] 1#![macro_use]
2 2
3//use crate::pac::rng::{regs, Rng}; 3use core::future::Future;
4use crate::pac; 4use core::task::Poll;
5use crate::peripherals; 5use defmt::*;
6use crate::interrupt; 6use embassy::traits;
7use futures::future::poll_fn; 7use embassy::util::{AtomicWaker, Unborrow};
8use embassy::util::{Unborrow, AtomicWaker};
9use embassy_extras::unborrow; 8use embassy_extras::unborrow;
10use rand_core::{RngCore, CryptoRng}; 9use futures::future::poll_fn;
10use rand_core::{CryptoRng, RngCore};
11 11
12use defmt::*; 12use crate::interrupt;
13use crate::pac;
13 14
14static RNG_WAKER: AtomicWaker = AtomicWaker::new(); 15static RNG_WAKER: AtomicWaker = AtomicWaker::new();
15 16
@@ -27,7 +28,7 @@ pub struct Random<T: Instance> {
27} 28}
28 29
29impl<T: Instance> Random<T> { 30impl<T: Instance> Random<T> {
30 pub fn new(inner: impl Unborrow<Target=T>) -> Self { 31 pub fn new(inner: impl Unborrow<Target = T>) -> Self {
31 unborrow!(inner); 32 unborrow!(inner);
32 let mut random = Self { inner }; 33 let mut random = Self { inner };
33 random.reset(); 34 random.reset();
@@ -55,7 +56,7 @@ impl<T: Instance> RngCore for Random<T> {
55 loop { 56 loop {
56 let bits = unsafe { T::regs().sr().read() }; 57 let bits = unsafe { T::regs().sr().read() };
57 if bits.drdy() { 58 if bits.drdy() {
58 return unsafe{ T::regs().dr().read() } 59 return unsafe { T::regs().dr().read() };
59 } 60 }
60 } 61 }
61 } 62 }
@@ -76,18 +77,12 @@ impl<T: Instance> RngCore for Random<T> {
76 } 77 }
77 78
78 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { 79 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
79 self.fill_bytes( dest ); 80 self.fill_bytes(dest);
80 Ok(()) 81 Ok(())
81 } 82 }
82} 83}
83 84
84impl<T: Instance> CryptoRng for Random<T> { } 85impl<T: Instance> CryptoRng for Random<T> {}
85
86use core::future::Future;
87use core::marker::PhantomData;
88use embassy::traits;
89use core::task::{Poll, Context};
90use core::pin::Pin;
91 86
92pub enum Error { 87pub enum Error {
93 SeedError, 88 SeedError,
@@ -96,6 +91,7 @@ pub enum Error {
96 91
97impl<T: Instance> traits::rng::Rng for Random<T> { 92impl<T: Instance> traits::rng::Rng for Random<T> {
98 type Error = Error; 93 type Error = Error;
94 #[rustfmt::skip]
99 type RngFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>>; 95 type RngFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>>;
100 96
101 fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a> { 97 fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a> {
@@ -127,7 +123,8 @@ impl<T: Instance> traits::rng::Rng for Random<T> {
127 } else { 123 } else {
128 Poll::Pending 124 Poll::Pending
129 } 125 }
130 } ).await?; 126 })
127 .await?;
131 let random_bytes = unsafe { T::regs().dr().read() }.to_be_bytes(); 128 let random_bytes = unsafe { T::regs().dr().read() }.to_be_bytes();
132 for (dest, src) in chunk.iter_mut().zip(random_bytes.iter()) { 129 for (dest, src) in chunk.iter_mut().zip(random_bytes.iter()) {
133 *dest = *src 130 *dest = *src