diff options
| author | Ulf Lilleengen <[email protected]> | 2025-11-04 13:10:45 +0100 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2025-11-04 13:13:22 +0100 |
| commit | 729a7c2cc5e5fe1d9badb0a0f1c758ba2c57b6aa (patch) | |
| tree | 967f9fcb91f33b80fa7decc25a0c948927d607c0 /examples | |
| parent | 18a5872a2586335496aec056e24edacef6fd76cb (diff) | |
feat: initial support for nrf54 CRACEN peripheral
The CRACEN peripheral supports random number generation,
digest and key generation, and key exchange.
The initial support implements random number generation.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf54l15/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/nrf54l15/src/bin/rng.rs | 29 |
2 files changed, 31 insertions, 0 deletions
diff --git a/examples/nrf54l15/Cargo.toml b/examples/nrf54l15/Cargo.toml index 14a80efe7..4ef77279f 100644 --- a/examples/nrf54l15/Cargo.toml +++ b/examples/nrf54l15/Cargo.toml | |||
| @@ -14,6 +14,8 @@ embassy-nrf = { version = "0.8.0", path = "../../embassy-nrf", features = ["defm | |||
| 14 | embedded-io = { version = "0.6.0", features = ["defmt-03"] } | 14 | embedded-io = { version = "0.6.0", features = ["defmt-03"] } |
| 15 | embedded-io-async = { version = "0.6.1", features = ["defmt-03"] } | 15 | embedded-io-async = { version = "0.6.1", features = ["defmt-03"] } |
| 16 | 16 | ||
| 17 | rand = { version = "0.9.0", default-features = false } | ||
| 18 | |||
| 17 | defmt = "1.0.1" | 19 | defmt = "1.0.1" |
| 18 | defmt-rtt = "1.0.0" | 20 | defmt-rtt = "1.0.0" |
| 19 | panic-probe = { version = "1.0.0", features = ["print-defmt"] } | 21 | panic-probe = { version = "1.0.0", features = ["print-defmt"] } |
diff --git a/examples/nrf54l15/src/bin/rng.rs b/examples/nrf54l15/src/bin/rng.rs new file mode 100644 index 000000000..3be035b9c --- /dev/null +++ b/examples/nrf54l15/src/bin/rng.rs | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_nrf::cracen::Cracen; | ||
| 6 | use embassy_nrf::{bind_interrupts, cracen, peripherals}; | ||
| 7 | use rand::Rng as _; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_nrf::init(Default::default()); | ||
| 13 | let mut rng = Cracen::new_blocking(p.CRACEN); | ||
| 14 | |||
| 15 | // Async API | ||
| 16 | let mut bytes = [0; 4]; | ||
| 17 | rng.blocking_fill_bytes(&mut bytes); | ||
| 18 | defmt::info!("Some random bytes: {:?}", bytes); | ||
| 19 | |||
| 20 | // Sync API with `rand` | ||
| 21 | defmt::info!("A random number from 1 to 10: {:?}", rng.random_range(1..=10)); | ||
| 22 | |||
| 23 | let mut bytes = [0; 1024]; | ||
| 24 | rng.blocking_fill_bytes(&mut bytes); | ||
| 25 | let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros()); | ||
| 26 | let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones()); | ||
| 27 | defmt::info!("Chance of zero: {}%", zero_count * 100 / (bytes.len() as u32 * 8)); | ||
| 28 | defmt::info!("Chance of one: {}%", one_count * 100 / (bytes.len() as u32 * 8)); | ||
| 29 | } | ||
