From bb8e08fc0427421220cb150119ea118f319e3b31 Mon Sep 17 00:00:00 2001 From: ROMemories Date: Thu, 18 Apr 2024 15:51:33 +0200 Subject: [PATCH] refactor(arch): split the `nrf` arch into multiple submodule files Split the `nrf` arch to make it more manageable when it keeps growing. --- src/riot-rs-embassy/src/arch/nrf.rs | 93 ----------------------- src/riot-rs-embassy/src/arch/nrf/hwrng.rs | 26 +++++++ src/riot-rs-embassy/src/arch/nrf/mod.rs | 36 +++++++++ src/riot-rs-embassy/src/arch/nrf/usb.rs | 29 +++++++ src/riot-rs-embassy/src/lib.rs | 2 +- 5 files changed, 92 insertions(+), 94 deletions(-) delete mode 100644 src/riot-rs-embassy/src/arch/nrf.rs create mode 100644 src/riot-rs-embassy/src/arch/nrf/hwrng.rs create mode 100644 src/riot-rs-embassy/src/arch/nrf/mod.rs create mode 100644 src/riot-rs-embassy/src/arch/nrf/usb.rs diff --git a/src/riot-rs-embassy/src/arch/nrf.rs b/src/riot-rs-embassy/src/arch/nrf.rs deleted file mode 100644 index ac5637a5c..000000000 --- a/src/riot-rs-embassy/src/arch/nrf.rs +++ /dev/null @@ -1,93 +0,0 @@ -pub(crate) use embassy_executor::InterruptExecutor as Executor; - -#[cfg(context = "nrf52")] -pub use embassy_nrf::interrupt::SWI0_EGU0 as SWI; - -#[cfg(context = "nrf5340")] -pub use embassy_nrf::interrupt::EGU0 as SWI; - -pub use embassy_nrf::{config::Config, interrupt, peripherals, OptionalPeripherals}; - -#[cfg(context = "nrf52")] -#[interrupt] -unsafe fn SWI0_EGU0() { - // SAFETY: - // - called from ISR - // - not called before `start()`, as the interrupt is enabled by `start()` - // itself - unsafe { crate::EXECUTOR.on_interrupt() } -} - -#[cfg(context = "nrf5340")] -#[interrupt] -unsafe fn EGU0() { - unsafe { crate::EXECUTOR.on_interrupt() } -} - -#[cfg(feature = "usb")] -pub mod usb { - use embassy_nrf::{ - bind_interrupts, peripherals, - usb::{ - self, - vbus_detect::{self, HardwareVbusDetect}, - Driver, - }, - }; - - use crate::arch; - - #[cfg(context = "nrf52")] - bind_interrupts!(struct Irqs { - USBD => usb::InterruptHandler; - POWER_CLOCK => vbus_detect::InterruptHandler; - }); - - #[cfg(context = "nrf5340")] - bind_interrupts!(struct Irqs { - USBD => usb::InterruptHandler; - USBREGULATOR => vbus_detect::InterruptHandler; - }); - - pub type UsbDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>; - - pub fn driver(peripherals: &mut arch::OptionalPeripherals) -> UsbDriver { - let usbd = peripherals.USBD.take().unwrap(); - Driver::new(usbd, Irqs, HardwareVbusDetect::new(Irqs)) - } -} - -#[cfg(feature = "hwrng")] -pub mod hwrng { - use crate::arch; - - embassy_nrf::bind_interrupts!(struct Irqs { - RNG => embassy_nrf::rng::InterruptHandler; - }); - - pub fn construct_rng(peripherals: &mut arch::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 = "nrf51", context = "nrf52"))] { - let rng = embassy_nrf::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"), - arch::hwrng::Irqs, - ); - - riot_rs_random::construct_rng(rng); - } else if #[cfg(context = "riot-rs")] { - compile_error!("hardware RNG is not supported on this architecture"); - } - } - } -} - -pub fn init(config: Config) -> OptionalPeripherals { - let peripherals = embassy_nrf::init(config); - OptionalPeripherals::from(peripherals) -} diff --git a/src/riot-rs-embassy/src/arch/nrf/hwrng.rs b/src/riot-rs-embassy/src/arch/nrf/hwrng.rs new file mode 100644 index 000000000..5629f7c37 --- /dev/null +++ b/src/riot-rs-embassy/src/arch/nrf/hwrng.rs @@ -0,0 +1,26 @@ +use crate::arch; + +embassy_nrf::bind_interrupts!(struct Irqs { + RNG => embassy_nrf::rng::InterruptHandler; +}); + +pub fn construct_rng(peripherals: &mut arch::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 = "nrf51", context = "nrf52"))] { + let rng = embassy_nrf::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"), + arch::hwrng::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-embassy/src/arch/nrf/mod.rs b/src/riot-rs-embassy/src/arch/nrf/mod.rs new file mode 100644 index 000000000..1dccaf5cf --- /dev/null +++ b/src/riot-rs-embassy/src/arch/nrf/mod.rs @@ -0,0 +1,36 @@ +#[cfg(feature = "hwrng")] +pub mod hwrng; + +#[cfg(feature = "usb")] +pub mod usb; + +pub(crate) use embassy_executor::InterruptExecutor as Executor; + +#[cfg(context = "nrf52")] +pub use embassy_nrf::interrupt::SWI0_EGU0 as SWI; + +#[cfg(context = "nrf5340")] +pub use embassy_nrf::interrupt::EGU0 as SWI; + +pub use embassy_nrf::{config::Config, interrupt, peripherals, OptionalPeripherals}; + +#[cfg(context = "nrf52")] +#[interrupt] +unsafe fn SWI0_EGU0() { + // SAFETY: + // - called from ISR + // - not called before `start()`, as the interrupt is enabled by `start()` + // itself + unsafe { crate::EXECUTOR.on_interrupt() } +} + +#[cfg(context = "nrf5340")] +#[interrupt] +unsafe fn EGU0() { + unsafe { crate::EXECUTOR.on_interrupt() } +} + +pub fn init(config: Config) -> OptionalPeripherals { + let peripherals = embassy_nrf::init(config); + OptionalPeripherals::from(peripherals) +} diff --git a/src/riot-rs-embassy/src/arch/nrf/usb.rs b/src/riot-rs-embassy/src/arch/nrf/usb.rs new file mode 100644 index 000000000..8772a2ead --- /dev/null +++ b/src/riot-rs-embassy/src/arch/nrf/usb.rs @@ -0,0 +1,29 @@ +use embassy_nrf::{ + bind_interrupts, peripherals, + usb::{ + self, + vbus_detect::{self, HardwareVbusDetect}, + Driver, + }, +}; + +use crate::arch; + +#[cfg(context = "nrf52")] +bind_interrupts!(struct Irqs { + USBD => usb::InterruptHandler; + POWER_CLOCK => vbus_detect::InterruptHandler; +}); + +#[cfg(context = "nrf5340")] +bind_interrupts!(struct Irqs { + USBD => usb::InterruptHandler; + USBREGULATOR => vbus_detect::InterruptHandler; +}); + +pub type UsbDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>; + +pub fn driver(peripherals: &mut arch::OptionalPeripherals) -> UsbDriver { + let usbd = peripherals.USBD.take().unwrap(); + Driver::new(usbd, Irqs, HardwareVbusDetect::new(Irqs)) +} diff --git a/src/riot-rs-embassy/src/lib.rs b/src/riot-rs-embassy/src/lib.rs index 0a849512c..57a11966d 100644 --- a/src/riot-rs-embassy/src/lib.rs +++ b/src/riot-rs-embassy/src/lib.rs @@ -8,7 +8,7 @@ pub mod define_peripherals; cfg_if::cfg_if! { if #[cfg(context = "nrf")] { - #[path = "arch/nrf.rs"] + #[path = "arch/nrf/mod.rs"] pub mod arch; } else if #[cfg(context = "rp2040")] { #[path = "arch/rp2040.rs"]