diff --git a/esp-hal-embassy/src/executor/interrupt.rs b/esp-hal-embassy/src/executor/interrupt.rs index 9ec25e3ed..28ce6ccd5 100644 --- a/esp-hal-embassy/src/executor/interrupt.rs +++ b/esp-hal-embassy/src/executor/interrupt.rs @@ -99,7 +99,7 @@ impl InterruptExecutor { unsafe { (*self.executor.get()) .as_mut_ptr() - .write(raw::Executor::new(SWI as *mut ())); + .write(raw::Executor::new((SWI as usize) as *mut ())); EXECUTORS[SWI as usize].set((*self.executor.get()).as_mut_ptr()); } diff --git a/esp-hal-embassy/src/executor/mod.rs b/esp-hal-embassy/src/executor/mod.rs index 51e43f34e..5ed537704 100644 --- a/esp-hal-embassy/src/executor/mod.rs +++ b/esp-hal-embassy/src/executor/mod.rs @@ -7,19 +7,18 @@ mod thread; fn __pender(context: *mut ()) { use esp_hal::interrupt::software::SoftwareInterrupt; - let context = (context as usize).to_le_bytes(); - - match context[0] { + match context as usize { // For interrupt executors, the context value is the // software interrupt number 0 => unsafe { SoftwareInterrupt::<0>::steal().raise() }, 1 => unsafe { SoftwareInterrupt::<1>::steal().raise() }, 2 => unsafe { SoftwareInterrupt::<2>::steal().raise() }, + #[cfg(not(multi_core))] 3 => unsafe { SoftwareInterrupt::<3>::steal().raise() }, - other => { - assert_eq!(other, THREAD_MODE_CONTEXT); - // THREAD_MODE_CONTEXT id is reserved for thread mode executors - thread::pend_thread_mode(context[1] as usize) - } + // THREAD_MODE_CONTEXT + core ID + 16 => thread::pend_thread_mode(0), + #[cfg(multi_core)] + 17 => thread::pend_thread_mode(1), + _ => unreachable!(), } } diff --git a/esp-hal-embassy/src/executor/thread.rs b/esp-hal-embassy/src/executor/thread.rs index 61a014302..4af62ba3e 100644 --- a/esp-hal-embassy/src/executor/thread.rs +++ b/esp-hal-embassy/src/executor/thread.rs @@ -3,19 +3,17 @@ use core::marker::PhantomData; use embassy_executor::{raw, Spawner}; -use esp_hal::get_core; +use esp_hal::{get_core, Cpu}; #[cfg(multi_core)] use esp_hal::{interrupt::software::SoftwareInterrupt, macros::handler}; use portable_atomic::{AtomicBool, Ordering}; -pub(crate) const THREAD_MODE_CONTEXT: u8 = 16; +pub(crate) const THREAD_MODE_CONTEXT: usize = 16; /// global atomic used to keep track of whether there is work to do since sev() /// is not available on either Xtensa or RISC-V -#[cfg(not(multi_core))] -static SIGNAL_WORK_THREAD_MODE: [AtomicBool; 1] = [AtomicBool::new(false)]; -#[cfg(multi_core)] -static SIGNAL_WORK_THREAD_MODE: [AtomicBool; 2] = [AtomicBool::new(false), AtomicBool::new(false)]; +static SIGNAL_WORK_THREAD_MODE: [AtomicBool; Cpu::COUNT] = + [const { AtomicBool::new(false) }; Cpu::COUNT]; #[cfg(multi_core)] #[handler] @@ -72,12 +70,7 @@ This will use software-interrupt 3 which isn't available for anything else to wa } Self { - inner: raw::Executor::new(usize::from_le_bytes([ - THREAD_MODE_CONTEXT, - get_core() as u8, - 0, - 0, - ]) as *mut ()), + inner: raw::Executor::new((THREAD_MODE_CONTEXT + get_core() as usize) as *mut ()), not_send: PhantomData, } } diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index eceb005f7..e2f4c1c3e 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Pins::steal()` to unsafely obtain GPIO. (#2335) - `I2c::with_timeout` (#2361) - `Spi::half_duplex_read` and `Spi::half_duplex_write` (#2373) +- `Cpu::COUNT` and `Cpu::current()` (#?) ### Changed diff --git a/esp-hal/src/lib.rs b/esp-hal/src/lib.rs index 749247273..6622d34c5 100644 --- a/esp-hal/src/lib.rs +++ b/esp-hal/src/lib.rs @@ -363,6 +363,17 @@ pub enum Cpu { AppCpu = 1, } +impl Cpu { + /// The number of available cores. + pub const COUNT: usize = 1 + cfg!(multi_core) as usize; + + /// Returns the core the application is currently executing on + #[inline(always)] + pub fn current() -> Self { + get_core() + } +} + /// Which core the application is currently executing on #[inline(always)] pub fn get_core() -> Cpu {