66//! - [Random Blinky](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/blinky_random.rs)
77
88use core:: cmp;
9+ use core:: marker:: PhantomData ;
910use core:: mem;
1011
1112use crate :: rcc:: { rec, rec:: RngClkSel } ;
@@ -113,10 +114,7 @@ impl RngNist for RNG {
113114 // Enable RNG
114115 self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
115116
116- Rng {
117- rb : self ,
118- _mode : NIST ,
119- }
117+ Rng :: new ( self )
120118 }
121119}
122120
@@ -167,10 +165,7 @@ impl RngExt for RNG {
167165 // Enable RNG
168166 self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
169167
170- Rng {
171- rb : self ,
172- _mode : NORMAL ,
173- }
168+ Rng :: new ( self )
174169 }
175170
176171 /// This uses the register values specified in RM0481 Rev 2 section 32.6.2 RNG configuration B
@@ -212,10 +207,7 @@ impl RngExt for RNG {
212207 // Enable RNG
213208 self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
214209
215- Rng {
216- rb : self ,
217- _mode : FAST ,
218- }
210+ Rng :: new ( self )
219211 }
220212}
221213
@@ -241,18 +233,28 @@ pub struct NORMAL;
241233
242234pub struct Rng < MODE > {
243235 rb : RNG ,
244- _mode : MODE ,
236+ _mode : PhantomData < MODE > ,
245237}
246238
247239impl < MODE > Rng < MODE > {
240+ fn new ( rb : RNG ) -> Self {
241+ Self {
242+ rb,
243+ _mode : PhantomData ,
244+ }
245+ }
248246 /// Returns 32 bits of randomness, or error
247+ /// Automatically resets the seed error flag upon SeedError but will still return SeedError
248+ /// Upon receiving SeedError the user is expected to keep polling this function until a valid value is returned
249249 pub fn value ( & mut self ) -> Result < u32 , Error > {
250250 loop {
251251 let status = self . rb . sr ( ) . read ( ) ;
252252
253253 if status. cecs ( ) . bit ( ) {
254254 return Err ( Error :: ClockError ) ;
255255 } else if status. secs ( ) . bit ( ) {
256+ // Reset seed error flag so as to leave the peripheral in a valid state ready for use
257+ self . rb . sr ( ) . modify ( |_, w| w. seis ( ) . clear_bit ( ) ) ;
256258 return Err ( Error :: SeedError ) ;
257259 } else if status. drdy ( ) . bit ( ) {
258260 return Ok ( self . rb . dr ( ) . read ( ) . rndata ( ) . bits ( ) ) ;
0 commit comments