Skip to content
Open
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
logger:
- log
- defmt
features:
- rand
env: # Peripheral Feature flags
FLAGS: rt

Expand All @@ -45,6 +47,6 @@ jobs:
- name: Install thumbv8m rust target
run: rustup target add thumbv8m.main-none-eabihf
- name: Build
run: cargo build --verbose --release --examples --target thumbv8m.main-none-eabihf --features ${{ matrix.mcu }},${{ matrix.logger }},${{ env.FLAGS }}
run: cargo build --verbose --release --examples --target thumbv8m.main-none-eabihf --features ${{ matrix.mcu }},${{ matrix.features }},${{ matrix.logger }},${{ env.FLAGS }}
- name: Test
run: cargo test --lib --target x86_64-unknown-linux-gnu --features ${{ matrix.mcu }},${{ matrix.logger }},${{ env.FLAGS }}
run: cargo test --lib --target x86_64-unknown-linux-gnu --features ${{ matrix.mcu }},${{ matrix.features }},${{ matrix.logger }},${{ env.FLAGS }}
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ ethernet = [] # Only STM32H563/73 have ethernet
otfdec = [] # Only STM32H573/33 have OTFDEC
sdmmc2 = [] # Only STM32H563/73 have SDMMC2

rand = ["dep:rand_core"]

rt = ["stm32h5/rt"]
stm32h503 = ["stm32h5/stm32h503", "device-selected", "rm0492"]
stm32h523 = ["stm32h5/stm32h523", "device-selected", "rm0481", "h523_h533"]
Expand All @@ -64,6 +66,7 @@ embedded-hal = "1.0.0"
defmt = { version = "1.0.0", optional = true }
paste = "1.0.15"
log = { version = "0.4.20", optional = true}
rand_core = { version = "0.6", default-features = false, optional = true }

[dev-dependencies]
log = { version = "0.4.20"}
Expand Down
72 changes: 72 additions & 0 deletions examples/blinky_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Note: Code taken from stm32h7xx-hal
//! This example toggles the Pin PA5 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 embedded_hal::delay::DelayNs;
use stm32h5xx_hal::{delay::Delay, 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 = pwr.vos0().freeze();

// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
//let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SYSCFG);
let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SBS);

info!("");
info!("stm32h5xx-hal example - Random Blinky");
info!("");

let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);

// Configure PA5 as output.
let mut led = gpioa.pa5.into_push_pull_output();

// Get the delay provider.
let mut delay = Delay::new(cp.SYST, &ccdr.clocks);

// Get true random number generator
let mut rng = dp.RNG.rng(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),
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub mod icache;
#[cfg(feature = "device-selected")]
pub mod delay;

#[cfg(feature = "device-selected")]
pub mod rng;

#[cfg(feature = "device-selected")]
mod sealed {
pub trait Sealed {}
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pub use crate::icache::ICacheExt as _stm32h5xx_hal_icache_ICacheExt;
pub use crate::pwr::PwrExt as _stm32h5xx_hal_pwr_PwrExt;
pub use crate::rcc::RccExt as _stm32h5xx_hal_rcc_RccExt;

pub use crate::rng::{RngCore as _, RngExt as _};
pub use crate::time::U32Ext as _;
pub use fugit::{ExtU32 as _, RateExtU32 as _};
Loading