aboutsummaryrefslogtreecommitdiff
path: root/examples/mcxa/src/bin
diff options
context:
space:
mode:
authorFelipe Balbi <[email protected]>2025-12-08 10:16:42 -0800
committerFelipe Balbi <[email protected]>2025-12-09 14:47:32 -0800
commitfee793cbf8a0bdd020636ce433cc0883105dd2db (patch)
treeab5cc7da66d3b462a8da58149ed5386e4452e873 /examples/mcxa/src/bin
parentcf069b3e4e6a02660893ef8014e4ab69df6d46e0 (diff)
MCXA TRNG driver
Diffstat (limited to 'examples/mcxa/src/bin')
-rw-r--r--examples/mcxa/src/bin/trng.rs88
1 files changed, 88 insertions, 0 deletions
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
4use embassy_executor::Spawner;
5use hal::bind_interrupts;
6use hal::config::Config;
7use hal::trng::{self, AsyncTrng, InterruptHandler, Trng};
8use rand_core::RngCore;
9use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
10
11bind_interrupts!(
12 struct Irqs {
13 TRNG0 => InterruptHandler;
14 }
15);
16
17#[embassy_executor::main]
18async 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}