Skip to content

Commit 30bcad2

Browse files
committed
Configurable tuning parameters (esp-rs#233)
* Make certain settings configurable * Add MD documentation
1 parent 38f0780 commit 30bcad2

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

esp-wifi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ esp-wifi-sys = { version = "0.1.0", path = "../esp-wifi-sys" }
3232
embassy-sync = { workspace = true, optional = true }
3333
embassy-futures = { workspace = true, optional = true }
3434
embassy-net-driver = { workspace = true, optional = true }
35+
toml-cfg.workspace = true
3536

3637
[features]
3738
default = [ "utils", "mtu-1492" ]

esp-wifi/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ const HEAP_SIZE: usize = 72 * 1024;
104104
#[cfg(all(coex, feature = "big-heap"))]
105105
const HEAP_SIZE: usize = 110 * 1024;
106106

107+
#[derive(Debug)]
108+
#[toml_cfg::toml_config]
109+
struct Config {
110+
#[default(5)]
111+
rx_queue_size: usize,
112+
#[default(3)]
113+
tx_queue_size: usize,
114+
#[default(10)]
115+
static_rx_buf_num: usize,
116+
#[default(32)]
117+
dynamic_rx_buf_num: usize,
118+
#[default(0)]
119+
static_tx_buf_num: usize,
120+
#[default(32)]
121+
dynamic_tx_buf_num: usize,
122+
#[default(0)]
123+
ampdu_rx_enable: usize,
124+
#[default(0)]
125+
ampdu_tx_enable: usize,
126+
#[default(0)]
127+
amsdu_tx_enable: usize,
128+
#[default(6)]
129+
rx_ba_win: usize,
130+
#[default(1)]
131+
max_burst_size: usize,
132+
}
133+
107134
#[cfg_attr(esp32, link_section = ".dram2_uninit")]
108135
static mut HEAP_DATA: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
109136

@@ -233,6 +260,8 @@ pub fn initialize(
233260
rom_ets_update_cpu_frequency(240); // we know it's 240MHz because of the check above
234261
}
235262

263+
log::info!("esp-wifi configuration {:?}", crate::CONFIG);
264+
236265
crate::common_adapter::chip_specific::enable_wifi_power_domain();
237266

238267
init_heap();

esp-wifi/src/wifi/mod.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use num_traits::FromPrimitive;
3838
pub use os_adapter::*;
3939
use smoltcp::phy::{Device, DeviceCapabilities, RxToken, TxToken};
4040

41+
const ETHERNET_FRAME_HEADER_SIZE: usize = 18;
42+
4143
#[cfg(feature = "mtu-1514")]
4244
const MTU: usize = 1514;
4345
#[cfg(feature = "mtu-1500")]
@@ -105,15 +107,15 @@ impl WifiMode {
105107
#[derive(Debug, Clone, Copy)]
106108
pub(crate) struct DataFrame<'a> {
107109
len: usize,
108-
data: [u8; 1536],
110+
data: [u8; MTU + ETHERNET_FRAME_HEADER_SIZE],
109111
_phantom: PhantomData<&'a ()>,
110112
}
111113

112114
impl<'a> DataFrame<'a> {
113115
pub(crate) fn new() -> DataFrame<'a> {
114116
DataFrame {
115117
len: 0,
116-
data: [0u8; 1536],
118+
data: [0u8; MTU + ETHERNET_FRAME_HEADER_SIZE],
117119
_phantom: Default::default(),
118120
}
119121
}
@@ -130,10 +132,13 @@ impl<'a> DataFrame<'a> {
130132
}
131133
}
132134

133-
pub(crate) static DATA_QUEUE_RX: Mutex<RefCell<SimpleQueue<DataFrame, 5>>> =
135+
const RX_QUEUE_SIZE: usize = crate::CONFIG.rx_queue_size;
136+
const TX_QUEUE_SIZE: usize = crate::CONFIG.tx_queue_size;
137+
138+
pub(crate) static DATA_QUEUE_RX: Mutex<RefCell<SimpleQueue<DataFrame, RX_QUEUE_SIZE>>> =
134139
Mutex::new(RefCell::new(SimpleQueue::new()));
135140

136-
pub(crate) static DATA_QUEUE_TX: Mutex<RefCell<SimpleQueue<DataFrame, 3>>> =
141+
pub(crate) static DATA_QUEUE_TX: Mutex<RefCell<SimpleQueue<DataFrame, TX_QUEUE_SIZE>>> =
137142
Mutex::new(RefCell::new(SimpleQueue::new()));
138143

