diff options
| author | Felipe Balbi <[email protected]> | 2025-12-11 19:52:29 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-11 19:52:29 +0000 |
| commit | 031faab0f42932178135aaf5d37448c272f7a757 (patch) | |
| tree | 700e8f4312ef9e9882687bf179fdbec07a6ce218 /examples | |
| parent | 3588023e3e04b18cf98a2a0d10756e1f236ca351 (diff) | |
| parent | 5331751412413fe45f3ccb4d634fe63d5c335f0f (diff) | |
Merge pull request #5022 from felipebalbi/mcxa/trng
MCXA TRNG driver
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/mcxa/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/mcxa/src/bin/trng.rs | 106 |
2 files changed, 107 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..5f6e2408c --- /dev/null +++ b/examples/mcxa/src/bin/trng.rs | |||
| @@ -0,0 +1,106 @@ | |||
| 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, 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 mut trng = Trng::new_blocking_128(p.TRNG0.reborrow()); | ||
| 25 | let rand = trng.blocking_next_u32(); | ||
| 26 | defmt::info!("128-bit {}", rand); | ||
| 27 | |||
| 28 | drop(trng); | ||
| 29 | |||
| 30 | let mut trng = Trng::new_blocking_256(p.TRNG0.reborrow()); | ||
| 31 | let rand = trng.blocking_next_u32(); | ||
| 32 | defmt::info!("256-bit {}", rand); | ||
| 33 | |||
| 34 | drop(trng); | ||
| 35 | |||
| 36 | let mut trng = Trng::new_blocking_512(p.TRNG0.reborrow()); | ||
| 37 | let rand = trng.blocking_next_u32(); | ||
| 38 | defmt::info!("512-bit {}", rand); | ||
| 39 | |||
| 40 | drop(trng); | ||
| 41 | |||
| 42 | let config = trng::Config::default(); | ||
| 43 | let mut trng = Trng::new_blocking_with_custom_config(p.TRNG0.reborrow(), config); | ||
| 44 | |||
| 45 | defmt::info!("========== BLOCKING =========="); | ||
| 46 | |||
| 47 | defmt::info!("Generate 10 u32"); | ||
| 48 | for _ in 0..10 { | ||
| 49 | let rand = trng.blocking_next_u32(); | ||
| 50 | defmt::info!("{}", rand); | ||
| 51 | } | ||
| 52 | |||
| 53 | defmt::info!("Generate 10 u64"); | ||
| 54 | for _ in 0..10 { | ||
| 55 | let rand = trng.blocking_next_u64(); | ||
| 56 | defmt::info!("{}", rand); | ||
| 57 | } | ||
| 58 | |||
| 59 | let mut buf = [0_u8; 256]; | ||
| 60 | |||
| 61 | defmt::info!("Generate 10 256-byte buffers"); | ||
| 62 | for _ in 0..10 { | ||
| 63 | trng.blocking_fill_bytes(&mut buf); | ||
| 64 | defmt::info!("{:02x}", buf); | ||
| 65 | } | ||
| 66 | |||
| 67 | defmt::info!("RngCore"); | ||
| 68 | |||
| 69 | for _ in 0..10 { | ||
| 70 | defmt::info!("u32: {}", trng.next_u32()); | ||
| 71 | defmt::info!("u64: {}", trng.next_u64()); | ||
| 72 | } | ||
| 73 | |||
| 74 | drop(trng); | ||
| 75 | |||
| 76 | defmt::info!("========== ASYNC =========="); | ||
| 77 | |||
| 78 | let mut trng = Trng::new_with_custom_config(p.TRNG0.reborrow(), Irqs, config); | ||
| 79 | |||
| 80 | defmt::info!("Generate 10 u32"); | ||
| 81 | for _ in 0..10 { | ||
| 82 | let rand = trng.async_next_u32().await.unwrap(); | ||
| 83 | defmt::info!("{}", rand); | ||
| 84 | } | ||
| 85 | |||
| 86 | defmt::info!("Generate 10 u64"); | ||
| 87 | for _ in 0..10 { | ||
| 88 | let rand = trng.async_next_u64().await.unwrap(); | ||
| 89 | defmt::info!("{}", rand); | ||
| 90 | } | ||
| 91 | |||
| 92 | let mut buf = [0_u8; 256]; | ||
| 93 | |||
| 94 | defmt::info!("Generate 10 256-byte buffers"); | ||
| 95 | for _ in 0..10 { | ||
| 96 | trng.async_fill_bytes(&mut buf).await.unwrap(); | ||
| 97 | defmt::info!("{:02x}", buf); | ||
| 98 | } | ||
| 99 | |||
| 100 | defmt::info!("RngCore"); | ||
| 101 | |||
| 102 | for _ in 0..10 { | ||
| 103 | defmt::info!("u32: {}", trng.next_u32()); | ||
| 104 | defmt::info!("u64: {}", trng.next_u64()); | ||
| 105 | } | ||
| 106 | } | ||
