diff --git a/docs/tuning.md b/docs/tuning.md index a1402a79..7e7f3d81 100644 --- a/docs/tuning.md +++ b/docs/tuning.md @@ -41,6 +41,11 @@ You can set the following settings |ampdu_tx_enable|WiFi AMPDU TX feature enable flag. (0 or 1) See [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv418wifi_init_config_t)| |amsdu_tx_enable|WiFi AMSDU TX feature enable flag. (0 or 1) See [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv418wifi_init_config_t)| |rx_ba_win|WiFi Block Ack RX window size. See [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv418wifi_init_config_t)| +|country_code|Country code. See [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-country-code)| +|country_code_operating_class|If not 0: Operating Class table number. See [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-country-code)| +|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.| ## Globally disable logging diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index ec48a7ed..c9edb2c1 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -35,7 +35,7 @@ embassy-net-driver = { workspace = true, optional = true } toml-cfg.workspace = true [features] -default = [ "utils", "mtu-1492" ] +default = [ "utils" ] # chip features esp32c3 = [ "esp32c3-hal", "dep:esp32c3", "esp-wifi-sys/esp32c3", "esp-hal-common/esp32c3" ] @@ -73,8 +73,4 @@ phy-enable-usb = [] ps-min-modem = [] esp-now = [ "wifi" ] big-heap = [] -mtu-1514 = [] -mtu-1500 = [] -mtu-1492 = [] -mtu-746 = [] ipv6 = ["smoltcp?/proto-ipv6"] diff --git a/esp-wifi/src/lib.rs b/esp-wifi/src/lib.rs index e4da44f1..4e9866c7 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -89,20 +89,27 @@ pub fn current_millis() -> u64 { get_systimer_count() / (TICKS_PER_SECOND / 1000) } -#[cfg(all(not(coex), not(feature = "big-heap")))] -const HEAP_SIZE: usize = 64 * 1024; +#[allow(unused)] +#[cfg(all(not(feature = "big-heap")))] +const DEFAULT_HEAP_SIZE: usize = 64 * 1024; -#[cfg(all(coex, not(feature = "big-heap")))] -const HEAP_SIZE: usize = 64 * 1024; +#[allow(unused)] +#[cfg(all(not(esp32s2), feature = "big-heap"))] +const DEFAULT_HEAP_SIZE: usize = 110 * 1024; -#[cfg(all(not(coex), not(esp32s2), feature = "big-heap"))] -const HEAP_SIZE: usize = 110 * 1024; +#[allow(unused)] +#[cfg(all(esp32s2, feature = "big-heap"))] +const DEFAULT_HEAP_SIZE: usize = 72 * 1024; -#[cfg(all(not(coex), esp32s2, feature = "big-heap"))] -const HEAP_SIZE: usize = 72 * 1024; +const HEAP_SIZE: usize = crate::CONFIG.heap_size; -#[cfg(all(coex, feature = "big-heap"))] -const HEAP_SIZE: usize = 110 * 1024; +#[allow(unused)] +#[cfg(debug_assertions)] +const DEFAULT_TICK_RATE_HZ: u32 = 50; + +#[allow(unused)] +#[cfg(not(debug_assertions))] +const DEFAULT_TICK_RATE_HZ: u32 = 100; #[derive(Debug)] #[toml_cfg::toml_config] @@ -129,6 +136,16 @@ struct Config { rx_ba_win: usize, #[default(1)] max_burst_size: usize, + #[default("CN")] + country_code: &'static str, + #[default(0)] + country_code_operating_class: u8, + #[default(1492)] + mtu: usize, + #[default(DEFAULT_HEAP_SIZE)] + heap_size: usize, + #[default(DEFAULT_TICK_RATE_HZ)] + tick_rate_hz: u32, } #[cfg_attr(esp32, link_section = ".dram2_uninit")] diff --git a/esp-wifi/src/timer_esp32.rs b/esp-wifi/src/timer_esp32.rs index 594e6ab1..48c85a3f 100644 --- a/esp-wifi/src/timer_esp32.rs +++ b/esp-wifi/src/timer_esp32.rs @@ -20,10 +20,7 @@ pub const TICKS_PER_SECOND: u64 = 40_000_000; pub const COUNTER_BIT_MASK: u64 = 0xFFFF_FFFF_FFFF_FFFF; -#[cfg(debug_assertions)] -const TIMER_DELAY: fugit::HertzU64 = fugit::HertzU64::from_raw(50); -#[cfg(not(debug_assertions))] -const TIMER_DELAY: fugit::HertzU64 = fugit::HertzU64::from_raw(100); +const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz); static TIMER1: Mutex>>>> = Mutex::new(RefCell::new(None)); diff --git a/esp-wifi/src/timer_esp32c2.rs b/esp-wifi/src/timer_esp32c2.rs index fb76807d..e39e3f3d 100644 --- a/esp-wifi/src/timer_esp32c2.rs +++ b/esp-wifi/src/timer_esp32c2.rs @@ -15,10 +15,7 @@ pub const TICKS_PER_SECOND: u64 = 16_000_000; pub const COUNTER_BIT_MASK: u64 = 0x000F_FFFF_FFFF_FFFF; -#[cfg(debug_assertions)] -const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(50); -#[cfg(not(debug_assertions))] -const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(100); +const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz); static ALARM0: Mutex>>> = Mutex::new(RefCell::new(None)); diff --git a/esp-wifi/src/timer_esp32c3.rs b/esp-wifi/src/timer_esp32c3.rs index 9a84f74d..fe6b6a43 100644 --- a/esp-wifi/src/timer_esp32c3.rs +++ b/esp-wifi/src/timer_esp32c3.rs @@ -15,10 +15,7 @@ pub const TICKS_PER_SECOND: u64 = 16_000_000; pub const COUNTER_BIT_MASK: u64 = 0x000F_FFFF_FFFF_FFFF; -#[cfg(debug_assertions)] -const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(50); -#[cfg(not(debug_assertions))] -const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(100); +const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz); static ALARM0: Mutex>>> = Mutex::new(RefCell::new(None)); diff --git a/esp-wifi/src/timer_esp32c6.rs b/esp-wifi/src/timer_esp32c6.rs index 7ee51d10..ce7d7ab0 100644 --- a/esp-wifi/src/timer_esp32c6.rs +++ b/esp-wifi/src/timer_esp32c6.rs @@ -15,10 +15,7 @@ pub const TICKS_PER_SECOND: u64 = 16_000_000; pub const COUNTER_BIT_MASK: u64 = 0x000F_FFFF_FFFF_FFFF; -#[cfg(debug_assertions)] -const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(50); -#[cfg(not(debug_assertions))] -const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(100); +const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz); static ALARM0: Mutex>>> = Mutex::new(RefCell::new(None)); diff --git a/esp-wifi/src/timer_esp32s2.rs b/esp-wifi/src/timer_esp32s2.rs index 289fed0d..2cd6e0ac 100644 --- a/esp-wifi/src/timer_esp32s2.rs +++ b/esp-wifi/src/timer_esp32s2.rs @@ -20,10 +20,7 @@ pub const TICKS_PER_SECOND: u64 = 40_000_000; pub const COUNTER_BIT_MASK: u64 = 0xFFFF_FFFF_FFFF_FFFF; -#[cfg(debug_assertions)] -const TIMER_DELAY: fugit::HertzU64 = fugit::HertzU64::from_raw(50); -#[cfg(not(debug_assertions))] -const TIMER_DELAY: fugit::HertzU64 = fugit::HertzU64::from_raw(100); +const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz); static TIMER1: Mutex>>>> = Mutex::new(RefCell::new(None)); diff --git a/esp-wifi/src/timer_esp32s3.rs b/esp-wifi/src/timer_esp32s3.rs index 8cdb8a4a..2640ea08 100644 --- a/esp-wifi/src/timer_esp32s3.rs +++ b/esp-wifi/src/timer_esp32s3.rs @@ -18,10 +18,7 @@ pub const TICKS_PER_SECOND: u64 = 40_000_000; pub const COUNTER_BIT_MASK: u64 = 0xFFFF_FFFF_FFFF_FFFF; -#[cfg(debug_assertions)] -const TIMER_DELAY: fugit::HertzU64 = fugit::HertzU64::from_raw(50); -#[cfg(not(debug_assertions))] -const TIMER_DELAY: fugit::HertzU64 = fugit::HertzU64::from_raw(100); +const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz); static TIMER1: Mutex>>>> = Mutex::new(RefCell::new(None)); diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index 809d86ce..88796d3d 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -40,14 +40,7 @@ use smoltcp::phy::{Device, DeviceCapabilities, RxToken, TxToken}; const ETHERNET_FRAME_HEADER_SIZE: usize = 18; -#[cfg(feature = "mtu-1514")] -const MTU: usize = 1514; -#[cfg(feature = "mtu-1500")] -const MTU: usize = 1500; -#[cfg(feature = "mtu-1492")] -const MTU: usize = 1492; -#[cfg(feature = "mtu-746")] -const MTU: usize = 746; +const MTU: usize = crate::CONFIG.mtu; #[cfg(esp32)] use esp32_hal as hal; @@ -661,7 +654,13 @@ pub fn wifi_start() -> Result<(), WifiError> { crate::binary::include::wifi_ps_type_t_WIFI_PS_NONE ))?; - let cntry_code = [b'C', b'N', 0]; + let mut cntry_code = [0u8; 3]; + cntry_code[..crate::CONFIG.country_code.len()] + .copy_from_slice(crate::CONFIG.country_code.as_bytes()); + if crate::CONFIG.country_code_operating_class != 0 { + cntry_code[2] = crate::CONFIG.country_code_operating_class; + } + let country = wifi_country_t { cc: core::mem::transmute(cntry_code), schan: 1, diff --git a/esp-wifi/src/wifi_interface.rs b/esp-wifi/src/wifi_interface.rs index a9273eed..f0f24799 100644 --- a/esp-wifi/src/wifi_interface.rs +++ b/esp-wifi/src/wifi_interface.rs @@ -7,7 +7,7 @@ use embedded_svc::ipv4; use smoltcp::iface::{Interface, SocketHandle, SocketSet}; use smoltcp::socket::{dhcpv4::Socket as Dhcpv4Socket, tcp::Socket as TcpSocket}; use smoltcp::time::Instant; -use smoltcp::wire::{IpAddress, IpCidr, IpEndpoint, Ipv4Address, DnsQueryType}; +use smoltcp::wire::{DnsQueryType, IpAddress, IpCidr, IpEndpoint, Ipv4Address}; use crate::current_millis; use crate::wifi::{get_ap_mac, get_sta_mac, WifiDevice, WifiMode}; @@ -225,9 +225,10 @@ impl<'a> WifiStack<'a> { } if let (Some(&dns), Some(dns_handle)) = - (dns, *self.dns_socket_handle.borrow()) { - - sockets.get_mut::(dns_handle) + (dns, *self.dns_socket_handle.borrow()) + { + sockets + .get_mut::(dns_handle) .update_servers(&[dns.into()]); } } @@ -291,7 +292,7 @@ impl<'a> WifiStack<'a> { pub fn configure_dns( &'a self, servers: &[IpAddress], - query_storage: &'a mut [Option] + query_storage: &'a mut [Option], ) { if let Some(old_handle) = self.dns_socket_handle.take() { self.with_mut(|_interface, _device, sockets| sockets.remove(old_handle)); @@ -306,7 +307,8 @@ impl<'a> WifiStack<'a> { pub fn update_dns_servers(&self, servers: &[IpAddress]) { if let Some(dns_handle) = *self.dns_socket_handle.borrow_mut() { self.with_mut(|_interface, _device, sockets| { - sockets.get_mut::(dns_handle) + sockets + .get_mut::(dns_handle) .update_servers(servers); }); } @@ -319,7 +321,8 @@ impl<'a> WifiStack<'a> { ) -> Result, WifiStackError> { use smoltcp::socket::dns; - match query_type { // check if name is already an IP + match query_type { + // check if name is already an IP DnsQueryType::A => { if let Ok(ip) = name.parse::() { return Ok([ip.into()].into_iter().collect()); @@ -339,7 +342,8 @@ impl<'a> WifiStack<'a> { }; let query = self.with_mut(|interface, _device, sockets| { - sockets.get_mut::(dns_handle) + sockets + .get_mut::(dns_handle) .start_query(interface.context(), name, query_type) .map_err(|e| WifiStackError::DnsQueryError(e)) })?; @@ -348,13 +352,15 @@ impl<'a> WifiStack<'a> { self.work(); let result = self.with_mut(|_interface, _device, sockets| { - sockets.get_mut::(dns_handle).get_query_result(query) + sockets + .get_mut::(dns_handle) + .get_query_result(query) }); match result { - Ok(addrs) => return Ok(addrs), // query finished - Err(dns::GetQueryResultError::Pending) => {}, // query not finished - Err(_) => return Err(WifiStackError::DnsQueryFailed) + Ok(addrs) => return Ok(addrs), // query finished + Err(dns::GetQueryResultError::Pending) => {} // query not finished + Err(_) => return Err(WifiStackError::DnsQueryFailed), } } } @@ -431,7 +437,7 @@ pub enum WifiStackError { MissingIp, DnsNotConfigured, DnsQueryError(smoltcp::socket::dns::StartQueryError), - DnsQueryFailed + DnsQueryFailed, } impl Display for WifiStackError { @@ -634,7 +640,7 @@ impl<'s, 'n: 's> Read for Socket<'s, 'n> { Ok(0) => continue, // no data Ok(n) => return Ok(n), Err(RecvError::Finished) => return Err(IoError::SocketClosed), // eof - Err(RecvError::InvalidState) => return Err(IoError::TcpRecvError) + Err(RecvError::InvalidState) => return Err(IoError::TcpRecvError), } } })