@@ -65,7 +65,11 @@ fn setup_clocks(prec: rec::Rng, clocks: &CoreClocks) -> Hertz {
65
65
/// own verification or request documentation from ST directly.
66
66
/// Requires RNG to be disabled since some register values can only be written when RNGEN = 0
67
67
pub trait RngNist {
68
- fn rng_nist_st_an4230 ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng ;
68
+ fn rng_nist_st_an4230 (
69
+ self ,
70
+ prec : rec:: Rng ,
71
+ clocks : & CoreClocks ,
72
+ ) -> Rng < NIST > ;
69
73
}
70
74
71
75
#[ cfg( any(
@@ -79,7 +83,11 @@ impl RngNist for RNG {
79
83
/// using this HAL has not been performed. Users can/should do their
80
84
/// own verification or request documentation from ST directly.
81
85
/// Requires RNG to be disabled since some register values can only be written when RNGEN = 0
82
- fn rng_nist_st_an4230 ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng {
86
+ fn rng_nist_st_an4230 (
87
+ self ,
88
+ prec : rec:: Rng ,
89
+ clocks : & CoreClocks ,
90
+ ) -> Rng < NIST > {
83
91
let rng_clk = setup_clocks ( prec, clocks) ;
84
92
85
93
// ST has tested this configuration only with a RNG clock of 48MHz
@@ -105,18 +113,21 @@ impl RngNist for RNG {
105
113
// Enable RNG
106
114
self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
107
115
108
- Rng { rb : self }
116
+ Rng {
117
+ rb : self ,
118
+ mode : NIST ,
119
+ }
109
120
}
110
121
}
111
122
112
123
pub trait RngExt {
113
- fn rng ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng ;
114
- fn rng_fast ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng ;
124
+ fn rng ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng < NORMAL > ;
125
+ fn rng_fast ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng < FAST > ;
115
126
}
116
127
117
128
impl RngExt for RNG {
118
129
/// This uses the register values specified in RM0481 Rev 2 section 32.6.2 RNG configuration C
119
- fn rng ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng {
130
+ fn rng ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng < NORMAL > {
120
131
setup_clocks ( prec, clocks) ;
121
132
122
133
// Set control register values, also need to write 1 to CONDRST to be able to set the other values
@@ -156,11 +167,14 @@ impl RngExt for RNG {
156
167
// Enable RNG
157
168
self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
158
169
159
- Rng { rb : self }
170
+ Rng {
171
+ rb : self ,
172
+ _mode : NORMAL ,
173
+ }
160
174
}
161
175
162
176
/// This uses the register values specified in RM0481 Rev 2 section 32.6.2 RNG configuration B
163
- fn rng_fast ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng {
177
+ fn rng_fast ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng < FAST > {
164
178
setup_clocks ( prec, clocks) ;
165
179
166
180
// Set control register values, also need to write 1 to CONDRST to be able to set the other values
@@ -198,7 +212,10 @@ impl RngExt for RNG {
198
212
// Enable RNG
199
213
self . cr ( ) . modify ( |_, w| w. rngen ( ) . set_bit ( ) ) ;
200
214
201
- Rng { rb : self }
215
+ Rng {
216
+ rb : self ,
217
+ _mode : FAST ,
218
+ }
202
219
}
203
220
}
204
221
@@ -207,27 +224,39 @@ pub trait RngCore<W> {
207
224
fn fill ( & mut self , dest : & mut [ W ] ) -> Result < ( ) , Error > ;
208
225
}
209
226
210
- pub struct Rng {
227
+ #[ cfg( any(
228
+ feature = "stm32h562" ,
229
+ feature = "stm32h563" ,
230
+ feature = "stm32h573"
231
+ ) ) ]
232
+ /// NIST mode (type state)
233
+ /// Use [RngNist::rng_nist_st_an4230] to generate [Rng] in this mode
234
+ pub struct NIST ;
235
+ /// FAST mode (type state)
236
+ /// Use [RngExt::rng_fast] to generate [Rng] in this mode
237
+ pub struct FAST ;
238
+ /// NORMAL mode (type state)
239
+ /// Use [RngExt::rng] to generate [Rng] in this mode
240
+ pub struct NORMAL ;
241
+
242
+ pub struct Rng < MODE > {
211
243
rb : RNG ,
244
+ _mode : MODE ,
212
245
}
213
246
214
- impl Rng {
247
+ impl < MODE > Rng < MODE > {
215
248
/// Returns 32 bits of randomness, or error
216
249
pub fn value ( & mut self ) -> Result < u32 , Error > {
217
- nb:: block!( self . nb_value( ) )
218
- }
219
-
220
- /// Returns 32 bits of randomness, or error
221
- pub fn nb_value ( & mut self ) -> nb:: Result < u32 , Error > {
222
- let status = self . rb . sr ( ) . read ( ) ;
223
- if status. cecs ( ) . bit ( ) {
224
- Err ( nb:: Error :: Other ( Error :: ClockError ) )
225
- } else if status. secs ( ) . bit ( ) {
226
- Err ( nb:: Error :: Other ( Error :: SeedError ) )
227
- } else if status. drdy ( ) . bit ( ) {
228
- Ok ( self . rb . dr ( ) . read ( ) . rndata ( ) . bits ( ) )
229
- } else {
230
- Err ( nb:: Error :: WouldBlock )
250
+ loop {
251
+ let status = self . rb . sr ( ) . read ( ) ;
252
+
253
+ if status. cecs ( ) . bit ( ) {
254
+ return Err ( Error :: ClockError ) ;
255
+ } else if status. secs ( ) . bit ( ) {
256
+ return Err ( Error :: SeedError ) ;
257
+ } else if status. drdy ( ) . bit ( ) {
258
+ return Ok ( self . rb . dr ( ) . read ( ) . rndata ( ) . bits ( ) ) ;
259
+ }
231
260
}
232
261
}
233
262
@@ -246,7 +275,7 @@ impl Rng {
246
275
}
247
276
}
248
277
249
- impl core:: iter:: Iterator for Rng {
278
+ impl < MODE > core:: iter:: Iterator for Rng < MODE > {
250
279
type Item = u32 ;
251
280
252
281
fn next ( & mut self ) -> Option < u32 > {
@@ -257,7 +286,7 @@ impl core::iter::Iterator for Rng {
257
286
macro_rules! rng_core {
258
287
( $( $type: ty) ,+) => {
259
288
$(
260
- impl RngCore <$type> for Rng {
289
+ impl < MODE > RngCore <$type> for Rng < MODE > {
261
290
/// Returns a single element with random value, or error
262
291
fn gen ( & mut self ) -> Result <$type, Error > {
263
292
let val = self . value( ) ?;
@@ -290,7 +319,7 @@ macro_rules! rng_core {
290
319
macro_rules! rng_core_large {
291
320
( $( $type: ty) ,+) => {
292
321
$(
293
- impl RngCore <$type> for Rng {
322
+ impl < MODE > RngCore <$type> for Rng < MODE > {
294
323
fn gen ( & mut self ) -> Result <$type, Error > {
295
324
const WORDS : usize = mem:: size_of:: <$type>( ) / mem:: size_of:: <u32 >( ) ;
296
325
let mut res: $type = 0 ;
@@ -316,7 +345,7 @@ macro_rules! rng_core_large {
316
345
macro_rules! rng_core_transmute {
317
346
( $( $type: ty = $from: ty) ,+) => {
318
347
$(
319
- impl RngCore <$type> for Rng {
348
+ impl < MODE > RngCore <$type> for Rng < MODE > {
320
349
fn gen ( & mut self ) -> Result <$type, Error > {
321
350
let num = <Self as RngCore <$from>>:: gen ( self ) ?;
322
351
Ok ( unsafe { mem:: transmute:: <$from, $type>( num) } )
@@ -359,7 +388,7 @@ rng_core_large!(usize);
359
388
// rand_core
360
389
#[ cfg( feature = "rand" ) ]
361
390
#[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
362
- impl rand_core:: RngCore for Rng {
391
+ impl < MODE > rand_core:: RngCore for Rng < MODE > {
363
392
/// Generate a random u32
364
393
/// Panics if RNG fails.
365
394
fn next_u32 ( & mut self ) -> u32 {
@@ -393,3 +422,10 @@ impl rand_core::RngCore for Rng {
393
422
} )
394
423
}
395
424
}
425
+
426
+ #[ cfg( all(
427
+ feature = "rand" ,
428
+ any( feature = "stm32h562" , feature = "stm32h563" , feature = "stm32h573" )
429
+ ) ) ]
430
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
431
+ impl rand_core:: CryptoRng for Rng < NIST > { }
0 commit comments