#![cfg_attr( all(docsrs, not(not_really_docsrs)), doc = "
You might want to browse the esp-wifi documentation on the esp-rs website instead.
The documentation here on docs.rs is built for a single chip only (ESP32-C3, in particular), while on the esp-rs website you can select your exact chip from the list of supported devices. Available peripherals and their APIs might change depending on the chip.
{feature}"#)]
//! ## Additional configuration
//!
//! We've exposed some configuration options that don't fit into cargo
//! features. These can be set via environment variables, or via cargo's `[env]`
//! section inside `.cargo/config.toml`. Below is a table of tunable parameters
//! for this crate:
#![doc = ""]
#![doc = include_str!(concat!(env!("OUT_DIR"), "/esp_wifi_config_table.md"))]
#![doc = ""]
//! It's important to note that due to a [bug in cargo](https://github.com/rust-lang/cargo/issues/10358),
//! any modifications to the environment, local or otherwise will only get
//! picked up on a full clean build of the project.
#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")]
#![no_std]
#![cfg_attr(xtensa, feature(asm_experimental_arch))]
#![cfg_attr(feature = "sys-logs", feature(c_variadic))]
#![allow(rustdoc::bare_urls)]
// allow until num-derive doesn't generate this warning anymore (unknown_lints because Xtensa
// toolchain doesn't know about that lint, yet)
#![allow(unknown_lints)]
#![allow(non_local_definitions)]
extern crate alloc;
// MUST be the first module
mod fmt;
use core::marker::PhantomData;
use common_adapter::chip_specific::phy_mem_init;
use esp_config::*;
use esp_hal as hal;
use esp_hal::peripheral::Peripheral;
#[cfg(not(feature = "esp32"))]
use esp_hal::timer::systimer::Alarm;
use fugit::MegahertzU32;
use hal::{
clock::Clocks,
rng::{Rng, Trng},
system::RadioClockController,
timer::{timg::Timer as TimgTimer, AnyTimer, PeriodicTimer},
Blocking,
};
use portable_atomic::Ordering;
#[cfg(feature = "wifi")]
use crate::wifi::WifiError;
use crate::{
tasks::init_tasks,
timer::{setup_timer_isr, shutdown_timer_isr},
};
mod binary {
pub use esp_wifi_sys::*;
}
mod compat;
mod preempt;
mod timer;
#[cfg(feature = "wifi")]
pub mod wifi;
#[cfg(feature = "ble")]
pub mod ble;
#[cfg(feature = "esp-now")]
pub mod esp_now;
pub mod config;
pub(crate) mod common_adapter;
#[doc(hidden)]
pub mod tasks;
pub(crate) mod memory_fence;
// this is just to verify that we use the correct defaults in `build.rs`
#[allow(clippy::assertions_on_constants)] // TODO: try assert_eq once it's usable in const context
const _: () = {
cfg_if::cfg_if! {
if #[cfg(not(esp32h2))] {
core::assert!(binary::include::CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM == 10);
core::assert!(binary::include::CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM == 32);
core::assert!(binary::include::WIFI_STATIC_TX_BUFFER_NUM == 0);
core::assert!(binary::include::CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM == 32);
core::assert!(binary::include::CONFIG_ESP_WIFI_AMPDU_RX_ENABLED == 1);
core::assert!(binary::include::CONFIG_ESP_WIFI_AMPDU_TX_ENABLED == 1);
core::assert!(binary::include::WIFI_AMSDU_TX_ENABLED == 0);
core::assert!(binary::include::CONFIG_ESP32_WIFI_RX_BA_WIN == 6);
}
};
};
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
/// Tunable parameters for the WiFi driver
#[allow(unused)] // currently there are no ble tunables
struct Config {
rx_queue_size: usize,
tx_queue_size: usize,
static_rx_buf_num: usize,
dynamic_rx_buf_num: usize,
static_tx_buf_num: usize,
dynamic_tx_buf_num: usize,
csi_enable: bool,
ampdu_rx_enable: bool,
ampdu_tx_enable: bool,
amsdu_tx_enable: bool,
rx_ba_win: usize,
max_burst_size: usize,
country_code: &'static str,
country_code_operating_class: u8,
mtu: usize,
tick_rate_hz: u32,
listen_interval: u16,
beacon_timeout: u16,
ap_beacon_timeout: u16,
failure_retry_cnt: u8,
scan_method: u32,
}
pub(crate) const CONFIG: config::EspWifiConfig = config::EspWifiConfig {
rx_queue_size: esp_config_int!(usize, "ESP_WIFI_RX_QUEUE_SIZE"),
tx_queue_size: esp_config_int!(usize, "ESP_WIFI_TX_QUEUE_SIZE"),
static_rx_buf_num: esp_config_int!(usize, "ESP_WIFI_STATIC_RX_BUF_NUM"),
dynamic_rx_buf_num: esp_config_int!(usize, "ESP_WIFI_DYNAMIC_RX_BUF_NUM"),
static_tx_buf_num: esp_config_int!(usize, "ESP_WIFI_STATIC_TX_BUF_NUM"),
dynamic_tx_buf_num: esp_config_int!(usize, "ESP_WIFI_DYNAMIC_TX_BUF_NUM"),
csi_enable: esp_config_bool!("ESP_WIFI_CSI_ENABLE"),
ampdu_rx_enable: esp_config_bool!("ESP_WIFI_AMPDU_RX_ENABLE"),
ampdu_tx_enable: esp_config_bool!("ESP_WIFI_AMPDU_TX_ENABLE"),
amsdu_tx_enable: esp_config_bool!("ESP_WIFI_AMSDU_TX_ENABLE"),
rx_ba_win: esp_config_int!(usize, "ESP_WIFI_RX_BA_WIN"),
max_burst_size: esp_config_int!(usize, "ESP_WIFI_MAX_BURST_SIZE"),
country_code: esp_config_str!("ESP_WIFI_COUNTRY_CODE"),
country_code_operating_class: esp_config_int!(u8, "ESP_WIFI_COUNTRY_CODE_OPERATING_CLASS"),
mtu: esp_config_int!(usize, "ESP_WIFI_MTU"),
tick_rate_hz: esp_config_int!(u32, "ESP_WIFI_TICK_RATE_HZ"),
listen_interval: esp_config_int!(u16, "ESP_WIFI_LISTEN_INTERVAL"),
beacon_timeout: esp_config_int!(u16, "ESP_WIFI_BEACON_TIMEOUT"),
ap_beacon_timeout: esp_config_int!(u16, "ESP_WIFI_AP_BEACON_TIMEOUT"),
failure_retry_cnt: esp_config_int!(u8, "ESP_WIFI_FAILURE_RETRY_CNT"),
scan_method: esp_config_int!(u32, "ESP_WIFI_SCAN_METHOD"),
};
// Validate the configuration at compile time
#[allow(clippy::assertions_on_constants)]
const _: () = {
// We explicitely use `core` assert here because this evaluation happens at
// compile time and won't bloat the binary
core::assert!(
CONFIG.rx_ba_win < CONFIG.dynamic_rx_buf_num,
"WiFi configuration check: rx_ba_win should not be larger than dynamic_rx_buf_num!"
);
core::assert!(CONFIG.rx_ba_win < (CONFIG.static_rx_buf_num * 2), "WiFi configuration check: rx_ba_win should not be larger than double of the static_rx_buf_num!");
};
type TimeBase = PeriodicTimer<'static, Blocking, AnyTimer>;
pub(crate) mod flags {
use portable_atomic::{AtomicBool, AtomicUsize};
pub(crate) static ESP_WIFI_INITIALIZED: AtomicBool = AtomicBool::new(false);
pub(crate) static WIFI: AtomicUsize = AtomicUsize::new(0);
pub(crate) static BLE: AtomicBool = AtomicBool::new(false);
}
#[derive(Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EspWifiController<'d> {
_inner: PhantomData<&'d ()>,
}
impl EspWifiController<'_> {
/// Is the WiFi part of the radio running
pub fn wifi(&self) -> bool {
crate::flags::WIFI.load(Ordering::Acquire) > 0
}
/// Is the BLE part of the radio running
pub fn ble(&self) -> bool {
crate::flags::BLE.load(Ordering::Acquire)
}
/// De-initialize the radio
pub fn deinit(self) -> Result<(), InitializationError> {
if crate::flags::ESP_WIFI_INITIALIZED.load(Ordering::Acquire) {
// safety: no other driver can be using this if this is callable
unsafe { deinit_unchecked() }
} else {
Ok(())
}
}
pub(crate) unsafe fn conjure() -> Self {
Self {
_inner: PhantomData,
}
}
}
impl Drop for EspWifiController<'_> {
fn drop(&mut self) {
if crate::flags::ESP_WIFI_INITIALIZED.load(Ordering::Acquire) {
// safety: no other driver can be using this if this is callable
unsafe { deinit_unchecked().ok() };
}
}
}
/// A trait to allow better UX for initializing esp-wifi.
///
/// This trait is meant to be used only for the `init` function.
/// Calling `timers()` multiple times may panic.
pub trait EspWifiTimerSource: private::Sealed {
/// Returns the timer source.
fn timer(self) -> TimeBase;
}
/// Helper trait to reduce boilerplate.
///
/// We can't blanket-implement for `Into+ 'd, _rng: impl EspWifiRngSource, _radio_clocks: impl Peripheral
+ 'd,
) -> Result