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

Split wifi state for AP and STA #288

Merged
merged 3 commits into from
Oct 24, 2023
Merged
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
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);
bugadani marked this conversation as resolved.
Show resolved Hide resolved
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);
bugadani marked this conversation as resolved.
Show resolved Hide resolved
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
37 changes: 17 additions & 20 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 @@ -1238,26 +1237,28 @@ impl Wifi for WifiController<'_> {
}

fn connect(&mut self) -> Result<(), Self::Error> {
esp_wifi_result!(unsafe {
WIFI_STATE = -1;
esp_wifi_connect()
})
esp_wifi_result!(unsafe { esp_wifi_connect() })
}

fn disconnect(&mut self) -> Result<(), Self::Error> {
esp_wifi_result!(unsafe { esp_wifi_disconnect() })
}

fn is_started(&self) -> Result<bool, Self::Error> {
match crate::wifi::get_wifi_state() {
crate::wifi::WifiState::Invalid => Ok(false),
// We assume that wifi has been started in every other states
_ => Ok(true),
if matches!(
crate::wifi::get_sta_state(),
WifiState::StaStarted | WifiState::StaConnected | WifiState::StaDisconnected
) {
return Ok(true);
}
if matches!(crate::wifi::get_ap_state(), WifiState::ApStarted) {
return Ok(true);
}
Ok(false)
}

fn is_connected(&self) -> Result<bool, Self::Error> {
match crate::wifi::get_wifi_state() {
match crate::wifi::get_sta_state() {
crate::wifi::WifiState::StaConnected => Ok(true),
crate::wifi::WifiState::StaDisconnected => Err(WifiError::Disconnected),
//FIXME: Should any other enum value trigger an error instead of returning false?
Expand Down Expand Up @@ -1355,19 +1356,14 @@ pub(crate) mod embassy {

match self.get_wifi_mode() {
Ok(WifiMode::Sta) => {
if matches!(get_wifi_state(), WifiState::StaConnected) {
if matches!(get_sta_state(), WifiState::StaConnected) {
embassy_net_driver::LinkState::Up
} else {
embassy_net_driver::LinkState::Down
}
}
Ok(WifiMode::Ap) => {
if matches!(
get_wifi_state(),
WifiState::ApStart
| WifiState::ApStaConnected
| WifiState::ApStaDisconnected
) {
if matches!(get_ap_state(), WifiState::ApStarted) {
embassy_net_driver::LinkState::Up
} else {
embassy_net_driver::LinkState::Down
Expand Down Expand Up @@ -1463,7 +1459,8 @@ mod asynch {
embedded_svc::wifi::Wifi::stop(self)?;
WifiEventFuture::new(event).await;

unsafe { WIFI_STATE = -1 };
AP_STATE.store(WifiState::Invalid, Ordering::Relaxed);
STA_STATE.store(WifiState::Invalid, Ordering::Relaxed);

Ok(())
}
Expand Down
Loading
Loading