Skip to content

Commit

Permalink
Use atomic enum to store state
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 18, 2023
1 parent 4b2aa1c commit 9272184
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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" ]
Expand Down
12 changes: 7 additions & 5 deletions esp-wifi/src/common_adapter/common_adapter_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions esp-wifi/src/common_adapter/common_adapter_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions esp-wifi/src/common_adapter/common_adapter_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions esp-wifi/src/common_adapter/common_adapter_esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 7 additions & 5 deletions esp-wifi/src/common_adapter/common_adapter_esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions esp-wifi/src/common_adapter/common_adapter_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion esp-wifi/src/preempt/mod.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
3 changes: 2 additions & 1 deletion esp-wifi/src/timer/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::cell::RefCell;
use core::sync::atomic::Ordering;

use critical_section::Mutex;

Expand Down Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions esp-wifi/src/timer/xtensa.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use atomic_polyfill::AtomicU32;
use core::cell::RefCell;
use core::sync::atomic::Ordering;

use critical_section::Mutex;

Expand All @@ -14,7 +16,6 @@ use crate::{
},
preempt::preempt::task_switch,
};
use atomic_polyfill::{AtomicU32, Ordering};

pub type TimeBase = Timer<Timer0<TIMG1>>;

Expand Down Expand Up @@ -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)]
Expand Down
9 changes: 4 additions & 5 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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(())
Expand Down
22 changes: 14 additions & 8 deletions esp-wifi/src/wifi/os_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<RefCell<EnumSet<WifiEvent>>> =
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,
Expand Down Expand Up @@ -72,11 +76,11 @@ impl From<WifiEvent> 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 {
Expand Down Expand Up @@ -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),
}

Expand Down

0 comments on commit 9272184

Please sign in to comment.