Skip to content

Commit

Permalink
feat: hwrng for stm32 & rp (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Sep 16, 2024
2 parents ec3e3e9 + 86249d2 commit 4ae4f38
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 20 deletions.
28 changes: 15 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }

Expand Down Expand Up @@ -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" }
Expand Down
11 changes: 10 additions & 1 deletion book/src/support_matrix.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<tr>
<th>Chip</th>
<th>Testing Board</th>
<th colspan="6">Functionality</th>
<th colspan="7">Functionality</th>
</tr>
<tr>
<th></th>
Expand All @@ -15,6 +15,7 @@
<th>User USB</th>
<th>Wi-Fi</th>
<th>Ethernet over USB</th>
<th>Hardware Random Number Generator</th>
</tr>
</thead>
<tbody>
Expand All @@ -27,6 +28,7 @@
<td class="support-cell" title="available in hardware, but not currently supported by RIOT-rs"></td>
<td class="support-cell" title="supported with some caveats">☑️</td>
<td class="support-cell" title="available in hardware, but not currently supported by RIOT-rs"></td>
<td class="support-cell" title="available in hardware, but not currently supported by RIOT-rs"></td>
</tr>
<tr>
<td>nRF52xxx</td>
Expand All @@ -37,6 +39,7 @@
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
</tr>
<tr>
<td>nRF53xx</td>
Expand All @@ -47,6 +50,7 @@
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
</tr>
<tr>
<td>RP2040</td>
Expand All @@ -57,6 +61,7 @@
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
</tr>
<tr>
<td>RP2040</td>
Expand All @@ -67,6 +72,7 @@
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
</tr>
<tr>
<td>STM32F401RETX</td>
Expand All @@ -77,6 +83,7 @@
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
</tr>
<tr>
<td>STM32F755ZITX</td>
Expand All @@ -87,6 +94,7 @@
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="available in hardware, but not currently supported by RIOT-rs"></td>
<td class="support-cell" title="supported"></td>
</tr>
<tr>
<td>STM32W55RGVX</td>
Expand All @@ -97,6 +105,7 @@
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="not available on this piece of hardware"></td>
<td class="support-cell" title="supported"></td>
<td class="support-cell" title="supported"></td>
</tr>
</tbody>
</table>
Expand Down
10 changes: 10 additions & 0 deletions doc/support_matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -43,6 +46,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: supported
logging: supported
wifi: not_available

Expand All @@ -51,6 +55,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: supported
logging: supported
wifi: not_available

Expand All @@ -59,6 +64,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: supported
logging: supported
wifi: not_available

Expand All @@ -67,6 +73,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: not_currently_supported
logging: supported
wifi: not_available

Expand All @@ -75,6 +82,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: not_available
logging: supported
wifi: not_available

Expand All @@ -83,6 +91,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: supported
logging: supported
wifi: not_available

Expand All @@ -91,6 +100,7 @@ chips:
support:
gpio: supported
debug_output: supported
hwrng: supported
logging: supported
wifi: not_available

Expand Down
3 changes: 3 additions & 0 deletions examples/coap/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ apps:
- ?release
- network
- random
conflicts:
# see https://github.com/future-proof-iot/RIOT-rs/issues/418
- thumbv6m-none-eabi
3 changes: 3 additions & 0 deletions laze-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ modules:
# doesn't have an entry in the cfg(feature = "hwrng") part of init_task
- nrf51
- nrf52
- rp
- stm32h755zitx
- stm32wb55rgvx
env:
global:
FEATURES:
Expand Down
5 changes: 4 additions & 1 deletion src/riot-rs-boards/st-nucleo-h755zi-q/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion src/riot-rs-boards/st-nucleo-wb55/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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-rng", "stm32-usb"] }
riot-rs-rt.workspace = true
2 changes: 1 addition & 1 deletion src/riot-rs-embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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-rp/hwrng", "riot-rs-stm32/hwrng"]

wifi = []
wifi-cyw43 = ["riot-rs-rp/wifi-cyw43", "net", "wifi"]
Expand Down
4 changes: 4 additions & 0 deletions src/riot-rs-rp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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 = []

Expand Down
3 changes: 3 additions & 0 deletions src/riot-rs-rp/src/hwrng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn construct_rng(_peripherals: &mut crate::OptionalPeripherals) {
riot_rs_random::construct_rng(embassy_rp::clocks::RoscRng);
}
3 changes: 3 additions & 0 deletions src/riot-rs-rp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 9 additions & 1 deletion src/riot-rs-stm32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions src/riot-rs-stm32/src/hwrng.rs
Original file line number Diff line number Diff line change
@@ -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<peripherals::RNG>;
});

#[cfg(feature = "stm32-rng")]
bind_interrupts!(struct Irqs {
RNG => rng::InterruptHandler<peripherals::RNG>;
});

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");
}
}
}
Loading

0 comments on commit 4ae4f38

Please sign in to comment.