Skip to content

Commit

Permalink
Add thread mode context id and fix up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed May 1, 2024
1 parent ef651e4 commit 24510e3
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 41 deletions.
5 changes: 1 addition & 4 deletions esp-hal/src/embassy/executor/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ use crate::{
system::SoftwareInterrupt,
};

/// The number to offset SW interrupts when passed through the pender context.
pub const SW_OFFSET: u8 = 16;

static mut EXECUTORS: [CallbackContext; 4] = [
CallbackContext::new(),
CallbackContext::new(),
Expand Down Expand Up @@ -123,7 +120,7 @@ impl<const SWI: u8> InterruptExecutor<SWI> {
unsafe {
(*self.executor.get())
.as_mut_ptr()
.write(raw::Executor::new((SWI + SW_OFFSET) as *mut ()));
.write(raw::Executor::new(SWI as *mut ()));

EXECUTORS[SWI as usize].set((*self.executor.get()).as_mut_ptr());
}
Expand Down
18 changes: 10 additions & 8 deletions esp-hal/src/embassy/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ fn __pender(context: *mut ()) {
let context = (context as usize).to_le_bytes();

match context[0] {
// 0 is reserved for thread mode executors
0 => thread::pend_thread_mode(context[1] as usize),
// For interrupt executors, the context value is the
// software interrupt number + `SW_OFFSET`
16 => unsafe { SoftwareInterrupt::<0>::steal().raise() },
17 => unsafe { SoftwareInterrupt::<1>::steal().raise() },
18 => unsafe { SoftwareInterrupt::<2>::steal().raise() },
19 => unsafe { SoftwareInterrupt::<3>::steal().raise() },
_ => {}
// software interrupt number
0 => unsafe { SoftwareInterrupt::<0>::steal().raise() },
1 => unsafe { SoftwareInterrupt::<1>::steal().raise() },
2 => unsafe { SoftwareInterrupt::<2>::steal().raise() },
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)
}
}
}
9 changes: 8 additions & 1 deletion esp-hal/src/embassy/executor/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::get_core;
#[cfg(multi_core)]
use crate::peripherals::SYSTEM;

pub const THREAD_MODE_CONTEXT: u8 = 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))]
Expand Down Expand Up @@ -68,7 +70,12 @@ impl Executor {
}

