Skip to content

Commit 36ce8e2

Browse files
author
Henrik Snöman
committed
Removed clock error type
1 parent 3e2d1c6 commit 36ce8e2

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/rng.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ use crate::time::Hertz;
1616

1717
#[derive(Debug)]
1818
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
19-
pub enum Error {
20-
///Note: The clock error has no impact on generated random numbers that is the application can still read the RNG_DR register
21-
ClockError = 0,
22-
SeedError = 1,
23-
}
19+
pub struct SeedError;
2420

2521
/// Return the kernel clock for the Random Number Generator
2622
///
@@ -212,8 +208,8 @@ impl RngExt for RNG {
212208
}
213209

214210
pub trait RngCore<W> {
215-
fn gen(&mut self) -> Result<W, Error>;
216-
fn fill(&mut self, dest: &mut [W]) -> Result<(), Error>;
211+
fn gen(&mut self) -> Result<W, SeedError>;
212+
fn fill(&mut self, dest: &mut [W]) -> Result<(), SeedError>;
217213
}
218214

219215
#[cfg(any(
@@ -246,7 +242,7 @@ impl<MODE> Rng<MODE> {
246242
/// Returns 32 bits of randomness, or error
247243
/// Automatically resets the seed error flag upon SeedError but will still return SeedError
248244
/// Upon receiving SeedError the user is expected to keep polling this function until a valid value is returned
249-
pub fn value(&mut self) -> Result<u32, Error> {
245+
pub fn value(&mut self) -> Result<u32, SeedError> {
250246
'outer: loop {
251247
let status = self.rb.sr().read();
252248

@@ -262,7 +258,7 @@ impl<MODE> Rng<MODE> {
262258
} else if status.secs().bit() {
263259
// Reset seed error flag so as to leave the peripheral in a valid state ready for use
264260
self.rb.sr().modify(|_, w| w.seis().clear_bit());
265-
return Err(Error::SeedError);
261+
return Err(SeedError);
266262
} else if status.drdy().bit() {
267263
return Ok(self.rb.dr().read().rndata().bits());
268264
}
@@ -285,7 +281,7 @@ impl<MODE> Rng<MODE> {
285281
}
286282

287283
impl<MODE> core::iter::Iterator for Rng<MODE> {
288-
type Item = Result<u32, Error>;
284+
type Item = Result<u32, SeedError>;
289285

290286
fn next(&mut self) -> Option<Self::Item> {
291287
Some(self.value())
@@ -297,13 +293,13 @@ macro_rules! rng_core {
297293
$(
298294
impl<MODE> RngCore<$type> for Rng<MODE> {
299295
/// Returns a single element with random value, or error
300-
fn gen(&mut self) -> Result<$type, Error> {
296+
fn gen(&mut self) -> Result<$type, SeedError> {
301297
let val = self.value()?;
302298
Ok(val as $type)
303299
}
304300

305301
/// Fills buffer with random values, or return error
306-
fn fill(&mut self, buffer: &mut [$type]) -> Result<(), Error> {
302+
fn fill(&mut self, buffer: &mut [$type]) -> Result<(), SeedError> {
307303
const BATCH_SIZE: usize = mem::size_of::<u32>() / mem::size_of::<$type>();
308304
let mut i = 0_usize;
309305
while i < buffer.len() {
@@ -329,7 +325,7 @@ macro_rules! rng_core_large {
329325
($($type:ty),+) => {
330326
$(
331327
impl<MODE> RngCore<$type> for Rng<MODE> {
332-
fn gen(&mut self) -> Result<$type, Error> {
328+
fn gen(&mut self) -> Result<$type, SeedError> {
333329
const WORDS: usize = mem::size_of::<$type>() / mem::size_of::<u32>();
334330
let mut res: $type = 0;
335331

@@ -340,7 +336,7 @@ macro_rules! rng_core_large {
340336
Ok(res)
341337
}
342338

343-
fn fill(&mut self, dest: &mut [$type]) -> Result<(), Error> {
339+
fn fill(&mut self, dest: &mut [$type]) -> Result<(), SeedError> {
344340
let len = dest.len() * (mem::size_of::<$type>() / mem::size_of::<u32>());
345341
let ptr = dest.as_mut_ptr() as *mut u32;
346342
let slice_u32 = unsafe { core::slice::from_raw_parts_mut(ptr, len) };
@@ -355,12 +351,12 @@ macro_rules! rng_core_transmute {
355351
($($type:ty = $from:ty),+) => {
356352
$(
357353
impl<MODE> RngCore<$type> for Rng<MODE> {
358-
fn gen(&mut self) -> Result<$type, Error> {
354+
fn gen(&mut self) -> Result<$type, SeedError> {
359355
let num = <Self as RngCore<$from>>::gen(self)?;
360356
Ok(unsafe { mem::transmute::<$from, $type>(num) })
361357
}
362358

363-
fn fill(&mut self, dest: &mut [$type]) -> Result<(), Error> {
359+
fn fill(&mut self, dest: &mut [$type]) -> Result<(), SeedError> {
364360
let unsigned_slice = unsafe { mem::transmute::<&mut [$type], &mut [$from]>(dest) };
365361
<Self as RngCore<$from>>::fill(self, unsigned_slice)
366362
}

0 commit comments

Comments
 (0)