Skip to content

Commit

Permalink
Support ps-max-modem (esp-rs#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed May 23, 2024
1 parent e110f79 commit 418bf78
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
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

0 comments on commit 418bf78

Please sign in to comment.