|
| 1 | +use core::cmp; |
| 2 | +use core::mem; |
| 3 | + |
| 4 | +use crate::hal::blocking::rng; |
| 5 | +use crate::rcc::Clocks; |
| 6 | +use crate::stm32; |
| 7 | +use crate::stm32::RNG; |
| 8 | +use crate::time::U32Ext; |
| 9 | +use core::num::NonZeroU32; |
| 10 | +use core::ops::Shl; |
| 11 | +use rand_core::RngCore; |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +pub enum ErrorKind { |
| 15 | + /// The RNG_CLK was not correctly detected (fRNG_CLK< fHCLK/16). |
| 16 | + /// See CECS in RNG peripheral documentation. |
| 17 | + ClockError = 2, |
| 18 | + /// RNG detected more than 64 consecutive bits of the same value (0 or 1) OR |
| 19 | + /// more than 32 consecutive 01 pairs. |
| 20 | + /// See SECS in RNG peripheral documentation. |
| 21 | + SeedError = 4, |
| 22 | +} |
| 23 | + |
| 24 | +impl From<ErrorKind> for rand_core::Error { |
| 25 | + fn from(err: ErrorKind) -> rand_core::Error { |
| 26 | + let err_code = NonZeroU32::new(rand_core::Error::CUSTOM_START + err as u32).unwrap(); |
| 27 | + rand_core::Error::from(err_code) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +pub trait RngExt { |
| 32 | + fn constrain(self, clocks: Clocks) -> Rng; |
| 33 | +} |
| 34 | + |
| 35 | +impl RngExt for RNG { |
| 36 | + /// Enable RNG_CLK and the RNG peripheral. |
| 37 | + /// Note that clocks must already be configured such that RNG_CLK is not less than 1/16 HCLK, |
| 38 | + /// otherwise all reads of the RNG would return a ClockError (CECS error). |
| 39 | + /// This function will panic if pll48clk < 1/16 hclk. |
| 40 | + fn constrain(self, clocks: Clocks) -> Rng { |
| 41 | + let rcc = unsafe { &*stm32::RCC::ptr() }; |
| 42 | + |
| 43 | + cortex_m::interrupt::free(|_| { |
| 44 | + // enable RNG_CLK (peripheral clock) |
| 45 | + rcc.ahb2enr.modify(|_, w| w.rngen().enabled()); |
| 46 | + // give RNG_CLK time to start |
| 47 | + let _ = rcc.ahb2enr.read().rngen().is_enabled(); |
| 48 | + |
| 49 | + // reset the RNG |
| 50 | + rcc.ahb2rstr.modify(|_, w| w.rngrst().set_bit()); |
| 51 | + rcc.ahb2rstr.modify(|_, w| w.rngrst().clear_bit()); |
| 52 | + |
| 53 | + // verify the clock configuration is valid |
| 54 | + let hclk = clocks.hclk(); |
| 55 | + let rng_clk = clocks.pll48clk().unwrap_or(0u32.hz()); |
| 56 | + assert!(rng_clk.0 >= (hclk.0 / 16)); |
| 57 | + |
| 58 | + // enable the RNG peripheral |
| 59 | + self.cr.modify(|_, w| w.rngen().set_bit()); |
| 60 | + }); |
| 61 | + |
| 62 | + Rng { rb: self } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +pub struct Rng { |
| 67 | + rb: RNG, |
| 68 | +} |
| 69 | + |
| 70 | +impl Rng { |
| 71 | + /// Returns 32 bits of random data from RNDATA, or error. |
| 72 | + /// May fail if, for example RNG_CLK is misconfigured. |
| 73 | + fn next_random_word(&mut self) -> Result<u32, ErrorKind> { |
| 74 | + loop { |
| 75 | + let status = self.rb.sr.read(); |
| 76 | + if status.cecs().bit() { |
| 77 | + return Err(ErrorKind::ClockError); |
| 78 | + } |
| 79 | + if status.secs().bit() { |
| 80 | + return Err(ErrorKind::SeedError); |
| 81 | + } |
| 82 | + if status.drdy().bit() { |
| 83 | + return Ok(self.rb.dr.read().rndata().bits()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + pub fn release(self) -> RNG { |
| 89 | + self.rb |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl rng::Read for Rng { |
| 94 | + type Error = rand_core::Error; |
| 95 | + |
| 96 | + fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 97 | + self.try_fill_bytes(buffer) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl RngCore for Rng { |
| 102 | + fn next_u32(&mut self) -> u32 { |
| 103 | + self.next_random_word().unwrap() |
| 104 | + } |
| 105 | + |
| 106 | + fn next_u64(&mut self) -> u64 { |
| 107 | + let w1 = self.next_u32(); |
| 108 | + let w2 = self.next_u32(); |
| 109 | + (w1 as u64).shl(32) | (w2 as u64) |
| 110 | + } |
| 111 | + |
| 112 | + fn fill_bytes(&mut self, dest: &mut [u8]) { |
| 113 | + self.try_fill_bytes(dest).unwrap() |
| 114 | + } |
| 115 | + |
| 116 | + /// Fills buffer with random values, or returns an error |
| 117 | + fn try_fill_bytes(&mut self, buffer: &mut [u8]) -> Result<(), rand_core::Error> { |
| 118 | + const BATCH_SIZE: usize = 4 / mem::size_of::<u8>(); |
| 119 | + let mut i = 0_usize; |
| 120 | + while i < buffer.len() { |
| 121 | + let random_word = self.next_random_word()?; |
| 122 | + let bytes = random_word.to_ne_bytes(); |
| 123 | + let n = cmp::min(BATCH_SIZE, buffer.len() - i); |
| 124 | + buffer[i..i + n].copy_from_slice(&bytes[..n]); |
| 125 | + i += n; |
| 126 | + } |
| 127 | + Ok(()) |
| 128 | + } |
| 129 | +} |
0 commit comments