Skip to content

Commit

Permalink
make ps a runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Nov 5, 2024
1 parent 727ec80 commit 6af0c4a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 76 deletions.
5 changes: 1 addition & 4 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,4 @@ features = [
"coex",
"esp-hal/default",
]
default-target = "riscv32imc-unknown-none-elf"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(modem_powersaving, values("max", "min", "none"))'] }
default-target = "riscv32imc-unknown-none-elf"
2 changes: 0 additions & 2 deletions esp-wifi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ fn main() -> Result<(), Box<dyn Error>> {
("scan_method", Value::UnsignedInteger(0), "0 = WIFI_FAST_SCAN, 1 = WIFI_ALL_CHANNEL_SCAN, defaults to 0"),
("dump_packets", Value::Bool(false), "Dump packets via an info log statement"),
("phy_enable_usb", Value::Bool(true), "Keeps USB running when using WiFi. This allows debugging and log messages via USB Serial JTAG. Turn off for best WiFi performance."),
("modem_powersaving", Value::String("none".to_owned()), "Modem power saving. Possible values: \"max\", \"min\", \"none\". Defaults to \"none\" or \"min\" when coex is enabled."),
("strchr_available", Value::Bool(false), "Enable to use a custom or third party strchr implementation"),
],
true
);
Expand Down
47 changes: 47 additions & 0 deletions esp-wifi/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use esp_wifi_sys::include;

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
/// Tunable parameters for the WiFi driver
#[allow(unused)] // currently there are no ble tunables
pub(crate) struct EspWifiConfig {
pub(crate) rx_queue_size: usize,
pub(crate) tx_queue_size: usize,
pub(crate) static_rx_buf_num: usize,
pub(crate) dynamic_rx_buf_num: usize,
pub(crate) static_tx_buf_num: usize,
pub(crate) dynamic_tx_buf_num: usize,
pub(crate) ampdu_rx_enable: bool,
pub(crate) ampdu_tx_enable: bool,
pub(crate) amsdu_tx_enable: bool,
pub(crate) rx_ba_win: usize,
pub(crate) max_burst_size: usize,
pub(crate) country_code: &'static str,
pub(crate) country_code_operating_class: u8,
pub(crate) mtu: usize,
pub(crate) tick_rate_hz: u32,
pub(crate) listen_interval: u16,
pub(crate) beacon_timeout: u16,
pub(crate) ap_beacon_timeout: u16,
pub(crate) failure_retry_cnt: u8,
pub(crate) scan_method: u32,
}

#[non_exhaustive]
#[derive(Default)]
pub enum PowerSaveMode {
None,
#[default]
Minimum,
Maximum,
}

impl From<PowerSaveMode> for include::wifi_ps_type_t {
fn from(s: PowerSaveMode) -> Self {
match s {
PowerSaveMode::None => include::wifi_ps_type_t_WIFI_PS_NONE,
PowerSaveMode::Minimum => include::wifi_ps_type_t_WIFI_PS_MIN_MODEM,
PowerSaveMode::Maximum => include::wifi_ps_type_t_WIFI_PS_MAX_MODEM,
}
}
}
28 changes: 9 additions & 19 deletions esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use portable_atomic::{AtomicBool, AtomicU8, Ordering};

use crate::{
binary::include::*,
config::PowerSaveMode,
hal::peripheral::{Peripheral, PeripheralRef},
wifi::{Protocol, RxControlInfo, WifiError},
EspWifiController,
Expand Down Expand Up @@ -335,6 +336,14 @@ impl EspNowManager<'_> {
Ok(())
}

/// Configures modem power saving
///
/// This is ignored, and set to `PowerSaveMode::Minimum` when `coex` is
/// enabled.
pub fn set_power_saving(&self, ps: PowerSaveMode) -> Result<(), WifiError> {
crate::wifi::apply_power_saving(ps)
}

/// Set primary WiFi channel.
/// Should only be used when using ESP-NOW without AP or STA.
pub fn set_channel(&self, channel: u8) -> Result<(), EspNowError> {
Expand Down Expand Up @@ -682,25 +691,6 @@ impl<'d> EspNow<'d> {
check_error!({
esp_wifi_set_inactive_time(wifi_interface_t_WIFI_IF_STA, crate::CONFIG.beacon_timeout)
})?;
cfg_if::cfg_if! {
if #[cfg(modem_powersaving = "min")] {
check_error!({esp_wifi_set_ps(
crate::binary::include::wifi_ps_type_t_WIFI_PS_MIN_MODEM
)})?;
} else if #[cfg(modem_powersaving = "max")] {
check_error!({esp_wifi_set_ps(
crate::binary::include::wifi_ps_type_t_WIFI_PS_MAX_MODEM
)})?;
} else if #[cfg(coex)] {
check_error!({esp_wifi_set_ps(
crate::binary::include::wifi_ps_type_t_WIFI_PS_MIN_MODEM
)})?;
} else {
check_error!({esp_wifi_set_ps(
crate::binary::include::wifi_ps_type_t_WIFI_PS_NONE
)})?;
}
};
check_error!({ esp_now_init() })?;
check_error!({ esp_now_register_recv_cb(Some(rcv_cb)) })?;
check_error!({ esp_now_register_send_cb(Some(send_cb)) })?;
Expand Down
33 changes: 3 additions & 30 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub mod ble;
#[cfg(feature = "esp-now")]
pub mod esp_now;

pub mod config;

pub(crate) mod common_adapter;

