aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rp23/src/bin/trng.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/rp23/src/bin/trng.rs b/examples/rp23/src/bin/trng.rs
new file mode 100644
index 000000000..e146baa2e
--- /dev/null
+++ b/examples/rp23/src/bin/trng.rs
@@ -0,0 +1,64 @@
1//! This example shows TRNG usage
2
3#![no_std]
4#![no_main]
5
6use defmt::*;
7use embassy_executor::Spawner;
8use embassy_rp::bind_interrupts;
9use embassy_rp::block::ImageDef;
10use embassy_rp::gpio::{Level, Output};
11use embassy_rp::peripherals::TRNG;
12use embassy_rp::trng::Trng;
13use embassy_time::Timer;
14use rand::RngCore;
15use {defmt_rtt as _, panic_probe as _};
16
17#[link_section = ".start_block"]
18#[used]
19pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
20
21// Program metadata for `picotool info`
22#[link_section = ".bi_entries"]
23#[used]
24pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [
25 embassy_rp::binary_info::rp_program_name!(c"example"),
26 embassy_rp::binary_info::rp_cargo_version!(),
27 embassy_rp::binary_info::rp_program_description!(c"Blinky"),
28 embassy_rp::binary_info::rp_program_build_attribute!(),
29];
30
31bind_interrupts!(struct Irqs {
32 TRNG_IRQ => embassy_rp::trng::InterruptHandler<TRNG>;
33});
34
35#[embassy_executor::main]
36async fn main(_spawner: Spawner) {
37 let peripherals = embassy_rp::init(Default::default());
38
39 // Initialize the TRNG with default configuration
40 let mut trng = Trng::new(peripherals.TRNG, Irqs, embassy_rp::trng::Config::default());
41 // A buffer to collect random bytes in.
42 let mut randomness = [0u8; 58];
43
44 let mut led = Output::new(peripherals.PIN_25, Level::Low);
45
46 loop {
47 trng.fill_bytes(&mut randomness).await;
48 info!("Random bytes async {}", &randomness);
49 trng.blocking_fill_bytes(&mut randomness);
50 info!("Random bytes blocking {}", &randomness);
51 let random_u32 = trng.next_u32();
52 let random_u64 = trng.next_u64();
53 info!("Random u32 {} u64 {}", random_u32, random_u64);
54 // Random number of blinks between 0 and 31
55 let blinks = random_u32 % 32;
56 for _ in 0..blinks {
57 led.set_high();
58 Timer::after_millis(20).await;
59 led.set_low();
60 Timer::after_millis(20).await;
61 }
62 Timer::after_millis(1000).await;
63 }
64}