diff options
| author | Felipe Balbi <[email protected]> | 2025-12-08 10:16:42 -0800 |
|---|---|---|
| committer | Felipe Balbi <[email protected]> | 2025-12-09 14:47:32 -0800 |
| commit | fee793cbf8a0bdd020636ce433cc0883105dd2db (patch) | |
| tree | ab5cc7da66d3b462a8da58149ed5386e4452e873 /examples/mcxa | |
| parent | cf069b3e4e6a02660893ef8014e4ab69df6d46e0 (diff) | |
MCXA TRNG driver
Diffstat (limited to 'examples/mcxa')
| -rw-r--r-- | examples/mcxa/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/mcxa/src/bin/trng.rs | 88 |
2 files changed, 89 insertions, 0 deletions
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" | |||
| 21 | embedded-io-async = "0.6.1" | 21 | embedded-io-async = "0.6.1" |
| 22 | heapless = "0.9.2" | 22 | heapless = "0.9.2" |
| 23 | panic-probe = { version = "1.0", features = ["print-defmt"] } | 23 | panic-probe = { version = "1.0", features = ["print-defmt"] } |
| 24 | rand_core = "0.9" | ||
| 24 | static_cell = "2.1.1" | 25 | static_cell = "2.1.1" |
| 25 | tmp108 = "0.4.0" | 26 | tmp108 = "0.4.0" |
| 26 | 27 | ||
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 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use hal::bind_interrupts; | ||
| 6 | use hal::config::Config; | ||
| 7 | use hal::trng::{self, AsyncTrng, InterruptHandler, Trng}; | ||
| 8 | use rand_core::RngCore; | ||
| 9 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 10 | |||
| 11 | bind_interrupts!( | ||
| 12 | struct Irqs { | ||
| 13 | TRNG0 => InterruptHandler; | ||
| 14 | } | ||
| 15 | ); | ||
| 16 | |||
| 17 | #[embassy_executor::main] | ||
| 18 | async fn main(_spawner: Spawner) { | ||
| 19 | let config = Config::default(); | ||
| 20 | let mut p = hal::init(config); | ||
| 21 | |||
| 22 | defmt::info!("TRNG example"); | ||
| 23 | |||
| 24 | let config = trng::Config::default(); | ||
| 25 | let mut trng = Trng::new(p.TRNG0.reborrow(), config); | ||
| 26 | |||
| 27 | defmt::info!("========== BLOCKING =========="); | ||
| 28 | |||
| 29 | defmt::info!("Generate 10 u32"); | ||
| 30 | for _ in 0..10 { | ||
| 31 | let rand = trng.blocking_next_u32(); | ||
| 32 | defmt::info!("{}", rand); | ||
| 33 | } | ||
| 34 | |||
| 35 | defmt::info!("Generate 10 u64"); | ||
| 36 | for _ in 0..10 { | ||
| 37 | let rand = trng.blocking_next_u64(); | ||
| 38 | defmt::info!("{}", rand); | ||
| 39 | } | ||
| 40 | |||
| 41 | let mut buf = [0_u8; 256]; | ||
| 42 | |||
| 43 | defmt::info!("Generate 10 256-byte buffers"); | ||
| 44 | for _ in 0..10 { | ||
| 45 | trng.blocking_fill_bytes(&mut buf); | ||
| 46 | defmt::info!("{:02x}", buf); | ||
| 47 | } | ||
| 48 | |||
| 49 | defmt::info!("RngCore"); | ||
| 50 | |||
| 51 | for _ in 0..10 { | ||
| 52 | defmt::info!("u32: {}", trng.next_u32()); | ||
| 53 | defmt::info!("u64: {}", trng.next_u64()); | ||
| 54 | } | ||
| 55 | |||
| 56 | drop(trng); | ||
| 57 | |||
| 58 | defmt::info!("========== ASYNC =========="); | ||
| 59 | |||
| 60 | let mut trng = AsyncTrng::new(p.TRNG0.reborrow(), Irqs, config); | ||
| 61 | |||
| 62 | defmt::info!("Generate 10 u32"); | ||
| 63 | for _ in 0..10 { | ||
| 64 | let rand = trng.async_next_u32().await.unwrap(); | ||
| 65 | defmt::info!("{}", rand); | ||
| 66 | } | ||
| 67 | |||
| 68 | defmt::info!("Generate 10 u64"); | ||
| 69 | for _ in 0..10 { | ||
| 70 | let rand = trng.async_next_u64().await.unwrap(); | ||
| 71 | defmt::info!("{}", rand); | ||
| 72 | } | ||
| 73 | |||
| 74 | let mut buf = [0_u8; 256]; | ||
| 75 | |||
| 76 | defmt::info!("Generate 10 256-byte buffers"); | ||
| 77 | for _ in 0..10 { | ||
| 78 | trng.async_fill_bytes(&mut buf).await.unwrap(); | ||
| 79 | defmt::info!("{:02x}", buf); | ||
| 80 | } | ||
| 81 | |||
| 82 | defmt::info!("RngCore"); | ||
| 83 | |||
| 84 | for _ in 0..10 { | ||
| 85 | defmt::info!("u32: {}", trng.next_u32()); | ||
| 86 | defmt::info!("u64: {}", trng.next_u64()); | ||
| 87 | } | ||
| 88 | } | ||
