-
Notifications
You must be signed in to change notification settings - Fork 102
/
blinky_random.rs
70 lines (55 loc) · 2.02 KB
/
blinky_random.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! This example toggles the Pin PE1 with a randomly generated period between 0
//! and 200 milliseconds. The random period is generated by the internal
//! hardware random number generator.
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use stm32h7xx_hal::{pac, prelude::*};
#[macro_use]
mod utilities;
use log::info;
#[entry]
fn main() -> ! {
utilities::logger::init();
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().expect("cannot take peripherals");
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SYSCFG);
info!("");
info!("stm32h7xx-hal example - Random Blinky");
info!("");
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
// Configure PE1 as output.
let mut led = gpioe.pe1.into_push_pull_output();
// Get the delay provider.
let mut delay = cp.SYST.delay(ccdr.clocks);
// Get true random number generator
let mut rng = dp.RNG.constrain(ccdr.peripheral.RNG, &ccdr.clocks);
let mut random_bytes = [0u16; 3];
match rng.fill(&mut random_bytes) {
Ok(()) => info!("random bytes: {:?}", random_bytes),
Err(err) => info!("RNG error: {:?}", err),
}
loop {
let random_element: Result<u32, _> = rng.gen();
match random_element {
Ok(random) => {
// NOTE: the result of the expression `random % 200`
// is biased. This bias is called "modulo-bias". It is
// acceptable here for simplicity, but may not be
// acceptable for your application.
let period = random % 200_u32;
led.toggle();
delay.delay_ms(period);
}
Err(err) => info!("RNG error: {:?}", err),
}
}
}