diff --git a/Cargo.toml b/Cargo.toml index 0675979d..88ccff2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ esp32s2-hal = { version = "0.12.0" } smoltcp = { version = "0.10.0", default-features=false, features = ["proto-igmp", "proto-ipv4", "proto-dns", "socket-tcp", "socket-icmp", "socket-udp", "socket-dns", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] } critical-section = "1.1.1" atomic-polyfill = "1.0.2" +atomic_enum = "0.2.0" # uses core atomic types which should be fine as long as only load and store is used on them log = "0.4.18" embedded-svc = { version = "0.25.1", default-features = false, features = [] } enumset = { version = "1", default-features = false } diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index dd2359af..c089a46a 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -14,7 +14,6 @@ esp32s3-hal = { workspace = true, optional = true } esp32s2-hal = { workspace = true, optional = true } smoltcp = { workspace = true, optional = true } critical-section.workspace = true -atomic-polyfill.workspace = true log = { workspace = true, optional = true } embedded-svc = { workspace = true, optional = true } enumset = { workspace = true, optional = true } @@ -31,6 +30,8 @@ embassy-net-driver = { workspace = true, optional = true } toml-cfg.workspace = true libm.workspace = true cfg-if.workspace = true +atomic-polyfill = { workspace = true } +atomic_enum = { workspace = true } [features] default = [ "utils", "log" ] diff --git a/esp-wifi/src/common_adapter/common_adapter_esp32.rs b/esp-wifi/src/common_adapter/common_adapter_esp32.rs index cd50ae58..7f6b2207 100644 --- a/esp-wifi/src/common_adapter/common_adapter_esp32.rs +++ b/esp-wifi/src/common_adapter/common_adapter_esp32.rs @@ -3,9 +3,11 @@ use crate::binary::include::*; use crate::common_adapter::RADIO_CLOCKS; use crate::hal::system::RadioClockController; use crate::hal::system::RadioPeripherals; -use atomic_polyfill::AtomicU32; use esp32_hal::prelude::ram; +use atomic_polyfill::AtomicU32; +use core::sync::atomic::Ordering; + const SOC_PHY_DIG_REGS_MEM_SIZE: usize = 21 * 4; static mut SOC_PHY_DIG_REGS_MEM: [u8; SOC_PHY_DIG_REGS_MEM_SIZE] = [0u8; SOC_PHY_DIG_REGS_MEM_SIZE]; @@ -36,7 +38,7 @@ pub(crate) fn phy_mem_init() { } pub(crate) unsafe fn phy_enable() { - let count = PHY_ACCESS_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { // #if CONFIG_IDF_TARGET_ESP32 @@ -77,7 +79,7 @@ pub(crate) unsafe fn phy_enable() { #[allow(unused)] pub(crate) unsafe fn phy_disable() { - let count = PHY_ACCESS_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { phy_digital_regs_store(); @@ -116,7 +118,7 @@ fn phy_digital_regs_store() { pub(crate) unsafe fn phy_enable_clock() { trace!("phy_enable_clock"); - let count = PHY_CLOCK_ENABLE_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_CLOCK_ENABLE_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { unwrap!(RADIO_CLOCKS.as_mut()).enable(RadioPeripherals::Phy); @@ -128,7 +130,7 @@ pub(crate) unsafe fn phy_enable_clock() { pub(crate) unsafe fn phy_disable_clock() { trace!("phy_disable_clock"); - let count = PHY_CLOCK_ENABLE_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_CLOCK_ENABLE_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { unwrap!(RADIO_CLOCKS.as_mut()).disable(RadioPeripherals::Phy); diff --git a/esp-wifi/src/common_adapter/common_adapter_esp32c2.rs b/esp-wifi/src/common_adapter/common_adapter_esp32c2.rs index 95f77c2f..efca435b 100644 --- a/esp-wifi/src/common_adapter/common_adapter_esp32c2.rs +++ b/esp-wifi/src/common_adapter/common_adapter_esp32c2.rs @@ -4,7 +4,9 @@ use crate::common_adapter::RADIO_CLOCKS; use crate::compat::common::str_from_c; use crate::hal::system::RadioClockController; use crate::hal::system::RadioPeripherals; + use atomic_polyfill::AtomicU32; +use core::sync::atomic::Ordering; const SOC_PHY_DIG_REGS_MEM_SIZE: usize = 21 * 4; @@ -26,7 +28,7 @@ pub(crate) fn phy_mem_init() { } pub(crate) unsafe fn phy_enable() { - let count = PHY_ACCESS_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { phy_enable_clock(); @@ -77,7 +79,7 @@ pub(crate) unsafe fn phy_enable() { #[allow(unused)] pub(crate) unsafe fn phy_disable() { - let count = PHY_ACCESS_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { phy_digital_regs_store(); diff --git a/esp-wifi/src/common_adapter/common_adapter_esp32c3.rs b/esp-wifi/src/common_adapter/common_adapter_esp32c3.rs index 2c8cb7e7..256c0ae5 100644 --- a/esp-wifi/src/common_adapter/common_adapter_esp32c3.rs +++ b/esp-wifi/src/common_adapter/common_adapter_esp32c3.rs @@ -4,7 +4,9 @@ use crate::common_adapter::RADIO_CLOCKS; use crate::compat::common::str_from_c; use crate::hal::system::RadioClockController; use crate::hal::system::RadioPeripherals; + use atomic_polyfill::AtomicU32; +use core::sync::atomic::Ordering; const SOC_PHY_DIG_REGS_MEM_SIZE: usize = 21 * 4; @@ -61,7 +63,7 @@ pub(crate) fn phy_mem_init() { } pub(crate) unsafe fn phy_enable() { - let count = PHY_ACCESS_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { phy_enable_clock(); @@ -112,7 +114,7 @@ pub(crate) unsafe fn phy_enable() { #[allow(unused)] pub(crate) unsafe fn phy_disable() { - let count = PHY_ACCESS_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { phy_digital_regs_store(); diff --git a/esp-wifi/src/common_adapter/common_adapter_esp32c6.rs b/esp-wifi/src/common_adapter/common_adapter_esp32c6.rs index 18878436..b0c05f91 100644 --- a/esp-wifi/src/common_adapter/common_adapter_esp32c6.rs +++ b/esp-wifi/src/common_adapter/common_adapter_esp32c6.rs @@ -4,7 +4,9 @@ use crate::common_adapter::RADIO_CLOCKS; use crate::compat::common::str_from_c; use crate::hal::system::RadioClockController; use crate::hal::system::RadioPeripherals; + use atomic_polyfill::AtomicU32; +use core::sync::atomic::Ordering; const SOC_PHY_DIG_REGS_MEM_SIZE: usize = 21 * 4; @@ -26,7 +28,7 @@ pub(crate) fn phy_mem_init() { } pub(crate) unsafe fn phy_enable() { - let count = PHY_ACCESS_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { phy_enable_clock(); @@ -76,7 +78,7 @@ pub(crate) unsafe fn phy_enable() { #[allow(unused)] pub(crate) unsafe fn phy_disable() { - let count = PHY_ACCESS_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { phy_digital_regs_store(); diff --git a/esp-wifi/src/common_adapter/common_adapter_esp32s2.rs b/esp-wifi/src/common_adapter/common_adapter_esp32s2.rs index f4faa58b..cd2185ed 100644 --- a/esp-wifi/src/common_adapter/common_adapter_esp32s2.rs +++ b/esp-wifi/src/common_adapter/common_adapter_esp32s2.rs @@ -3,9 +3,11 @@ use crate::binary::include::*; use crate::common_adapter::RADIO_CLOCKS; use crate::hal::system::RadioClockController; use crate::hal::system::RadioPeripherals; -use atomic_polyfill::AtomicU32; use esp32s2_hal::prelude::ram; +use atomic_polyfill::AtomicU32; +use core::sync::atomic::Ordering; + const SOC_PHY_DIG_REGS_MEM_SIZE: usize = 21 * 4; static mut SOC_PHY_DIG_REGS_MEM: [u8; SOC_PHY_DIG_REGS_MEM_SIZE] = [0u8; SOC_PHY_DIG_REGS_MEM_SIZE]; @@ -59,7 +61,7 @@ pub(crate) fn phy_mem_init() { } pub(crate) unsafe fn phy_enable() { - let count = PHY_ACCESS_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { phy_enable_clock(); @@ -96,7 +98,7 @@ pub(crate) unsafe fn phy_enable() { #[allow(unused)] pub(crate) unsafe fn phy_disable() { - let count = PHY_ACCESS_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { phy_digital_regs_store(); @@ -130,7 +132,7 @@ fn phy_digital_regs_store() { } pub(crate) unsafe fn phy_enable_clock() { - let count = PHY_CLOCK_ENABLE_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_CLOCK_ENABLE_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { unwrap!(RADIO_CLOCKS.as_mut()).enable(RadioPeripherals::Phy); @@ -142,7 +144,7 @@ pub(crate) unsafe fn phy_enable_clock() { #[allow(unused)] pub(crate) unsafe fn phy_disable_clock() { - let count = PHY_CLOCK_ENABLE_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_CLOCK_ENABLE_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { unwrap!(RADIO_CLOCKS.as_mut()).disable(RadioPeripherals::Phy); diff --git a/esp-wifi/src/common_adapter/common_adapter_esp32s3.rs b/esp-wifi/src/common_adapter/common_adapter_esp32s3.rs index 75085c10..49458dbb 100644 --- a/esp-wifi/src/common_adapter/common_adapter_esp32s3.rs +++ b/esp-wifi/src/common_adapter/common_adapter_esp32s3.rs @@ -3,7 +3,9 @@ use crate::binary::include::*; use crate::common_adapter::RADIO_CLOCKS; use crate::hal::system::RadioClockController; use crate::hal::system::RadioPeripherals; + use atomic_polyfill::AtomicU32; +use core::sync::atomic::Ordering; const SOC_PHY_DIG_REGS_MEM_SIZE: usize = 21 * 4; @@ -60,7 +62,7 @@ pub(crate) fn enable_wifi_power_domain() { } pub(crate) unsafe fn phy_enable() { - let count = PHY_ACCESS_REF.fetch_add(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_add(1, Ordering::SeqCst); if count == 0 { critical_section::with(|_| { phy_enable_clock(); @@ -108,7 +110,7 @@ pub(crate) unsafe fn phy_enable() { #[allow(unused)] pub(crate) unsafe fn phy_disable() { - let count = PHY_ACCESS_REF.fetch_sub(1, atomic_polyfill::Ordering::SeqCst); + let count = PHY_ACCESS_REF.fetch_sub(1, Ordering::SeqCst); if count == 1 { critical_section::with(|_| { phy_digital_regs_store(); diff --git a/esp-wifi/src/esp_now/mod.rs b/esp-wifi/src/esp_now/mod.rs index e6993057..2a6b45f2 100644 --- a/esp-wifi/src/esp_now/mod.rs +++ b/esp-wifi/src/esp_now/mod.rs @@ -7,9 +7,11 @@ //! For more information see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html use core::marker::PhantomData; +use core::sync::atomic::Ordering; use core::{cell::RefCell, fmt::Debug}; -use atomic_polyfill::{AtomicBool, AtomicU8, Ordering}; +use atomic_polyfill::{AtomicBool, AtomicU8}; + use critical_section::Mutex; use crate::compat::queue::SimpleQueue; diff --git a/esp-wifi/src/preempt/mod.rs b/esp-wifi/src/preempt/mod.rs index 6f7f03f6..4c3dfdeb 100644 --- a/esp-wifi/src/preempt/mod.rs +++ b/esp-wifi/src/preempt/mod.rs @@ -1,4 +1,5 @@ -use atomic_polyfill::*; +use atomic_polyfill::AtomicBool; +use core::sync::atomic::Ordering; pub static mut FIRST_SWITCH: AtomicBool = AtomicBool::new(true); diff --git a/esp-wifi/src/timer/riscv.rs b/esp-wifi/src/timer/riscv.rs index c278fa0a..e194e802 100644 --- a/esp-wifi/src/timer/riscv.rs +++ b/esp-wifi/src/timer/riscv.rs @@ -1,4 +1,5 @@ use core::cell::RefCell; +use core::sync::atomic::Ordering; use critical_section::Mutex; @@ -50,7 +51,7 @@ pub fn setup_multitasking() { riscv::interrupt::enable(); } - while unsafe { crate::preempt::FIRST_SWITCH.load(core::sync::atomic::Ordering::Relaxed) } {} + while unsafe { crate::preempt::FIRST_SWITCH.load(Ordering::Relaxed) } {} } #[interrupt] diff --git a/esp-wifi/src/timer/xtensa.rs b/esp-wifi/src/timer/xtensa.rs index b3cbf9a5..5ac420b9 100644 --- a/esp-wifi/src/timer/xtensa.rs +++ b/esp-wifi/src/timer/xtensa.rs @@ -1,4 +1,6 @@ +use atomic_polyfill::AtomicU32; use core::cell::RefCell; +use core::sync::atomic::Ordering; use critical_section::Mutex; @@ -14,7 +16,6 @@ use crate::{ }, preempt::preempt::task_switch, }; -use atomic_polyfill::{AtomicU32, Ordering}; pub type TimeBase = Timer>; @@ -72,7 +73,7 @@ pub fn setup_multitasking() { ); } - while unsafe { crate::preempt::FIRST_SWITCH.load(core::sync::atomic::Ordering::Relaxed) } {} + while unsafe { crate::preempt::FIRST_SWITCH.load(Ordering::Relaxed) } {} } #[allow(non_snake_case)] diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index c8e57eef..4eee8690 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -1,6 +1,8 @@ #[doc(hidden)] pub mod os_adapter; +use atomic_polyfill::AtomicUsize; +use core::sync::atomic::Ordering; use core::{cell::RefCell, mem::MaybeUninit}; use crate::common_adapter::*; @@ -35,9 +37,6 @@ use esp_wifi_sys::include::wifi_mode_t_WIFI_MODE_NULL; use num_derive::FromPrimitive; use num_traits::FromPrimitive; -use atomic_polyfill::AtomicUsize; -use core::sync::atomic::Ordering; - #[doc(hidden)] pub use os_adapter::*; use smoltcp::phy::{Device, DeviceCapabilities, RxToken, TxToken}; @@ -1461,8 +1460,8 @@ mod asynch { WifiEventFuture::new(event).await; unsafe { - AP_STATE = WifiState::Invalid; - STA_STATE = WifiState::Invalid; + AP_STATE.store(WifiState::Invalid, Ordering::Relaxed); + STA_STATE.store(WifiState::Invalid, Ordering::Relaxed); } Ok(()) diff --git a/esp-wifi/src/wifi/os_adapter.rs b/esp-wifi/src/wifi/os_adapter.rs index 5edaffa6..f24a592a 100644 --- a/esp-wifi/src/wifi/os_adapter.rs +++ b/esp-wifi/src/wifi/os_adapter.rs @@ -7,7 +7,9 @@ pub(crate) mod os_adapter_chip_specific; use core::cell::RefCell; +use core::sync::atomic::Ordering; +use atomic_enum::atomic_enum; use critical_section::Mutex; use enumset::EnumSet; @@ -33,18 +35,20 @@ use crate::compat::common::syslog; use super::WifiEvent; -pub(crate) static mut STA_STATE: WifiState = WifiState::Invalid; -pub(crate) static mut AP_STATE: WifiState = WifiState::Invalid; +pub(crate) static STA_STATE: AtomicWifiState = AtomicWifiState::new(WifiState::Invalid); +pub(crate) static AP_STATE: AtomicWifiState = AtomicWifiState::new(WifiState::Invalid); // useful for waiting for events - clear and wait for the event bit to be set again pub(crate) static WIFI_EVENTS: Mutex>> = Mutex::new(RefCell::new(enumset::enum_set!())); pub fn is_connected() -> bool { - unsafe { STA_STATE == WifiState::StaConnected } + unsafe { get_sta_state() == WifiState::StaConnected } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[atomic_enum] +#[derive(PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum WifiState { StaStarted, StaConnected, @@ -72,11 +76,11 @@ impl From for WifiState { } pub fn get_ap_state() -> WifiState { - unsafe { AP_STATE } + AP_STATE.load(Ordering::Relaxed) } pub fn get_sta_state() -> WifiState { - unsafe { STA_STATE } + STA_STATE.load(Ordering::Relaxed) } pub fn get_wifi_state() -> WifiState { @@ -939,8 +943,10 @@ pub unsafe extern "C" fn event_post( WifiEvent::StaConnected | WifiEvent::StaDisconnected | WifiEvent::StaStart - | WifiEvent::StaStop => STA_STATE = WifiState::from(event), - WifiEvent::ApStart | WifiEvent::ApStop => AP_STATE = WifiState::from(event), + | WifiEvent::StaStop => STA_STATE.store(WifiState::from(event), Ordering::Relaxed), + WifiEvent::ApStart | WifiEvent::ApStop => { + AP_STATE.store(WifiState::from(event), Ordering::Relaxed) + } other => debug!("Unhandled event: {:?}", other), }