Skip to content

Commit f3c5286

Browse files
authored
Add Cpu::COUNT and clean up in esp-hal-embassy (#2411)
* Add Cpu::COUNT and simplify * Avoid a bounds check during pending
1 parent 6bf03f6 commit f3c5286

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

esp-hal-embassy/src/executor/interrupt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<const SWI: u8> InterruptExecutor<SWI> {
9999
unsafe {
100100
(*self.executor.get())
101101
.as_mut_ptr()
102-
.write(raw::Executor::new(SWI as *mut ()));
102+
.write(raw::Executor::new((SWI as usize) as *mut ()));
103103

104104
EXECUTORS[SWI as usize].set((*self.executor.get()).as_mut_ptr());
105105
}

esp-hal-embassy/src/executor/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ mod thread;
77
fn __pender(context: *mut ()) {
88
use esp_hal::interrupt::software::SoftwareInterrupt;
99

10-
let context = (context as usize).to_le_bytes();
11-
12-
match context[0] {
10+
match context as usize {
1311
// For interrupt executors, the context value is the
1412
// software interrupt number
1513
0 => unsafe { SoftwareInterrupt::<0>::steal().raise() },
1614
1 => unsafe { SoftwareInterrupt::<1>::steal().raise() },
1715
2 => unsafe { SoftwareInterrupt::<2>::steal().raise() },
16+
#[cfg(not(multi_core))]
1817
3 => unsafe { SoftwareInterrupt::<3>::steal().raise() },
19-
other => {
20-
assert_eq!(other, THREAD_MODE_CONTEXT);
21-
// THREAD_MODE_CONTEXT id is reserved for thread mode executors
22-
thread::pend_thread_mode(context[1] as usize)
23-
}
18+
// THREAD_MODE_CONTEXT + core ID
19+
16 => thread::pend_thread_mode(0),
20+
#[cfg(multi_core)]
21+
17 => thread::pend_thread_mode(1),
22+
_ => unreachable!(),
2423
}
2524
}

esp-hal-embassy/src/executor/thread.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
use core::marker::PhantomData;
44

55
use embassy_executor::{raw, Spawner};
6-
use esp_hal::get_core;
6+
use esp_hal::{get_core, Cpu};
77
#[cfg(multi_core)]
88
use esp_hal::{interrupt::software::SoftwareInterrupt, macros::handler};
99
use portable_atomic::{AtomicBool, Ordering};
1010

11-
pub(crate) const THREAD_MODE_CONTEXT: u8 = 16;
11+
pub(crate) const THREAD_MODE_CONTEXT: usize = 16;
1212

1313
/// global atomic used to keep track of whether there is work to do since sev()
1414
/// is not available on either Xtensa or RISC-V
15-
#[cfg(not(multi_core))]
16-
static SIGNAL_WORK_THREAD_MODE: [AtomicBool; 1] = [AtomicBool::new(false)];
17-
#[cfg(multi_core)]
18-
static SIGNAL_WORK_THREAD_MODE: [AtomicBool; 2] = [AtomicBool::new(false), AtomicBool::new(false)];
15+
static SIGNAL_WORK_THREAD_MODE: [AtomicBool; Cpu::COUNT] =
16+
[const { AtomicBool::new(false) }; Cpu::COUNT];
1917

2018
#[cfg(multi_core)]
2119
#[handler]
@@ -72,12 +70,7 @@ This will use software-interrupt 3 which isn't available for anything else to wa
7270
}
7371

7472
Self {
75-
inner: raw::Executor::new(usize::from_le_bytes([
76-
THREAD_MODE_CONTEXT,
77-
get_core() as u8,
78-
0,
79-
0,
80-
]) as *mut ()),
73+
inner: raw::Executor::new((THREAD_MODE_CONTEXT + get_core() as usize) as *mut ()),
8174
not_send: PhantomData,
8275
}
8376
}

esp-hal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- `Pins::steal()` to unsafely obtain GPIO. (#2335)
2121
- `I2c::with_timeout` (#2361)
2222
- `Spi::half_duplex_read` and `Spi::half_duplex_write` (#2373)
23+
- `Cpu::COUNT` and `Cpu::current()` (#?)
2324

2425
### Changed
2526

esp-hal/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ pub enum Cpu {
363363
AppCpu = 1,
364364
}
365365

366+
impl Cpu {
367+
/// The number of available cores.
368+
pub const COUNT: usize = 1 + cfg!(multi_core) as usize;
369+
370+
/// Returns the core the application is currently executing on
371+
#[inline(always)]
372+
pub fn current() -> Self {
373+
get_core()
374+
}
375+
}
376+
366377
/// Which core the application is currently executing on
367378
#[inline(always)]
368379
pub fn get_core() -> Cpu {

0 commit comments

Comments
 (0)