3
3
//!
4
4
//! # Examples
5
5
//!
6
- //! - [Random Blinky](https://github.com/stm32-rs/stm32h7xx -hal/blob/master/examples/blinky_random.rs)
6
+ //! - [Random Blinky](https://github.com/stm32-rs/stm32h5xx -hal/blob/master/examples/blinky_random.rs)
7
7
8
8
use core:: cmp;
9
9
use core:: mem;
@@ -20,33 +20,28 @@ pub enum ErrorKind {
20
20
SeedError = 1 ,
21
21
}
22
22
23
- pub trait KerClk {
24
- /// Return the kernel clock for the Random Number Generator
25
- ///
26
- /// # Panics
27
- ///
28
- /// Panics if the kernel clock is not running
29
- fn kernel_clk_unwrap ( prec : rec:: Rng , clocks : & CoreClocks ) -> Hertz ;
30
- }
31
-
32
- impl KerClk for RNG {
33
- fn kernel_clk_unwrap ( prec : rec:: Rng , clocks : & CoreClocks ) -> Hertz {
34
- match prec. get_kernel_clk_mux ( ) {
35
- //RngClkSel::Hsi48 => {
36
- RngClkSel :: Hsi48Ker => {
37
- clocks. hsi48_ck ( ) . expect ( "RNG: HSI48 must be enabled" )
38
- }
39
- RngClkSel :: Pll1Q => {
40
- clocks. pll1_q_ck ( ) . expect ( "RNG: PLL1_Q must be enabled" )
41
- }
42
- RngClkSel :: Lse => unimplemented ! ( ) ,
43
- RngClkSel :: Lsi => {
44
- clocks. lsi_ck ( ) . expect ( "RNG: LSI must be enabled" )
45
- }
23
+ /// Return the kernel clock for the Random Number Generator
24
+ ///
25
+ /// # Panics
26
+ ///
27
+ /// Panics if the kernel clock is not running
28
+ fn kernel_clk_unwrap ( prec : rec:: Rng , clocks : & CoreClocks ) -> Hertz {
29
+ match prec. get_kernel_clk_mux ( ) {
30
+ //RngClkSel::Hsi48 => {
31
+ RngClkSel :: Hsi48Ker => {
32
+ clocks. hsi48_ck ( ) . expect ( "RNG: HSI48 must be enabled" )
33
+ }
34
+ RngClkSel :: Pll1Q => {
35
+ clocks. pll1_q_ck ( ) . expect ( "RNG: PLL1_Q must be enabled" )
36
+ }
37
+ RngClkSel :: Lse => unimplemented ! ( ) ,
38
+ RngClkSel :: Lsi => {
39
+ clocks. lsi_ck ( ) . expect ( "RNG: LSI must be enabled" )
46
40
}
47
41
}
48
42
}
49
43
44
+
50
45
pub trait RngExt {
51
46
fn constrain ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng ;
52
47
}
@@ -56,7 +51,7 @@ impl RngExt for RNG {
56
51
let prec = prec. enable ( ) . reset ( ) ;
57
52
58
53
let hclk = clocks. hclk ( ) ;
59
- let rng_clk = Self :: kernel_clk_unwrap ( prec, clocks) ;
54
+ let rng_clk = kernel_clk_unwrap ( prec, clocks) ;
60
55
61
56
// Otherwise clock checker will always flag an error
62
57
// See RM0433 Rev 6 Section 33.3.6
@@ -94,6 +89,25 @@ impl Rng {
94
89
}
95
90
}
96
91
}
92
+
93
+ /// Returns 32 bits of randomness, or error
94
+ pub fn nb_value ( & mut self ) -> nb:: Result < u32 , ErrorKind > {
95
+ loop {
96
+ let status = self . rb . sr ( ) . read ( ) ;
97
+ if status. cecs ( ) . bit ( ) {
98
+ return Err ( ErrorKind :: ClockError ) ;
99
+ }
100
+ if status. secs ( ) . bit ( ) {
101
+ return Err ( ErrorKind :: SeedError ) ;
102
+ }
103
+ if status. drdy ( ) . bit ( ) {
104
+ return Ok ( self . rb . dr ( ) . read ( ) . rndata ( ) . bits ( ) ) ;
105
+ }
106
+ else {
107
+ return Err ( nb:: Error :: WouldBlock ) ;
108
+ }
109
+ }
110
+ }
97
111
98
112
pub fn release ( self ) -> RNG {
99
113
self . rb
@@ -130,7 +144,7 @@ macro_rules! rng_core {
130
144
131
145
/// Fills buffer with random values, or return error
132
146
fn fill( & mut self , buffer: & mut [ $type] ) -> Result <( ) , ErrorKind > {
133
- const BATCH_SIZE : usize = 4 / mem:: size_of:: <$type>( ) ;
147
+ const BATCH_SIZE : usize = mem :: size_of :: < u32 > ( ) / mem:: size_of:: <$type>( ) ;
134
148
let mut i = 0_usize ;
135
149
while i < buffer. len( ) {
136
150
let random_word = self . value( ) ?;
@@ -160,7 +174,7 @@ macro_rules! rng_core_large {
160
174
let mut res: $type = 0 ;
161
175
162
176
for i in 0 ..WORDS {
163
- res |= ( self . value( ) ? as $type) << ( i * ( mem :: size_of :: < u32 > ( ) * 8 ) )
177
+ res |= ( self . value( ) ? as $type) << ( i * ( u32 :: BITS as usize ) )
164
178
}
165
179
166
180
Ok ( res)
@@ -258,6 +272,3 @@ impl rand_core::RngCore for Rng {
258
272
}
259
273
}
260
274
261
- #[ cfg( feature = "rand" ) ]
262
- #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
263
- impl rand_core:: CryptoRng for Rng { }
0 commit comments