Self {
inner: raw::Executor::new(usize::from_le_bytes([0, get_core() as u8, 0, 0]) as *mut ()),
inner: raw::Executor::new(usize::from_le_bytes([
THREAD_MODE_CONTEXT,
get_core() as u8,
0,
0,
]) as *mut ()),
not_send: PhantomData,
}
}
Expand Down
7 changes: 3 additions & 4 deletions esp-hal/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,13 @@ pub struct SoftwareInterruptControl {

impl SoftwareInterruptControl {
fn new() -> Self {
// the thread-executor uses SW-INT0 when used on a multi-core system
// we cannot easily require `software_interrupt0` there since it's created
// before `main` via proc-macro

SoftwareInterruptControl {
software_interrupt0: SoftwareInterrupt {},
software_interrupt1: SoftwareInterrupt {},
software_interrupt2: SoftwareInterrupt {},
// the thread-executor uses SW-INT3 when used on a multi-core system
// we cannot easily require `software_interrupt3` there since it's created
// before `main` via proc-macro so we cfg it away from users
#[cfg(not(all(feature = "embassy", multi_core)))]
software_interrupt3: SoftwareInterrupt {},
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: async embassy embassy-executor-thread embassy-time-timg0 embassy-generic-timers
//% FEATURES: async embassy embassy-time-timg0 embassy-generic-timers

#![no_std]
#![no_main]
Expand Down
8 changes: 5 additions & 3 deletions examples/src/bin/embassy_multicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use esp_hal::{
timer::TimerGroup,
};
use esp_println::println;
use static_cell::make_static;
use static_cell::StaticCell;

static mut APP_CORE_STACK: Stack<8192> = Stack::new();

Expand Down Expand Up @@ -63,13 +63,15 @@ async fn main(_spawner: Spawner) {

let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);

let led_ctrl_signal = &*make_static!(Signal::new());
static LED_CTRL: StaticCell<Signal<CriticalSectionRawMutex, bool>> = StaticCell::new();
let led_ctrl_signal = &*LED_CTRL.init(Signal::new());

let led = io.pins.gpio0.into_push_pull_output();

let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, move || {
let executor = make_static!(Executor::new());
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(control_led(led, led_ctrl_signal)).ok();
});
Expand Down
26 changes: 14 additions & 12 deletions examples/src/bin/embassy_multicore_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use esp_hal::{
timer::TimerGroup,
};
use esp_println::println;
use static_cell::make_static;
use static_cell::StaticCell;

static mut APP_CORE_STACK: Stack<8192> = Stack::new();

Expand Down Expand Up @@ -82,29 +82,31 @@ fn main() -> ! {

let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);

let led_ctrl_signal = &*make_static!(Signal::new());
static LED_CTRL: StaticCell<Signal<CriticalSectionRawMutex, bool>> = StaticCell::new();
let led_ctrl_signal = &*LED_CTRL.init(Signal::new());

let led = io.pins.gpio0.into_push_pull_output();

static EXECUTOR_CORE_1: StaticCell<InterruptExecutor<1>> = StaticCell::new();
let executor_core1 =
InterruptExecutor::new(system.software_interrupt_control.software_interrupt1);
let executor_core1 = make_static!(executor_core1);
let executor_core1 = EXECUTOR_CORE_1.init(executor_core1);

let cpu1_fnctn = move || {
let spawner = executor_core1.start(Priority::Priority1);
let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, move || {
let spawner = executor_core1.start(Priority::Priority1);

spawner.spawn(control_led(led, led_ctrl_signal)).ok();
spawner.spawn(control_led(led, led_ctrl_signal)).ok();

// Just loop to show that the main thread does not need to poll the executor.
loop {}
};
let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, cpu1_fnctn)
// Just loop to show that the main thread does not need to poll the executor.
loop {}
})
.unwrap();

static EXECUTOR_CORE_0: StaticCell<InterruptExecutor<0>> = StaticCell::new();
let executor_core0 =
InterruptExecutor::new(system.software_interrupt_control.software_interrupt0);
let executor_core0 = make_static!(executor_core0);
let executor_core0 = EXECUTOR_CORE_0.init(executor_core0);

let spawner = executor_core0.start(Priority::Priority1);
spawner.spawn(enable_disable_led(led_ctrl_signal)).ok();
Expand Down
5 changes: 3 additions & 2 deletions examples/src/bin/embassy_multiprio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use esp_hal::{
timer::TimerGroup,
};
use esp_println::println;
use static_cell::make_static;
use static_cell::StaticCell;

/// Periodically print something.
#[embassy_executor::task]
Expand Down Expand Up @@ -82,8 +82,9 @@ async fn main(low_prio_spawner: Spawner) {
let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);

static EXECUTOR: StaticCell<InterruptExecutor<2>> = StaticCell::new();
let executor = InterruptExecutor::new(system.software_interrupt_control.software_interrupt2);
let executor = make_static!(executor);
let executor = EXECUTOR.init(executor);

let spawner = executor.start(Priority::Priority3);
spawner.must_spawn(high_prio());
Expand Down
5 changes: 3 additions & 2 deletions examples/src/bin/embassy_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use esp_hal::{
uart::{config::AtCmdConfig, Uart, UartRx, UartTx},
Async,
};
use static_cell::make_static;
use static_cell::StaticCell;

// rx_fifo_full_threshold
const READ_BUF_SIZE: usize = 64;
Expand Down Expand Up @@ -90,7 +90,8 @@ async fn main(spawner: Spawner) {
.unwrap();
let (tx, rx) = uart0.split();

let signal = &*make_static!(Signal::new());
static SIGNAL: StaticCell<Signal<NoopRawMutex, usize>> = StaticCell::new();
let signal = &*SIGNAL.init(Signal::new());

spawner.spawn(reader(rx, &signal)).ok();
spawner.spawn(writer(tx, &signal)).ok();
Expand Down
5 changes: 3 additions & 2 deletions examples/src/bin/embassy_twai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use esp_hal::{
twai::{self, EspTwaiFrame, TwaiRx, TwaiTx},
};
use esp_println::println;
use static_cell::make_static;
use static_cell::StaticCell;

type TwaiOutbox = Channel<NoopRawMutex, EspTwaiFrame, 16>;

Expand Down Expand Up @@ -132,7 +132,8 @@ async fn main(spawner: Spawner) {
)
.unwrap();

let channel = &*make_static!(Channel::new());
static CHANNEL: StaticCell<TwaiOutbox> = StaticCell::new();
let channel = &*CHANNEL.init(Channel::new());

spawner.spawn(receiver(rx, channel)).ok();
spawner.spawn(transmitter(tx, channel)).ok();
Expand Down
6 changes: 4 additions & 2 deletions examples/src/bin/embassy_usb_serial_jtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use esp_hal::{
usb_serial_jtag::{UsbSerialJtag, UsbSerialJtagRx, UsbSerialJtagTx},
Async,
};
use static_cell::make_static;
use static_cell::StaticCell;

const MAX_BUFFER_SIZE: usize = 512;

Expand Down Expand Up @@ -75,7 +75,9 @@ async fn main(spawner: Spawner) -> () {

let (tx, rx) = UsbSerialJtag::new_async(peripherals.USB_DEVICE).split();

let signal = &*make_static!(Signal::new());
static SIGNAL: StaticCell<Signal<NoopRawMutex, heapless::String<MAX_BUFFER_SIZE>>> =
StaticCell::new();
let signal = &*SIGNAL.init(Signal::new());

spawner.spawn(reader(rx, &signal)).unwrap();
spawner.spawn(writer(tx, &signal)).unwrap();
Expand Down

0 comments on commit 24510e3

Please sign in to comment.