2021-05-06 03:59:16 +02:00
|
|
|
#![macro_use]
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2021-05-10 01:19:07 +02:00
|
|
|
use core::future::Future;
|
|
|
|
use core::task::Poll;
|
|
|
|
use embassy::traits;
|
2021-09-11 01:53:53 +02:00
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy::waitqueue::AtomicWaker;
|
2021-07-29 13:44:51 +02:00
|
|
|
use embassy_hal_common::unborrow;
|
2021-05-10 01:19:07 +02:00
|
|
|
use futures::future::poll_fn;
|
|
|
|
use rand_core::{CryptoRng, RngCore};
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2021-05-10 01:19:07 +02:00
|
|
|
use crate::pac;
|
2021-06-03 17:09:29 +02:00
|
|
|
use crate::peripherals;
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2021-05-13 20:28:53 +02:00
|
|
|
pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new();
|
2021-04-26 20:11:46 +02:00
|
|
|
|
2021-08-26 20:03:54 +02:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-05-14 16:11:43 +02:00
|
|
|
pub enum Error {
|
|
|
|
SeedError,
|
|
|
|
ClockError,
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:39:33 +02:00
|
|
|
pub struct Rng<T: Instance> {
|
2021-05-11 06:34:10 +02:00
|
|
|
_inner: T,
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 15:39:33 +02:00
|
|
|
impl<T: Instance> Rng<T> {
|
2021-05-10 01:19:07 +02:00
|
|
|
pub fn new(inner: impl Unborrow<Target = T>) -> Self {
|
2021-06-09 13:54:53 +02:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
2021-04-26 20:11:46 +02:00
|
|
|
unborrow!(inner);
|
2021-05-11 06:34:10 +02:00
|
|
|
let mut random = Self { _inner: inner };
|
2021-05-06 22:38:53 +02:00
|
|
|
random.reset();
|
|
|
|
random
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
reg.set_ie(true);
|
|
|
|
});
|
|
|
|
T::regs().sr().modify(|reg| {
|
|
|
|
reg.set_seis(false);
|
|
|
|
reg.set_ceis(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Reference manual says to discard the first.
|
|
|
|
let _ = self.next_u32();
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:39:33 +02:00
|
|
|
impl<T: Instance> RngCore for Rng<T> {
|
2021-05-06 20:33:29 +02:00
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
loop {
|
|
|
|
let bits = unsafe { T::regs().sr().read() };
|
|
|
|
if bits.drdy() {
|
2021-05-10 01:19:07 +02:00
|
|
|
return unsafe { T::regs().dr().read() };
|
2021-05-06 20:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
let mut rand = self.next_u32() as u64;
|
|
|
|
rand |= (self.next_u32() as u64) << 32;
|
|
|
|
rand
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
|
|
|
for chunk in dest.chunks_mut(4) {
|
|
|
|
let rand = self.next_u32();
|
|
|
|
for (slot, num) in chunk.iter_mut().zip(rand.to_be_bytes().iter()) {
|
|
|
|
*slot = *num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
2021-05-10 01:19:07 +02:00
|
|
|
self.fill_bytes(dest);
|
2021-05-06 20:33:29 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:39:33 +02:00
|
|
|
impl<T: Instance> CryptoRng for Rng<T> {}
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2021-09-01 15:39:33 +02:00
|
|
|
impl<T: Instance> traits::rng::Rng for Rng<T> {
|
2021-05-06 20:33:29 +02:00
|
|
|
type Error = Error;
|
2021-05-10 01:19:07 +02:00
|
|
|
#[rustfmt::skip]
|
2021-08-04 19:39:54 +02:00
|
|
|
type RngFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + 'a;
|
2021-05-06 20:33:29 +02:00
|
|
|
|
|
|
|
fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a> {
|
|
|
|
unsafe {
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async move {
|
|
|
|
for chunk in dest.chunks_mut(4) {
|
2021-05-06 20:58:41 +02:00
|
|
|
poll_fn(|cx| {
|
|
|
|
RNG_WAKER.register(cx.waker());
|
|
|
|
unsafe {
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_ie(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let bits = unsafe { T::regs().sr().read() };
|
|
|
|
|
|
|
|
if bits.drdy() {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else if bits.seis() {
|
2021-05-06 22:38:53 +02:00
|
|
|
self.reset();
|
2021-05-06 20:58:41 +02:00
|
|
|
Poll::Ready(Err(Error::SeedError))
|
|
|
|
} else if bits.ceis() {
|
2021-05-06 22:38:53 +02:00
|
|
|
self.reset();
|
2021-05-06 20:58:41 +02:00
|
|
|
Poll::Ready(Err(Error::ClockError))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
2021-05-10 01:19:07 +02:00
|
|
|
})
|
|
|
|
.await?;
|
2021-05-06 20:33:29 +02:00
|
|
|
let random_bytes = unsafe { T::regs().dr().read() }.to_be_bytes();
|
|
|
|
for (dest, src) in chunk.iter_mut().zip(random_bytes.iter()) {
|
|
|
|
*dest = *src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
2021-08-30 15:55:29 +02:00
|
|
|
}
|
2021-08-27 22:10:01 +02:00
|
|
|
|
2021-04-26 20:11:46 +02:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
2021-05-06 20:33:29 +02:00
|
|
|
fn regs() -> pac::rng::Rng;
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:54:53 +02:00
|
|
|
pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral {}
|
2021-04-26 20:11:46 +02:00
|
|
|
|
2021-06-03 17:09:29 +02:00
|
|
|
crate::pac::peripherals!(
|
|
|
|
(rng, $inst:ident) => {
|
|
|
|
impl Instance for peripherals::$inst {}
|
|
|
|
|
|
|
|
impl sealed::Instance for peripherals::$inst {
|
|
|
|
fn regs() -> crate::pac::rng::Rng {
|
|
|
|
crate::pac::RNG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! irq {
|
|
|
|
($irq:ident) => {
|
|
|
|
mod rng_irq {
|
|
|
|
use crate::interrupt;
|
|
|
|
|
|
|
|
#[interrupt]
|
|
|
|
unsafe fn $irq() {
|
|
|
|
let bits = $crate::pac::RNG.sr().read();
|
|
|
|
if bits.drdy() || bits.seis() || bits.ceis() {
|
|
|
|
$crate::pac::RNG.cr().write(|reg| reg.set_ie(false));
|
|
|
|
$crate::rng::RNG_WAKER.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
crate::pac::interrupts!(
|
|
|
|
(RNG) => {
|
|
|
|
irq!(RNG);
|
|
|
|
};
|
|
|
|
|
|
|
|
(RNG_LPUART1) => {
|
|
|
|
irq!(RNG_LPUART1);
|
|
|
|
};
|
|
|
|
|
|
|
|
(AES_RNG_LPUART1) => {
|
|
|
|
irq!(AES_RNG_LPUART1);
|
|
|
|
};
|
|
|
|
|
|
|
|
(AES_RNG) => {
|
|
|
|
irq!(AES_RNG);
|
|
|
|
};
|
|
|
|
|
|
|
|
(HASH_RNG) => {
|
|
|
|
irq!(HASH_RNG);
|
|
|
|
};
|
|
|
|
);
|