embassy/embassy-stm32/src/rng.rs

183 lines
4.3 KiB
Rust
Raw Normal View History

#![macro_use]
2021-05-09 23:19:07 +00:00
use core::task::Poll;
2022-06-12 20:15:44 +00:00
use embassy_hal_common::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
2021-05-09 23:19:07 +00:00
use futures::future::poll_fn;
use rand_core::{CryptoRng, RngCore};
use crate::{pac, peripherals, Peripheral};
pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new();
2021-04-26 18:11:46 +00:00
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
SeedError,
ClockError,
}
2022-02-10 15:06:42 +00:00
pub struct Rng<'d, T: Instance> {
_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> {
pub fn new(inner: impl Peripheral<P = T> + 'd) -> Self {
2021-06-09 11:54:53 +00:00
T::enable();
T::reset();
into_ref!(inner);
2022-07-22 23:29:35 +00:00
let mut random = Self { _inner: inner };
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 18:11:46 +00:00
}
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
unsafe {
T::regs().cr().modify(|reg| {
reg.set_rngen(true);
})
}
for chunk in dest.chunks_mut(4) {
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() {
self.reset();
Poll::Ready(Err(Error::SeedError))
} else if bits.ceis() {
self.reset();
Poll::Ready(Err(Error::ClockError))
} else {
Poll::Pending
}
})
.await?;
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 18:11:46 +00:00
}
2022-02-10 15:06:42 +00:00
impl<'d, T: Instance> RngCore for Rng<'d, T> {
fn next_u32(&mut self) -> u32 {
loop {
let bits = unsafe { T::regs().sr().read() };
if bits.drdy() {
2021-05-09 23:19:07 +00:00
return unsafe { T::regs().dr().read() };
}
}
}
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);
Ok(())
}
}
2022-02-10 15:06:42 +00:00
impl<'d, T: Instance> CryptoRng for Rng<'d, T> {}
2021-04-26 18:11:46 +00:00
pub(crate) mod sealed {
use super::*;
pub trait Instance {
fn regs() -> pac::rng::Rng;
2021-04-26 18:11:46 +00:00
}
}
2021-06-09 11:54:53 +00:00
pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral {}
2021-04-26 18:11:46 +00:00
foreach_peripheral!(
(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();
}
}
}
};
}
foreach_interrupt!(
(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);
};
);