Skip to content

Commit

Permalink
Configurable tuning parameters (esp-rs#233)
Browse files Browse the repository at this point in the history
* Make certain settings configurable

* Add MD documentation
  • Loading branch information
bjoernQ committed May 23, 2024
1 parent 38f0780 commit 30bcad2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
1 change: 1 addition & 0 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ esp-wifi-sys = { version = "0.1.0", path = "../esp-wifi-sys" }
embassy-sync = { workspace = true, optional = true }
embassy-futures = { workspace = true, optional = true }
embassy-net-driver = { workspace = true, optional = true }
toml-cfg.workspace = true

[features]
default = [ "utils", "mtu-1492" ]
Expand Down
29 changes: 29 additions & 0 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ const HEAP_SIZE: usize = 72 * 1024;
#[cfg(all(coex, feature = "big-heap"))]
const HEAP_SIZE: usize = 110 * 1024;

#[derive(Debug)]
#[toml_cfg::toml_config]
struct Config {
#[default(5)]
rx_queue_size: usize,
#[default(3)]
tx_queue_size: usize,
#[default(10)]
static_rx_buf_num: usize,
#[default(32)]
dynamic_rx_buf_num: usize,
#[default(0)]
static_tx_buf_num: usize,
#[default(32)]
dynamic_tx_buf_num: usize,
#[default(0)]
ampdu_rx_enable: usize,
#[default(0)]
ampdu_tx_enable: usize,
#[default(0)]
amsdu_tx_enable: usize,
#[default(6)]
rx_ba_win: usize,
#[default(1)]
max_burst_size: usize,
}

#[cfg_attr(esp32, link_section = ".dram2_uninit")]
static mut HEAP_DATA: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];

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

log::info!("esp-wifi configuration {:?}", crate::CONFIG);

crate::common_adapter::chip_specific::enable_wifi_power_domain();

init_heap();
Expand Down
51 changes: 31 additions & 20 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use num_traits::FromPrimitive;
pub use os_adapter::*;
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")]
Expand Down Expand Up @@ -105,15 +107,15 @@ impl WifiMode {
#[derive(Debug, Clone, Copy)]
pub(crate) struct DataFrame<'a> {
len: usize,
data: [u8; 1536],
data: [u8; MTU + ETHERNET_FRAME_HEADER_SIZE],
_phantom: PhantomData<&'a ()>,
}

impl<'a> DataFrame<'a> {
pub(crate) fn new() -> DataFrame<'a> {
DataFrame {
len: 0,
data: [0u8; 1536],
data: [0u8; MTU + ETHERNET_FRAME_HEADER_SIZE],
_phantom: Default::default(),
}
}
Expand All @@ -130,10 +132,13 @@ impl<'a> DataFrame<'a> {
}
}

pub(crate) static DATA_QUEUE_RX: Mutex<RefCell<SimpleQueue<DataFrame, 5>>> =
const RX_QUEUE_SIZE: usize = crate::CONFIG.rx_queue_size;
const TX_QUEUE_SIZE: usize = crate::CONFIG.tx_queue_size;

pub(crate) static DATA_QUEUE_RX: Mutex<RefCell<SimpleQueue<DataFrame, RX_QUEUE_SIZE>>> =
Mutex::new(RefCell::new(SimpleQueue::new()));

pub(crate) static DATA_QUEUE_TX: Mutex<RefCell<SimpleQueue<DataFrame, 3>>> =
pub(crate) static DATA_QUEUE_TX: Mutex<RefCell<SimpleQueue<DataFrame, TX_QUEUE_SIZE>>> =
Mutex::new(RefCell::new(SimpleQueue::new()));

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -174,6 +179,8 @@ pub enum WifiEvent {
#[repr(i32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive)]
pub enum InternalWifiError {
///Out of memory
EspErrNoMem = 0x101,
///Invalid argument
EspErrInvalidArg = 0x102,
///WiFi driver was not installed by esp_wifi_init */
Expand Down Expand Up @@ -528,19 +535,19 @@ static mut G_CONFIG: wifi_init_config_t = wifi_init_config_t {
sha256_vector: None,
crc32: None,
},
static_rx_buf_num: 10,
dynamic_rx_buf_num: 32,
tx_buf_type: 1, // offset 0x78
static_tx_buf_num: 0,
dynamic_tx_buf_num: 32,
static_rx_buf_num: crate::CONFIG.static_rx_buf_num as i32,
dynamic_rx_buf_num: crate::CONFIG.dynamic_rx_buf_num as i32,
tx_buf_type: 1,
static_tx_buf_num: crate::CONFIG.static_tx_buf_num as i32,
dynamic_tx_buf_num: crate::CONFIG.dynamic_tx_buf_num as i32,
cache_tx_buf_num: 0,
csi_enable: 1,
ampdu_rx_enable: 0,
ampdu_tx_enable: 0,
amsdu_tx_enable: 0,
ampdu_rx_enable: crate::CONFIG.ampdu_rx_enable as i32,
ampdu_tx_enable: crate::CONFIG.ampdu_tx_enable as i32,
amsdu_tx_enable: crate::CONFIG.amsdu_tx_enable as i32,
nvs_enable: 0,
nano_enable: 0,
rx_ba_win: 6,
rx_ba_win: crate::CONFIG.rx_ba_win as i32,
wifi_task_core_id: 0,
beacon_max_len: 752,
mgmt_sbuf_num: 32,
Expand Down Expand Up @@ -608,23 +615,26 @@ unsafe extern "C" fn recv_cb(
len: u16,
eb: *mut crate::binary::c_types::c_void,
) -> esp_err_t {
critical_section::with(|cs| {
let src = core::slice::from_raw_parts_mut(buffer as *mut u8, len as usize);
let packet = DataFrame::from_bytes(src);

let res = critical_section::with(|cs| {
let mut queue = DATA_QUEUE_RX.borrow_ref_mut(cs);
if !queue.is_full() {
let src = core::slice::from_raw_parts_mut(buffer as *mut u8, len as usize);
let packet = DataFrame::from_bytes(src);
queue.enqueue(packet).unwrap();
esp_wifi_internal_free_rx_buffer(eb);

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

0
} else {
esp_wifi_internal_free_rx_buffer(eb);
log::error!("RX QUEUE FULL");
1
}
})
});
esp_wifi_internal_free_rx_buffer(eb);

res
}

#[ram]
Expand Down Expand Up @@ -941,6 +951,7 @@ impl<'d> Device for WifiDevice<'d> {
if !tx.is_full() {
Some(WifiTxToken::default())
} else {
log::warn!("no Tx token available");
None
}
})
Expand All @@ -949,7 +960,7 @@ impl<'d> Device for WifiDevice<'d> {
fn capabilities(&self) -> smoltcp::phy::DeviceCapabilities {
let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = MTU;
caps.max_burst_size = Some(1);
caps.max_burst_size = Some(crate::CONFIG.max_burst_size);
caps
}
}
Expand Down

0 comments on commit 30bcad2

Please sign in to comment.