From 80c504cd950349caa96ae37898f6f3325030c615 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 14 Dec 2020 00:36:29 +0100 Subject: Add std impl for rand --- .cargo/config | 27 --------------------------- embassy/Cargo.toml | 5 ++++- embassy/src/rand.rs | 15 +++++++++++++++ examples/.cargo/config | 27 +++++++++++++++++++++++++++ test-build.sh | 5 ++++- 5 files changed, 50 insertions(+), 29 deletions(-) delete mode 100644 .cargo/config create mode 100644 examples/.cargo/config diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 3f319ae55..000000000 --- a/.cargo/config +++ /dev/null @@ -1,27 +0,0 @@ -[target.'cfg(all(target_arch = "arm", target_os = "none"))'] -runner = "probe-run --chip nRF52840_xxAA --defmt" - -rustflags = [ - # LLD (shipped with the Rust toolchain) is used as the default linker - "-C", "link-arg=--nmagic", - "-C", "link-arg=-Tlink.x", - "-C", "link-arg=-Tdefmt.x", - - # if you run into problems with LLD switch to the GNU linker by commenting out - # this line - # "-C", "linker=arm-none-eabi-ld", - - # if you need to link to pre-compiled C libraries provided by a C toolchain - # use GCC as the linker by commenting out both lines above and then - # uncommenting the three lines below - # "-C", "linker=arm-none-eabi-gcc", - # "-C", "link-arg=-Wl,-Tlink.x", - # "-C", "link-arg=-nostartfiles", -] - -[build] -# Pick ONE of these compilation targets -# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ -# target = "thumbv7m-none-eabi" # Cortex-M3 -# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU) -target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml index 0e4c801bb..08d273dbb 100644 --- a/embassy/Cargo.toml +++ b/embassy/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Dario Nieuwenhuis "] edition = "2018" [features] -std = ["futures/std"] +std = ["futures/std", "rand_core"] defmt-trace = [] defmt-debug = [] defmt-info = [] @@ -16,6 +16,9 @@ defmt-error = [] defmt = { version = "0.1.3", optional = true } log = { version = "0.4.11", optional = true } +# std-only +rand_core = { version = "0.5.1", optional = true, features = ["std"] } + cortex-m = "0.6.4" futures = { version = "0.3.5", default-features = false } pin-project = { version = "1.0.2", default-features = false } diff --git a/embassy/src/rand.rs b/embassy/src/rand.rs index 7e3788380..6e78c24ae 100644 --- a/embassy/src/rand.rs +++ b/embassy/src/rand.rs @@ -4,6 +4,9 @@ pub trait Rand { fn rand(&self, buf: &mut [u8]); } +#[cfg(feature = "std")] +static mut RAND: Option<&'static dyn Rand> = Some(&if_std::Rand); +#[cfg(not(feature = "std"))] static mut RAND: Option<&'static dyn Rand> = None; pub unsafe fn set_rand(rand: &'static dyn Rand) { @@ -13,3 +16,15 @@ pub unsafe fn set_rand(rand: &'static dyn Rand) { pub fn rand(buf: &mut [u8]) { unsafe { unwrap!(RAND, "No rand set").rand(buf) } } + +#[cfg(feature = "std")] +mod if_std { + use rand_core::{OsRng, RngCore}; + + pub(crate) struct Rand; + impl super::Rand for Rand { + fn rand(&self, buf: &mut [u8]) { + OsRng.fill_bytes(buf) + } + } +} diff --git a/examples/.cargo/config b/examples/.cargo/config new file mode 100644 index 000000000..3f319ae55 --- /dev/null +++ b/examples/.cargo/config @@ -0,0 +1,27 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +runner = "probe-run --chip nRF52840_xxAA --defmt" + +rustflags = [ + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=--nmagic", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + + # if you run into problems with LLD switch to the GNU linker by commenting out + # this line + # "-C", "linker=arm-none-eabi-ld", + + # if you need to link to pre-compiled C libraries provided by a C toolchain + # use GCC as the linker by commenting out both lines above and then + # uncommenting the three lines below + # "-C", "linker=arm-none-eabi-gcc", + # "-C", "link-arg=-Wl,-Tlink.x", + # "-C", "link-arg=-nostartfiles", +] + +[build] +# Pick ONE of these compilation targets +# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ +# target = "thumbv7m-none-eabi" # Cortex-M3 +# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU) +target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) diff --git a/test-build.sh b/test-build.sh index ae603b289..52bf7bb7b 100755 --- a/test-build.sh +++ b/test-build.sh @@ -5,7 +5,10 @@ set -euxo pipefail # examples (cd examples; cargo build --target thumbv7em-none-eabi --bins) -# embassy +# embassy std +(cd embassy; cargo build --features log,std) + +# embassy embedded (cd embassy; cargo build --target thumbv7em-none-eabi) (cd embassy; cargo build --target thumbv7em-none-eabi --features log) (cd embassy; cargo build --target thumbv7em-none-eabi --features defmt) -- cgit