2022-12-13 08:45:11 +00:00
|
|
|
//! Multicore support
|
2022-12-10 07:26:35 +00:00
|
|
|
//!
|
|
|
|
//! This module handles setup of the 2nd cpu core on the rp2040, which we refer to as core1.
|
|
|
|
//! It provides functionality for setting up the stack, and starting core1.
|
|
|
|
//!
|
|
|
|
//! The entrypoint for core1 can be any function that never returns, including closures.
|
2022-12-10 11:57:45 +00:00
|
|
|
//!
|
|
|
|
//! Enable the `critical-section-impl` feature in embassy-rp when sharing data across cores using
|
|
|
|
//! the `embassy-sync` primitives and `CriticalSectionRawMutex`.
|
2022-12-13 08:45:11 +00:00
|
|
|
//!
|
|
|
|
//! # Usage
|
2023-05-29 19:30:28 +00:00
|
|
|
//!
|
2022-12-13 08:45:11 +00:00
|
|
|
//! ```no_run
|
2023-05-29 19:30:28 +00:00
|
|
|
//! use embassy_rp::multicore::Stack;
|
|
|
|
//! use static_cell::StaticCell;
|
|
|
|
//! use embassy_executor::Executor;
|
|
|
|
//!
|
2022-12-13 08:45:11 +00:00
|
|
|
//! static mut CORE1_STACK: Stack<4096> = Stack::new();
|
|
|
|
//! static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
|
|
|
|
//! static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
|
|
|
|
//!
|
2023-05-29 19:30:28 +00:00
|
|
|
//! # // workaround weird error: `main` function not found in crate `rust_out`
|
|
|
|
//! # let _ = ();
|
|
|
|
//!
|
|
|
|
//! #[embassy_executor::task]
|
|
|
|
//! async fn core0_task() {
|
|
|
|
//! // ...
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[embassy_executor::task]
|
|
|
|
//! async fn core1_task() {
|
|
|
|
//! // ...
|
|
|
|
//! }
|
|
|
|
//!
|
2022-12-13 08:45:11 +00:00
|
|
|
//! #[cortex_m_rt::entry]
|
|
|
|
//! fn main() -> ! {
|
|
|
|
//! let p = embassy_rp::init(Default::default());
|
|
|
|
//!
|
2023-05-29 19:30:28 +00:00
|
|
|
//! embassy_rp::multicore::spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
|
2022-12-13 08:45:11 +00:00
|
|
|
//! let executor1 = EXECUTOR1.init(Executor::new());
|
2023-05-29 19:30:28 +00:00
|
|
|
//! executor1.run(|spawner| spawner.spawn(core1_task()).unwrap());
|
2022-12-13 08:45:11 +00:00
|
|
|
//! });
|
|
|
|
//!
|
|
|
|
//! let executor0 = EXECUTOR0.init(Executor::new());
|
2023-05-29 19:30:28 +00:00
|
|
|
//! executor0.run(|spawner| spawner.spawn(core0_task()).unwrap())
|
2022-12-13 08:45:11 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
2022-12-10 07:26:35 +00:00
|
|
|
|
|
|
|
use core::mem::ManuallyDrop;
|
2022-12-23 19:46:49 +00:00
|
|
|
use core::sync::atomic::{compiler_fence, AtomicBool, Ordering};
|
2022-12-13 03:02:28 +00:00
|
|
|
|
2023-06-08 14:08:40 +00:00
|
|
|
use crate::interrupt::InterruptExt;
|
2022-12-13 12:49:51 +00:00
|
|
|
use crate::peripherals::CORE1;
|
2023-07-20 14:57:54 +00:00
|
|
|
use crate::{gpio, install_stack_guard, interrupt, pac};
|
2022-12-13 03:02:28 +00:00
|
|
|
|
|
|
|
const PAUSE_TOKEN: u32 = 0xDEADBEEF;
|
|
|
|
const RESUME_TOKEN: u32 = !0xDEADBEEF;
|
|
|
|
static IS_CORE1_INIT: AtomicBool = AtomicBool::new(false);
|
2022-12-10 07:26:35 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2024-10-13 22:01:49 +00:00
|
|
|
unsafe fn core1_setup(stack_bottom: *mut usize) {
|
2024-03-18 22:28:58 +00:00
|
|
|
if install_stack_guard(stack_bottom).is_err() {
|
2023-07-20 14:57:54 +00:00
|
|
|
// currently only happens if the MPU was already set up, which
|
|
|
|
// would indicate that the core is already in use from outside
|
|
|
|
// embassy, somehow. trap if so since we can't deal with that.
|
2022-12-10 07:26:35 +00:00
|
|
|
cortex_m::asm::udf();
|
|
|
|
}
|
2023-05-02 06:39:05 +00:00
|
|
|
unsafe {
|
|
|
|
gpio::init();
|
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
/// Data type for a properly aligned stack of N bytes
|
2022-12-10 07:26:35 +00:00
|
|
|
#[repr(C, align(32))]
|
|
|
|
pub struct Stack<const SIZE: usize> {
|
|
|
|
/// Memory to be used for the stack
|
2022-12-13 12:49:51 +00:00
|
|
|
pub mem: [u8; SIZE],
|
2022-12-10 07:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<const SIZE: usize> Stack<SIZE> {
|
|
|
|
/// Construct a stack of length SIZE, initialized to 0
|
|
|
|
pub const fn new() -> Stack<SIZE> {
|
2022-12-13 12:49:51 +00:00
|
|
|
Stack { mem: [0_u8; SIZE] }
|
2022-12-10 07:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 03:20:26 +00:00
|
|
|
#[cfg(all(feature = "rt", feature = "rp2040"))]
|
2022-12-13 03:02:28 +00:00
|
|
|
#[interrupt]
|
|
|
|
#[link_section = ".data.ram_func"]
|
|
|
|
unsafe fn SIO_IRQ_PROC1() {
|
|
|
|
let sio = pac::SIO;
|
|
|
|
// Clear IRQ
|
|
|
|
sio.fifo().st().write(|w| w.set_wof(false));
|
|
|
|
|
|
|
|
while sio.fifo().st().read().vld() {
|
|
|
|
// Pause CORE1 execution and disable interrupts
|
|
|
|
if fifo_read_wfe() == PAUSE_TOKEN {
|
|
|
|
cortex_m::interrupt::disable();
|
|
|
|
// Signal to CORE0 that execution is paused
|
|
|
|
fifo_write(PAUSE_TOKEN);
|
|
|
|
// Wait for `resume` signal from CORE0
|
|
|
|
while fifo_read_wfe() != RESUME_TOKEN {
|
|
|
|
cortex_m::asm::nop();
|
|
|
|
}
|
|
|
|
cortex_m::interrupt::enable();
|
|
|
|
// Signal to CORE0 that execution is resumed
|
|
|
|
fifo_write(RESUME_TOKEN);
|
|
|
|
}
|
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:32:31 +00:00
|
|
|
#[cfg(all(feature = "rt", feature = "_rp235x"))]
|
2024-08-08 03:20:26 +00:00
|
|
|
#[interrupt]
|
|
|
|
#[link_section = ".data.ram_func"]
|
|
|
|
unsafe fn SIO_IRQ_FIFO() {
|
|
|
|
let sio = pac::SIO;
|
|
|
|
// Clear IRQ
|
|
|
|
sio.fifo().st().write(|w| w.set_wof(false));
|
|
|
|
|
|
|
|
while sio.fifo().st().read().vld() {
|
|
|
|
// Pause CORE1 execution and disable interrupts
|
|
|
|
if fifo_read_wfe() == PAUSE_TOKEN {
|
|
|
|
cortex_m::interrupt::disable();
|
|
|
|
// Signal to CORE0 that execution is paused
|
|
|
|
fifo_write(PAUSE_TOKEN);
|
|
|
|
// Wait for `resume` signal from CORE0
|
|
|
|
while fifo_read_wfe() != RESUME_TOKEN {
|
|
|
|
cortex_m::asm::nop();
|
|
|
|
}
|
|
|
|
cortex_m::interrupt::enable();
|
|
|
|
// Signal to CORE0 that execution is resumed
|
|
|
|
fifo_write(RESUME_TOKEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
/// Spawn a function on this core
|
|
|
|
pub fn spawn_core1<F, const SIZE: usize>(_core1: CORE1, stack: &'static mut Stack<SIZE>, entry: F)
|
|
|
|
where
|
|
|
|
F: FnOnce() -> bad::Never + Send + 'static,
|
|
|
|
{
|
|
|
|
// The first two ignored `u64` parameters are there to take up all of the registers,
|
|
|
|
// which means that the rest of the arguments are taken from the stack,
|
|
|
|
// where we're able to put them from core 0.
|
|
|
|
extern "C" fn core1_startup<F: FnOnce() -> bad::Never>(
|
|
|
|
_: u64,
|
|
|
|
_: u64,
|
2023-05-15 23:21:28 +00:00
|
|
|
entry: *mut ManuallyDrop<F>,
|
2022-12-13 12:49:51 +00:00
|
|
|
stack_bottom: *mut usize,
|
|
|
|
) -> ! {
|
2024-10-13 22:01:49 +00:00
|
|
|
unsafe { core1_setup(stack_bottom) };
|
2023-05-15 23:21:28 +00:00
|
|
|
|
|
|
|
let entry = unsafe { ManuallyDrop::take(&mut *entry) };
|
|
|
|
|
|
|
|
// make sure the preceding read doesn't get reordered past the following fifo write
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// Signal that it's safe for core 0 to get rid of the original value now.
|
|
|
|
fifo_write(1);
|
|
|
|
|
|
|
|
IS_CORE1_INIT.store(true, Ordering::Release);
|
|
|
|
// Enable fifo interrupt on CORE1 for `pause` functionality.
|
2024-08-08 03:20:26 +00:00
|
|
|
#[cfg(feature = "rp2040")]
|
|
|
|
unsafe {
|
|
|
|
interrupt::SIO_IRQ_PROC1.enable()
|
|
|
|
};
|
2024-08-12 08:32:31 +00:00
|
|
|
#[cfg(feature = "_rp235x")]
|
2024-08-08 03:20:26 +00:00
|
|
|
unsafe {
|
|
|
|
interrupt::SIO_IRQ_FIFO.enable()
|
|
|
|
};
|
2022-12-13 12:49:51 +00:00
|
|
|
|
2024-09-27 08:19:35 +00:00
|
|
|
// Enable FPU
|
2024-10-13 19:47:40 +00:00
|
|
|
#[cfg(all(feature = "_rp235x", has_fpu))]
|
2024-09-27 08:19:35 +00:00
|
|
|
unsafe {
|
|
|
|
let p = cortex_m::Peripherals::steal();
|
|
|
|
p.SCB.cpacr.modify(|cpacr| cpacr | (3 << 20) | (3 << 22));
|
|
|
|
}
|
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
entry()
|
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// Reset the core
|
2023-06-15 23:32:18 +00:00
|
|
|
let psm = pac::PSM;
|
|
|
|
psm.frce_off().modify(|w| w.set_proc1(true));
|
|
|
|
while !psm.frce_off().read().proc1() {
|
|
|
|
cortex_m::asm::nop();
|
2022-12-13 12:49:51 +00:00
|
|
|
}
|
2023-06-15 23:32:18 +00:00
|
|
|
psm.frce_off().modify(|w| w.set_proc1(false));
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2023-05-15 23:42:35 +00:00
|
|
|
// The ARM AAPCS ABI requires 8-byte stack alignment.
|
|
|
|
// #[align] on `struct Stack` ensures the bottom is aligned, but the top could still be
|
|
|
|
// unaligned if the user chooses a stack size that's not multiple of 8.
|
|
|
|
// So, we round down to the next multiple of 8.
|
|
|
|
let stack_words = stack.mem.len() / 8 * 2;
|
|
|
|
let mem = unsafe { core::slice::from_raw_parts_mut(stack.mem.as_mut_ptr() as *mut usize, stack_words) };
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// Set up the stack
|
|
|
|
let mut stack_ptr = unsafe { mem.as_mut_ptr().add(mem.len()) };
|
2022-12-13 03:02:28 +00:00
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// We don't want to drop this, since it's getting moved to the other core.
|
|
|
|
let mut entry = ManuallyDrop::new(entry);
|
2022-12-13 03:02:28 +00:00
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// Push the arguments to `core1_startup` onto the stack.
|
|
|
|
unsafe {
|
|
|
|
// Push `stack_bottom`.
|
|
|
|
stack_ptr = stack_ptr.sub(1);
|
|
|
|
stack_ptr.cast::<*mut usize>().write(mem.as_mut_ptr());
|
2022-12-13 03:02:28 +00:00
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// Push `entry`.
|
|
|
|
stack_ptr = stack_ptr.sub(1);
|
2023-05-15 23:21:28 +00:00
|
|
|
stack_ptr.cast::<*mut ManuallyDrop<F>>().write(&mut entry);
|
2022-12-13 12:49:51 +00:00
|
|
|
}
|
2022-12-13 03:02:28 +00:00
|
|
|
|
2022-12-13 12:49:51 +00:00
|
|
|
// Make sure the compiler does not reorder the stack writes after to after the
|
|
|
|
// below FIFO writes, which would result in them not being seen by the second
|
|
|
|
// core.
|
|
|
|
//
|
|
|
|
// From the compiler perspective, this doesn't guarantee that the second core
|
|
|
|
// actually sees those writes. However, we know that the RP2040 doesn't have
|
|
|
|
// memory caches, and writes happen in-order.
|
|
|
|
compiler_fence(Ordering::Release);
|
|
|
|
|
|
|
|
let p = unsafe { cortex_m::Peripherals::steal() };
|
|
|
|
let vector_table = p.SCB.vtor.read();
|
|
|
|
|
|
|
|
// After reset, core 1 is waiting to receive commands over FIFO.
|
|
|
|
// This is the sequence to have it jump to some code.
|
|
|
|
let cmd_seq = [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
vector_table as usize,
|
|
|
|
stack_ptr as usize,
|
|
|
|
core1_startup::<F> as usize,
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut seq = 0;
|
|
|
|
let mut fails = 0;
|
|
|
|
loop {
|
|
|
|
let cmd = cmd_seq[seq] as u32;
|
|
|
|
if cmd == 0 {
|
|
|
|
fifo_drain();
|
|
|
|
cortex_m::asm::sev();
|
|
|
|
}
|
|
|
|
fifo_write(cmd);
|
|
|
|
|
|
|
|
let response = fifo_read();
|
|
|
|
if cmd == response {
|
|
|
|
seq += 1;
|
|
|
|
} else {
|
|
|
|
seq = 0;
|
|
|
|
fails += 1;
|
|
|
|
if fails > 16 {
|
|
|
|
// The second core isn't responding, and isn't going to take the entrypoint
|
|
|
|
panic!("CORE1 not responding");
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 12:49:51 +00:00
|
|
|
if seq >= cmd_seq.len() {
|
|
|
|
break;
|
|
|
|
}
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2022-12-13 12:49:51 +00:00
|
|
|
|
|
|
|
// Wait until the other core has copied `entry` before returning.
|
|
|
|
fifo_read();
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 03:02:28 +00:00
|
|
|
/// Pause execution on CORE1.
|
|
|
|
pub fn pause_core1() {
|
|
|
|
if IS_CORE1_INIT.load(Ordering::Acquire) {
|
|
|
|
fifo_write(PAUSE_TOKEN);
|
|
|
|
// Wait for CORE1 to signal it has paused execution.
|
|
|
|
while fifo_read() != PAUSE_TOKEN {}
|
|
|
|
}
|
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 03:02:28 +00:00
|
|
|
/// Resume CORE1 execution.
|
|
|
|
pub fn resume_core1() {
|
|
|
|
if IS_CORE1_INIT.load(Ordering::Acquire) {
|
|
|
|
fifo_write(RESUME_TOKEN);
|
|
|
|
// Wait for CORE1 to signal it has resumed execution.
|
|
|
|
while fifo_read() != RESUME_TOKEN {}
|
|
|
|
}
|
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 03:02:28 +00:00
|
|
|
// Push a value to the inter-core FIFO, block until space is available
|
|
|
|
#[inline(always)]
|
|
|
|
fn fifo_write(value: u32) {
|
2023-06-15 23:32:18 +00:00
|
|
|
let sio = pac::SIO;
|
|
|
|
// Wait for the FIFO to have enough space
|
|
|
|
while !sio.fifo().st().read().rdy() {
|
|
|
|
cortex_m::asm::nop();
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2023-06-15 23:32:18 +00:00
|
|
|
sio.fifo().wr().write_value(value);
|
2022-12-13 03:02:28 +00:00
|
|
|
// Fire off an event to the other core.
|
|
|
|
// This is required as the other core may be `wfe` (waiting for event)
|
|
|
|
cortex_m::asm::sev();
|
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 03:02:28 +00:00
|
|
|
// Pop a value from inter-core FIFO, block until available
|
|
|
|
#[inline(always)]
|
|
|
|
fn fifo_read() -> u32 {
|
2023-06-15 23:32:18 +00:00
|
|
|
let sio = pac::SIO;
|
|
|
|
// Wait until FIFO has data
|
|
|
|
while !sio.fifo().st().read().vld() {
|
|
|
|
cortex_m::asm::nop();
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2023-06-15 23:32:18 +00:00
|
|
|
sio.fifo().rd().read()
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 03:02:28 +00:00
|
|
|
// Pop a value from inter-core FIFO, `wfe` until available
|
|
|
|
#[inline(always)]
|
2023-06-08 16:00:19 +00:00
|
|
|
#[allow(unused)]
|
2022-12-13 03:02:28 +00:00
|
|
|
fn fifo_read_wfe() -> u32 {
|
2023-06-15 23:32:18 +00:00
|
|
|
let sio = pac::SIO;
|
|
|
|
// Wait until FIFO has data
|
|
|
|
while !sio.fifo().st().read().vld() {
|
|
|
|
cortex_m::asm::wfe();
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2023-06-15 23:32:18 +00:00
|
|
|
sio.fifo().rd().read()
|
2022-12-13 03:02:28 +00:00
|
|
|
}
|
2022-12-10 07:26:35 +00:00
|
|
|
|
2022-12-13 03:02:28 +00:00
|
|
|
// Drain inter-core FIFO
|
|
|
|
#[inline(always)]
|
|
|
|
fn fifo_drain() {
|
2023-06-15 23:32:18 +00:00
|
|
|
let sio = pac::SIO;
|
|
|
|
while sio.fifo().st().read().vld() {
|
|
|
|
let _ = sio.fifo().rd().read();
|
2022-12-10 07:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/nvzqz/bad-rs/blob/master/src/never.rs
|
|
|
|
mod bad {
|
|
|
|
pub(crate) type Never = <F as HasOutput>::Output;
|
|
|
|
|
|
|
|
pub trait HasOutput {
|
|
|
|
type Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<O> HasOutput for fn() -> O {
|
|
|
|
type Output = O;
|
|
|
|
}
|
|
|
|
|
|
|
|
type F = fn() -> !;
|
|
|
|
}
|