aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/mcxa/Cargo.toml1
-rw-r--r--examples/mcxa/src/bin/trng.rs88
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"
21embedded-io-async = "0.6.1" 21embedded-io-async = "0.6.1"
22heapless = "0.9.2" 22heapless = "0.9.2"
23panic-probe = { version = "1.0", features = ["print-defmt"] } 23panic-probe = { version = "1.0", features = ["print-defmt"] }
24rand_core = "0.9"
24static_cell = "2.1.1" 25static_cell = "2.1.1"
25tmp108 = "0.4.0" 26tmp108 = "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
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}