33//!
44//! # Examples
55//!
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)
77
88use core:: cmp;
99use core:: mem;
@@ -20,33 +20,28 @@ pub enum ErrorKind {
2020 SeedError = 1 ,
2121}
2222
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" )
4640 }
4741 }
4842}
4943
44+
5045pub trait RngExt {
5146 fn constrain ( self , prec : rec:: Rng , clocks : & CoreClocks ) -> Rng ;
5247}
@@ -56,7 +51,7 @@ impl RngExt for RNG {
5651 let prec = prec. enable ( ) . reset ( ) ;
5752
5853 let hclk = clocks. hclk ( ) ;
59- let rng_clk = Self :: kernel_clk_unwrap ( prec, clocks) ;
54+ let rng_clk = kernel_clk_unwrap ( prec, clocks) ;
6055
6156 // Otherwise clock checker will always flag an error
6257 // See RM0433 Rev 6 Section 33.3.6
@@ -94,6 +89,25 @@ impl Rng {
9489 }
9590 }
9691 }
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+ }
97111
98112 pub fn release ( self ) -> RNG {
99113 self . rb
@@ -130,7 +144,7 @@ macro_rules! rng_core {
130144
131145 /// Fills buffer with random values, or return error
132146 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>( ) ;
134148 let mut i = 0_usize ;
135149 while i < buffer. len( ) {
136150 let random_word = self . value( ) ?;
@@ -160,7 +174,7 @@ macro_rules! rng_core_large {
160174 let mut res: $type = 0 ;
161175
162176 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 ) )
164178 }
165179
166180 Ok ( res)
@@ -258,6 +272,3 @@ impl rand_core::RngCore for Rng {
258272 }
259273}
260274
261- #[ cfg( feature = "rand" ) ]
262- #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
263- impl rand_core:: CryptoRng for Rng { }
0 commit comments