#[doc(hidden)]
Expand Down Expand Up @@ -162,34 +164,7 @@ const _: () = {
};
};

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
/// Tunable parameters for the WiFi driver
#[allow(unused)] // currently there are no ble tunables
struct Config {
rx_queue_size: usize,
tx_queue_size: usize,
static_rx_buf_num: usize,
dynamic_rx_buf_num: usize,
static_tx_buf_num: usize,
dynamic_tx_buf_num: usize,
ampdu_rx_enable: bool,
ampdu_tx_enable: bool,
amsdu_tx_enable: bool,
rx_ba_win: usize,
max_burst_size: usize,
country_code: &'static str,
country_code_operating_class: u8,
mtu: usize,
tick_rate_hz: u32,
listen_interval: u16,
beacon_timeout: u16,
ap_beacon_timeout: u16,
failure_retry_cnt: u8,
scan_method: u32,
}

pub(crate) const CONFIG: Config = Config {
pub(crate) const CONFIG: config::EspWifiConfig = config::EspWifiConfig {
rx_queue_size: esp_config_int!(usize, "ESP_WIFI_RX_QUEUE_SIZE"),
tx_queue_size: esp_config_int!(usize, "ESP_WIFI_TX_QUEUE_SIZE"),
static_rx_buf_num: esp_config_int!(usize, "ESP_WIFI_STATIC_RX_BUF_NUM"),
Expand Down Expand Up @@ -364,11 +339,9 @@ impl private::Sealed for Trng<'_> {}
/// ```rust, no_run
#[doc = esp_hal::before_snippet!()]
/// use esp_hal::{rng::Rng, timg::TimerGroup};
/// use esp_wifi::EspWifiInitFor;
///
/// let timg0 = TimerGroup::new(peripherals.TIMG0);
/// let init = esp_wifi::init(
/// EspWifiInitFor::Wifi,
/// timg0.timer0,
/// Rng::new(peripherals.RNG),
/// peripherals.RADIO_CLK,
Expand Down
36 changes: 19 additions & 17 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use esp_wifi_sys::include::{
wifi_promiscuous_pkt_type_t,
};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
#[doc(hidden)]
pub(crate) use os_adapter::*;
#[cfg(feature = "sniffer")]
Expand All @@ -64,6 +63,7 @@ pub use state::*;

use crate::{
common_adapter::*,
config::PowerSaveMode,
esp_wifi_result,
hal::{
macros::ram,
Expand Down Expand Up @@ -105,7 +105,6 @@ use crate::binary::{
esp_wifi_set_country,
esp_wifi_set_mode,
esp_wifi_set_protocol,
esp_wifi_set_ps,
esp_wifi_set_tx_done_cb,
esp_wifi_start,
esp_wifi_stop,
Expand Down Expand Up @@ -1704,20 +1703,6 @@ pub(crate) fn wifi_start() -> Result<(), WifiError> {
))?;
};

cfg_if::cfg_if! {
if #[cfg(modem_powersaving = "min")] {
let ps_mode = include::wifi_ps_type_t_WIFI_PS_MIN_MODEM;
} else if #[cfg(modem_powersaving = "max")] {
let ps_mode = include::wifi_ps_type_t_WIFI_PS_MAX_MODEM;
} else if #[cfg(coex)] {
let ps_mode = include::wifi_ps_type_t_WIFI_PS_MIN_MODEM;
} else {
let ps_mode = include::wifi_ps_type_t_WIFI_PS_NONE;
}
};

esp_wifi_result!(esp_wifi_set_ps(ps_mode))?;

let mut cntry_code = [0u8; 3];
cntry_code[..crate::CONFIG.country_code.len()]
.copy_from_slice(crate::CONFIG.country_code.as_bytes());
Expand Down Expand Up @@ -2606,6 +2591,14 @@ impl<'d> WifiController<'d> {
Ok(())
}

/// Configures modem power saving
///
/// This is ignored, and set to `PowerSaveMode::Minimum` when `coex` is
/// enabled.
pub fn set_power_saving(&mut self, ps: PowerSaveMode) -> Result<(), WifiError> {
apply_power_saving(ps)
}

/// Checks if Wi-Fi is enabled as a station.
pub fn is_sta_enabled(&self) -> Result<bool, WifiError> {
WifiMode::try_from(&self.config).map(|m| m.is_sta())
Expand Down Expand Up @@ -3180,8 +3173,9 @@ fn dump_packet_info(_buffer: &[u8]) {
#[macro_export]
macro_rules! esp_wifi_result {
($value:expr) => {{
use num_traits::FromPrimitive;
let result = $value;
if result != include::ESP_OK as i32 {
if result != esp_wifi_sys::include::ESP_OK as i32 {
warn!("{} returned an error: {}", stringify!($value), result);
Err(WifiError::InternalError(unwrap!(FromPrimitive::from_i32(
result
Expand Down Expand Up @@ -3272,6 +3266,14 @@ pub(crate) mod embassy {
}
}

pub(crate) fn apply_power_saving(mut ps: PowerSaveMode) -> Result<(), WifiError> {
if cfg!(coex) {
ps = PowerSaveMode::Minimum;
}
esp_wifi_result!(unsafe { esp_wifi_sys::include::esp_wifi_set_ps(ps.into()) })?;
Ok(())
}

mod asynch {
use core::task::Poll;

Expand Down
4 changes: 0 additions & 4 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,18 @@ 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",
Expand All @@ -278,7 +275,6 @@ esp32s3 = [
"esp-hal/esp32s3",
"esp-hal-embassy?/esp32s3",
"esp-wifi?/esp32s3",
"esp-wifi?/phy-enable-usb",
]
# Async & Embassy:
embassy = [
Expand Down

0 comments on commit 6af0c4a

Please sign in to comment.