@@ -16,11 +16,7 @@ use crate::time::Hertz;
16
16
17
17
#[ derive( Debug ) ]
18
18
#[ 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 ;
24
20
25
21
/// Return the kernel clock for the Random Number Generator
26
22
///
@@ -212,8 +208,8 @@ impl RngExt for RNG {
212
208
}
213
209
214
210
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 > ;
217
213
}
218
214
219
215
#[ cfg( any(
@@ -246,7 +242,7 @@ impl<MODE> Rng<MODE> {
246
242
/// Returns 32 bits of randomness, or error
247
243
/// Automatically resets the seed error flag upon SeedError but will still return SeedError
248
244
/// 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 > {
250
246
' outer: loop {
251
247
let status = self . rb . sr ( ) . read ( ) ;
252
248
@@ -262,7 +258,7 @@ impl<MODE> Rng<MODE> {
262
258
} else if status. secs ( ) . bit ( ) {
263
259
// Reset seed error flag so as to leave the peripheral in a valid state ready for use
264
260
self . rb . sr ( ) . modify ( |_, w| w. seis ( ) . clear_bit ( ) ) ;
265
- return Err ( Error :: SeedError ) ;
261
+ return Err ( SeedError ) ;
266
262
} else if status. drdy ( ) . bit ( ) {
267
263
return Ok ( self . rb . dr ( ) . read ( ) . rndata ( ) . bits ( ) ) ;
268
264
}
@@ -285,7 +281,7 @@ impl<MODE> Rng<MODE> {
285
281
}
286
282
287
283
impl < MODE > core:: iter:: Iterator for Rng < MODE > {
288
- type Item = Result < u32 , Error > ;
284
+ type Item = Result < u32 , SeedError > ;
289
285
290
286
fn next ( & mut self ) -> Option < Self :: Item > {
291
287
Some ( self . value ( ) )
@@ -297,13 +293,13 @@ macro_rules! rng_core {
297
293
$(
298
294
impl <MODE > RngCore <$type> for Rng <MODE > {
299
295
/// 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 > {
301
297
let val = self . value( ) ?;
302
298
Ok ( val as $type)
303
299
}
304
300
305
301
/// 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 > {
307
303
const BATCH_SIZE : usize = mem:: size_of:: <u32 >( ) / mem:: size_of:: <$type>( ) ;
308
304
let mut i = 0_usize ;
309
305
while i < buffer. len( ) {
@@ -329,7 +325,7 @@ macro_rules! rng_core_large {
329
325
( $( $type: ty) ,+) => {
330
326
$(
331
327
impl <MODE > RngCore <$type> for Rng <MODE > {
332
- fn gen ( & mut self ) -> Result <$type, Error > {
328
+ fn gen ( & mut self ) -> Result <$type, SeedError > {
333
329
const WORDS : usize = mem:: size_of:: <$type>( ) / mem:: size_of:: <u32 >( ) ;
334
330
let mut res: $type = 0 ;
335
331
@@ -340,7 +336,7 @@ macro_rules! rng_core_large {
340
336
Ok ( res)
341
337
}
342
338
343
- fn fill( & mut self , dest: & mut [ $type] ) -> Result <( ) , Error > {
339
+ fn fill( & mut self , dest: & mut [ $type] ) -> Result <( ) , SeedError > {
344
340
let len = dest. len( ) * ( mem:: size_of:: <$type>( ) / mem:: size_of:: <u32 >( ) ) ;
345
341
let ptr = dest. as_mut_ptr( ) as * mut u32 ;
346
342
let slice_u32 = unsafe { core:: slice:: from_raw_parts_mut( ptr, len) } ;
@@ -355,12 +351,12 @@ macro_rules! rng_core_transmute {
355
351
( $( $type: ty = $from: ty) ,+) => {
356
352
$(
357
353
impl <MODE > RngCore <$type> for Rng <MODE > {
358
- fn gen ( & mut self ) -> Result <$type, Error > {
354
+ fn gen ( & mut self ) -> Result <$type, SeedError > {
359
355
let num = <Self as RngCore <$from>>:: gen ( self ) ?;
360
356
Ok ( unsafe { mem:: transmute:: <$from, $type>( num) } )
361
357
}
362
358
363
- fn fill( & mut self , dest: & mut [ $type] ) -> Result <( ) , Error > {
359
+ fn fill( & mut self , dest: & mut [ $type] ) -> Result <( ) , SeedError > {
364
360
let unsigned_slice = unsafe { mem:: transmute:: <& mut [ $type] , & mut [ $from] >( dest) } ;
365
361
<Self as RngCore <$from>>:: fill( self , unsigned_slice)
366
362
}
0 commit comments