Skip to content

Commit

Permalink
Adjust for latest HAL (esp-rs#139)
Browse files Browse the repository at this point in the history
* Adjust for latest HAL

* ESP32-S3 adjustments needed after update

* Use ESP32-C2 HAL 0.5.1
  • Loading branch information
bjoernQ committed May 23, 2024
1 parent d94c01b commit b39e5fd
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 119 deletions.
3 changes: 3 additions & 0 deletions esp-wifi/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ rustflags = [

"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-Trom_functions.x",

# for now disable loop optimization
"-C", "target-feature=-loop",
]

[target.xtensa-esp32s2-none-elf]
Expand Down
37 changes: 16 additions & 21 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ license = "MIT OR Apache-2.0"
embedded-hal = "0.2.3"
nb = "1.0.0"
void = { version = "1.0.2", default-features = false }
esp32c3-hal = { version = "0.5.0", optional = true }
esp32c2-hal = { version = "0.3.0", optional = true }
esp32-hal = { version = "0.8.0", optional = true, features = [ "rt" ] }
esp32s3-hal = { version = "0.5.0", optional = true, features = [ "rt" ] }
esp32s2-hal = { version = "0.5.0", optional = true, features = [ "rt" ] }
esp32c3 = { version = "0.9.1", features = ["critical-section"], optional = true }
esp32c2 = { version = "0.6.1", features = ["critical-section"], optional = true }
riscv-rt = { version = "0.11.0", optional = true }
riscv = { version = "0.10.1", optional = true }
xtensa-lx-rt = { version = "0.14.0", optional = true }
xtensa-lx = { version = "0.7.0", optional = true }
esp32c3-hal = { version = "0.7.0", optional = true }
esp32c2-hal = { version = "0.5.1", optional = true }
esp32-hal = { version = "0.10.0", optional = true, features = [ "rt" ] }
esp32s3-hal = { version = "0.7.0", optional = true, features = [ "rt" ] }
esp32s2-hal = { version = "0.7.0", optional = true, features = [ "rt" ] }
esp32c3 = { version = "0.11.0", features = ["critical-section"], optional = true }
esp32c2 = { version = "0.8.0", features = ["critical-section"], optional = true }
smoltcp = { version = "0.9.1", default-features=false, features = ["proto-igmp", "proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] }
critical-section = "1.1.1"
atomic-polyfill = "1.0.1"
Expand Down Expand Up @@ -50,31 +46,30 @@ futures-util = { version = "0.3.17", default-features = false }

[target.xtensa-esp32-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32", "log" ] }
esp-backtrace = { version = "0.4.0", features = [ "esp32", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.5.0", features = [ "esp32", "panic-handler", "exception-handler", "print-uart" ] }

# change this for ESP32C3 / ESP32C2
[target.riscv32imc-unknown-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32c3", "log" ] }
esp-backtrace = { version = "0.4.0", features = [ "esp32c3", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.5.0", features = [ "esp32c3", "panic-handler", "exception-handler", "print-uart" ] }

[target.xtensa-esp32s3-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32s3", "log" ] }
esp-backtrace = { version = "0.4.0", features = [ "esp32s3", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.5.0", features = [ "esp32s3", "panic-handler", "exception-handler", "print-uart" ] }

[target.xtensa-esp32s2-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32s2", "log", "critical-section" ] }
esp-backtrace = { version = "0.4.0", features = [ "esp32s2", "panic-handler", "exception-handler", "print-uart" ] }
xtensa-atomic-emulation-trap = "0.3.0"
esp-backtrace = { version = "0.5.0", features = [ "esp32s2", "panic-handler", "exception-handler", "print-uart" ] }

[features]
default = [ "utils" ]

# chip features
esp32c3 = [ "riscv-target", "riscv", "riscv-rt", "esp32c3-hal", "dep:esp32c3", "esp-wifi-sys/esp32c3" ]
esp32c2 = [ "riscv-target", "riscv", "riscv-rt", "esp32c2-hal", "dep:esp32c2", "esp-wifi-sys/esp32c2" ]
esp32 = [ "esp32-hal", "xtensa-lx-rt/esp32", "xtensa-lx/esp32", "esp-wifi-sys/esp32" ]
esp32s3 = [ "esp32s3-hal", "xtensa-lx-rt/esp32s3", "xtensa-lx/esp32s3", "esp-wifi-sys/esp32s3" ]
esp32s2 = [ "esp32s2-hal", "xtensa-lx-rt/esp32s2", "xtensa-lx/esp32s2", "esp-wifi-sys/esp32s2" ]
esp32c3 = [ "riscv-target", "esp32c3-hal", "dep:esp32c3", "esp-wifi-sys/esp32c3" ]
esp32c2 = [ "riscv-target", "esp32c2-hal", "dep:esp32c2", "esp-wifi-sys/esp32c2" ]
esp32 = [ "esp32-hal", "esp-wifi-sys/esp32" ]
esp32s3 = [ "esp32s3-hal", "esp-wifi-sys/esp32s3" ]
esp32s2 = [ "esp32s2-hal", "esp-wifi-sys/esp32s2" ]

# async features
esp32c3-async = [ "esp32c3-hal/embassy", "esp32c3-hal/embassy-time-timg0", "async" ]
Expand Down
4 changes: 0 additions & 4 deletions esp-wifi/examples/access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ use hal::{peripherals::Peripherals, prelude::*, Rtc};
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
use smoltcp::iface::SocketStorage;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

#[entry]
fn main() -> ! {
Expand Down
4 changes: 0 additions & 4 deletions esp-wifi/examples/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ use hal::{
prelude::*,
Rng, Rtc, IO,
};
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3"))]
use xtensa_lx_rt::entry;

#[entry]
fn main() -> ! {
Expand Down
5 changes: 0 additions & 5 deletions esp-wifi/examples/coex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ use smoltcp::{iface::SocketStorage, wire::Ipv4Address};
#[cfg(feature = "esp32c3")]
use hal::system::SystemExt;

#[cfg(feature = "esp32c3")]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3"))]
use xtensa_lx_rt::entry;

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");

Expand Down
5 changes: 0 additions & 5 deletions esp-wifi/examples/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ use smoltcp::wire::Ipv4Address;
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");

Expand Down
5 changes: 0 additions & 5 deletions esp-wifi/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

macro_rules! singleton {
($val:expr) => {{
type T = impl Sized;
Expand Down
5 changes: 0 additions & 5 deletions esp-wifi/examples/embassy_dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");

Expand Down
5 changes: 0 additions & 5 deletions esp-wifi/examples/embassy_esp_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

#[embassy_executor::task]
async fn run(mut esp_now: EspNow) {
let mut ticker = Ticker::every(Duration::from_secs(5));
Expand Down
5 changes: 0 additions & 5 deletions esp-wifi/examples/esp_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ use hal::{peripherals::Peripherals, prelude::*, Rtc};
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

#[entry]
fn main() -> ! {
init_logger(log::LevelFilter::Info);
Expand Down
4 changes: 0 additions & 4 deletions esp-wifi/examples/static_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ use hal::{peripherals::Peripherals, prelude::*, Rtc};
#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use hal::system::SystemExt;

#[cfg(any(feature = "esp32c3", feature = "esp32c2"))]
use riscv_rt::entry;
use smoltcp::iface::SocketStorage;
#[cfg(any(feature = "esp32", feature = "esp32s3", feature = "esp32s2"))]
use xtensa_lx_rt::entry;

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
Expand Down
6 changes: 4 additions & 2 deletions esp-wifi/smoketest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ cargo "+esp" run --example embassy_esp_now --release --target xtensa-esp32-none-
cargo "+esp" run --example access_point --release --target xtensa-esp32-none-elf --features "esp32,embedded-svc,wifi"
cargo "+esp" run --example embassy_access_point --release --target xtensa-esp32-none-elf --features "esp32,esp32-async,embedded-svc,wifi,embassy-net"

set CARGO_PROFILE_RELEASE_OPT_LEVEL=3
set CARGO_PROFILE_RELEASE_OPT_LEVEL=1
set CARGO_PROFILE_RELEASE_LTO=off
echo.
echo Connect ESP32-S3
pause
cargo "+esp" run --example ble --release --target xtensa-esp32s3-none-elf --features "esp32s3,ble"
set CARGO_PROFILE_RELEASE_OPT_LEVEL=3
cargo "+esp" run --example dhcp --release --target xtensa-esp32s3-none-elf --features "esp32s3,embedded-svc,wifi"
cargo "+esp" run --example static_ip --release --target xtensa-esp32s3-none-elf --features "esp32s3,embedded-svc,wifi"
cargo "+esp" run --example embassy_dhcp --release --target xtensa-esp32s3-none-elf --features "esp32s3,esp32s3-async,embedded-svc,wifi,embassy-net"
Expand All @@ -62,12 +63,13 @@ cargo "+esp" run --example embassy_esp_now --release --target xtensa-esp32s2-non
cargo "+esp" run --example access_point --release --target xtensa-esp32s2-none-elf --features "esp32s2,embedded-svc,wifi"
cargo "+esp" run --example embassy_access_point --release --target xtensa-esp32s2-none-elf --features "esp32s2,esp32s2-async,embedded-svc,wifi,embassy-net"

set CARGO_PROFILE_RELEASE_OPT_LEVEL=3
set CARGO_PROFILE_RELEASE_OPT_LEVEL=2
set CARGO_PROFILE_RELEASE_LTO=false
echo.
echo Connect ESP32-C2 and modify the 'target.riscv32imc-unknown-none-elf.dev-dependencies' section
pause
cargo "+nightly" run --example ble --release --target riscv32imc-unknown-none-elf --features "esp32c2,ble"
set CARGO_PROFILE_RELEASE_OPT_LEVEL=3
cargo "+nightly" run --example dhcp --release --target riscv32imc-unknown-none-elf --features "esp32c2,embedded-svc,wifi"
cargo "+nightly" run --example static_ip --release --target riscv32imc-unknown-none-elf --features "esp32c2,embedded-svc,wifi"
cargo "+nightly" run --example embassy_dhcp --release --target riscv32imc-unknown-none-elf --features "esp32c2,esp32c2-async,embedded-svc,wifi,embassy-net"
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ unsafe extern "C" fn ble_npl_eventq_put(queue: *const ble_npl_eventq, event: *co
.as_mut()
.unwrap()
.queued = true;
EVENT_QUEUE.enqueue((*event).dummy as usize);
EVENT_QUEUE.enqueue((*event).dummy as usize).unwrap();
});
}

Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/os_adapter_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ pub(crate) unsafe extern "C" fn set_isr(n: i32, f: unsafe extern "C" fn(), arg:

pub(crate) unsafe extern "C" fn ints_on(mask: u32) {
log::trace!("chip_ints_on esp32 {:b}", mask);
xtensa_lx::interrupt::enable_mask(mask);
hal::xtensa_lx::interrupt::enable_mask(mask);
}

#[cfg(coex)]
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/os_adapter_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub(crate) fn create_ble_config() -> esp_bt_controller_config_t {
pub(crate) unsafe extern "C" fn interrupt_on(intr_num: i32) {
log::trace!("interrupt_on {}", intr_num);
unsafe {
xtensa_lx::interrupt::enable_mask(1 << 1);
esp32s3_hal::xtensa_lx::interrupt::enable_mask(1 << 1);
}
}

Expand Down
8 changes: 0 additions & 8 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use esp32s2_hal as hal;
#[cfg(feature = "esp32s3")]
use esp32s3_hal as hal;

use hal::*;

use fugit::MegahertzU32;
use hal::clock::Clocks;
use linked_list_allocator::Heap;
Expand Down Expand Up @@ -75,12 +73,6 @@ use timer::{get_systimer_count, TICKS_PER_SECOND};
#[cfg(all(feature = "embedded-svc", feature = "wifi"))]
pub mod wifi_interface;

#[cfg(feature = "esp32c3")]
use esp32c3_hal::interrupt;

#[cfg(feature = "esp32c2")]
use esp32c2_hal::interrupt;

pub fn current_millis() -> u64 {
get_systimer_count() / (TICKS_PER_SECOND / 1000)
}
Expand Down
12 changes: 6 additions & 6 deletions esp-wifi/src/preempt/preempt_xtensa.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::*;
use xtensa_lx_rt::exception::Context;
use crate::hal::trapframe::TrapFrame;

#[derive(Debug, Clone, Copy)]
pub struct TaskContext {
trap_frame: Context,
trap_frame: TrapFrame,
}

static mut CTX_TASKS: [TaskContext; MAX_TASK] = [TaskContext {
trap_frame: Context {
trap_frame: TrapFrame {
PC: 0,
PS: 0,
A0: 0,
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn task_create(task: extern "C" fn()) -> usize {
}
}

pub fn task_to_trap_frame(id: usize, trap_frame: &mut Context) {
pub fn task_to_trap_frame(id: usize, trap_frame: &mut TrapFrame) {
unsafe {
trap_frame.PC = CTX_TASKS[id].trap_frame.PC;
trap_frame.PS = CTX_TASKS[id].trap_frame.PS;
Expand Down Expand Up @@ -155,7 +155,7 @@ pub fn task_to_trap_frame(id: usize, trap_frame: &mut Context) {
}
}

pub fn trap_frame_to_task(id: usize, trap_frame: &Context) {
pub fn trap_frame_to_task(id: usize, trap_frame: &TrapFrame) {
unsafe {
CTX_TASKS[id].trap_frame.PC = trap_frame.PC;
CTX_TASKS[id].trap_frame.PS = trap_frame.PS;
Expand Down Expand Up @@ -220,7 +220,7 @@ pub fn next_task() {
}
}

pub fn task_switch(trap_frame: &mut Context) {
pub fn task_switch(trap_frame: &mut TrapFrame) {
unsafe {
if FIRST_SWITCH.load(Ordering::Relaxed) {
FIRST_SWITCH.store(false, Ordering::Relaxed);
Expand Down
4 changes: 3 additions & 1 deletion esp-wifi/src/timer_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use core::cell::RefCell;

use atomic_polyfill::{AtomicU64, Ordering};
use critical_section::Mutex;
use esp32_hal::xtensa_lx;
use esp32_hal::xtensa_lx_rt;
use esp32_hal::xtensa_lx_rt::exception::Context;
use esp32_hal::{
interrupt,
peripherals::{self, TIMG1},
prelude::*,
timer::{Timer, Timer0},
};
use log::trace;
use xtensa_lx_rt::exception::Context;

use crate::preempt::preempt::task_switch;
use esp32_hal::macros::interrupt;
Expand Down
13 changes: 8 additions & 5 deletions esp-wifi/src/timer_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
.unwrap();
}

esp32c2_hal::interrupt::enable(Interrupt::SW_INTR_3, hal::interrupt::Priority::Priority1)
.unwrap();
esp32c2_hal::interrupt::enable(
Interrupt::ETS_FROM_CPU_INTR3,
hal::interrupt::Priority::Priority1,
)
.unwrap();

unsafe {
riscv::interrupt::enable();
esp32c2_hal::riscv::interrupt::enable();
}

while unsafe { crate::preempt::FIRST_SWITCH.load(core::sync::atomic::Ordering::Relaxed) } {}
Expand Down Expand Up @@ -155,9 +158,9 @@ fn SYSTIMER_TARGET0(trap_frame: &mut TrapFrame) {
}

#[interrupt]
fn SW_INTR_3(trap_frame: &mut TrapFrame) {
fn ETS_FROM_CPU_INTR3(trap_frame: &mut TrapFrame) {
unsafe {
// clear SW_INTR_3
// clear ETS_FROM_CPU_INTR3
(&*pac::SYSTEM::PTR)
.cpu_intr_from_cpu_3
.modify(|_, w| w.cpu_intr_from_cpu_3().clear_bit());
Expand Down
15 changes: 9 additions & 6 deletions esp-wifi/src/timer_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::cell::RefCell;
use critical_section::Mutex;
use esp32c3 as pac;
use esp32c3_hal as hal;
use esp32c3_hal::interrupt::TrapFrame;
use esp32c3_hal::prelude::*;
use esp32c3_hal::trapframe::TrapFrame;
use hal::peripherals::Interrupt;
use hal::systimer::{Alarm, Periodic, Target};

Expand Down Expand Up @@ -54,11 +54,14 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
.unwrap();
}

esp32c3_hal::interrupt::enable(Interrupt::SW_INTR_3, hal::interrupt::Priority::Priority1)
.unwrap();
esp32c3_hal::interrupt::enable(
Interrupt::FROM_CPU_INTR3,
hal::interrupt::Priority::Priority1,
)
.unwrap();

unsafe {
riscv::interrupt::enable();
esp32c3_hal::riscv::interrupt::enable();
}

while unsafe { crate::preempt::FIRST_SWITCH.load(core::sync::atomic::Ordering::Relaxed) } {}
Expand Down Expand Up @@ -173,9 +176,9 @@ fn SYSTIMER_TARGET0(trap_frame: &mut TrapFrame) {
}

#[interrupt]
fn SW_INTR_3(trap_frame: &mut TrapFrame) {
fn FROM_CPU_INTR3(trap_frame: &mut TrapFrame) {
unsafe {
// clear SW_INTR_3
// clear FROM_CPU_INTR3
(&*pac::SYSTEM::PTR)
.cpu_intr_from_cpu_3
.modify(|_, w| w.cpu_intr_from_cpu_3().clear_bit());
Expand Down
Loading

0 comments on commit b39e5fd

Please sign in to comment.