2021-05-06 01:59:16 +00:00
|
|
|
#![macro_use]
|
2021-05-06 18:33:29 +00:00
|
|
|
|
2022-09-22 14:42:49 +00:00
|
|
|
use core::future::poll_fn;
|
2023-07-30 23:41:12 +00:00
|
|
|
use core::marker::PhantomData;
|
2021-05-09 23:19:07 +00:00
|
|
|
use core::task::Poll;
|
2022-06-12 20:15:44 +00:00
|
|
|
|
2023-07-28 11:23:22 +00:00
|
|
|
use embassy_hal_internal::{into_ref, PeripheralRef};
|
2022-08-22 19:46:09 +00:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2021-05-09 23:19:07 +00:00
|
|
|
use rand_core::{CryptoRng, RngCore};
|
2021-05-06 18:33:29 +00:00
|
|
|
|
2023-07-30 23:41:12 +00:00
|
|
|
use crate::interrupt::typelevel::Interrupt;
|
2023-07-01 01:32:01 +00:00
|
|
|
use crate::{interrupt, pac, peripherals, Peripheral};
|
2021-05-06 18:33:29 +00:00
|
|
|
|
2023-07-30 23:41:12 +00:00
|
|
|
static RNG_WAKER: AtomicWaker = AtomicWaker::new();
|
2021-04-26 18:11:46 +00:00
|
|
|
|
2021-08-26 18:03:54 +00:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-05-14 14:11:43 +00:00
|
|
|
pub enum Error {
|
|
|
|
SeedError,
|
|
|
|
ClockError,
|
|
|
|
}
|
|
|
|
|
2023-07-30 23:41:12 +00:00
|
|
|
pub struct InterruptHandler<T: Instance> {
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
|
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
let bits = T::regs().sr().read();
|
|
|
|
if bits.drdy() || bits.seis() || bits.ceis() {
|
|
|
|
T::regs().cr().modify(|reg| reg.set_ie(false));
|
|
|
|
RNG_WAKER.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:06:42 +00:00
|
|
|
pub struct Rng<'d, T: Instance> {
|
2022-07-23 12:00:19 +00:00
|
|
|
_inner: PeripheralRef<'d, T>,
|
2021-04-26 18:11:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 15:06:42 +00:00
|
|
|
impl<'d, T: Instance> Rng<'d, T> {
|
2023-07-30 23:41:12 +00:00
|
|
|
pub fn new(
|
|
|
|
inner: impl Peripheral<P = T> + 'd,
|
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
|
|
|
) -> Self {
|
2021-06-09 11:54:53 +00:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
2022-07-23 12:00:19 +00:00
|
|
|
into_ref!(inner);
|
2022-07-22 23:29:35 +00:00
|
|
|
let mut random = Self { _inner: inner };
|
2021-05-06 20:38:53 +00:00
|
|
|
random.reset();
|
2023-07-30 23:41:12 +00:00
|
|
|
|
|
|
|
T::Interrupt::unpend();
|
|
|
|
unsafe { T::Interrupt::enable() };
|
|
|
|
|
2021-05-06 20:38:53 +00:00
|
|
|
random
|
|
|
|
}
|
|
|
|
|
2023-07-01 01:15:39 +00:00
|
|
|
#[cfg(rng_v1)]
|
2021-05-06 20:38:53 +00:00
|
|
|
pub fn reset(&mut self) {
|
2023-07-01 01:32:01 +00:00
|
|
|
T::regs().cr().write(|reg| {
|
|
|
|
reg.set_rngen(false);
|
2023-06-19 01:07:26 +00:00
|
|
|
});
|
|
|
|
T::regs().sr().modify(|reg| {
|
|
|
|
reg.set_seis(false);
|
|
|
|
reg.set_ceis(false);
|
|
|
|
});
|
2023-07-01 01:32:01 +00:00
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
});
|
2021-05-06 20:38:53 +00:00
|
|
|
// Reference manual says to discard the first.
|
|
|
|
let _ = self.next_u32();
|
2021-04-26 18:11:46 +00:00
|
|
|
}
|
2022-01-26 23:08:02 +00:00
|
|
|
|
2023-07-01 01:15:39 +00:00
|
|
|
#[cfg(not(rng_v1))]
|
|
|
|
pub fn reset(&mut self) {
|
2023-07-01 01:32:01 +00:00
|
|
|
T::regs().cr().write(|reg| {
|
2023-07-01 01:15:39 +00:00
|
|
|
reg.set_rngen(false);
|
|
|
|
reg.set_condrst(true);
|
|
|
|
// set RNG config "A" according to reference manual
|
|
|
|
// this has to be written within the same write access as setting the CONDRST bit
|
|
|
|
reg.set_nistc(pac::rng::vals::Nistc::DEFAULT);
|
|
|
|
reg.set_rng_config1(pac::rng::vals::RngConfig1::CONFIGA);
|
|
|
|
reg.set_rng_config2(pac::rng::vals::RngConfig2::CONFIGA_B);
|
|
|
|
reg.set_rng_config3(pac::rng::vals::RngConfig3::CONFIGA);
|
|
|
|
reg.set_clkdiv(pac::rng::vals::Clkdiv::NODIV);
|
|
|
|
});
|
|
|
|
// wait for CONDRST to be set
|
|
|
|
while !T::regs().cr().read().condrst() {}
|
|
|
|
// magic number must be written immediately before every read or write access to HTCR
|
|
|
|
T::regs().htcr().write(|w| w.set_htcfg(pac::rng::vals::Htcfg::MAGIC));
|
|
|
|
// write recommended value according to reference manual
|
|
|
|
// note: HTCR can only be written during conditioning
|
|
|
|
T::regs()
|
|
|
|
.htcr()
|
|
|
|
.write(|w| w.set_htcfg(pac::rng::vals::Htcfg::RECOMMENDED));
|
|
|
|
// finish conditioning
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
reg.set_condrst(false);
|
|
|
|
});
|
|
|
|
// wait for CONDRST to be reset
|
|
|
|
while T::regs().cr().read().condrst() {}
|
|
|
|
}
|
|
|
|
|
2023-07-01 01:32:01 +00:00
|
|
|
pub fn recover_seed_error(&mut self) -> () {
|
|
|
|
self.reset();
|
|
|
|
// reset should also clear the SEIS flag
|
|
|
|
if T::regs().sr().read().seis() {
|
|
|
|
warn!("recovering from seed error failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// wait for SECS to be cleared by RNG
|
|
|
|
while T::regs().sr().read().secs() {}
|
|
|
|
}
|
2022-01-26 23:08:02 +00:00
|
|
|
|
2023-07-01 01:32:01 +00:00
|
|
|
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
|
2022-01-26 23:08:02 +00:00
|
|
|
for chunk in dest.chunks_mut(4) {
|
2023-07-01 01:32:01 +00:00
|
|
|
let bits = T::regs().sr().read();
|
|
|
|
if bits.seis() {
|
|
|
|
// in case of noise-source or seed error we try to recover here
|
|
|
|
// but we must not use the data in DR and we return an error
|
|
|
|
// to leave retry-logic to the application
|
|
|
|
self.recover_seed_error();
|
|
|
|
return Err(Error::SeedError);
|
|
|
|
} else if bits.ceis() {
|
|
|
|
// clock error detected, DR could still be used but keep it safe,
|
|
|
|
// clear the error and abort
|
|
|
|
T::regs().sr().modify(|sr| sr.set_ceis(false));
|
|
|
|
return Err(Error::ClockError);
|
|
|
|
} else if bits.drdy() {
|
|
|
|
// DR can be read up to four times until the output buffer is empty
|
|
|
|
// DRDY is cleared automatically when that happens
|
|
|
|
let random_word = T::regs().dr().read();
|
|
|
|
// reference manual: always check if DR is zero
|
|
|
|
if random_word == 0 {
|
|
|
|
return Err(Error::SeedError);
|
|
|
|
}
|
|
|
|
// write bytes to chunk
|
|
|
|
for (dest, src) in chunk.iter_mut().zip(random_word.to_be_bytes().iter()) {
|
|
|
|
*dest = *src
|
2022-01-26 23:08:02 +00:00
|
|
|
}
|
2023-07-01 01:32:01 +00:00
|
|
|
} else {
|
|
|
|
// wait for interrupt
|
|
|
|
poll_fn(|cx| {
|
|
|
|
// quick check to avoid registration if already done.
|
|
|
|
let bits = T::regs().sr().read();
|
|
|
|
if bits.drdy() || bits.seis() || bits.ceis() {
|
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
RNG_WAKER.register(cx.waker());
|
|
|
|
T::regs().cr().modify(|reg| reg.set_ie(true));
|
|
|
|
// Need to check condition **after** `register` to avoid a race
|
|
|
|
// condition that would result in lost notifications.
|
|
|
|
let bits = T::regs().sr().read();
|
|
|
|
if bits.drdy() || bits.seis() || bits.ceis() {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
2022-01-26 23:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-26 18:11:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 15:06:42 +00:00
|
|
|
impl<'d, T: Instance> RngCore for Rng<'d, T> {
|
2021-05-06 18:33:29 +00:00
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
loop {
|
2023-06-19 01:07:26 +00:00
|
|
|
let sr = T::regs().sr().read();
|
2023-02-08 15:52:49 +00:00
|
|
|
if sr.seis() | sr.ceis() {
|
|
|
|
self.reset();
|
|
|
|
} else if sr.drdy() {
|
2023-06-19 01:07:26 +00:00
|
|
|
return T::regs().dr().read();
|
2021-05-06 18:33:29 +00: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-09 23:19:07 +00:00
|
|
|
self.fill_bytes(dest);
|
2021-05-06 18:33:29 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:06:42 +00:00
|
|
|
impl<'d, T: Instance> CryptoRng for Rng<'d, T> {}
|
2021-05-06 18:33:29 +00:00
|
|
|
|
2021-04-26 18:11:46 +00:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
2021-05-06 18:33:29 +00:00
|
|
|
fn regs() -> pac::rng::Rng;
|
2021-04-26 18:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 23:41:12 +00:00
|
|
|
pub trait Instance: sealed::Instance + Peripheral<P = Self> + crate::rcc::RccPeripheral + 'static + Send {
|
|
|
|
type Interrupt: interrupt::typelevel::Interrupt;
|
|
|
|
}
|
2021-04-26 18:11:46 +00:00
|
|
|
|
2023-07-30 23:41:12 +00:00
|
|
|
foreach_interrupt!(
|
|
|
|
($inst:ident, rng, RNG, GLOBAL, $irq:ident) => {
|
|
|
|
impl Instance for peripherals::$inst {
|
|
|
|
type Interrupt = crate::interrupt::typelevel::$irq;
|
|
|
|
}
|
2021-06-03 15:09:29 +00:00
|
|
|
|
|
|
|
impl sealed::Instance for peripherals::$inst {
|
|
|
|
fn regs() -> crate::pac::rng::Rng {
|
2023-07-30 23:41:12 +00:00
|
|
|
crate::pac::$inst
|
2021-06-03 15:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
);
|