Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more configuration options #234

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/tuning.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down Expand Up @@ -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"]
37 changes: 27 additions & 10 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")]
Expand Down
5 changes: 1 addition & 4 deletions esp-wifi/src/timer_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Timer<Timer0<TIMG1>>>>> = Mutex::new(RefCell::new(None));

Expand Down
5 changes: 1 addition & 4 deletions esp-wifi/src/timer_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Alarm<Periodic, 0>>>> = Mutex::new(RefCell::new(None));

Expand Down
5 changes: 1 addition & 4 deletions esp-wifi/src/timer_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Alarm<Periodic, 0>>>> = Mutex::new(RefCell::new(None));

Expand Down
5 changes: 1 addition & 4 deletions esp-wifi/src/timer_esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Alarm<Periodic, 0>>>> = Mutex::new(RefCell::new(None));

Expand Down
5 changes: 1 addition & 4 deletions esp-wifi/src/timer_esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Timer<Timer0<TIMG1>>>>> = Mutex::new(RefCell::new(None));

Expand Down
5 changes: 1 addition & 4 deletions esp-wifi/src/timer_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Timer<Timer0<TIMG1>>>>> = Mutex::new(RefCell::new(None));

Expand Down
17 changes: 8 additions & 9 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 20 additions & 14 deletions esp-wifi/src/wifi_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -225,9 +225,10 @@ impl<'a> WifiStack<'a> {
}

if let (Some(&dns), Some(dns_handle)) =
(dns, *self.dns_socket_handle.borrow()) {

sockets.get_mut::<smoltcp::socket::dns::Socket>(dns_handle)
(dns, *self.dns_socket_handle.borrow())
{
sockets
.get_mut::<smoltcp::socket::dns::Socket>(dns_handle)
.update_servers(&[dns.into()]);
}
}
Expand Down Expand Up @@ -291,7 +292,7 @@ impl<'a> WifiStack<'a> {
pub fn configure_dns(
&'a self,
servers: &[IpAddress],
query_storage: &'a mut [Option<smoltcp::socket::dns::DnsQuery>]
query_storage: &'a mut [Option<smoltcp::socket::dns::DnsQuery>],
) {
if let Some(old_handle) = self.dns_socket_handle.take() {
self.with_mut(|_interface, _device, sockets| sockets.remove(old_handle));
Expand All @@ -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::<smoltcp::socket::dns::Socket>(dns_handle)
sockets
.get_mut::<smoltcp::socket::dns::Socket>(dns_handle)
.update_servers(servers);
});
}
Expand All @@ -319,7 +321,8 @@ impl<'a> WifiStack<'a> {
) -> Result<heapless::Vec<IpAddress, 1>, 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::<Ipv4Address>() {
return Ok([ip.into()].into_iter().collect());
Expand All @@ -339,7 +342,8 @@ impl<'a> WifiStack<'a> {
};

let query = self.with_mut(|interface, _device, sockets| {
sockets.get_mut::<dns::Socket>(dns_handle)
sockets
.get_mut::<dns::Socket>(dns_handle)
.start_query(interface.context(), name, query_type)
.map_err(|e| WifiStackError::DnsQueryError(e))
})?;
Expand All @@ -348,13 +352,15 @@ impl<'a> WifiStack<'a> {
self.work();

let result = self.with_mut(|_interface, _device, sockets| {
sockets.get_mut::<dns::Socket>(dns_handle).get_query_result(query)
sockets
.get_mut::<dns::Socket>(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),
}
}
}
Expand Down Expand Up @@ -431,7 +437,7 @@ pub enum WifiStackError {
MissingIp,
DnsNotConfigured,
DnsQueryError(smoltcp::socket::dns::StartQueryError),
DnsQueryFailed
DnsQueryFailed,
}

impl Display for WifiStackError {
Expand Down Expand Up @@ -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),
}
}
})
Expand Down
Loading