Skip to content

Commit ff7b858

Browse files
author
Henrik Snöman
committed
Fixed some requested changes
1 parent e464b34 commit ff7b858

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

examples/blinky_random.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use stm32h5xx_hal::{
1313
delay::Delay,
1414
pac,
1515
prelude::*,
16-
rng::{RngCore, RngExt},
1716
};
1817
#[macro_use]
1918
mod utilities;

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pub use crate::rcc::RccExt as _stm32h5xx_hal_rcc_RccExt;
88

99
pub use crate::time::U32Ext as _;
1010
pub use fugit::{ExtU32 as _, RateExtU32 as _};
11+
pub use crate::rng::{RngCore as _, RngExt as _};

src/rng.rs

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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
88
use core::cmp;
99
use 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+
5045
pub 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

Comments
 (0)