139144
#[derive(Debug, Clone, Copy)]
@@ -174,6 +179,8 @@ pub enum WifiEvent {
174179
#[repr(i32)]
175180
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive)]
176181
pub enum InternalWifiError {
182+
///Out of memory
183+
EspErrNoMem = 0x101,
177184
///Invalid argument
178185
EspErrInvalidArg = 0x102,
179186
///WiFi driver was not installed by esp_wifi_init */
@@ -528,19 +535,19 @@ static mut G_CONFIG: wifi_init_config_t = wifi_init_config_t {
528535
sha256_vector: None,
529536
crc32: None,
530537
},
531-
static_rx_buf_num: 10,
532-
dynamic_rx_buf_num: 32,
533-
tx_buf_type: 1, // offset 0x78
534-
static_tx_buf_num: 0,
535-
dynamic_tx_buf_num: 32,
538+
static_rx_buf_num: crate::CONFIG.static_rx_buf_num as i32,
539+
dynamic_rx_buf_num: crate::CONFIG.dynamic_rx_buf_num as i32,
540+
tx_buf_type: 1,
541+
static_tx_buf_num: crate::CONFIG.static_tx_buf_num as i32,
542+
dynamic_tx_buf_num: crate::CONFIG.dynamic_tx_buf_num as i32,
536543
cache_tx_buf_num: 0,
537544
csi_enable: 1,
538-
ampdu_rx_enable: 0,
539-
ampdu_tx_enable: 0,
540-
amsdu_tx_enable: 0,
545+
ampdu_rx_enable: crate::CONFIG.ampdu_rx_enable as i32,
546+
ampdu_tx_enable: crate::CONFIG.ampdu_tx_enable as i32,
547+
amsdu_tx_enable: crate::CONFIG.amsdu_tx_enable as i32,
541548
nvs_enable: 0,
542549
nano_enable: 0,
543-
rx_ba_win: 6,
550+
rx_ba_win: crate::CONFIG.rx_ba_win as i32,
544551
wifi_task_core_id: 0,
545552
beacon_max_len: 752,
546553
mgmt_sbuf_num: 32,
@@ -608,23 +615,26 @@ unsafe extern "C" fn recv_cb(
608615
len: u16,
609616
eb: *mut crate::binary::c_types::c_void,
610617
) -> esp_err_t {
611-
critical_section::with(|cs| {
618+
let src = core::slice::from_raw_parts_mut(buffer as *mut u8, len as usize);
619+
let packet = DataFrame::from_bytes(src);
620+
621+
let res = critical_section::with(|cs| {
612622
let mut queue = DATA_QUEUE_RX.borrow_ref_mut(cs);
613623
if !queue.is_full() {
614-
let src = core::slice::from_raw_parts_mut(buffer as *mut u8, len as usize);
615-
let packet = DataFrame::from_bytes(src);
616624
queue.enqueue(packet).unwrap();
617-
esp_wifi_internal_free_rx_buffer(eb);
618625

619626
#[cfg(feature = "embassy-net")]
620627
embassy::RECEIVE_WAKER.wake();
621628

622629
0
623630
} else {
624-
esp_wifi_internal_free_rx_buffer(eb);
631+
log::error!("RX QUEUE FULL");
625632
1
626633
}
627-
})
634+
});
635+
esp_wifi_internal_free_rx_buffer(eb);
636+
637+
res
628638
}
629639

630640
#[ram]
@@ -941,6 +951,7 @@ impl<'d> Device for WifiDevice<'d> {
941951
if !tx.is_full() {
942952
Some(WifiTxToken::default())
943953
} else {
954+
log::warn!("no Tx token available");
944955
None
945956
}
946957
})
@@ -949,7 +960,7 @@ impl<'d> Device for WifiDevice<'d> {
949960
fn capabilities(&self) -> smoltcp::phy::DeviceCapabilities {
950961
let mut caps = DeviceCapabilities::default();
951962
caps.max_transmission_unit = MTU;
952-
caps.max_burst_size = Some(1);
963+
caps.max_burst_size = Some(crate::CONFIG.max_burst_size);
953964
caps
954965
}
955966
}

0 commit comments

Comments
 (0)