Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embassy: enable usage of esp-wifi with riot-rs-threads #300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/riot-rs-embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ wifi = []
wifi-cyw43 = ["riot-rs-rp/wifi-cyw43", "net", "wifi"]
wifi-esp = ["riot-rs-esp/wifi-esp", "net", "wifi"]

threading = ["dep:riot-rs-threads"]
threading = ["dep:riot-rs-threads", "riot-rs-esp/threading"]
override-network-config = []
override-usb-config = []

Expand Down
6 changes: 6 additions & 0 deletions src/riot-rs-esp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ once_cell = { workspace = true }
paste = { workspace = true }
riot-rs-debug = { workspace = true }
riot-rs-embassy-common = { workspace = true }
riot-rs-macros = { path = "../riot-rs-macros", optional = true }
riot-rs-threads = { path = "../riot-rs-threads", optional = true }
riot-rs-utils = { workspace = true }

[target.'cfg(context = "cortex-m")'.dependencies]
Expand Down Expand Up @@ -91,3 +93,7 @@ wifi-esp = ["dep:esp-wifi", "dep:embassy-time", "wifi"]
executor-interrupt = ["embassy-executor/executor-interrupt"]
## Enables the single thread-mode executor.
executor-single-thread = ["esp-hal-embassy/executors"]

## Enables threading support.
# Required for the usage of esp-wifi together with `riot-rs-threads`.
threading = ["dep:riot-rs-macros", "dep:riot-rs-threads"]
1 change: 1 addition & 0 deletions src/riot-rs-esp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![feature(impl_trait_in_assoc_type)]
#![feature(trait_alias)]
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use once_cell::sync::OnceCell;

Expand Down
89 changes: 89 additions & 0 deletions src/riot-rs-esp/src/wifi/esp_wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub type NetworkDevice = WifiDevice<'static, WifiStaDevice>;
// sure.
pub static WIFI_INIT: OnceCell<EspWifiInitialization> = OnceCell::new();

#[cfg(feature = "threading")]
pub static WIFI_THREAD_ID: OnceCell<riot_rs_threads::ThreadId> = OnceCell::new();

pub fn init(peripherals: &mut crate::OptionalPeripherals, spawner: Spawner) -> NetworkDevice {
let wifi = peripherals.WIFI.take().unwrap();
let init = WIFI_INIT.get().unwrap();
Expand All @@ -31,6 +34,18 @@ pub fn init(peripherals: &mut crate::OptionalPeripherals, spawner: Spawner) -> N

#[embassy_executor::task]
async fn connection(mut controller: WifiController<'static>) {
#[cfg(feature = "threading")]
{
let thread_id = WIFI_THREAD_ID.get().unwrap();

// Disable esp-wifi interrupts that are initialized in esp-wifi
// until the `esp_wifi_thread` runs.
interrupt::disable(esp_hal::Cpu::ProCpu, Interrupt::FROM_CPU_INTR3);
interrupt::disable(esp_hal::Cpu::ProCpu, Interrupt::SYSTIMER_TARGET0);
// Wake-up the `esp_wifi_thread`.
riot_rs_threads::thread_flags::set(*thread_id, 0b1);
}

debug!("start connection task");

#[cfg(not(feature = "defmt"))]
Expand Down Expand Up @@ -68,3 +83,77 @@ async fn connection(mut controller: WifiController<'static>) {
}
}
}

#[cfg(feature = "threading")]
mod wifi_thread {
use esp_hal::{
interrupt,
peripherals::{Interrupt, SYSTIMER},
};

#[cfg(context = "esp32c6")]
use esp_hal::peripherals::INTPRI as SystemPeripheral;
#[cfg(context = "esp32c3")]
use esp_hal::peripherals::SYSTEM as SystemPeripheral;

use super::*;

// Handle the systimer alarm 0 interrupt, configured in esp-wifi.
extern "C" fn systimer_target0_() {
// SAFETY: constant pointer to register block is valid.
let systimer = unsafe { &*SYSTIMER::PTR };
// Clear interrupt.
systimer.int_clr().write(|w| w.target0().clear_bit_by_one());
// Wake up `esp_wifi_thread`.
if !riot_rs_threads::wakeup(*WIFI_THREAD_ID.get().unwrap()) {
// We're already in the context of `esp_wifi_thread`, so yield
// directly.
yield_to_esp_wifi_scheduler();
}
}

fn yield_to_esp_wifi_scheduler() {
// SAFETY: constant pointer to register block is valid.
let ptr = unsafe { &*SystemPeripheral::PTR };
// CPU Interrupt 3 triggers the scheduler in `esp-wifi`.
ptr.cpu_intr_from_cpu_3()
.modify(|_, w| w.cpu_intr_from_cpu_3().set_bit());
}

// Thread that runs the esp-wifi scheduler.
///
/// Because it runs at highest priority, it can't be preempted by any riot-rs threads and therefore
/// the two schedulers won't interleave.
#[riot_rs_macros::thread(autostart, priority = riot_rs_threads::SCHED_PRIO_LEVELS as u8 - 1)]
fn esp_wifi_thread() {
WIFI_THREAD_ID
.set(riot_rs_threads::current_pid().unwrap())
.unwrap();

// Wait until `embassy` is initialized.
riot_rs_threads::thread_flags::wait_one(0b1);

// Bind the periodic systimer that is configured in esp-wifi to our own handler.
//
// SAFETY: This overwrites the existing handler from esp-wifi, which is okay because
// we want to handle the interrupt differently. It needs to be done after the esp-hal
// initialization finished.
unsafe {
interrupt::bind_interrupt(
Interrupt::SYSTIMER_TARGET0,
core::mem::transmute(systimer_target0_ as *const ()),
);
}
interrupt::enable(Interrupt::SYSTIMER_TARGET0, interrupt::Priority::Priority2).unwrap();

loop {
interrupt::enable(Interrupt::FROM_CPU_INTR3, interrupt::Priority::Priority1).unwrap();
// Yield to the esp-wifi scheduler tasks, so that they get a chance to run.
yield_to_esp_wifi_scheduler();
// Disable esp-wifi scheduler so that it won't interleave with the riot-rs-threads scheduler.
interrupt::disable(esp_hal::Cpu::ProCpu, Interrupt::FROM_CPU_INTR3);
// Sleep until the systimer alarm 0 interrupts again.
riot_rs_threads::sleep()
}
}
}
Loading