From 92a2cc7c318a08e28b7b987b5bb01f6b0320da2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 29 Aug 2023 19:39:17 +0200 Subject: [PATCH] Update to embassy-executor 0.3 (#756) --- .github/workflows/ci.yml | 12 ++--- CHANGELOG.md | 2 + esp-hal-common/Cargo.toml | 2 +- .../src/embassy/executor/xtensa/interrupt.rs | 34 ++++++------ .../src/embassy/executor/xtensa/mod.rs | 20 +++++++ .../src/embassy/executor/xtensa/thread.rs | 53 ++++++++++--------- esp32-hal/Cargo.toml | 2 +- esp32c2-hal/Cargo.toml | 2 +- esp32c3-hal/Cargo.toml | 2 +- esp32c6-hal/Cargo.toml | 2 +- esp32h2-hal/Cargo.toml | 2 +- esp32s2-hal/Cargo.toml | 2 +- esp32s3-hal/Cargo.toml | 2 +- 13 files changed, 80 insertions(+), 57 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0f2cb6990f..e92ca5c531e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -320,12 +320,12 @@ jobs: # FIXME: `time-systick` feature disabled for now, see 'esp32s2-hal/Cargo.toml'. # - name: check esp32s2-hal (async, systick) # run: cd esp32s2-hal/ && cargo check --example=embassy_hello_world --features=embassy,embassy-time-systick,executor - - name: check esp32-hal (embassy, timg0) + - name: check esp32s2-hal (embassy, timg0) run: | cd esp32s2-hal/ cargo check --example=embassy_hello_world --features=embassy,embassy-time-timg0,embassy-executor-thread cargo check --example=embassy_multiprio --features=embassy,embassy-time-timg0,embassy-executor-interrupt - - name: check esp32-hal (embassy, timg0, async) + - name: check esp32s2-hal (embassy, timg0, async) run: | cd esp32s2-hal/ cargo check --example=embassy_wait --features=embassy,embassy-time-timg0,async,embassy-executor-thread @@ -360,28 +360,28 @@ jobs: # confident that they link. - name: check esp32s3-hal (common features) run: cd esp32s3-hal/ && cargo check --examples --features=eh1,ufmt - - name: check esp32-hal (embassy, timg0) + - name: check esp32s3-hal (embassy, timg0) run: | cd esp32s3-hal/ cargo check --example=embassy_hello_world --features=embassy,embassy-time-timg0,embassy-executor-thread cargo check --example=embassy_multicore --features=embassy,embassy-time-timg0,embassy-executor-thread cargo check --example=embassy_multicore_interrupt --features=embassy,embassy-time-timg0,embassy-executor-interrupt cargo check --example=embassy_multiprio --features=embassy,embassy-time-timg0,embassy-executor-interrupt - - name: check esp32-hal (embassy, systick) + - name: check esp32s3-hal (embassy, systick) run: | cd esp32s3-hal/ cargo check --example=embassy_hello_world --features=embassy,embassy-time-systick,embassy-executor-thread cargo check --example=embassy_multicore --features=embassy,embassy-time-systick,embassy-executor-thread cargo check --example=embassy_multicore_interrupt --features=embassy,embassy-time-systick,embassy-executor-interrupt cargo check --example=embassy_multiprio --features=embassy,embassy-time-systick,embassy-executor-interrupt - - name: check esp32-hal (embassy, timg0, async) + - name: check esp32s3-hal (embassy, timg0, async) run: | cd esp32s3-hal/ cargo check --example=embassy_wait --features=embassy,embassy-time-timg0,async,embassy-executor-thread cargo check --example=embassy_spi --features=embassy,embassy-time-timg0,async,embassy-executor-thread cargo check --example=embassy_serial --features=embassy,embassy-time-timg0,async,embassy-executor-thread cargo check --example=embassy_i2c --features=embassy,embassy-time-timg0,async,embassy-executor-thread - - name: check esp32-hal (embassy, systick, async) + - name: check esp32s3-hal (embassy, systick, async) run: | cd esp32s3-hal/ cargo check --example=embassy_wait --features=embassy,embassy-time-systick,async,embassy-executor-thread diff --git a/CHANGELOG.md b/CHANGELOG.md index 718816afff0..630836f7b59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add multicore-aware embassy executor for Xtensa MCUs (#723). - Add interrupt-executor for Xtensa MCUs (#723). - Add PARL_IO RX driver for ESP32-C6 / ESP32-H2 (#760) +- Add multicore-aware embassy executor for Xtensa MCUs (#723, #756). +- Add interrupt-executor for Xtensa MCUs (#723, #756). ### Changed diff --git a/esp-hal-common/Cargo.toml b/esp-hal-common/Cargo.toml index 12765432057..b3604f515b1 100644 --- a/esp-hal-common/Cargo.toml +++ b/esp-hal-common/Cargo.toml @@ -36,7 +36,7 @@ usb-device = { version = "0.2.9", optional = true } # async embedded-hal-async = { version = "=1.0.0-rc.1", optional = true } embedded-io-async = { version = "0.5.0", optional = true } -embassy-executor = { version = "0.2.1", features = ["pender-callback", "integrated-timers"], optional = true } # pender is temporary +embassy-executor = { version = "0.3.0", features = ["integrated-timers"], optional = true } embassy-sync = { version = "0.2.0", optional = true } embassy-time = { version = "0.1.3", features = ["nightly"], optional = true } embassy-futures = { version = "0.1.0", optional = true } diff --git a/esp-hal-common/src/embassy/executor/xtensa/interrupt.rs b/esp-hal-common/src/embassy/executor/xtensa/interrupt.rs index 8ed5d61dae0..dedea2df6d7 100644 --- a/esp-hal-common/src/embassy/executor/xtensa/interrupt.rs +++ b/esp-hal-common/src/embassy/executor/xtensa/interrupt.rs @@ -3,14 +3,10 @@ use core::{ cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, - ptr, sync::atomic::{AtomicUsize, Ordering}, }; -use embassy_executor::{ - raw::{self, Pender}, - SendSpawner, -}; +use embassy_executor::{raw, SendSpawner}; #[cfg(dport)] use peripherals::DPORT as SystemPeripheral; #[cfg(system)] @@ -22,34 +18,39 @@ static FROM_CPU_IRQ_USED: AtomicUsize = AtomicUsize::new(0); pub trait SwPendableInterrupt { fn enable(priority: interrupt::Priority); + fn number() -> usize; fn pend(); fn clear(); } macro_rules! from_cpu { - ($cpu:literal) => { + ($irq:literal) => { paste::paste! { - pub struct []; + pub struct []; /// We don't allow using the same interrupt for multiple executors. - impl [] { + impl [] { fn set_bit(value: bool) { let system = unsafe { &*SystemPeripheral::PTR }; system - .[] - .write(|w| w.[]().bit(value)); + .[] + .write(|w| w.[]().bit(value)); } } - impl SwPendableInterrupt for [] { + impl SwPendableInterrupt for [] { fn enable(priority: interrupt::Priority) { - let mask = 1 << $cpu; + let mask = 1 << $irq; if FROM_CPU_IRQ_USED.fetch_or(mask, Ordering::SeqCst) & mask != 0 { - panic!(concat!("FROM_CPU_", $cpu, " is already used by a different executor.")); + panic!(concat!("FROM_CPU_", $irq, " is already used by a different executor.")); } - interrupt::enable(peripherals::Interrupt::[], priority).unwrap(); + interrupt::enable(peripherals::Interrupt::[], priority).unwrap(); + } + + fn number() -> usize { + $irq } fn pend() { @@ -165,10 +166,7 @@ where unsafe { (*self.executor.get()) .as_mut_ptr() - .write(raw::Executor::new(Pender::new_from_callback( - |_| SWI::pend(), - ptr::null_mut(), - ))) + .write(raw::Executor::new(SWI::number() as *mut ())) } SWI::enable(priority); diff --git a/esp-hal-common/src/embassy/executor/xtensa/mod.rs b/esp-hal-common/src/embassy/executor/xtensa/mod.rs index 27014363e58..918b6b31d11 100644 --- a/esp-hal-common/src/embassy/executor/xtensa/mod.rs +++ b/esp-hal-common/src/embassy/executor/xtensa/mod.rs @@ -11,3 +11,23 @@ pub mod thread; #[cfg(feature = "embassy-executor-thread")] pub use thread::*; + +#[export_name = "__pender"] +fn __pender(context: *mut ()) { + let context = (context as usize).to_le_bytes(); + + cfg_if::cfg_if! { + if #[cfg(feature = "embassy-executor-interrupt")] { + match context[0] { + #[cfg(feature = "embassy-executor-thread")] + 0 => pend_thread_mode(context[1] as usize), + 1 => FromCpu1::pend(), + 2 => FromCpu2::pend(), + 3 => FromCpu3::pend(), + _ => {} + } + } else { + pend_thread_mode(context[1] as usize); + } + } +} diff --git a/esp-hal-common/src/embassy/executor/xtensa/thread.rs b/esp-hal-common/src/embassy/executor/xtensa/thread.rs index 2cc7789ed1a..184ae1560f8 100644 --- a/esp-hal-common/src/embassy/executor/xtensa/thread.rs +++ b/esp-hal-common/src/embassy/executor/xtensa/thread.rs @@ -4,10 +4,7 @@ use core::{ sync::atomic::{AtomicBool, Ordering}, }; -use embassy_executor::{ - raw::{self, Pender}, - Spawner, -}; +use embassy_executor::{raw, Spawner}; #[cfg(all(dport, multi_core))] use peripherals::DPORT as SystemPeripheral; #[cfg(all(system, multi_core))] @@ -39,6 +36,32 @@ fn FROM_CPU_INTR0() { } } +pub(super) fn pend_thread_mode(core: usize) { + use core::sync::atomic::Ordering; + + #[cfg(dport)] + use crate::peripherals::DPORT as SystemPeripheral; + #[cfg(system)] + use crate::peripherals::SYSTEM as SystemPeripheral; + + // Signal that there is work to be done. + SIGNAL_WORK_THREAD_MODE[core].store(true, Ordering::SeqCst); + + // 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 != crate::get_core() 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. + + let system = unsafe { &*SystemPeripheral::PTR }; + system + .cpu_intr_from_cpu_0 + .write(|w| w.cpu_intr_from_cpu_0().bit(true)); + } +} + /// Multi-core Xtensa Executor pub struct Executor { inner: raw::Executor, @@ -56,27 +79,7 @@ impl Executor { .unwrap(); Self { - inner: raw::Executor::new(Pender::new_from_callback( - |ctx| { - let core = ctx as usize; - - // Signal that there is work to be done. - SIGNAL_WORK_THREAD_MODE[core].store(true, Ordering::SeqCst); - - // 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 != get_core() as usize { - // We need to clear the interrupt from software. - - let system = unsafe { &*SystemPeripheral::PTR }; - system - .cpu_intr_from_cpu_0 - .write(|w| w.cpu_intr_from_cpu_0().bit(true)); - } - }, - get_core() as usize as *mut (), - )), + inner: raw::Executor::new(usize::from_le_bytes([0, get_core() as u8, 0, 0]) as *mut ()), not_send: PhantomData, } } diff --git a/esp32-hal/Cargo.toml b/esp32-hal/Cargo.toml index 17cf338535a..e3a838298e6 100644 --- a/esp32-hal/Cargo.toml +++ b/esp32-hal/Cargo.toml @@ -32,7 +32,7 @@ embassy-time = { version = "0.1.3", features = ["nightly"], optional = true } aes = "0.8.3" critical-section = "1.1.2" crypto-bigint = { version = "0.5.2", default-features = false } -embassy-executor = { version = "0.2.1", features = ["nightly"] } +embassy-executor = { version = "0.3.0", features = ["nightly"] } embassy-sync = "0.2.0" embedded-graphics = "0.8.1" embedded-hal-1 = { version = "=1.0.0-rc.1", package = "embedded-hal" } diff --git a/esp32c2-hal/Cargo.toml b/esp32c2-hal/Cargo.toml index c6f2d71a3e6..02b1a8e0e84 100644 --- a/esp32c2-hal/Cargo.toml +++ b/esp32c2-hal/Cargo.toml @@ -30,7 +30,7 @@ embassy-time = { version = "0.1.3", features = ["nightly"], optional = true } [dev-dependencies] critical-section = "1.1.2" -embassy-executor = { version = "0.2.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } +embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } embedded-graphics = "0.8.1" embedded-hal-1 = { version = "=1.0.0-rc.1", package = "embedded-hal" } embedded-hal-async = "=1.0.0-rc.1" diff --git a/esp32c3-hal/Cargo.toml b/esp32c3-hal/Cargo.toml index 36163704d05..d9aa6fc25d4 100644 --- a/esp32c3-hal/Cargo.toml +++ b/esp32c3-hal/Cargo.toml @@ -33,7 +33,7 @@ embassy-time = { version = "0.1.3", features = ["nightly"], optional = true } aes = "0.8.3" critical-section = "1.1.2" crypto-bigint = { version = "0.5.2", default-features = false } -embassy-executor = { version = "0.2.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } +embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } embedded-can = "0.4.1" embedded-graphics = "0.8.1" embedded-hal = { version = "0.2.7", features = ["unproven"] } diff --git a/esp32c6-hal/Cargo.toml b/esp32c6-hal/Cargo.toml index 9d0e59de3dd..d6e7212a13f 100644 --- a/esp32c6-hal/Cargo.toml +++ b/esp32c6-hal/Cargo.toml @@ -33,7 +33,7 @@ embassy-time = { version = "0.1.3", features = ["nightly"], optional = true } aes = "0.8.3" critical-section = "1.1.2" crypto-bigint = { version = "0.5.2", default-features = false} -embassy-executor = { version = "0.2.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } +embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } embedded-graphics = "0.8.1" embedded-hal-1 = { version = "=1.0.0-rc.1", package = "embedded-hal" } embedded-hal-async = "=1.0.0-rc.1" diff --git a/esp32h2-hal/Cargo.toml b/esp32h2-hal/Cargo.toml index 0eddb761383..d5a40de661b 100644 --- a/esp32h2-hal/Cargo.toml +++ b/esp32h2-hal/Cargo.toml @@ -33,7 +33,7 @@ embassy-time = { version = "0.1.3", features = ["nightly"], optional = true } aes = "0.8.3" critical-section = "1.1.2" crypto-bigint = { version = "0.5.2", default-features = false } -embassy-executor = { version = "0.2.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } +embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } embedded-graphics = "0.8.1" embedded-hal-1 = { version = "=1.0.0-rc.1", package = "embedded-hal" } embedded-hal-async = "=1.0.0-rc.1" diff --git a/esp32s2-hal/Cargo.toml b/esp32s2-hal/Cargo.toml index cc046e570c5..f8fce0443f6 100644 --- a/esp32s2-hal/Cargo.toml +++ b/esp32s2-hal/Cargo.toml @@ -33,7 +33,7 @@ xtensa-atomic-emulation-trap = "0.4.0" aes = "0.8.3" critical-section = "1.1.2" crypto-bigint = { version = "0.5.2", default-features = false } -embassy-executor = { version = "0.2.1", features = ["nightly"] } +embassy-executor = { version = "0.3.0", features = ["nightly"] } embassy-sync = "0.2.0" embedded-graphics = "0.8.1" embedded-hal-1 = { version = "=1.0.0-rc.1", package = "embedded-hal" } diff --git a/esp32s3-hal/Cargo.toml b/esp32s3-hal/Cargo.toml index 49ebb6172b3..a76e870085b 100644 --- a/esp32s3-hal/Cargo.toml +++ b/esp32s3-hal/Cargo.toml @@ -33,7 +33,7 @@ r0 = { version = "1.0.0", optional = true } aes = "0.8.3" critical-section = "1.1.2" crypto-bigint = { version = "0.5.2", default-features = false } -embassy-executor = { version = "0.2.1", features = ["nightly"] } +embassy-executor = { version = "0.3.0", features = ["nightly"] } embassy-sync = "0.2.0" embedded-graphics = "0.8.1" embedded-hal = { version = "0.2.7", features = ["unproven"] }