From 09f39fa6cfefc151ee5feed52bde120610efc579 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 12 Sep 2024 12:51:02 +0200 Subject: [PATCH 1/6] feat(hwrng): wire up hwrng for stm32 --- Cargo.lock | 1 + laze-project.yml | 2 ++ .../st-nucleo-h755zi-q/Cargo.toml | 5 ++- src/riot-rs-boards/st-nucleo-wb55/Cargo.toml | 2 +- src/riot-rs-embassy/Cargo.toml | 2 +- src/riot-rs-stm32/Cargo.toml | 10 +++++- src/riot-rs-stm32/src/hwrng.rs | 36 +++++++++++++++++++ src/riot-rs-stm32/src/lib.rs | 3 ++ 8 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/riot-rs-stm32/src/hwrng.rs diff --git a/Cargo.lock b/Cargo.lock index d8f32d356..d0573f5f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3818,6 +3818,7 @@ dependencies = [ "embassy-stm32", "portable-atomic", "riot-rs-embassy-common", + "riot-rs-random", "static_cell", ] diff --git a/laze-project.yml b/laze-project.yml index 5535bd8fe..2cdb90c57 100644 --- a/laze-project.yml +++ b/laze-project.yml @@ -602,6 +602,8 @@ modules: # doesn't have an entry in the cfg(feature = "hwrng") part of init_task - nrf51 - nrf52 + - stm32h755zitx + - stm32wb55rgvx env: global: FEATURES: diff --git a/src/riot-rs-boards/st-nucleo-h755zi-q/Cargo.toml b/src/riot-rs-boards/st-nucleo-h755zi-q/Cargo.toml index 2188a27b0..3a8dedf20 100644 --- a/src/riot-rs-boards/st-nucleo-h755zi-q/Cargo.toml +++ b/src/riot-rs-boards/st-nucleo-h755zi-q/Cargo.toml @@ -12,5 +12,8 @@ embassy-stm32 = { workspace = true, features = [ ] } riot-rs-debug = { workspace = true, features = ["rtt-target"] } riot-rs-rt.workspace = true -riot-rs-stm32 = { workspace = true, features = ["stm32-usb-synopsis"] } +riot-rs-stm32 = { workspace = true, features = [ + "stm32-hash-rng", + "stm32-usb-synopsis", +] } stm32 = { path = "../../riot-rs-chips/stm32" } diff --git a/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml b/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml index a16747c25..6d3ecf0e4 100644 --- a/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml +++ b/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml @@ -9,5 +9,5 @@ edition = "2021" [dependencies] embassy-stm32 = { workspace = true, features = ["stm32wb55rg"] } riot-rs-debug = { workspace = true, features = ["rtt-target"] } -riot-rs-stm32 = { workspace = true, features = ["stm32-usb"] } +riot-rs-stm32 = { workspace = true, features = ["stm32-usb", "stm32-rng"] } riot-rs-rt.workspace = true diff --git a/src/riot-rs-embassy/Cargo.toml b/src/riot-rs-embassy/Cargo.toml index acafe3421..99f0fe491 100644 --- a/src/riot-rs-embassy/Cargo.toml +++ b/src/riot-rs-embassy/Cargo.toml @@ -77,7 +77,7 @@ usb = [ net = ["dep:embassy-net", "time"] usb-ethernet = ["usb", "net"] ## Use a hardware RNG to seed into the riot-rs-random system-wide RNG -hwrng = ["riot-rs-nrf/hwrng"] +hwrng = ["riot-rs-nrf/hwrng", "riot-rs-stm32/hwrng"] wifi = [] wifi-cyw43 = ["riot-rs-rp/wifi-cyw43", "net", "wifi"] diff --git a/src/riot-rs-stm32/Cargo.toml b/src/riot-rs-stm32/Cargo.toml index 4d06d9e88..cc7d65099 100644 --- a/src/riot-rs-stm32/Cargo.toml +++ b/src/riot-rs-stm32/Cargo.toml @@ -15,8 +15,9 @@ embassy-executor = { workspace = true, default-features = false, features = [ "arch-cortex-m", ] } embassy-stm32 = { workspace = true, default-features = false } -riot-rs-embassy-common = { workspace = true } portable-atomic = { workspace = true } +riot-rs-embassy-common = { workspace = true } +riot-rs-random = { workspace = true, optional = true } static_cell = { workspace = true } [target.'cfg(context = "stm32")'.dependencies] @@ -33,6 +34,13 @@ external-interrupts = [ "riot-rs-embassy-common/external-interrupts", ] +## Enables seeding the random number generator from hardware. +hwrng = ["dep:riot-rs-random"] +# These are chosen automatically by riot-rs-boards and select the correct stm32 +# rng peripheral support. +stm32-hash-rng = [] +stm32-rng = [] + ## Enables USB support. usb = [] # These are chosen automatically by riot-rs-boards and select the correct stm32 diff --git a/src/riot-rs-stm32/src/hwrng.rs b/src/riot-rs-stm32/src/hwrng.rs new file mode 100644 index 000000000..10a99fdc9 --- /dev/null +++ b/src/riot-rs-stm32/src/hwrng.rs @@ -0,0 +1,36 @@ +use embassy_stm32::rng::Rng; +use embassy_stm32::{bind_interrupts, peripherals, rng}; + +#[cfg(not(any(feature = "stm32-hash-rng", feature = "stm32-rng")))] +compile_error!("no stm32 rng feature enabled"); + +#[cfg(feature = "stm32-hash-rng")] +bind_interrupts!(struct Irqs { + HASH_RNG => rng::InterruptHandler; +}); + +#[cfg(feature = "stm32-rng")] +bind_interrupts!(struct Irqs { + RNG => rng::InterruptHandler; +}); + +pub fn construct_rng(peripherals: &mut crate::OptionalPeripherals) { + cfg_if::cfg_if! { + // The union of all contexts that wind up in a construct_rng should be synchronized + // with laze-project.yml's hwrng module. + if #[cfg(any(context = "stm32"))] { + let rng = Rng::new( + peripherals + .RNG + // We don't even have to take it out, just use it to seed the RNG + .as_mut() + .expect("RNG has not been previously used"), + Irqs, + ); + + riot_rs_random::construct_rng(rng); + } else if #[cfg(context = "riot-rs")] { + compile_error!("hardware RNG is not supported on this architecture"); + } + } +} diff --git a/src/riot-rs-stm32/src/lib.rs b/src/riot-rs-stm32/src/lib.rs index 1d49c001e..38fdf9e97 100644 --- a/src/riot-rs-stm32/src/lib.rs +++ b/src/riot-rs-stm32/src/lib.rs @@ -18,6 +18,9 @@ pub use embassy_stm32::{interrupt, peripherals, OptionalPeripherals, Peripherals #[cfg(feature = "executor-interrupt")] pub(crate) use embassy_executor::InterruptExecutor as Executor; +#[cfg(feature = "hwrng")] +pub mod hwrng; + #[cfg(feature = "usb")] cfg_if::cfg_if! { if #[cfg(feature = "stm32-usb")] { From 9d789ebdff769770d25680497af5d899b10ba6ed Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 12 Sep 2024 13:28:36 +0200 Subject: [PATCH 2/6] feat(hwrng): wire up hwrng for rp2040 --- Cargo.lock | 1 + laze-project.yml | 1 + src/riot-rs-boards/st-nucleo-wb55/Cargo.toml | 2 +- src/riot-rs-embassy/Cargo.toml | 2 +- src/riot-rs-rp/Cargo.toml | 4 ++++ src/riot-rs-rp/src/hwrng.rs | 3 +++ src/riot-rs-rp/src/lib.rs | 3 +++ 7 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/riot-rs-rp/src/hwrng.rs diff --git a/Cargo.lock b/Cargo.lock index d0573f5f4..6c8ffea60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3780,6 +3780,7 @@ dependencies = [ "embassy-rp", "riot-rs-debug", "riot-rs-embassy-common", + "riot-rs-random", "riot-rs-utils", "static_cell", ] diff --git a/laze-project.yml b/laze-project.yml index 2cdb90c57..e32bb8b77 100644 --- a/laze-project.yml +++ b/laze-project.yml @@ -602,6 +602,7 @@ modules: # doesn't have an entry in the cfg(feature = "hwrng") part of init_task - nrf51 - nrf52 + - rp - stm32h755zitx - stm32wb55rgvx env: diff --git a/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml b/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml index 6d3ecf0e4..6fa2ae22d 100644 --- a/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml +++ b/src/riot-rs-boards/st-nucleo-wb55/Cargo.toml @@ -9,5 +9,5 @@ edition = "2021" [dependencies] embassy-stm32 = { workspace = true, features = ["stm32wb55rg"] } riot-rs-debug = { workspace = true, features = ["rtt-target"] } -riot-rs-stm32 = { workspace = true, features = ["stm32-usb", "stm32-rng"] } +riot-rs-stm32 = { workspace = true, features = ["stm32-rng", "stm32-usb"] } riot-rs-rt.workspace = true diff --git a/src/riot-rs-embassy/Cargo.toml b/src/riot-rs-embassy/Cargo.toml index 99f0fe491..f8e07d403 100644 --- a/src/riot-rs-embassy/Cargo.toml +++ b/src/riot-rs-embassy/Cargo.toml @@ -77,7 +77,7 @@ usb = [ net = ["dep:embassy-net", "time"] usb-ethernet = ["usb", "net"] ## Use a hardware RNG to seed into the riot-rs-random system-wide RNG -hwrng = ["riot-rs-nrf/hwrng", "riot-rs-stm32/hwrng"] +hwrng = ["riot-rs-nrf/hwrng", "riot-rs-rp/hwrng", "riot-rs-stm32/hwrng"] wifi = [] wifi-cyw43 = ["riot-rs-rp/wifi-cyw43", "net", "wifi"] diff --git a/src/riot-rs-rp/Cargo.toml b/src/riot-rs-rp/Cargo.toml index 62fab0635..dc24f8e0b 100644 --- a/src/riot-rs-rp/Cargo.toml +++ b/src/riot-rs-rp/Cargo.toml @@ -19,6 +19,7 @@ embassy-rp = { workspace = true, default-features = false, features = [ ] } riot-rs-debug = { workspace = true } riot-rs-embassy-common = { workspace = true } +riot-rs-random = { workspace = true, optional = true } riot-rs-utils = { workspace = true } static_cell = { workspace = true, optional = true } @@ -40,6 +41,9 @@ embassy-rp = { workspace = true, default-features = false, features = [ ## Enables GPIO interrupt support. external-interrupts = ["riot-rs-embassy-common/external-interrupts"] +## Enables seeding the random number generator from hardware. +hwrng = ["dep:riot-rs-random"] + ## Enables USB support. usb = [] diff --git a/src/riot-rs-rp/src/hwrng.rs b/src/riot-rs-rp/src/hwrng.rs new file mode 100644 index 000000000..835ad34a5 --- /dev/null +++ b/src/riot-rs-rp/src/hwrng.rs @@ -0,0 +1,3 @@ +pub fn construct_rng(_peripherals: &mut crate::OptionalPeripherals) { + riot_rs_random::construct_rng(embassy_rp::clocks::RoscRng); +} diff --git a/src/riot-rs-rp/src/lib.rs b/src/riot-rs-rp/src/lib.rs index 2db7dd4fd..97f7385ea 100644 --- a/src/riot-rs-rp/src/lib.rs +++ b/src/riot-rs-rp/src/lib.rs @@ -16,6 +16,9 @@ mod wifi; #[cfg(feature = "wifi-cyw43")] pub mod cyw43; +#[cfg(feature = "hwrng")] +pub mod hwrng; + #[cfg(feature = "usb")] pub mod usb; From dbbc8522af6b73dd1d111c39d09eaac1b13d7fe5 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 12 Sep 2024 13:36:01 +0200 Subject: [PATCH 3/6] doc: add & populate hwrng support in support matrix --- book/src/support_matrix.html | 11 ++++++++++- doc/support_matrix.yml | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/book/src/support_matrix.html b/book/src/support_matrix.html index f04c8f108..2b2cbf2b9 100644 --- a/book/src/support_matrix.html +++ b/book/src/support_matrix.html @@ -4,7 +4,7 @@ Chip Testing Board - Functionality + Functionality @@ -15,6 +15,7 @@ User USB Wi-Fi Ethernet over USB + Hardware Random Number Generator @@ -27,6 +28,7 @@ ❌ ☑️ ❌ + ❌ nRF52xxx @@ -37,6 +39,7 @@ ✅ – ✅ + ✅ nRF53xx @@ -47,6 +50,7 @@ ✅ – ✅ + ✅ RP2040 @@ -57,6 +61,7 @@ ✅ – ✅ + ✅ RP2040 @@ -67,6 +72,7 @@ ✅ ✅ ✅ + ✅ STM32F401RETX @@ -77,6 +83,7 @@ – – – + – STM32F755ZITX @@ -87,6 +94,7 @@ ✅ – ❌ + ✅ STM32W55RGVX @@ -97,6 +105,7 @@ ✅ – ✅ + ✅ diff --git a/doc/support_matrix.yml b/doc/support_matrix.yml index 11fa0f101..c6acc641e 100644 --- a/doc/support_matrix.yml +++ b/doc/support_matrix.yml @@ -35,6 +35,9 @@ functionalities: - name: ethernet_over_usb title: Ethernet over USB description: + - name: hwrng + title: Hardware Random Number Generator + description: # Encodes support status for each chip. chips: @@ -43,6 +46,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: supported logging: supported wifi: not_available @@ -51,6 +55,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: supported logging: supported wifi: not_available @@ -59,6 +64,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: supported logging: supported wifi: not_available @@ -67,6 +73,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: not_currently_supported logging: supported wifi: not_available @@ -75,6 +82,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: not_available logging: supported wifi: not_available @@ -83,6 +91,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: supported logging: supported wifi: not_available @@ -91,6 +100,7 @@ chips: support: gpio: supported debug_output: supported + hwrng: supported logging: supported wifi: not_available From 3ea68a82b7a2453ed120b6d8029d0242372b7701 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 16 Sep 2024 10:06:56 +0200 Subject: [PATCH 4/6] chore(build): `cargo update` --- Cargo.lock | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c8ffea60..8e3f0a2a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,9 +129,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.87" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10f00e1f6e58a40e807377c75c6a7f97bf9044fab57816f2414e6f5f4499d7b8" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" [[package]] name = "arrayvec" @@ -360,9 +360,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.18" +version = "1.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" +checksum = "2d74707dde2ba56f86ae90effb3b43ddd369504387e718014de010cec7959800" dependencies = [ "shlex", ] @@ -3176,9 +3176,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe" dependencies = [ "critical-section", "portable-atomic", @@ -3538,9 +3538,9 @@ version = "0.1.1" [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" dependencies = [ "bitflags 2.6.0", ] @@ -3942,9 +3942,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.36" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.6.0", "errno", @@ -3961,11 +3961,10 @@ checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" [[package]] name = "ruzstd" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c8b8f3d26bd9f945e5cbae77f7cdfbf37af9a66956f1115eb4516e45df519f4" +checksum = "99c3938e133aac070997ddc684d4b393777d293ba170f2988c8fd5ea2ad4ce21" dependencies = [ - "byteorder", "twox-hash", ] @@ -3983,10 +3982,11 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "scroll-ring" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eda0dff67036ba3b032aa68eb47ceb9b53b6e848445f53d10bf93e033b17df2" +checksum = "827595b27476e68bcbb86dfe8e11f364e4d4efd01cb5f01d845950456006f6ec" dependencies = [ + "portable-atomic", "ringbuf", "try-lock", ] From 5b0aa3b79614e9827e8fe3e4322363f75cc79ed5 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 16 Sep 2024 12:17:52 +0200 Subject: [PATCH 5/6] fix(deps): pin `once_cell` to `1.19.0` See https://github.com/matklad/once_cell/pull/260. --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e3f0a2a7..e91d8d2ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3176,9 +3176,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" dependencies = [ "critical-section", "portable-atomic", diff --git a/Cargo.toml b/Cargo.toml index 2b1b9e127..392f3db1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ cortex-m-semihosting = { version = "0.5" } critical-section = { version = "1.1.2" } # Disabling default features may reduce the code size by not providing atomics # for types larger than the pointer width, which we do not use. -portable-atomic = { version = "1.6.0", default-features = false, features = [ +portable-atomic = { version = "1.7.0", default-features = false, features = [ "require-cas", ] } @@ -92,7 +92,7 @@ document-features = "0.2.8" heapless = { version = "0.8.0", default-features = false } konst = { version = "0.3.8", default-features = false } ld-memory = { version = "0.2.9" } -once_cell = { version = "1.19.0", default-features = false, features = [ +once_cell = { version = "=1.19.0", default-features = false, features = [ "critical-section", ] } paste = { version = "1.0" } From 86249d266e2448016a0bfc25af932aab2a9e3a82 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 16 Sep 2024 13:00:16 +0200 Subject: [PATCH 6/6] fix(CI): disable `examples/coap` for armv6, work around #418 --- examples/coap/laze.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/coap/laze.yml b/examples/coap/laze.yml index 819ba1772..c90a51c28 100644 --- a/examples/coap/laze.yml +++ b/examples/coap/laze.yml @@ -8,3 +8,6 @@ apps: - ?release - network - random + conflicts: + # see https://github.com/future-proof-iot/RIOT-rs/issues/418 + - thumbv6m-none-eabi