improve RNG polling

This commit is contained in:
Jan Christoph Bernack 2023-07-01 03:32:01 +02:00 committed by Dario Nieuwenhuis
parent b65406791a
commit d6c5c1772c

View File

@ -3,11 +3,12 @@
use core::future::poll_fn; use core::future::poll_fn;
use core::task::Poll; use core::task::Poll;
use embassy_hal_internal::interrupt::InterruptExt;
use embassy_hal_internal::{into_ref, PeripheralRef}; use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
use rand_core::{CryptoRng, RngCore}; use rand_core::{CryptoRng, RngCore};
use crate::{pac, peripherals, Peripheral}; use crate::{interrupt, pac, peripherals, Peripheral};
pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new(); pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new();
@ -28,31 +29,31 @@ impl<'d, T: Instance> Rng<'d, T> {
into_ref!(inner); into_ref!(inner);
let mut random = Self { _inner: inner }; let mut random = Self { _inner: inner };
random.reset(); random.reset();
unsafe {
interrupt::RNG.enable();
}
random random
} }
#[cfg(rng_v1)] #[cfg(rng_v1)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
// rng_v2 locks up on seed error, needs reset T::regs().cr().write(|reg| {
#[cfg(rng_v2)] reg.set_rngen(false);
if T::regs().sr().read().seis() {
T::reset();
}
T::regs().cr().modify(|reg| {
reg.set_rngen(true);
reg.set_ie(true);
}); });
T::regs().sr().modify(|reg| { T::regs().sr().modify(|reg| {
reg.set_seis(false); reg.set_seis(false);
reg.set_ceis(false); reg.set_ceis(false);
}); });
T::regs().cr().modify(|reg| {
reg.set_rngen(true);
});
// Reference manual says to discard the first. // Reference manual says to discard the first.
let _ = self.next_u32(); let _ = self.next_u32();
} }
#[cfg(not(rng_v1))] #[cfg(not(rng_v1))]
pub fn reset(&mut self) { pub fn reset(&mut self) {
T::regs().cr().modify(|reg| { T::regs().cr().write(|reg| {
reg.set_rngen(false); reg.set_rngen(false);
reg.set_condrst(true); reg.set_condrst(true);
// set RNG config "A" according to reference manual // set RNG config "A" according to reference manual
@ -76,42 +77,68 @@ impl<'d, T: Instance> Rng<'d, T> {
T::regs().cr().modify(|reg| { T::regs().cr().modify(|reg| {
reg.set_rngen(true); reg.set_rngen(true);
reg.set_condrst(false); reg.set_condrst(false);
reg.set_ie(true);
}); });
// wait for CONDRST to be reset // wait for CONDRST to be reset
while T::regs().cr().read().condrst() {} while T::regs().cr().read().condrst() {}
} }
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() {}
}
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
T::regs().cr().modify(|reg| {
reg.set_rngen(true);
});
for chunk in dest.chunks_mut(4) { for chunk in dest.chunks_mut(4) {
poll_fn(|cx| { let bits = T::regs().sr().read();
RNG_WAKER.register(cx.waker()); if bits.seis() {
T::regs().cr().modify(|reg| { // in case of noise-source or seed error we try to recover here
reg.set_ie(true); // 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();
let bits = T::regs().sr().read(); return Err(Error::SeedError);
} else if bits.ceis() {
if bits.drdy() { // clock error detected, DR could still be used but keep it safe,
Poll::Ready(Ok(())) // clear the error and abort
} else if bits.seis() { T::regs().sr().modify(|sr| sr.set_ceis(false));
self.reset(); return Err(Error::ClockError);
Poll::Ready(Err(Error::SeedError)) } else if bits.drdy() {
} else if bits.ceis() { // DR can be read up to four times until the output buffer is empty
self.reset(); // DRDY is cleared automatically when that happens
Poll::Ready(Err(Error::ClockError)) let random_word = T::regs().dr().read();
} else { // reference manual: always check if DR is zero
Poll::Pending if random_word == 0 {
return Err(Error::SeedError);
} }
}) // write bytes to chunk
.await?; for (dest, src) in chunk.iter_mut().zip(random_word.to_be_bytes().iter()) {
let random_bytes = T::regs().dr().read().to_be_bytes(); *dest = *src
for (dest, src) in chunk.iter_mut().zip(random_bytes.iter()) { }
*dest = *src } 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;
} }
} }
@ -186,7 +213,7 @@ macro_rules! irq {
unsafe fn $irq() { unsafe fn $irq() {
let bits = $crate::pac::RNG.sr().read(); let bits = $crate::pac::RNG.sr().read();
if bits.drdy() || bits.seis() || bits.ceis() { if bits.drdy() || bits.seis() || bits.ceis() {
$crate::pac::RNG.cr().write(|reg| reg.set_ie(false)); $crate::pac::RNG.cr().modify(|reg| reg.set_ie(false));
$crate::rng::RNG_WAKER.wake(); $crate::rng::RNG_WAKER.wake();
} }
} }