Skip to content

Commit

Permalink
Provide arch module on RP2040
Browse files Browse the repository at this point in the history
This improves code portability between rp2040-hal and rp235x-hal
  • Loading branch information
jannic committed Aug 27, 2024
1 parent 2d815bf commit b372336
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rp2040-hal-examples/src/bin/pio_side_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ fn main() -> ! {

// PIO runs in background, independently from CPU
loop {
cortex_m::asm::wfi();
hal::arch::wfi();
}
}
68 changes: 68 additions & 0 deletions rp2040-hal/src/arch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Portable in-line assembly
//!
//! On the RP235x, this is useful to write code portable between ARM and RISC-V cores.
//! While there's no such choice on the RP2040, providing the same functions helps writing
//! code that works on both RP2040 and RP235x.

#[cfg(all(target_arch = "arm", target_os = "none"))]
mod inner {
pub use cortex_m::asm::{delay, dsb, nop, sev, wfe, wfi};
pub use cortex_m::interrupt::{disable as interrupt_disable, enable as interrupt_enable};

/// Are interrupts current enabled?
pub fn interrupts_enabled() -> bool {
cortex_m::register::primask::read().is_active()
}

/// Run the closure without interrupts
///
/// No critical-section token because we haven't blocked the second core
pub fn interrupt_free<T, F>(f: F) -> T
where
F: FnOnce() -> T,
{
let active = interrupts_enabled();
if active {
interrupt_disable();
}
let t = f();
if active {
unsafe {
interrupt_enable();
}
}
t
}
}

#[cfg(not(all(any(target_arch = "arm", target_arch = "riscv32"), target_os = "none")))]
mod inner {
/// Placeholder function to disable interrupts
pub fn interrupt_disable() {}
/// Placeholder function to enable interrupts
pub fn interrupt_enable() {}
/// Placeholder function to check if interrupts are enabled
pub fn interrupts_enabled() -> bool {
false
}
/// Placeholder function to wait for an event
pub fn wfe() {}
/// Placeholder function to do nothing
pub fn nop() {}
/// Placeholder function to emit a data synchronisation barrier
pub fn dsb() {}
/// Placeholder function to run a closure with interrupts disabled
pub fn interrupt_free<T, F>(f: F) -> T
where
F: FnOnce() -> T,
{
f()
}
/// Placeholder function to wait for some clock cycles
pub fn delay(_: u32) {}
/// Placeholder function to emit an event
pub fn sev() {}
}

pub use inner::*;

1 change: 1 addition & 0 deletions rp2040-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub use rp2040_pac as pac;
mod intrinsics;

pub mod adc;
pub mod arch;
#[macro_use]
pub mod async_utils;
pub(crate) mod atomic_register_access;
Expand Down

0 comments on commit b372336

Please sign in to comment.