From 6af0c4afebc9d918e84f4a77151e9f029320584e Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Tue, 5 Nov 2024 16:07:15 +0000 Subject: [PATCH] make ps a runtime config --- esp-wifi/Cargo.toml | 5 +--- esp-wifi/build.rs | 2 -- esp-wifi/src/config.rs | 47 +++++++++++++++++++++++++++++++++++++ esp-wifi/src/esp_now/mod.rs | 28 +++++++--------------- esp-wifi/src/lib.rs | 33 +++----------------------- esp-wifi/src/wifi/mod.rs | 36 ++++++++++++++-------------- hil-test/Cargo.toml | 4 ---- 7 files changed, 79 insertions(+), 76 deletions(-) create mode 100644 esp-wifi/src/config.rs diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index 4fb1a26f416..65d1beb2443 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -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"))'] } \ No newline at end of file +default-target = "riscv32imc-unknown-none-elf" \ No newline at end of file diff --git a/esp-wifi/build.rs b/esp-wifi/build.rs index b0ad927e257..23be6c6ab3b 100644 --- a/esp-wifi/build.rs +++ b/esp-wifi/build.rs @@ -129,8 +129,6 @@ fn main() -> Result<(), Box> { ("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 ); diff --git a/esp-wifi/src/config.rs b/esp-wifi/src/config.rs new file mode 100644 index 00000000000..31f4a0125fa --- /dev/null +++ b/esp-wifi/src/config.rs @@ -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 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, + } + } +} diff --git a/esp-wifi/src/esp_now/mod.rs b/esp-wifi/src/esp_now/mod.rs index 123f18f76b7..adc9e1889a1 100644 --- a/esp-wifi/src/esp_now/mod.rs +++ b/esp-wifi/src/esp_now/mod.rs @@ -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, @@ -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> { @@ -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)) })?; diff --git a/esp-wifi/src/lib.rs b/esp-wifi/src/lib.rs index b672d91a3a0..3a7d1e195b5 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -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)] @@ -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"), @@ -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, diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index 37edfb8308a..e43fd83682a 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -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")] @@ -64,6 +63,7 @@ pub use state::*; use crate::{ common_adapter::*, + config::PowerSaveMode, esp_wifi_result, hal::{ macros::ram, @@ -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, @@ -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()); @@ -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 { WifiMode::try_from(&self.config).map(|m| m.is_sta()) @@ -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 @@ -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; diff --git a/hil-test/Cargo.toml b/hil-test/Cargo.toml index 980d53dce0e..112d87941bb 100644 --- a/hil-test/Cargo.toml +++ b/hil-test/Cargo.toml @@ -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", @@ -278,7 +275,6 @@ esp32s3 = [ "esp-hal/esp32s3", "esp-hal-embassy?/esp32s3", "esp-wifi?/esp32s3", - "esp-wifi?/phy-enable-usb", ] # Async & Embassy: embassy = [