6
6
//! - [Random Blinky](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/blinky_random.rs)
7
7
8
8
use core:: cmp;
9
+ use core:: marker:: PhantomData ;
9
10
use core:: mem;
10
11
11
12
use crate :: rcc:: { rec, rec:: RngClkSel } ;
@@ -113,10 +114,7 @@ impl RngNist for RNG {
113
114
// Enable RNG
114
115
self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
115
116
116
- Rng {
117
- rb : self ,
118
- _mode : NIST ,
119
- }
117
+ Rng :: new ( self )
120
118
}
121
119
}
122
120
@@ -167,10 +165,7 @@ impl RngExt for RNG {
167
165
// Enable RNG
168
166
self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
169
167
170
- Rng {
171
- rb : self ,
172
- _mode : NORMAL ,
173
- }
168
+ Rng :: new ( self )
174
169
}
175
170
176
171
/// 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 {
212
207
// Enable RNG
213
208
self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
214
209
215
- Rng {
216
- rb : self ,
217
- _mode : FAST ,
218
- }
210
+ Rng :: new ( self )
219
211
}
220
212
}
221
213
@@ -241,18 +233,28 @@ pub struct NORMAL;
241
233
242
234
pub struct Rng < MODE > {
243
235
rb : RNG ,
244
- _mode : MODE ,
236
+ _mode : PhantomData < MODE > ,
245
237
}
246
238
247
239
impl < MODE > Rng < MODE > {
240
+ fn new ( rb : RNG ) -> Self {
241
+ Self {
242
+ rb,
243
+ _mode : PhantomData ,
244
+ }
245
+ }
248
246
/// 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
249
249
pub fn value ( & mut self ) -> Result < u32 , Error > {
250
250
loop {
251
251
let status = self . rb . sr ( ) . read ( ) ;
252
252
253
253
if status. cecs ( ) . bit ( ) {
254
254
return Err ( Error :: ClockError ) ;
255
255
} 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 ( ) ) ;
256
258
return Err ( Error :: SeedError ) ;
257
259
} else if status. drdy ( ) . bit ( ) {
258
260
return Ok ( self . rb . dr ( ) . read ( ) . rndata ( ) . bits ( ) ) ;
0 commit comments