|
| 1 | +//! True Random Number Generator (TRNG) |
| 2 | +
|
| 3 | +use rand::TryRngCore; |
| 4 | + |
| 5 | +use crate::{rcc::Rcc, stm32::RNG}; |
| 6 | +use core::{fmt::Formatter, marker::PhantomData}; |
| 7 | + |
| 8 | +pub enum RngError { |
| 9 | + NotReady, |
| 10 | + SeedError, |
| 11 | + ClockError, |
| 12 | +} |
| 13 | + |
| 14 | +impl core::fmt::Debug for RngError { |
| 15 | + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
| 16 | + match self { |
| 17 | + RngError::NotReady => write!(f, "RNG Not ready"), |
| 18 | + RngError::SeedError => write!(f, "RNG Seed error"), |
| 19 | + RngError::ClockError => write!(f, "RNG Clock error"), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl core::fmt::Display for RngError { |
| 25 | + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
| 26 | + core::fmt::Debug::fmt(self, f) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Extension trait for the RNG register block |
| 31 | +pub trait RngExt { |
| 32 | + fn constrain(self, rcc: &mut Rcc) -> Rng<Stopped>; |
| 33 | +} |
| 34 | + |
| 35 | +impl RngExt for RNG { |
| 36 | + /// Constrain the RNG register block and return an Rng<Stopped>. |
| 37 | + /// Enables the RNG peripheral in the AHB2ENR register. |
| 38 | + /// Enables the HSI48 clock used by RNG |
| 39 | + fn constrain(self, rcc: &mut Rcc) -> Rng<Stopped> { |
| 40 | + // Enable RNG in AHB2ENR |
| 41 | + rcc.ahb2enr().modify(|_, w| w.rngen().set_bit()); |
| 42 | + |
| 43 | + // Enable HSI48 clock used by the RNG |
| 44 | + rcc.enable_hsi48(); |
| 45 | + |
| 46 | + Rng { |
| 47 | + _state: PhantomData, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// Type states for the RNG peripheral |
| 53 | +pub struct Stopped; |
| 54 | +pub struct Running; |
| 55 | + |
| 56 | +/// True Random Number Generator (TRNG) |
| 57 | +pub struct Rng<State> { |
| 58 | + _state: core::marker::PhantomData<State>, |
| 59 | +} |
| 60 | + |
| 61 | +impl Rng<Stopped> { |
| 62 | + /// Start the RNG peripheral |
| 63 | + /// |
| 64 | + /// Enables clock error detection and starts random number generation. |
| 65 | + /// |
| 66 | + /// Retrurns an [`Rng`] in the `Running` state |
| 67 | + pub fn start(self) -> Rng<Running> { |
| 68 | + unsafe { |
| 69 | + (*RNG::ptr()) |
| 70 | + .cr() |
| 71 | + .modify(|_, w| w.rngen().set_bit().ced().clear_bit()) |
| 72 | + }; |
| 73 | + |
| 74 | + Rng { |
| 75 | + _state: PhantomData, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl Rng<Running> { |
| 81 | + /// Stop the RNG peripheral |
| 82 | + /// |
| 83 | + /// Returns an [`Rng`] in the `Stopped` state |
| 84 | + pub fn stop(self) -> Rng<Stopped> { |
| 85 | + unsafe { (*RNG::ptr()).cr().modify(|_, w| w.rngen().clear_bit()) }; |
| 86 | + |
| 87 | + Rng { |
| 88 | + _state: PhantomData, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// Check if the RNG is ready |
| 93 | + #[inline(always)] |
| 94 | + pub fn is_ready(&self) -> bool { |
| 95 | + unsafe { (*RNG::ptr()).sr().read().drdy().bit_is_set() } |
| 96 | + } |
| 97 | + |
| 98 | + /// Check if the seed error flag is set |
| 99 | + pub fn is_seed_error(&self) -> bool { |
| 100 | + unsafe { (*RNG::ptr()).sr().read().seis().bit_is_set() } |
| 101 | + } |
| 102 | + |
| 103 | + /// Check if the clock error flag is set |
| 104 | + pub fn is_clock_error(&self) -> bool { |
| 105 | + unsafe { (*RNG::ptr()).sr().read().ceis().bit_is_set() } |
| 106 | + } |
| 107 | + |
| 108 | + /// Blocking read of a random u32 from the RNG in polling mode. |
| 109 | + /// |
| 110 | + /// Returns an [`RngError`] if the RNG reports an error condition. |
| 111 | + /// Polls the data ready flag until a random word is ready to be read. |
| 112 | + /// |
| 113 | + /// For non-blocking operation use [`read_non_blocking()`] |
| 114 | + pub fn read_blocking(&self) -> Result<u32, RngError> { |
| 115 | + loop { |
| 116 | + match self.read_non_blocking() { |
| 117 | + Ok(value) => return Ok(value), |
| 118 | + Err(RngError::NotReady) => continue, |
| 119 | + Err(e) => return Err(e), |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// Non blocking read of a random u32 from the RNG in polling mode. |
| 125 | + /// |
| 126 | + /// Returns an [`RngError`] if the RNG is not ready or reports an error condition. |
| 127 | + /// |
| 128 | + /// For blocking reads use [`read_blocking()`] |
| 129 | + pub fn read_non_blocking(&self) -> Result<u32, RngError> { |
| 130 | + // Read the SR register to check if there is an error condition, |
| 131 | + // and if the DRDY bit is set to indicate a valid random number is available. |
| 132 | + let status = unsafe { (*RNG::ptr()).sr().read() }; |
| 133 | + |
| 134 | + // Check if the seed or clock error bits are set |
| 135 | + if status.seis().bit_is_set() { |
| 136 | + return Err(RngError::SeedError); |
| 137 | + } |
| 138 | + |
| 139 | + if status.ceis().bit_is_set() { |
| 140 | + return Err(RngError::ClockError); |
| 141 | + } |
| 142 | + |
| 143 | + if status.drdy().bit_is_set() { |
| 144 | + // Data is ready. Read the DR register and return the value. |
| 145 | + Ok(unsafe { (*RNG::ptr()).dr().read().bits() }) |
| 146 | + } else { |
| 147 | + Err(RngError::NotReady) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Implement [`rand::TryRngCore`] for the RNG peripheral. |
| 153 | +/// |
| 154 | +/// Since this is a fallible trait, to use this as a [`rand::RngCore`] with [`rand::Rng`], |
| 155 | +/// the [`unwrap_err`] method can be used for compatibility with [`rand::Rng`] but will panic on error. |
| 156 | +/// |
| 157 | +/// See https://docs.rs/rand/latest/rand/trait.TryRngCore.html |
| 158 | +impl TryRngCore for &Rng<Running> { |
| 159 | + type Error = RngError; |
| 160 | + |
| 161 | + fn try_next_u32(&mut self) -> Result<u32, Self::Error> { |
| 162 | + self.read_blocking() |
| 163 | + } |
| 164 | + |
| 165 | + fn try_next_u64(&mut self) -> Result<u64, Self::Error> { |
| 166 | + let mut result = 0u64; |
| 167 | + for _ in 0..2 { |
| 168 | + result |= self.try_next_u32()? as u64; |
| 169 | + result <<= 32; |
| 170 | + } |
| 171 | + Ok(result) |
| 172 | + } |
| 173 | + |
| 174 | + fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> { |
| 175 | + for chunk in dst.chunks_mut(size_of::<u32>()) { |
| 176 | + let value = self.try_next_u32()?; |
| 177 | + chunk.copy_from_slice(&value.to_ne_bytes()); |
| 178 | + } |
| 179 | + Ok(()) |
| 180 | + } |
| 181 | +} |
0 commit comments