diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index cbf0f87577d..dd2359afd47 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -30,6 +30,7 @@ embassy-futures = { workspace = true, optional = true } embassy-net-driver = { workspace = true, optional = true } toml-cfg.workspace = true libm.workspace = true +cfg-if.workspace = true [features] default = [ "utils", "log" ] @@ -68,6 +69,7 @@ wifi = [ "embedded-svc" ] ble = [ "esp32-hal?/bluetooth" ] phy-enable-usb = [] ps-min-modem = [] +ps-max-modem = [] esp-now = [ "wifi" ] big-heap = [] ipv6 = ["smoltcp?/proto-ipv6"] diff --git a/esp-wifi/src/esp_now/mod.rs b/esp-wifi/src/esp_now/mod.rs index d6b0dc1a94a..e6993057c2c 100644 --- a/esp-wifi/src/esp_now/mod.rs +++ b/esp-wifi/src/esp_now/mod.rs @@ -571,6 +571,28 @@ impl<'d> EspNow<'d> { }; check_error!({ esp_wifi_set_mode(wifi_mode_t_WIFI_MODE_STA) })?; check_error!({ esp_wifi_start() })?; + check_error!({ + esp_wifi_set_inactive_time(wifi_interface_t_WIFI_IF_STA, crate::CONFIG.beacon_timeout) + })?; + cfg_if::cfg_if! { + if #[cfg(feature = "ps-min-modem")] { + check_error!({esp_wifi_set_ps( + crate::binary::include::wifi_ps_type_t_WIFI_PS_MIN_MODEM + )})?; + } else if #[cfg(feature = "ps-max-modem")] { + 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 8e619ebb44e..aa96873c1b2 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -153,6 +153,10 @@ struct Config { heap_size: usize, #[default(DEFAULT_TICK_RATE_HZ)] tick_rate_hz: u32, + #[default(3)] + listen_interval: u16, + #[default(6)] + beacon_timeout: u16, } #[cfg_attr(esp32, link_section = ".dram2_uninit")] diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index 3af8655e9f7..17a3f7a04f5 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -17,6 +17,7 @@ use enumset::EnumSetType; use esp_wifi_sys::include::esp_interface_t_ESP_IF_WIFI_AP; use esp_wifi_sys::include::esp_wifi_disconnect; use esp_wifi_sys::include::esp_wifi_get_mode; +use esp_wifi_sys::include::esp_wifi_set_inactive_time; use esp_wifi_sys::include::esp_wifi_set_protocol; use esp_wifi_sys::include::wifi_ap_config_t; use esp_wifi_sys::include::wifi_auth_mode_t_WIFI_AUTH_WAPI_PSK; @@ -640,16 +641,30 @@ unsafe extern "C" fn esp_wifi_tx_done_cb( pub fn wifi_start() -> Result<(), WifiError> { unsafe { esp_wifi_result!(esp_wifi_start())?; - - #[cfg(any(coex, feature = "ps-min-modem"))] - esp_wifi_result!(esp_wifi_set_ps( - crate::binary::include::wifi_ps_type_t_WIFI_PS_MIN_MODEM + esp_wifi_result!(esp_wifi_set_inactive_time( + wifi_interface_t_WIFI_IF_STA, + crate::CONFIG.beacon_timeout ))?; - #[cfg(not(any(coex, feature = "ps-min-modem")))] - esp_wifi_result!(esp_wifi_set_ps( - crate::binary::include::wifi_ps_type_t_WIFI_PS_NONE - ))?; + cfg_if::cfg_if! { + if #[cfg(feature = "ps-min-modem")] { + esp_wifi_result!(esp_wifi_set_ps( + crate::binary::include::wifi_ps_type_t_WIFI_PS_MIN_MODEM + ))?; + } else if #[cfg(feature = "ps-max-modem")] { + esp_wifi_result!(esp_wifi_set_ps( + crate::binary::include::wifi_ps_type_t_WIFI_PS_MAX_MODEM + ))?; + } else if #[cfg(coex)] { + esp_wifi_result!(esp_wifi_set_ps( + crate::binary::include::wifi_ps_type_t_WIFI_PS_MIN_MODEM + ))?; + } else { + esp_wifi_result!(esp_wifi_set_ps( + crate::binary::include::wifi_ps_type_t_WIFI_PS_NONE + ))?; + } + }; let mut cntry_code = [0u8; 3]; cntry_code[..crate::CONFIG.country_code.len()] @@ -1110,7 +1125,7 @@ impl Wifi for WifiController<'_> { bssid_set: config.bssid.is_some(), bssid, channel: config.channel.unwrap_or(0u8), - listen_interval: 3, + listen_interval: crate::CONFIG.listen_interval, sort_method: wifi_sort_method_t_WIFI_CONNECT_AP_BY_SIGNAL, threshold: wifi_scan_threshold_t { rssi: -99,