Skip to content

Commit d1a32eb

Browse files
author
Henrik Snöman
committed
Updated to latest and fixed some desired changes
1 parent d7601dd commit d1a32eb

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/rng.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::time::Hertz;
1515

1616
#[derive(Debug)]
1717
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
18-
pub enum ErrorKind {
18+
pub enum Error {
1919
///Note: The clock error has no impact on generated random numbers that is the application can still read the RNG_DR register
2020
ClockError = 0,
2121
SeedError = 1,
@@ -33,7 +33,7 @@ fn kernel_clk_unwrap(prec: rec::Rng, clocks: &CoreClocks) -> Hertz {
3333
clocks.hsi48_ck().expect("RNG: HSI48 must be enabled")
3434
}
3535
RngClkSel::Pll1Q => {
36-
clocks.pll1_q_ck().expect("RNG: PLL1_Q must be enabled")
36+
clocks.pll1().q_ck().expect("RNG: PLL1_Q must be enabled")
3737
}
3838
RngClkSel::Lse => unimplemented!(),
3939
RngClkSel::Lsi => clocks.lsi_ck().expect("RNG: LSI must be enabled"),
@@ -59,8 +59,10 @@ fn setup_clocks(prec: rec::Rng, clocks: &CoreClocks) -> Hertz {
5959
feature = "stm32h573",
6060
))]
6161

62-
/// This uses the register values specified in AN4230 but have not
63-
/// performed the verification (buyer beware, users can/should do their own verification)
62+
/// Note:
63+
/// This uses the register values specified in AN4230 but verification
64+
/// using this HAL has not been performed. Users can/should do their
65+
/// own verification or request documentation from ST directly.
6466
/// Requires RNG to be disabled since some register values can only be written when RNGEN = 0
6567
pub trait RngNist {
6668
fn rng_nist_st_an4230(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng;
@@ -72,8 +74,10 @@ pub trait RngNist {
7274
feature = "stm32h573"
7375
))]
7476
impl RngNist for RNG {
75-
/// This uses the register values specified in AN4230 but have not
76-
/// performed the verification (buyer beware, users can/should do their own verification)
77+
/// Note:
78+
/// This uses the register values specified in AN4230 but verification
79+
/// using this HAL has not been performed. Users can/should do their
80+
/// own verification or request documentation from ST directly.
7781
/// Requires RNG to be disabled since some register values can only be written when RNGEN = 0
7882
fn rng_nist_st_an4230(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng {
7983
let rng_clk = setup_clocks(prec, clocks);
@@ -137,7 +141,9 @@ impl RngExt for RNG {
137141
self.htcr().write(|w| unsafe { w.bits(0xAAC7) });
138142

139143
// Set noise source control register
140-
#[cfg(not(feature = "stm32h503"))] // Not available on H503
144+
// Note:
145+
// This is currently not available in the PAC or SVD for H503 but is planned to be added
146+
#[cfg(not(feature = "stm32h503"))]
141147
self.nscr().write(|w| unsafe { w.bits(0x0003FFFF) });
142148

143149
// Configuration done, reset CONDRST, its value goes to 0 when the reset process is
@@ -197,8 +203,8 @@ impl RngExt for RNG {
197203
}
198204

199205
pub trait RngCore<W> {
200-
fn gen(&mut self) -> Result<W, ErrorKind>;
201-
fn fill(&mut self, dest: &mut [W]) -> Result<(), ErrorKind>;
206+
fn gen(&mut self) -> Result<W, Error>;
207+
fn fill(&mut self, dest: &mut [W]) -> Result<(), Error>;
202208
}
203209

204210
pub struct Rng {
@@ -207,17 +213,17 @@ pub struct Rng {
207213

208214
impl Rng {
209215
/// Returns 32 bits of randomness, or error
210-
pub fn value(&mut self) -> Result<u32, ErrorKind> {
216+
pub fn value(&mut self) -> Result<u32, Error> {
211217
nb::block!(self.nb_value())
212218
}
213219

214220
/// Returns 32 bits of randomness, or error
215-
pub fn nb_value(&mut self) -> nb::Result<u32, ErrorKind> {
221+
pub fn nb_value(&mut self) -> nb::Result<u32, Error> {
216222
let status = self.rb.sr().read();
217223
if status.cecs().bit() {
218-
Err(nb::Error::Other(ErrorKind::ClockError))
224+
Err(nb::Error::Other(Error::ClockError))
219225
} else if status.secs().bit() {
220-
Err(nb::Error::Other(ErrorKind::SeedError))
226+
Err(nb::Error::Other(Error::SeedError))
221227
} else if status.drdy().bit() {
222228
Ok(self.rb.dr().read().rndata().bits())
223229
} else {
@@ -253,13 +259,13 @@ macro_rules! rng_core {
253259
$(
254260
impl RngCore<$type> for Rng {
255261
/// Returns a single element with random value, or error
256-
fn gen(&mut self) -> Result<$type, ErrorKind> {
262+
fn gen(&mut self) -> Result<$type, Error> {
257263
let val = self.value()?;
258264
Ok(val as $type)
259265
}
260266

261267
/// Fills buffer with random values, or return error
262-
fn fill(&mut self, buffer: &mut [$type]) -> Result<(), ErrorKind> {
268+
fn fill(&mut self, buffer: &mut [$type]) -> Result<(), Error> {
263269
const BATCH_SIZE: usize = mem::size_of::<u32>() / mem::size_of::<$type>();
264270
let mut i = 0_usize;
265271
while i < buffer.len() {
@@ -285,7 +291,7 @@ macro_rules! rng_core_large {
285291
($($type:ty),+) => {
286292
$(
287293
impl RngCore<$type> for Rng {
288-
fn gen(&mut self) -> Result<$type, ErrorKind> {
294+
fn gen(&mut self) -> Result<$type, Error> {
289295
const WORDS: usize = mem::size_of::<$type>() / mem::size_of::<u32>();
290296
let mut res: $type = 0;
291297

@@ -296,7 +302,7 @@ macro_rules! rng_core_large {
296302
Ok(res)
297303
}
298304

299-
fn fill(&mut self, dest: &mut [$type]) -> Result<(), ErrorKind> {
305+
fn fill(&mut self, dest: &mut [$type]) -> Result<(), Error> {
300306
let len = dest.len() * (mem::size_of::<$type>() / mem::size_of::<u32>());
301307
let ptr = dest.as_mut_ptr() as *mut u32;
302308
let slice_u32 = unsafe { core::slice::from_raw_parts_mut(ptr, len) };
@@ -311,12 +317,12 @@ macro_rules! rng_core_transmute {
311317
($($type:ty = $from:ty),+) => {
312318
$(
313319
impl RngCore<$type> for Rng {
314-
fn gen(&mut self) -> Result<$type, ErrorKind> {
320+
fn gen(&mut self) -> Result<$type, Error> {
315321
let num = <Self as RngCore<$from>>::gen(self)?;
316322
Ok(unsafe { mem::transmute::<$from, $type>(num) })
317323
}
318324

319-
fn fill(&mut self, dest: &mut [$type]) -> Result<(), ErrorKind> {
325+
fn fill(&mut self, dest: &mut [$type]) -> Result<(), Error> {
320326
let unsigned_slice = unsafe { mem::transmute::<&mut [$type], &mut [$from]>(dest) };
321327
<Self as RngCore<$from>>::fill(self, unsigned_slice)
322328
}

0 commit comments

Comments
 (0)