Add Cpu::COUNT and clean up in esp-hal-embassy (#2411)

* Add Cpu::COUNT and simplify

* Avoid a bounds check during pending
This commit is contained in:
Dániel Buga 2024-10-26 15:36:10 +02:00 committed by GitHub
parent 6bf03f63a9
commit f3c5286028
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 21 deletions

View File

@ -99,7 +99,7 @@ impl<const SWI: u8> InterruptExecutor<SWI> {
unsafe { unsafe {
(*self.executor.get()) (*self.executor.get())
.as_mut_ptr() .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()); EXECUTORS[SWI as usize].set((*self.executor.get()).as_mut_ptr());
} }

View File

@ -7,19 +7,18 @@ mod thread;
fn __pender(context: *mut ()) { fn __pender(context: *mut ()) {
use esp_hal::interrupt::software::SoftwareInterrupt; use esp_hal::interrupt::software::SoftwareInterrupt;
let context = (context as usize).to_le_bytes(); match context as usize {
match context[0] {
// For interrupt executors, the context value is the // For interrupt executors, the context value is the
// software interrupt number // software interrupt number
0 => unsafe { SoftwareInterrupt::<0>::steal().raise() }, 0 => unsafe { SoftwareInterrupt::<0>::steal().raise() },
1 => unsafe { SoftwareInterrupt::<1>::steal().raise() }, 1 => unsafe { SoftwareInterrupt::<1>::steal().raise() },
2 => unsafe { SoftwareInterrupt::<2>::steal().raise() }, 2 => unsafe { SoftwareInterrupt::<2>::steal().raise() },
#[cfg(not(multi_core))]
3 => unsafe { SoftwareInterrupt::<3>::steal().raise() }, 3 => unsafe { SoftwareInterrupt::<3>::steal().raise() },
other => { // THREAD_MODE_CONTEXT + core ID
assert_eq!(other, THREAD_MODE_CONTEXT); 16 => thread::pend_thread_mode(0),
// THREAD_MODE_CONTEXT id is reserved for thread mode executors #[cfg(multi_core)]
thread::pend_thread_mode(context[1] as usize) 17 => thread::pend_thread_mode(1),
} _ => unreachable!(),
} }
} }

View File

@ -3,19 +3,17 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use embassy_executor::{raw, Spawner}; use embassy_executor::{raw, Spawner};
use esp_hal::get_core; use esp_hal::{get_core, Cpu};
#[cfg(multi_core)] #[cfg(multi_core)]
use esp_hal::{interrupt::software::SoftwareInterrupt, macros::handler}; use esp_hal::{interrupt::software::SoftwareInterrupt, macros::handler};
use portable_atomic::{AtomicBool, Ordering}; 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() /// global atomic used to keep track of whether there is work to do since sev()
/// is not available on either Xtensa or RISC-V /// is not available on either Xtensa or RISC-V
#[cfg(not(multi_core))] static SIGNAL_WORK_THREAD_MODE: [AtomicBool; Cpu::COUNT] =
static SIGNAL_WORK_THREAD_MODE: [AtomicBool; 1] = [AtomicBool::new(false)]; [const { AtomicBool::new(false) }; Cpu::COUNT];
#[cfg(multi_core)]
static SIGNAL_WORK_THREAD_MODE: [AtomicBool; 2] = [AtomicBool::new(false), AtomicBool::new(false)];
#[cfg(multi_core)] #[cfg(multi_core)]
#[handler] #[handler]
@ -72,12 +70,7 @@ This will use software-interrupt 3 which isn't available for anything else to wa
} }
Self { Self {
inner: raw::Executor::new(usize::from_le_bytes([ inner: raw::Executor::new((THREAD_MODE_CONTEXT + get_core() as usize) as *mut ()),
THREAD_MODE_CONTEXT,
get_core() as u8,
0,
0,
]) as *mut ()),
not_send: PhantomData, not_send: PhantomData,
} }
} }

View File

@ -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) - `Pins::steal()` to unsafely obtain GPIO. (#2335)
- `I2c::with_timeout` (#2361) - `I2c::with_timeout` (#2361)
- `Spi::half_duplex_read` and `Spi::half_duplex_write` (#2373) - `Spi::half_duplex_read` and `Spi::half_duplex_write` (#2373)
- `Cpu::COUNT` and `Cpu::current()` (#?)
### Changed ### Changed

View File

@ -363,6 +363,17 @@ pub enum Cpu {
AppCpu = 1, 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 /// Which core the application is currently executing on
#[inline(always)] #[inline(always)]
pub fn get_core() -> Cpu { pub fn get_core() -> Cpu {