Skip to content

Commit

Permalink
Support ps-max-modem
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed Sep 29, 2023
1 parent 16afb0f commit e87f0cc
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
2 changes: 2 additions & 0 deletions docs/tuning.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down Expand Up @@ -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"]
Expand Down
22 changes: 22 additions & 0 deletions esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) })?;
Expand Down
4 changes: 4 additions & 0 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
33 changes: 24 additions & 9 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions examples-esp32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions examples-esp32c2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions examples-esp32c3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions examples-esp32c6/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions examples-esp32s2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions examples-esp32s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit e87f0cc

Please sign in to comment.