Skip to content

Commit

Permalink
Remove get_core() in favour of Cpu::current() (#2533)
Browse files Browse the repository at this point in the history
* Remove `esp_hal::core()` in favour of `Cpu::current()`

* Update migration guide

* Update `CHANGELOG.md`
  • Loading branch information
jessebraham authored Nov 13, 2024
1 parent 92b9125 commit c19c7fc
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 51 deletions.
4 changes: 2 additions & 2 deletions esp-hal-embassy/src/executor/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::{cell::UnsafeCell, mem::MaybeUninit};

use embassy_executor::{raw, SendSpawner};
use esp_hal::{
core,
interrupt::{self, software::SoftwareInterrupt, InterruptHandler},
Cpu,
};
use portable_atomic::{AtomicUsize, Ordering};

Expand Down Expand Up @@ -87,7 +87,7 @@ impl<const SWI: u8> InterruptExecutor<SWI> {
.core
.compare_exchange(
usize::MAX,
core() as usize,
Cpu::current() as usize,
Ordering::Acquire,
Ordering::Relaxed,
)
Expand Down
8 changes: 4 additions & 4 deletions esp-hal-embassy/src/executor/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use core::marker::PhantomData;

use embassy_executor::{raw, Spawner};
use esp_hal::{core, Cpu};
use esp_hal::Cpu;
#[cfg(multi_core)]
use esp_hal::{interrupt::software::SoftwareInterrupt, macros::handler};
#[cfg(low_power_wait)]
Expand Down Expand Up @@ -35,7 +35,7 @@ pub(crate) fn pend_thread_mode(_core: usize) {
// If we are pending a task on the current core, we're done. Otherwise, we
// need to make sure the other core wakes up.
#[cfg(multi_core)]
if _core != core() as usize {
if _core != Cpu::current() as usize {
// We need to clear the interrupt from software. We don't actually
// need it to trigger and run the interrupt handler, we just need to
// kick waiti to return.
Expand Down Expand Up @@ -74,7 +74,7 @@ This will use software-interrupt 3 which isn't available for anything else to wa
}

Self {
inner: raw::Executor::new((THREAD_MODE_CONTEXT + core() as usize) as *mut ()),
inner: raw::Executor::new((THREAD_MODE_CONTEXT + Cpu::current() as usize) as *mut ()),
not_send: PhantomData,
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ This will use software-interrupt 3 which isn't available for anything else to wa
init(self.inner.spawner());

#[cfg(low_power_wait)]
let cpu = core() as usize;
let cpu = Cpu::current() as usize;

loop {
unsafe { self.inner.poll() };
Expand Down
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `{Spi, SpiDma, SpiDmaBus}` configuration methods (#2448)
- `Io::new_with_priority` and `Io::new_no_bind_interrupt`. (#2486)
- `parl_io::{no_clk_pin(), NoClkPin}` (#2531)
- Removed `get_core` function in favour of `Cpu::current` (#2533)

## [0.21.1]

Expand Down
7 changes: 7 additions & 0 deletions esp-hal/MIGRATING-0.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,10 @@ You can use `gpio::NoPin` instead.
In order to better comply with the Rust API Guidelines [getter names convention], we have removed the `get_` prefixes from all functions which previously had it. Due to the number of changes it's not practical to list all changes here, however if a function previous began with `get_`, you can simply remove this prefix.

[getter names convention]: https://rust-lang.github.io/api-guidelines/naming.html#c-getter

## The `get_core()` function has been removed in favour of `Cpu::current()`

```diff
- let core = esp_hal::get_core();
+ let core = esp_hal::Cpu::current();
```
12 changes: 6 additions & 6 deletions esp-hal/src/interrupt/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ pub fn enable_direct(
return Err(Error::InvalidInterruptPriority);
}
unsafe {
map(crate::core(), interrupt, cpu_interrupt);
set_priority(crate::core(), cpu_interrupt, level);
map(Cpu::current(), interrupt, cpu_interrupt);
set_priority(Cpu::current(), cpu_interrupt, level);
enable_cpu_interrupt(cpu_interrupt);
}
Ok(())
Expand Down Expand Up @@ -369,12 +369,12 @@ mod vectored {
pub(crate) unsafe fn init_vectoring() {
for (prio, num) in PRIORITY_TO_INTERRUPT.iter().enumerate() {
set_kind(
crate::core(),
Cpu::current(),
core::mem::transmute::<u32, CpuInterrupt>(*num as u32),
InterruptKind::Level,
);
set_priority(
crate::core(),
Cpu::current(),
core::mem::transmute::<u32, CpuInterrupt>(*num as u32),
core::mem::transmute::<u8, Priority>((prio as u8) + 1),
);
Expand Down Expand Up @@ -421,7 +421,7 @@ mod vectored {
let cpu_interrupt = core::mem::transmute::<u32, CpuInterrupt>(
PRIORITY_TO_INTERRUPT[(level as usize) - 1] as u32,
);
map(crate::core(), interrupt, cpu_interrupt);
map(Cpu::current(), interrupt, cpu_interrupt);
enable_cpu_interrupt(cpu_interrupt);
}
Ok(())
Expand Down Expand Up @@ -453,7 +453,7 @@ mod vectored {
#[no_mangle]
#[ram]
unsafe fn handle_interrupts(cpu_intr: CpuInterrupt, context: &mut TrapFrame) {
let core = crate::core();
let core = Cpu::current();
let status = status(core);

// this has no effect on level interrupts, but the interrupt may be an edge one
Expand Down
7 changes: 3 additions & 4 deletions esp-hal/src/interrupt/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn enable_direct(interrupt: Interrupt, cpu_interrupt: CpuInterrupt) -> Resul
return Err(Error::CpuInterruptReserved);
}
unsafe {
map(crate::core(), interrupt, cpu_interrupt);
map(Cpu::current(), interrupt, cpu_interrupt);

xtensa_lx::interrupt::enable_mask(
xtensa_lx::interrupt::get_mask() | 1 << cpu_interrupt as u32,
Expand Down Expand Up @@ -338,7 +338,6 @@ mod vectored {
use procmacros::ram;

use super::*;
use crate::core;

/// Interrupt priority levels.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -447,7 +446,7 @@ mod vectored {
interrupt_level_to_cpu_interrupt(level, chip_specific::interrupt_is_edge(interrupt))?;

unsafe {
map(core(), interrupt, cpu_interrupt);
map(Cpu::current(), interrupt, cpu_interrupt);

xtensa_lx::interrupt::enable_mask(
xtensa_lx::interrupt::get_mask() | 1 << cpu_interrupt as u32,
Expand Down Expand Up @@ -541,7 +540,7 @@ mod vectored {
#[no_mangle]
#[ram]
unsafe fn handle_interrupts(level: u32, save_frame: &mut Context) {
let core = crate::core();
let core = Cpu::current();

let cpu_interrupt_mask =
interrupt::get() & interrupt::get_mask() & CPU_INTERRUPT_LEVELS[level as usize];
Expand Down
32 changes: 13 additions & 19 deletions esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,26 @@ impl Cpu {
/// Returns the core the application is currently executing on
#[inline(always)]
pub fn current() -> Self {
core()
// This works for both RISCV and Xtensa because both
// get_raw_core functions return zero, _or_ something
// greater than zero; 1 in the case of RISCV and 0x2000
// in the case of Xtensa.
match raw_core() {
0 => Cpu::ProCpu,
#[cfg(all(multi_core, riscv))]
1 => Cpu::AppCpu,
#[cfg(all(multi_core, xtensa))]
0x2000 => Cpu::AppCpu,
_ => unreachable!(),
}
}

/// Returns an iterator over the "other" cores.
#[inline(always)]
pub fn other() -> impl Iterator<Item = Self> {
cfg_if::cfg_if! {
if #[cfg(multi_core)] {
match core() {
match Self::current() {
Cpu::ProCpu => [Cpu::AppCpu].into_iter(),
Cpu::AppCpu => [Cpu::ProCpu].into_iter(),
}
Expand All @@ -388,23 +399,6 @@ impl Cpu {
}
}

/// Which core the application is currently executing on
#[inline(always)]
pub fn core() -> Cpu {
// This works for both RISCV and Xtensa because both
// raw_core functions return zero, _or_ something
// greater than zero; 1 in the case of RISCV and 0x2000
// in the case of Xtensa.
match raw_core() {
0 => Cpu::ProCpu,
#[cfg(all(multi_core, riscv))]
1 => Cpu::AppCpu,
#[cfg(all(multi_core, xtensa))]
0x2000 => Cpu::AppCpu,
_ => unreachable!(),
}
}

/// Returns the raw value of the mhartid register.
///
/// Safety: This method should never return UNUSED_THREAD_ID_VALUE
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn software_reset_cpu() {
/// Retrieves the reason for the last reset as a SocResetReason enum value.
/// Returns `None` if the reset reason cannot be determined.
pub fn reset_reason() -> Option<SocResetReason> {
crate::rtc_cntl::reset_reason(crate::core())
crate::rtc_cntl::reset_reason(crate::Cpu::current())
}

/// Retrieves the cause of the last wakeup event as a SleepSource enum value.
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/soc/esp32/cpu_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<'d> CpuControl<'d> {
let entry = unsafe { ManuallyDrop::take(&mut *entry.cast::<ManuallyDrop<F>>()) };
entry();
loop {
unsafe { internal_park_core(crate::core()) };
unsafe { internal_park_core(Cpu::current()) };
}
}
None => panic!("No start function set"),
Expand Down
14 changes: 7 additions & 7 deletions esp-hal/src/soc/esp32/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub(crate) fn io_mux_reg(gpio_num: u8) -> &'static io_mux::GPIO0 {
}

pub(crate) fn gpio_intr_enable(int_enable: bool, nmi_enable: bool) -> u8 {
match crate::core() {
match Cpu::current() {
Cpu::AppCpu => int_enable as u8 | ((nmi_enable as u8) << 1),
// this should be bits 3 & 4 respectively, according to the TRM, but it doesn't seem to
// work. This does though.
Expand Down Expand Up @@ -789,13 +789,13 @@ pub(crate) enum InterruptStatusRegisterAccess {
impl InterruptStatusRegisterAccess {
pub(crate) fn interrupt_status_read(self) -> u32 {
match self {
Self::Bank0 => match crate::core() {
crate::Cpu::ProCpu => unsafe { GPIO::steal() }.pcpu_int().read().bits(),
crate::Cpu::AppCpu => unsafe { GPIO::steal() }.acpu_int().read().bits(),
Self::Bank0 => match Cpu::current() {
Cpu::ProCpu => unsafe { GPIO::steal() }.pcpu_int().read().bits(),
Cpu::AppCpu => unsafe { GPIO::steal() }.acpu_int().read().bits(),
},
Self::Bank1 => match crate::core() {
crate::Cpu::ProCpu => unsafe { GPIO::steal() }.pcpu_int1().read().bits(),
crate::Cpu::AppCpu => unsafe { GPIO::steal() }.acpu_int1().read().bits(),
Self::Bank1 => match Cpu::current() {
Cpu::ProCpu => unsafe { GPIO::steal() }.pcpu_int1().read().bits(),
Cpu::AppCpu => unsafe { GPIO::steal() }.acpu_int1().read().bits(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/soc/esp32s3/cpu_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl<'d> CpuControl<'d> {
let entry = unsafe { ManuallyDrop::take(&mut *entry.cast::<ManuallyDrop<F>>()) };
entry();
loop {
unsafe { internal_park_core(crate::core()) };
unsafe { internal_park_core(Cpu::current()) };
}
}
None => panic!("No start function set"),
Expand Down
9 changes: 6 additions & 3 deletions examples/src/bin/embassy_multicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}
use embassy_time::{Duration, Ticker};
use esp_backtrace as _;
use esp_hal::{
core,
cpu_control::{CpuControl, Stack},
gpio::{Level, Output},
timer::{timg::TimerGroup, AnyTimer},
Cpu,
};
use esp_hal_embassy::Executor;
use esp_println::println;
Expand All @@ -37,7 +37,7 @@ async fn control_led(
mut led: Output<'static>,
control: &'static Signal<CriticalSectionRawMutex, bool>,
) {
println!("Starting control_led() on core {}", core() as usize);
println!("Starting control_led() on core {}", Cpu::current() as usize);
loop {
if control.wait().await {
esp_println::println!("LED on");
Expand Down Expand Up @@ -76,7 +76,10 @@ async fn main(_spawner: Spawner) {
.unwrap();

// Sends periodic messages to control_led, enabling or disabling it.
println!("Starting enable_disable_led() on core {}", core() as usize);
println!(
"Starting enable_disable_led() on core {}",
Cpu::current() as usize
);
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
esp_println::println!("Sending LED on");
Expand Down
9 changes: 6 additions & 3 deletions examples/src/bin/embassy_multicore_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}
use embassy_time::{Duration, Ticker};
use esp_backtrace as _;
use esp_hal::{
core,
cpu_control::{CpuControl, Stack},
gpio::{Level, Output},
interrupt::{software::SoftwareInterruptControl, Priority},
prelude::*,
timer::{timg::TimerGroup, AnyTimer},
Cpu,
};
use esp_hal_embassy::InterruptExecutor;
use esp_println::println;
Expand All @@ -38,7 +38,7 @@ async fn control_led(
mut led: Output<'static>,
control: &'static Signal<CriticalSectionRawMutex, bool>,
) {
println!("Starting control_led() on core {}", core() as usize);
println!("Starting control_led() on core {}", Cpu::current() as usize);
loop {
if control.wait().await {
esp_println::println!("LED on");
Expand All @@ -53,7 +53,10 @@ async fn control_led(
/// Sends periodic messages to control_led, enabling or disabling it.
#[embassy_executor::task]
async fn enable_disable_led(control: &'static Signal<CriticalSectionRawMutex, bool>) {
println!("Starting enable_disable_led() on core {}", core() as usize);
println!(
"Starting enable_disable_led() on core {}",
Cpu::current() as usize
);
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
esp_println::println!("Sending LED on");
Expand Down

0 comments on commit c19c7fc

Please sign in to comment.