Skip to content

Commit

Permalink
refactor(arch): split the nrf arch into multiple submodule files
Browse files Browse the repository at this point in the history
Split the `nrf` arch to make it more manageable when it keeps growing.
  • Loading branch information
ROMemories authored and kaspar030 committed Apr 22, 2024
1 parent 4623f02 commit bb8e08f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 94 deletions.
93 changes: 0 additions & 93 deletions src/riot-rs-embassy/src/arch/nrf.rs

This file was deleted.

26 changes: 26 additions & 0 deletions src/riot-rs-embassy/src/arch/nrf/hwrng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::arch;

embassy_nrf::bind_interrupts!(struct Irqs {
RNG => embassy_nrf::rng::InterruptHandler<embassy_nrf::peripherals::RNG>;
});

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");
}
}
}
36 changes: 36 additions & 0 deletions src/riot-rs-embassy/src/arch/nrf/mod.rs
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 29 additions & 0 deletions src/riot-rs-embassy/src/arch/nrf/usb.rs
Original file line number Diff line number Diff line change
@@ -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<peripherals::USBD>;
POWER_CLOCK => vbus_detect::InterruptHandler;
});

#[cfg(context = "nrf5340")]
bind_interrupts!(struct Irqs {
USBD => usb::InterruptHandler<peripherals::USBD>;
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))
}
2 changes: 1 addition & 1 deletion src/riot-rs-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit bb8e08f

Please sign in to comment.