aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLiam Murphy <[email protected]>2021-06-29 17:26:16 +1000
committerLiam Murphy <[email protected]>2021-06-29 17:26:16 +1000
commit8a4ab298192c388bed09fc198ee1786561cd50f9 (patch)
treeecaed07d38549f0f453c8003cf45c2eaa692b81a /examples
parente6d6e82e54bca88ab1144802d2b716b867934225 (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')
-rw-r--r--examples/nrf/Cargo.toml1
-rw-r--r--examples/nrf/src/bin/rng.rs30
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"
29embedded-hal = { version = "0.2.4" } 29embedded-hal = { version = "0.2.4" }
30panic-probe = { version = "0.2.0", features = ["print-defmt"] } 30panic-probe = { version = "0.2.0", features = ["print-defmt"] }
31futures = { version = "0.3.8", default-features = false, features = ["async-await"] } 31futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
32rand = { 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"]
9mod example_common;
10
11use defmt::panic;
12use embassy::executor::Spawner;
13use embassy_nrf::Peripherals;
14use embassy_nrf::rng::Rng;
15use embassy_nrf::interrupt;
16use embassy::traits::rng::Rng as _;
17use rand::Rng as _;
18
19#[embassy::main]
20async 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}