From 854941f1b0952eea90a741c604cc4bc8bb81341b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Quentin?= Date: Fri, 29 Aug 2025 15:10:49 +0200 Subject: [PATCH] Integrate 802.15.4 into esp-radio more (#4003) * Integrate 802.15.4 into esp-radio more * Fix H2 * fmt * Fix * Deny combination of 802.15.4 and Wi-Fi --- esp-radio/Cargo.toml | 7 +++++- esp-radio/build.rs | 5 +++++ esp-radio/src/common_adapter/mod.rs | 7 ++++++ esp-radio/src/ieee802154/mod.rs | 10 --------- esp-radio/src/ieee802154/raw.rs | 35 ++++++++--------------------- xtask/src/lib.rs | 7 +++++- 6 files changed, 33 insertions(+), 38 deletions(-) diff --git a/esp-radio/Cargo.toml b/esp-radio/Cargo.toml index b63c0ee35..8e6208dff 100644 --- a/esp-radio/Cargo.toml +++ b/esp-radio/Cargo.toml @@ -147,7 +147,8 @@ coex = [] ## Enable WiFi channel state information. See [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv4N18wifi_init_config_t10csi_enableE), csi = [] -## Use IEEE 802.15.4 +## Use IEEE 802.15.4. +## Cannot be used together with Wi-Fi. ieee802154 = ["dep:byte", "dep:ieee802154"] #! ### Ecosystem Feature Flags @@ -176,6 +177,10 @@ unstable = [] ## For development you can enable the `unstable` and the chip feature by adding esp-radio as a dev-dependency. requires-unstable = [] +# These features are considered private and unstable. They are not covered by +# semver guarantees and may change or be removed without notice. +__docs_build = [] + [package.metadata.docs.rs] features = [ "esp32c3", diff --git a/esp-radio/build.rs b/esp-radio/build.rs index cf162ead6..8242b5cb8 100644 --- a/esp-radio/build.rs +++ b/esp-radio/build.rs @@ -39,6 +39,11 @@ fn main() -> Result<(), Box> { ); } + #[cfg(not(feature = "__docs_build"))] + if cfg!(feature = "ieee802154") && (cfg!(feature = "wifi") || cfg!(feature = "wifi-eap")) { + panic!("\n\n802.15.4 and Wi-Fi won't work together\n\n"); + } + if (cfg!(feature = "ble") || cfg!(feature = "coex") || cfg!(feature = "csi") diff --git a/esp-radio/src/common_adapter/mod.rs b/esp-radio/src/common_adapter/mod.rs index 7fcc6b159..022d60c74 100644 --- a/esp-radio/src/common_adapter/mod.rs +++ b/esp-radio/src/common_adapter/mod.rs @@ -347,6 +347,13 @@ pub(crate) unsafe fn phy_disable_clock() { } pub(crate) fn phy_calibrate() { + // Set PHY whether in combo module + // For comode mode, phy enable will be not in WiFi RX state + #[cfg(not(any(esp32s2, esp32h2)))] + unsafe { + esp_wifi_sys::include::phy_init_param_set(1); + } + let phy_version = unsafe { get_phy_version_str() }; trace!("phy_version {}", unsafe { str_from_c(phy_version) }); diff --git a/esp-radio/src/ieee802154/mod.rs b/esp-radio/src/ieee802154/mod.rs index 3bf849607..2ff3ab288 100644 --- a/esp-radio/src/ieee802154/mod.rs +++ b/esp-radio/src/ieee802154/mod.rs @@ -15,12 +15,10 @@ //! [IEEE 802.15.4]: https://en.wikipedia.org/wiki/IEEE_802.15.4 //! [esp-openthread]: https://github.com/esp-rs/esp-openthread -use alloc::vec::Vec; use core::cell::RefCell; use byte::{BytesExt, TryRead}; use critical_section::Mutex; -use esp_config::*; use esp_hal::{clock::PhyClockGuard, peripherals::IEEE802154}; use ieee802154::mac::{self, FooterMode, FrameSerDesContext}; @@ -71,14 +69,6 @@ impl From for Error { } } -struct QueueConfig { - rx_queue_size: usize, -} - -const CONFIG: QueueConfig = QueueConfig { - rx_queue_size: esp_config_int!(usize, "ESP_RADIO_CONFIG_IEEE802154_RX_QUEUE_SIZE"), -}; - /// IEEE 802.15.4 driver configuration #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Config { diff --git a/esp-radio/src/ieee802154/raw.rs b/esp-radio/src/ieee802154/raw.rs index beae91775..d33d16565 100644 --- a/esp-radio/src/ieee802154/raw.rs +++ b/esp-radio/src/ieee802154/raw.rs @@ -9,13 +9,10 @@ use esp_hal::{ peripherals::IEEE802154, }; use esp_wifi_sys::include::{ - esp_phy_calibration_data_t, - esp_phy_calibration_mode_t_PHY_RF_CAL_FULL, ieee802154_coex_event_t, ieee802154_coex_event_t_IEEE802154_IDLE, ieee802154_coex_event_t_IEEE802154_LOW, ieee802154_coex_event_t_IEEE802154_MIDDLE, - register_chipv7_phy, }; use super::{ @@ -30,22 +27,23 @@ use super::{ pib::*, }; -const PHY_ENABLE_VERSION_PRINT: u32 = 1; +const PHY_ENABLE_VERSION_PRINT: u8 = 1; + +const RX_QUEUE_SIZE: usize = + esp_config::esp_config_int!(usize, "ESP_RADIO_CONFIG_IEEE802154_RX_QUEUE_SIZE"); static mut RX_BUFFER: [u8; FRAME_SIZE] = [0u8; FRAME_SIZE]; static RX_QUEUE: Mutex>> = Mutex::new(RefCell::new(Queue::new())); static STATE: Mutex> = Mutex::new(RefCell::new(Ieee802154State::Idle)); unsafe extern "C" { - fn bt_bb_v2_init_cmplx(print_version: u32); // from libbtbb.a + fn bt_bb_v2_init_cmplx(print_version: u8); // from libbtbb.a fn bt_bb_set_zb_tx_on_delay(time: u16); // from libbtbb.a fn esp_coex_ieee802154_ack_pti_set(event: ieee802154_coex_event_t); // from ??? fn esp_coex_ieee802154_txrx_pti_set(event: ieee802154_coex_event_t); // from ??? - - fn phy_version_print(); // from libphy.a } #[derive(Debug, Clone, Copy, PartialEq)] @@ -80,31 +78,16 @@ pub(crate) fn esp_ieee802154_enable(mut radio: IEEE802154<'_>) -> PhyClockGuard< let phy_clock_guard = radio.enable_phy_clock(); radio.enable_modem_clock(true); - esp_phy_enable(); + unsafe { + crate::common_adapter::chip_specific::phy_enable(); + } esp_btbb_enable(); ieee802154_mac_init(); - unsafe { phy_version_print() }; // libphy.a info!("date={:x}", mac_date()); phy_clock_guard } -fn esp_phy_enable() { - unsafe { - let mut calibration_data = esp_phy_calibration_data_t { - version: [0u8; 4], - mac: [0u8; 6], - opaque: [0u8; 1894], - }; - - register_chipv7_phy( - core::ptr::null(), - &mut calibration_data as *mut esp_phy_calibration_data_t, - esp_phy_calibration_mode_t_PHY_RF_CAL_FULL, - ); - } -} - fn esp_btbb_enable() { unsafe { bt_bb_v2_init_cmplx(PHY_ENABLE_VERSION_PRINT) }; } @@ -390,7 +373,7 @@ fn ZB_MAC() { ); critical_section::with(|cs| { let mut queue = RX_QUEUE.borrow_ref_mut(cs); - if queue.len() <= crate::CONFIG.rx_queue_size { + if queue.len() <= RX_QUEUE_SIZE { let item = RawReceived { data: RX_BUFFER, channel: freq_to_channel(freq()), diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index ae6dc40c6..08025c98c 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -219,11 +219,16 @@ impl Package { if config.contains("bt") { features.push("ble".to_owned()); } + if config.contains("ieee802154") { + features.push("ieee802154".to_owned()); + // allow wifi + 802.15.4 + features.push("__docs_build".to_owned()); + } if config.contains("wifi") && config.contains("bt") { features.push("coex".to_owned()); } if features.iter().any(|f| { - f == "csi" || f == "ble" || f == "esp-now" || f == "sniffer" || f == "coex" + f == "csi" || f == "ble" || f == "esp-now" || f == "sniffer" || f == "coex" || f == "ieee802154" }) { features.push("unstable".to_owned()); }