Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Updated minimum `chrono` version to `0.4.23` to satisfy `cargo-audit`.
- Updated defmt from `0.3` to `1`.
- Updated `rand_core` `0.6` to `0.9`.
- `stm32wlxx_hal::rng::Rng` now implements `rand_core::TryRngCore` instead of `rand_core::RngCore`.
- `stm32wlxx_hal::rng::Rng` now implements `rand_core::TryCryptoRng` instead of `rand_core::CryptoRng`.
- Changed the edition from 2021 to 2024.
- Changed minimum supported rust version from 1.60 to 1.85.

Expand Down
30 changes: 18 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ nb = "1"
num-traits = { version = "0.2", default-features = false }
num-integer = { version = "0.1", default-features = false }
paste = "1"
rand_core = "0.6"
rand_core = "0.9"
stm32wl = { version = "0.15.1", default-features = false }
void = { version = "1", default-features = false }

Expand Down
45 changes: 16 additions & 29 deletions hal/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

use crate::pac;

use core::num::NonZeroU32;

/// RNG trait abstractions
pub use rand_core;

Expand All @@ -35,13 +33,13 @@ pub enum Error {
Clock,
}

impl From<Error> for rand_core::Error {
fn from(e: Error) -> Self {
match e {
// safety: 1 is non-zero
Error::Seed => unsafe { NonZeroU32::new_unchecked(1) }.into(),
// safety: 2 is non-zero
Error::Clock => unsafe { NonZeroU32::new_unchecked(2) }.into(),
impl core::error::Error for Error {}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Seed => f.write_str("seed"),
Error::Clock => f.write_str("clock"),
}
}
}
Expand Down Expand Up @@ -456,31 +454,20 @@ impl Rng {
}
}

impl rand_core::RngCore for Rng {
/// Not recommended for use, panics upon errors.
fn next_u32(&mut self) -> u32 {
let mut dws: [u32; 1] = [0; 1];
unwrap!(self.try_fill_u32(&mut dws));
dws[0]
}
impl rand_core::TryRngCore for Rng {
type Error = Error;

/// Not recommended for use, panics upon errors.
fn next_u64(&mut self) -> u64 {
let mut dws: [u32; 2] = [0; 2];
unwrap!(self.try_fill_u32(&mut dws));
u64::from(dws[0]) << 32 | u64::from(dws[1])
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.try_fill_u8(dest)
}

/// Not recommended for use, panics upon errors.
fn fill_bytes(&mut self, dest: &mut [u8]) {
unwrap!(self.try_fill_u8(dest))
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
self.try_u32()
}

/// Use this method if using the `RngCore` for `CryptoRng` traits.
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.try_fill_u8(dest)?;
Ok(())
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
self.try_u64()
}
}

impl rand_core::CryptoRng for Rng {}
impl rand_core::TryCryptoRng for Rng {}
4 changes: 2 additions & 2 deletions testsuite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ itertools = { version = "0.14", default-features = false }
nb = "1"
p256 = { version = "0.13", default-features = false, features = ["arithmetic", "ecdsa"] }
panic-probe = { version = "1", features = ["print-defmt" ] }
rand = { version = "0.8", default-features = false }
rand_chacha = { version = "0.3", default-features = false }
rand = { version = "0.9", default-features = false }
rand_chacha = { version = "0.9", default-features = false }
static_assertions = "1"

[dependencies.nucleo-wl55jc-bsp]
Expand Down
21 changes: 13 additions & 8 deletions testsuite/src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use nucleo_wl55jc_bsp::hal::{
flash::{self, AlignedAddr, Error, Flash, Page, flash_range},
pac::{self, DWT},
rcc,
rng::{self, Rng, rand_core::RngCore},
rng::{self, Rng, rand_core::SeedableRng as _, rand_core::TryRngCore as _},
};
use panic_probe as _;
use rand::Rng as RngTrait;
use rand::Rng as _;
use rand_chacha::ChaCha20Rng;
use static_assertions as sa;

const FREQ: u32 = 48_000_000;
Expand Down Expand Up @@ -53,7 +54,7 @@ mod tests {
// address to use for testing
// incremented by the test after the address is programmed
addr: usize,
rng: Rng,
rng: ChaCha20Rng,
}

#[init]
Expand All @@ -71,9 +72,13 @@ mod tests {

let mut rng: Rng = Rng::new(dp.RNG, rng::Clk::Msi, &mut dp.RCC);

let mut seed: [u8; 32] = [0; 32];
unwrap!(rng.try_fill_u8(&mut seed));
let mut cha20: ChaCha20Rng = ChaCha20Rng::from_seed(seed);

// flash only gets 20k program cycles
// change the location each time to prevent wearout of CI boards
let page: u8 = rng.gen_range(64..127);
let page: u8 = cha20.random_range(64..127);
let page: Page = unwrap!(Page::from_index(page));

defmt::info!(
Expand All @@ -86,7 +91,7 @@ mod tests {
flash: dp.FLASH,
page,
addr: page.addr(),
rng,
rng: cha20,
}
}

Expand Down Expand Up @@ -134,7 +139,7 @@ mod tests {
#[allow(static_mut_refs)]
unsafe {
BUF.iter_mut()
.for_each(|word| *word = ta.rng.gen_range(1..u64::MAX - 1))
.for_each(|word| *word = ta.rng.random_range(1..u64::MAX - 1))
};

let mut flash: Flash = Flash::unlock(&mut ta.flash);
Expand All @@ -160,7 +165,7 @@ mod tests {

#[test]
fn standard_program(ta: &mut TestArgs) {
let data: u64 = ta.rng.gen_range(1..u64::MAX - 1);
let data: u64 = ta.rng.random_range(1..u64::MAX - 1);
defmt::assert_ne!(data, u64::MAX);
defmt::assert_ne!(data, 0);

Expand Down Expand Up @@ -208,7 +213,7 @@ mod tests {
#[test]
fn program_bytes(ta: &mut TestArgs) {
let mut data: [u8; 17] = [0; 17];
ta.rng.fill_bytes(&mut data);
unwrap!(ta.rng.try_fill_bytes(&mut data));

defmt::trace!("data={}", data);

Expand Down
8 changes: 4 additions & 4 deletions testsuite/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use defmt::unwrap;
use defmt_rtt as _; // global logger
use nucleo_wl55jc_bsp::hal::{
cortex_m, pac, rcc,
rng::{Clk, Rng, rand_core::RngCore},
rng::{Clk, Rng, rand_core::TryRngCore as _},
};
use panic_probe as _;

Expand Down Expand Up @@ -52,11 +52,11 @@ mod tests {
let mut seed: [u8; 32] = [0; 32];

unwrap!(rng.try_fill_u8(&mut seed));
let mut cha20: ChaCha20Rng = ChaCha20Rng::from_seed([0u8; 32]);
let mut cha20: ChaCha20Rng = ChaCha20Rng::from_seed(seed);
unwrap!(rng.try_fill_u8(&mut seed));
let mut cha12: ChaCha12Rng = ChaCha12Rng::from_seed([0u8; 32]);
let mut cha12: ChaCha12Rng = ChaCha12Rng::from_seed(seed);
unwrap!(rng.try_fill_u8(&mut seed));
let mut cha8: ChaCha8Rng = ChaCha8Rng::from_seed([0u8; 32]);
let mut cha8: ChaCha8Rng = ChaCha8Rng::from_seed(seed);

let mut cp = unwrap!(pac::CorePeripherals::take());
cp.DCB.enable_trace();
Expand Down