Skip to content

Commit

Permalink
Add TRNG rp23 example
Browse files Browse the repository at this point in the history
  • Loading branch information
ionspin committed Sep 16, 2024
1 parent 9de7fa1 commit 02b920e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
19 changes: 5 additions & 14 deletions embassy-rp/src/trng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl<T: Instance> Trng<T> {
T::Interrupt::disable();
}

fn blocking_wait_for_succesfull_generation(&self) {
fn blocking_wait_for_successful_generation(&self) {
let regs = T::regs();

let trng_busy_register = regs.trng_busy();
Expand Down Expand Up @@ -249,16 +249,7 @@ impl<T: Instance> Trng<T> {
}

fn blocking_read_ehr_registers_into_array(&mut self, buffer: &mut [u8; TRNG_BLOCK_SIZE_BYTES]) {
let regs = T::regs();

let trng_busy_register = regs.trng_busy();
let trng_valid_register = regs.trng_valid();

while trng_busy_register.read().trng_busy() {}
if trng_valid_register.read().ehr_valid().not() {
panic!("RNG not busy, but ehr is not valid!")
}

self.blocking_wait_for_successful_generation();
self.read_ehr_registers_into_array(buffer);
}

Expand Down Expand Up @@ -342,7 +333,7 @@ impl<T: Instance> Trng<T> {

while bytes_transferred < destination_length {
let remaining = destination_length - bytes_transferred;
self.blocking_wait_for_succesfull_generation();
self.blocking_wait_for_successful_generation();
self.blocking_read_ehr_registers_into_array(&mut buffer);
match remaining {
size if size > TRNG_BLOCK_SIZE_BYTES => {
Expand All @@ -363,7 +354,7 @@ impl<T: Instance> Trng<T> {
pub fn blocking_next_u32(&mut self) -> u32 {
let regs = T::regs();
self.start_rng();
self.blocking_wait_for_succesfull_generation();
self.blocking_wait_for_successful_generation();
// 12.12.3 After successful generation, read the last result register, EHR_DATA[5] to
// clear all of the result registers.
let result = regs.ehr_data5().read();
Expand All @@ -375,7 +366,7 @@ impl<T: Instance> Trng<T> {
pub fn blocking_next_u64(&mut self) -> u64 {
let regs = T::regs();
self.start_rng();
self.blocking_wait_for_succesfull_generation();
self.blocking_wait_for_successful_generation();

let low = regs.ehr_data4().read() as u64;
// 12.12.3 After successful generation, read the last result register, EHR_DATA[5] to
Expand Down
2 changes: 1 addition & 1 deletion examples/rp23/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ portable-atomic = { version = "1.5", features = ["critical-section"] }
log = "0.4"
pio-proc = "0.2"
pio = "0.2.1"
rand = { version = "0.8.5", default-features = false }
rand = { version = "0.8.5", default-features = false, features = ["small_rng"]}
embedded-sdmmc = "0.7.0"

bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] }
Expand Down
62 changes: 62 additions & 0 deletions examples/rp23/src/bin/trng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! This example test the RP Pico on board LED.
//!
//! It does not work with the RP Pico W board. See wifi_blinky.rs.

#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_rp::block::ImageDef;
use embassy_rp::{bind_interrupts, gpio};
use embassy_rp::peripherals::TRNG;
use embassy_rp::trng::Trng;
use embassy_time::Timer;
use gpio::{Level, Output};
use rand::{RngCore, SeedableRng};
use rand::rngs::SmallRng;
use {defmt_rtt as _, panic_probe as _};

#[link_section = ".start_block"]
#[used]
pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();

// Program metadata for `picotool info`
#[link_section = ".bi_entries"]
#[used]
pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [
embassy_rp::binary_info::rp_program_name!(c"example"),
embassy_rp::binary_info::rp_cargo_version!(),
embassy_rp::binary_info::rp_program_description!(c"Blinky"),
embassy_rp::binary_info::rp_program_build_attribute!(),
];

bind_interrupts!(struct Irqs {
TRNG_IRQ => embassy_rp::trng::InterruptHandler<TRNG>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let peripherals = embassy_rp::init(Default::default());
// Initialize the TRNG with default configuration
let mut trng = Trng::new(peripherals.TRNG, Irqs, embassy_rp::trng::Config::default());
// A buffer to collect random bytes in.
let mut randomness = [0u8; 58];
// A small_rng pseudo RNG initialized from TRNG
let mut small_rng = SmallRng::from_rng(trng).unwrap();

loop {

trng.fill_bytes(&mut randomness).await;
info!("Random bytes async {}", &randomness);
trng.blocking_fill_bytes(&mut randomness).await;
info!("Random bytes blocking {}", &randomness);
let random_u32 = trng.next_u32();
let random_u64 = trng.next_u64();
info!("Random u32 {} u64 {}", random_u32, random_u64);
small_rng.fill_bytes(&mut randomness);
info!("Small rng {}", &randomness);
Timer::after_millis(1000).await;

}
}

0 comments on commit 02b920e

Please sign in to comment.