diff options
| author | Liam Murphy <[email protected]> | 2021-06-29 17:26:16 +1000 |
|---|---|---|
| committer | Liam Murphy <[email protected]> | 2021-06-29 17:26:16 +1000 |
| commit | 8a4ab298192c388bed09fc198ee1786561cd50f9 (patch) | |
| tree | ecaed07d38549f0f453c8003cf45c2eaa692b81a /examples/nrf | |
| parent | e6d6e82e54bca88ab1144802d2b716b867934225 (diff) | |
Add an nRF RNG driver
Resolves #187
Like the stm32 driver, this has both a non-blocking and blocking API, and implements `rand_core::RngCore` for the blocking API.
Diffstat (limited to 'examples/nrf')
| -rw-r--r-- | examples/nrf/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/nrf/src/bin/rng.rs | 30 |
2 files changed, 31 insertions, 0 deletions
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index fa1dab360..8db0ad53f 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml | |||
| @@ -29,3 +29,4 @@ cortex-m-rt = "0.6.13" | |||
| 29 | embedded-hal = { version = "0.2.4" } | 29 | embedded-hal = { version = "0.2.4" } |
| 30 | panic-probe = { version = "0.2.0", features = ["print-defmt"] } | 30 | panic-probe = { version = "0.2.0", features = ["print-defmt"] } |
| 31 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | 31 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } |
| 32 | rand = { version = "0.8.4", default-features = false } | ||
diff --git a/examples/nrf/src/bin/rng.rs b/examples/nrf/src/bin/rng.rs new file mode 100644 index 000000000..a4c204c2e --- /dev/null +++ b/examples/nrf/src/bin/rng.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(min_type_alias_impl_trait)] | ||
| 4 | #![feature(impl_trait_in_bindings)] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | #![allow(incomplete_features)] | ||
| 7 | |||
| 8 | #[path = "../example_common.rs"] | ||
| 9 | mod example_common; | ||
| 10 | |||
| 11 | use defmt::panic; | ||
| 12 | use embassy::executor::Spawner; | ||
| 13 | use embassy_nrf::Peripherals; | ||
| 14 | use embassy_nrf::rng::Rng; | ||
| 15 | use embassy_nrf::interrupt; | ||
| 16 | use embassy::traits::rng::Rng as _; | ||
| 17 | use rand::Rng as _; | ||
| 18 | |||
| 19 | #[embassy::main] | ||
| 20 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 21 | let mut rng = Rng::new(p.RNG, interrupt::take!(RNG)); | ||
| 22 | |||
| 23 | // Async API | ||
| 24 | let mut bytes = [0; 4]; | ||
| 25 | rng.fill_bytes(&mut bytes).await.unwrap(); // nRF RNG is infallible | ||
| 26 | defmt::info!("Some random bytes: {:?}", bytes); | ||
| 27 | |||
| 28 | // Sync API with `rand` | ||
| 29 | defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10)); | ||
| 30 | } | ||
