From fee793cbf8a0bdd020636ce433cc0883105dd2db Mon Sep 17 00:00:00 2001 From: Felipe Balbi Date: Mon, 8 Dec 2025 10:16:42 -0800 Subject: MCXA TRNG driver --- examples/mcxa/Cargo.toml | 1 + examples/mcxa/src/bin/trng.rs | 88 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 examples/mcxa/src/bin/trng.rs (limited to 'examples/mcxa') diff --git a/examples/mcxa/Cargo.toml b/examples/mcxa/Cargo.toml index d07cc4272..347659f5b 100644 --- a/examples/mcxa/Cargo.toml +++ b/examples/mcxa/Cargo.toml @@ -21,6 +21,7 @@ embassy-time-driver = "0.2.1" embedded-io-async = "0.6.1" heapless = "0.9.2" panic-probe = { version = "1.0", features = ["print-defmt"] } +rand_core = "0.9" static_cell = "2.1.1" tmp108 = "0.4.0" diff --git a/examples/mcxa/src/bin/trng.rs b/examples/mcxa/src/bin/trng.rs new file mode 100644 index 000000000..95b4d0a88 --- /dev/null +++ b/examples/mcxa/src/bin/trng.rs @@ -0,0 +1,88 @@ +#![no_std] +#![no_main] + +use embassy_executor::Spawner; +use hal::bind_interrupts; +use hal::config::Config; +use hal::trng::{self, AsyncTrng, InterruptHandler, Trng}; +use rand_core::RngCore; +use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; + +bind_interrupts!( + struct Irqs { + TRNG0 => InterruptHandler; + } +); + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let config = Config::default(); + let mut p = hal::init(config); + + defmt::info!("TRNG example"); + + let config = trng::Config::default(); + let mut trng = Trng::new(p.TRNG0.reborrow(), config); + + defmt::info!("========== BLOCKING =========="); + + defmt::info!("Generate 10 u32"); + for _ in 0..10 { + let rand = trng.blocking_next_u32(); + defmt::info!("{}", rand); + } + + defmt::info!("Generate 10 u64"); + for _ in 0..10 { + let rand = trng.blocking_next_u64(); + defmt::info!("{}", rand); + } + + let mut buf = [0_u8; 256]; + + defmt::info!("Generate 10 256-byte buffers"); + for _ in 0..10 { + trng.blocking_fill_bytes(&mut buf); + defmt::info!("{:02x}", buf); + } + + defmt::info!("RngCore"); + + for _ in 0..10 { + defmt::info!("u32: {}", trng.next_u32()); + defmt::info!("u64: {}", trng.next_u64()); + } + + drop(trng); + + defmt::info!("========== ASYNC =========="); + + let mut trng = AsyncTrng::new(p.TRNG0.reborrow(), Irqs, config); + + defmt::info!("Generate 10 u32"); + for _ in 0..10 { + let rand = trng.async_next_u32().await.unwrap(); + defmt::info!("{}", rand); + } + + defmt::info!("Generate 10 u64"); + for _ in 0..10 { + let rand = trng.async_next_u64().await.unwrap(); + defmt::info!("{}", rand); + } + + let mut buf = [0_u8; 256]; + + defmt::info!("Generate 10 256-byte buffers"); + for _ in 0..10 { + trng.async_fill_bytes(&mut buf).await.unwrap(); + defmt::info!("{:02x}", buf); + } + + defmt::info!("RngCore"); + + for _ in 0..10 { + defmt::info!("u32: {}", trng.next_u32()); + defmt::info!("u64: {}", trng.next_u64()); + } +} -- cgit