Merge pull request #262 from Liamolucko/nrf-rng

Add an nRF RNG driver
This commit is contained in:
Dario Nieuwenhuis
2021-07-01 01:55:55 +02:00
committed by GitHub
12 changed files with 311 additions and 0 deletions

View File

@@ -29,3 +29,4 @@ cortex-m-rt = "0.6.13"
embedded-hal = { version = "0.2.4" }
panic-probe = { version = "0.2.0", features = ["print-defmt"] }
futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
rand = { version = "0.8.4", default-features = false }

View File

@@ -0,0 +1,43 @@
#![no_std]
#![no_main]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::panic;
use embassy::executor::Spawner;
use embassy::traits::rng::Rng as _;
use embassy_nrf::interrupt;
use embassy_nrf::rng::Rng;
use embassy_nrf::Peripherals;
use rand::Rng as _;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let mut rng = unsafe { Rng::new(p.RNG, interrupt::take!(RNG)) };
// Async API
let mut bytes = [0; 4];
rng.fill_bytes(&mut bytes).await.unwrap(); // nRF RNG is infallible
defmt::info!("Some random bytes: {:?}", bytes);
// Sync API with `rand`
defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10));
let mut bytes = [0; 1024];
rng.fill_bytes(&mut bytes).await.unwrap();
let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros());
let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones());
defmt::info!(
"Chance of zero: {}%",
zero_count * 100 / (bytes.len() as u32 * 8)
);
defmt::info!(
"Chance of one: {}%",
one_count * 100 / (bytes.len() as u32 * 8)
);
}