From ba8daafb0b0a89ad5f32067171de60b718fa75fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 10 Oct 2024 10:24:50 +0200 Subject: [PATCH] Require float-save-restore in esp-wifi (#2322) * Add failing test case * Make sure task contexts are properly initialised * Require float-save-restore for esp-wifi --- esp-wifi/Cargo.toml | 5 +++++ esp-wifi/src/preempt/mod.rs | 4 ++-- esp-wifi/src/preempt/preempt_riscv.rs | 11 +++++++++++ esp-wifi/src/preempt/preempt_xtensa.rs | 13 ++++++++++++- hil-test/Cargo.toml | 4 ++++ hil-test/tests/esp_wifi_floats.rs | 1 - 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index 12ce27517..60d9d1254 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -44,6 +44,8 @@ portable_atomic_enum = { version = "0.3.1", features = ["portable-atomic"] } bt-hci = { version = "0.1.1", optional = true } esp-config = { version = "0.1.0", path = "../esp-config" } +xtensa-lx-rt = { version = "0.17.1", path = "../xtensa-lx-rt", optional = true } + [build-dependencies] esp-build = { version = "0.1.0", path = "../esp-build" } esp-config = { version = "0.1.0", path = "../esp-config", features = ["build"] } @@ -83,16 +85,19 @@ esp32h2 = [ esp32 = [ "esp-hal/esp32", "esp-wifi-sys/esp32", + "xtensa-lx-rt/float-save-restore", ] # Target the ESP32-S2. esp32s2 = [ "esp-hal/esp32s2", "esp-wifi-sys/esp32s2", + "xtensa-lx-rt/float-save-restore", ] # Target the ESP32-S3. esp32s3 = [ "esp-hal/esp32s3", "esp-wifi-sys/esp32s3", + "xtensa-lx-rt/float-save-restore", ] ## Enable Async support diff --git a/esp-wifi/src/preempt/mod.rs b/esp-wifi/src/preempt/mod.rs index be24112b8..e8d4fe16e 100644 --- a/esp-wifi/src/preempt/mod.rs +++ b/esp-wifi/src/preempt/mod.rs @@ -22,7 +22,7 @@ pub fn allocate_main_task() -> *mut Context { } let ptr = malloc(size_of::() as u32) as *mut Context; - core::ptr::write_bytes(ptr, 0, 1); + core::ptr::write(ptr, Context::new()); (*ptr).next = ptr; *ctx_now = ptr; ptr @@ -37,7 +37,7 @@ fn allocate_task() -> *mut Context { } let ptr = malloc(size_of::() as u32) as *mut Context; - core::ptr::write_bytes(ptr, 0, 1); + core::ptr::write(ptr, Context::new()); (*ptr).next = (**ctx_now).next; (**ctx_now).next = ptr; ptr diff --git a/esp-wifi/src/preempt/preempt_riscv.rs b/esp-wifi/src/preempt/preempt_riscv.rs index a729fb0bf..cfed68cf9 100644 --- a/esp-wifi/src/preempt/preempt_riscv.rs +++ b/esp-wifi/src/preempt/preempt_riscv.rs @@ -11,6 +11,17 @@ pub struct Context { pub allocated_stack: *const u8, } +impl Context { + pub fn new() -> Self { + Context { + trap_frame: TrapFrame::default(), + thread_semaphore: 0, + next: core::ptr::null_mut(), + allocated_stack: core::ptr::null(), + } + } +} + pub fn task_create( task: extern "C" fn(*mut c_types::c_void), param: *mut c_types::c_void, diff --git a/esp-wifi/src/preempt/preempt_xtensa.rs b/esp-wifi/src/preempt/preempt_xtensa.rs index eeb43f59b..1d5606b7c 100644 --- a/esp-wifi/src/preempt/preempt_xtensa.rs +++ b/esp-wifi/src/preempt/preempt_xtensa.rs @@ -11,6 +11,17 @@ pub struct Context { pub allocated_stack: *const u8, } +impl Context { + pub fn new() -> Self { + Context { + trap_frame: TrapFrame::new(), + thread_semaphore: 0, + next: core::ptr::null_mut(), + allocated_stack: core::ptr::null(), + } + } +} + pub fn task_create( task: extern "C" fn(*mut c_types::c_void), param: *mut c_types::c_void, @@ -28,7 +39,7 @@ pub fn task_create( // stack must be aligned by 16 let task_stack_ptr = stack as usize + task_stack_size; - let stack_ptr = task_stack_ptr - (task_stack_ptr % 0x10); + let stack_ptr = task_stack_ptr - (task_stack_ptr % 16); (*ctx).trap_frame.A1 = stack_ptr as u32; (*ctx).trap_frame.PS = 0x00040000 | (1 & 3) << 16; // For windowed ABI set WOE and CALLINC (pretend task was 'call4'd). diff --git a/hil-test/Cargo.toml b/hil-test/Cargo.toml index a256e43b6..fd5d4f81c 100644 --- a/hil-test/Cargo.toml +++ b/hil-test/Cargo.toml @@ -249,18 +249,21 @@ esp32c3 = [ "esp-hal/esp32c3", "esp-hal-embassy/esp32c3", "esp-wifi?/esp32c3", + "esp-wifi?/phy-enable-usb", ] esp32c6 = [ "esp-backtrace/esp32c6", "esp-hal/esp32c6", "esp-hal-embassy/esp32c6", "esp-wifi?/esp32c6", + "esp-wifi?/phy-enable-usb", ] esp32h2 = [ "esp-backtrace/esp32h2", "esp-hal/esp32h2", "esp-hal-embassy/esp32h2", "esp-wifi?/esp32h2", + "esp-wifi?/phy-enable-usb", ] esp32s2 = [ "embedded-test/xtensa-semihosting", @@ -275,6 +278,7 @@ esp32s3 = [ "esp-hal/esp32s3", "esp-hal-embassy/esp32s3", "esp-wifi?/esp32s3", + "esp-wifi?/phy-enable-usb", ] # Async & Embassy: embassy = [ diff --git a/hil-test/tests/esp_wifi_floats.rs b/hil-test/tests/esp_wifi_floats.rs index f509f3719..0b9a82c8a 100644 --- a/hil-test/tests/esp_wifi_floats.rs +++ b/hil-test/tests/esp_wifi_floats.rs @@ -2,7 +2,6 @@ //% CHIPS: esp32 esp32s2 esp32s3 //% FEATURES: esp-wifi esp-alloc -//% FEATURES: esp-wifi esp-alloc xtensa-lx-rt/float-save-restore #![no_std] #![no_main]