diff --git a/Cargo.toml b/Cargo.toml index ed7710d2..0675979d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,7 @@ embassy-futures = { version = "0.1.0" } embassy-net-driver = { version = "0.1.0" } toml-cfg = "0.1.3" libm = "0.2.7" +cfg-if = "1.0.0" embassy-net = { version = "0.1.0", features = ["nightly", "tcp", "udp", "dhcpv4", "medium-ethernet"] } bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "b82f1e7009bef7e32f0918be5b186188aa5e7109", features = ["macros"] } diff --git a/README.md b/README.md index 89041611..04516f6b 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,8 @@ Don't use this feature if your are _not_ using USB-SERIAL-JTAG since it might re | big-heap | Reserve more heap memory for the drivers | | ipv6 | IPv6 support | | phy-enable-usb | See _USB-SERIAL-JTAG_ below | -| ps-min-modem | Enable modem sleep | +| ps-min-modem | Enable minimum modem sleep | +| ps-max-modem | Enable maximum modem sleep | | log | Route log output to the `log` crate | | defmt | Add `defmt::Format` implementation | diff --git a/docs/tuning.md b/docs/tuning.md index 7e7f3d81..be903ac9 100644 --- a/docs/tuning.md +++ b/docs/tuning.md @@ -46,6 +46,8 @@ You can set the following settings |mtu|MTU, see [documentation](https://docs.rs/smoltcp/0.10.0/smoltcp/phy/struct.DeviceCapabilities.html#structfield.max_transmission_unit)| |heap_size|Size of the WiFi/BLE heap in bytes| |tick_rate_hz|Tick rate of the internal task scheduler in hertz.| +|listen_interval|Interval for station to listen to beacon from AP. The unit of listen interval is one beacon interval. For example, if beacon interval is 100 ms and listen interval is 3, the interval for station to listen to beacon is 300 ms| +|beacon_timeout|For Station, If the station does not receive a beacon frame from the connected SoftAP during the inactive time, disconnect from SoftAP. Default 6s. Range 6-30| ## Globally disable logging diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index cbf0f875..dd2359af 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 d6b0dc1a..e6993057 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 8e619ebb..aa96873c 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 3af8655e..17a3f7a0 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, diff --git a/examples-esp32/Cargo.toml b/examples-esp32/Cargo.toml index 86315527..cd881734 100644 --- a/examples-esp32/Cargo.toml +++ b/examples-esp32/Cargo.toml @@ -41,6 +41,7 @@ wifi = ["esp-wifi/wifi"] ble = ["esp-wifi/ble"] phy-enable-usb = ["esp-wifi/phy-enable-usb"] ps-min-modem = ["esp-wifi/ps-min-modem"] +ps-max-modem = ["esp-wifi/ps-max-modem"] esp-now = ["esp-wifi/esp-now"] ipv6 = ["esp-wifi/ipv6"] coex = ["esp-wifi/coex"] diff --git a/examples-esp32c2/Cargo.toml b/examples-esp32c2/Cargo.toml index 284ac48c..b9ddf9b8 100644 --- a/examples-esp32c2/Cargo.toml +++ b/examples-esp32c2/Cargo.toml @@ -42,6 +42,7 @@ wifi = ["esp-wifi/wifi", "embedded-svc"] ble = ["esp-wifi/ble"] phy-enable-usb = ["esp-wifi/phy-enable-usb"] ps-min-modem = ["esp-wifi/ps-min-modem"] +ps-max-modem = ["esp-wifi/ps-max-modem"] esp-now = ["esp-wifi/esp-now"] ipv6 = ["esp-wifi/ipv6"] #coex = ["esp-wifi/coex"] diff --git a/examples-esp32c3/Cargo.toml b/examples-esp32c3/Cargo.toml index 0557e158..ddac011c 100644 --- a/examples-esp32c3/Cargo.toml +++ b/examples-esp32c3/Cargo.toml @@ -42,6 +42,7 @@ wifi = ["esp-wifi/wifi", "embedded-svc"] ble = ["esp-wifi/ble"] phy-enable-usb = ["esp-wifi/phy-enable-usb"] ps-min-modem = ["esp-wifi/ps-min-modem"] +ps-max-modem = ["esp-wifi/ps-max-modem"] esp-now = ["esp-wifi/esp-now"] ipv6 = ["esp-wifi/ipv6"] coex = ["esp-wifi/coex"] diff --git a/examples-esp32c6/Cargo.toml b/examples-esp32c6/Cargo.toml index b36c4f46..53a01aed 100644 --- a/examples-esp32c6/Cargo.toml +++ b/examples-esp32c6/Cargo.toml @@ -42,6 +42,7 @@ wifi = ["esp-wifi/wifi", "embedded-svc"] ble = ["esp-wifi/ble"] phy-enable-usb = ["esp-wifi/phy-enable-usb"] ps-min-modem = ["esp-wifi/ps-min-modem"] +ps-max-modem = ["esp-wifi/ps-max-modem"] esp-now = ["esp-wifi/esp-now"] ipv6 = ["esp-wifi/ipv6"] #coex = ["esp-wifi/coex"] diff --git a/examples-esp32s2/Cargo.toml b/examples-esp32s2/Cargo.toml index f1c9c0e9..a2705678 100644 --- a/examples-esp32s2/Cargo.toml +++ b/examples-esp32s2/Cargo.toml @@ -40,6 +40,7 @@ embedded-svc = [ "esp-wifi/embedded-svc" ] wifi = ["esp-wifi/wifi", "embedded-svc"] phy-enable-usb = ["esp-wifi/phy-enable-usb"] ps-min-modem = ["esp-wifi/ps-min-modem"] +ps-max-modem = ["esp-wifi/ps-max-modem"] esp-now = ["esp-wifi/esp-now"] ipv6 = ["esp-wifi/ipv6"] log = ["esp-wifi/log", "dep:log", "esp-println/log"] diff --git a/examples-esp32s3/Cargo.toml b/examples-esp32s3/Cargo.toml index ac652f5d..7e122ff8 100644 --- a/examples-esp32s3/Cargo.toml +++ b/examples-esp32s3/Cargo.toml @@ -42,6 +42,7 @@ wifi = ["esp-wifi/wifi", "embedded-svc"] ble = ["esp-wifi/ble"] phy-enable-usb = ["esp-wifi/phy-enable-usb"] ps-min-modem = ["esp-wifi/ps-min-modem"] +ps-max-modem = ["esp-wifi/ps-max-modem"] esp-now = ["esp-wifi/esp-now"] ipv6 = ["esp-wifi/ipv6"] coex = ["esp-wifi/coex"]