use core::mem::ManuallyDrop;
use core::sync::atomic::compiler_fence;
use core::sync::atomic::Ordering;
use crate::pac;
use crate::Sio;
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
InvalidCore,
Unresponsive,
}
#[inline(always)]
fn install_stack_guard(stack_bottom: *mut usize) {
let core = unsafe { pac::CorePeripherals::steal() };
if core.MPU.ctrl.read() != 0 {
cortex_m::asm::udf();
}
let addr = (stack_bottom as u32 + 31) & !31;
let subregion_select = 0xff ^ (1 << ((addr >> 5) & 7));
unsafe {
core.MPU.ctrl.write(5); core.MPU.rbar.write((addr & !0xff) | 0x8);
core.MPU.rasr.write(
1 | (0x7 << 1) | (subregion_select << 8)
| 0x10000000, );
}
}
#[inline(always)]
fn core1_setup(stack_bottom: *mut usize) {
install_stack_guard(stack_bottom);
}
pub struct Multicore<'p> {
cores: [Core<'p>; 2],
}
#[repr(C, align(32))]
pub struct Stack<const SIZE: usize> {
pub mem: [usize; SIZE],
}
impl<const SIZE: usize> Stack<SIZE> {
pub const fn new() -> Stack<SIZE> {
Stack { mem: [0; SIZE] }
}
}
impl<'p> Multicore<'p> {
pub fn new(
psm: &'p mut pac::PSM,
ppb: &'p mut pac::PPB,
sio: &'p mut crate::sio::SioFifo,
) -> Self {
Self {
cores: [
Core { inner: None },
Core {
inner: Some((psm, ppb, sio)),
},
],
}
}
pub fn cores(&mut self) -> &'p mut [Core] {
&mut self.cores
}
}
pub struct Core<'p> {
inner: Option<(
&'p mut pac::PSM,
&'p mut pac::PPB,
&'p mut crate::sio::SioFifo,
)>,
}
impl<'p> Core<'p> {
pub fn id(&self) -> u8 {
match self.inner {
None => 0,
Some(..) => 1,
}
}
pub fn spawn<F>(&mut self, stack: &'static mut [usize], entry: F) -> Result<(), Error>
where
F: FnOnce() -> bad::Never + Send + 'static,
{
if let Some((psm, ppb, fifo)) = self.inner.as_mut() {
extern "C" fn core1_startup<F: FnOnce() -> bad::Never>(
_: u64,
_: u64,
entry: &mut ManuallyDrop<F>,
stack_bottom: *mut usize,
) -> ! {
core1_setup(stack_bottom);
let entry = unsafe { ManuallyDrop::take(entry) };
let peripherals = unsafe { pac::Peripherals::steal() };
let mut sio = Sio::new(peripherals.SIO);
sio.fifo.write_blocking(1);
entry()
}
psm.frce_off.modify(|_, w| w.proc1().set_bit());
while !psm.frce_off.read().proc1().bit_is_set() {
cortex_m::asm::nop();
}
psm.frce_off.modify(|_, w| w.proc1().clear_bit());
let mut stack_ptr = unsafe { stack.as_mut_ptr().add(stack.len()) };
let mut entry = ManuallyDrop::new(entry);
unsafe {
stack_ptr = stack_ptr.sub(1);
stack_ptr.cast::<*mut usize>().write(stack.as_mut_ptr());
stack_ptr = stack_ptr.sub(1);
stack_ptr.cast::<&mut ManuallyDrop<F>>().write(&mut entry);
}
compiler_fence(Ordering::Release);
let vector_table = ppb.vtor.read().bits();
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_blocking(cmd);
let response = fifo.read_blocking();
if cmd == response {
seq += 1;
} else {
seq = 0;
fails += 1;
if fails > 16 {
drop(ManuallyDrop::into_inner(entry));
return Err(Error::Unresponsive);
}
}
if seq >= cmd_seq.len() {
break;
}
}
fifo.read_blocking();
Ok(())
} else {
Err(Error::InvalidCore)
}
}
}
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() -> !;
}