Skip to content

Commit 37f93f5

Browse files
author
Henrik Snöman
committed
Added example
1 parent ec70ee8 commit 37f93f5

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

examples/blinky_random.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Note: Code taken from stm32h7xx-hal
2+
//! This example toggles the Pin PA5 with a randomly generated period between 0
3+
//! and 200 milliseconds. The random period is generated by the internal
4+
//! hardware random number generator.
5+
6+
#![deny(warnings)]
7+
#![no_main]
8+
#![no_std]
9+
10+
use cortex_m_rt::entry;
11+
use embedded_hal::delay::DelayNs;
12+
use stm32h5xx_hal::{
13+
delay::Delay,
14+
pac,
15+
prelude::*,
16+
rng::{RngCore, RngExt},
17+
};
18+
#[macro_use]
19+
mod utilities;
20+
21+
use log::info;
22+
23+
#[entry]
24+
fn main() -> ! {
25+
utilities::logger::init();
26+
let cp = cortex_m::Peripherals::take().unwrap();
27+
let dp = pac::Peripherals::take().expect("cannot take peripherals");
28+
29+
// Constrain and Freeze power
30+
info!("Setup PWR... ");
31+
let pwr = dp.PWR.constrain();
32+
let pwrcfg = pwr.vos0().freeze();
33+
34+
// Constrain and Freeze clock
35+
info!("Setup RCC... ");
36+
let rcc = dp.RCC.constrain();
37+
//let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SYSCFG);
38+
let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SBS);
39+
40+
info!("");
41+
info!("stm32h5xx-hal example - Random Blinky");
42+
info!("");
43+
44+
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
45+
46+
// Configure PA5 as output.
47+
let mut led = gpioa.pa5.into_push_pull_output();
48+
49+
// Get the delay provider.
50+
let mut delay = Delay::new(cp.SYST, &ccdr.clocks);
51+
52+
// Get true random number generator
53+
let mut rng = dp.RNG.constrain(ccdr.peripheral.RNG, &ccdr.clocks);
54+
let mut random_bytes = [0u16; 3];
55+
match rng.fill(&mut random_bytes) {
56+
Ok(()) => info!("random bytes: {:?}", random_bytes),
57+
Err(err) => info!("RNG error: {:?}", err),
58+
}
59+
60+
loop {
61+
let random_element: Result<u32, _> = rng.gen();
62+
63+
match random_element {
64+
Ok(random) => {
65+
// NOTE: the result of the expression `random % 200`
66+
// is biased. This bias is called "modulo-bias". It is
67+
// acceptable here for simplicity, but may not be
68+
// acceptable for your application.
69+
let period = random % 200_u32;
70+
71+
led.toggle();
72+
delay.delay_ms(period);
73+
}
74+
Err(err) => info!("RNG error: {:?}", err),
75+
}
76+
}
77+
}

src/rng.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ impl RngExt for RNG {
6262
// See RM0433 Rev 6 Section 33.3.6
6363
assert!(rng_clk > hclk / 32, "RNG: Clock too slow");
6464

65-
self.cr().modify(|_, w| w.ced().clear_bit().rngen().set_bit());
65+
self.cr()
66+
.modify(|_, w| w.ced().clear_bit().rngen().set_bit());
6667

6768
Rng { rb: self }
6869
}
@@ -259,4 +260,4 @@ impl rand_core::RngCore for Rng {
259260

260261
#[cfg(feature = "rand")]
261262
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
262-
impl rand_core::CryptoRng for Rng {}
263+
impl rand_core::CryptoRng for Rng {}

0 commit comments

Comments
 (0)