Skip to content

Commit

Permalink
refactor(rng): move hwrng to the nrf arch module
Browse files Browse the repository at this point in the history
This allows to enable doc generation for the `hwrng` Cargo feature,
to fix broken links in the docs.
  • Loading branch information
ROMemories committed Apr 16, 2024
1 parent e343fd3 commit e753c6a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions src/riot-rs-embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workspace = true
critical-section.workspace = true
linkme.workspace = true
static_cell.workspace = true
cfg-if.workspace = true

embassy-executor = { workspace = true, features = ["nightly"] }

Expand Down
9 changes: 9 additions & 0 deletions src/riot-rs-embassy/src/arch/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,12 @@ pub mod usb {
}
}
}

#[cfg(feature = "hwrng")]
pub mod hwrng {
use super::OptionalPeripherals;

pub fn construct_rng(_peripherals: &mut OptionalPeripherals) {
unimplemented!();
}
}
25 changes: 24 additions & 1 deletion src/riot-rs-embassy/src/arch/nrf52.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,32 @@ pub mod usb {

#[cfg(feature = "hwrng")]
pub mod hwrng {
embassy_nrf::bind_interrupts!(pub struct Irqs {
use crate::arch;

embassy_nrf::bind_interrupts!(struct Irqs {
RNG => embassy_nrf::rng::InterruptHandler<embassy_nrf::peripherals::RNG>;
});

pub fn construct_rng(peripherals: &mut arch::OptionalPeripherals) {
cfg_if::cfg_if! {
// The union of all contexts that wind up in a construct_rng should be synchronized
// with laze-project.yml's hwrng module.
if #[cfg(any(context = "nrf51", context = "nrf52"))] {
let rng = embassy_nrf::rng::Rng::new(
peripherals
.RNG
// We don't even have to take it out, just use it to seed the RNG
.as_mut()
.expect("RNG has not been previously used"),
arch::hwrng::Irqs,
);

riot_rs_random::construct_rng(rng);
} else if #[cfg(context = "riot-rs")] {
compile_error!("hardware RNG is not supported on this architecture");
}
}
}
}

pub fn init(config: Config) -> OptionalPeripherals {
Expand Down
15 changes: 1 addition & 14 deletions src/riot-rs-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,7 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) {
println!("riot-rs-embassy::init_task()");

#[cfg(feature = "hwrng")]
{
// The union of all contexts that wind up in a construct_rng should be synchronized with
// laze-project.yml's hwrng module.
#[cfg(any(context = "nrf51", context = "nrf52"))]
let rng = embassy_nrf::rng::Rng::new(
peripherals
.RNG
// We don't even have to take it out, just use it to seed the RNG
.as_mut()
.expect("RNG has not been previously used"),
arch::hwrng::Irqs,
);
riot_rs_random::construct_rng(rng);
}
arch::hwrng::construct_rng(&mut peripherals);
// Clock startup and entropy collection may lend themselves to parallelization, provided that
// doesn't impact runtime RAM or flash use.

Expand Down

0 comments on commit e753c6a

Please sign in to comment.