From b126301405119030f7c3b9ec2c0469097490144f Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Thu, 24 Oct 2024 15:16:01 +0100 Subject: [PATCH] fixup examples and esp-now --- esp-wifi/src/ble/controller/mod.rs | 35 ++----------------- esp-wifi/src/ble/npl.rs | 1 + esp-wifi/src/esp_now/mod.rs | 28 ++++++++++++--- esp-wifi/src/lib.rs | 8 ++--- esp-wifi/src/wifi/mod.rs | 22 ++++++++---- esp-wifi/src/wifi/utils.rs | 2 +- examples/src/bin/wifi_80211_tx.rs | 14 ++------ examples/src/bin/wifi_access_point.rs | 2 -- .../src/bin/wifi_access_point_with_sta.rs | 2 -- examples/src/bin/wifi_bench.rs | 2 -- examples/src/bin/wifi_ble.rs | 3 +- examples/src/bin/wifi_coex.rs | 2 -- examples/src/bin/wifi_dhcp.rs | 2 -- examples/src/bin/wifi_embassy_access_point.rs | 18 +++++----- .../bin/wifi_embassy_access_point_with_sta.rs | 18 +++++----- examples/src/bin/wifi_embassy_bench.rs | 18 +++++----- examples/src/bin/wifi_embassy_ble.rs | 28 ++++++++++----- examples/src/bin/wifi_embassy_dhcp.rs | 18 +++++----- examples/src/bin/wifi_embassy_esp_now.rs | 28 ++++++++++----- .../src/bin/wifi_embassy_esp_now_duplex.rs | 18 +++++----- examples/src/bin/wifi_embassy_trouble.rs | 30 +++++++++++----- examples/src/bin/wifi_esp_now.rs | 3 +- examples/src/bin/wifi_sniffer.rs | 14 ++------ examples/src/bin/wifi_static_ip.rs | 2 -- 24 files changed, 167 insertions(+), 151 deletions(-) diff --git a/esp-wifi/src/ble/controller/mod.rs b/esp-wifi/src/ble/controller/mod.rs index 2eeced9ecb1..995e4727b37 100644 --- a/esp-wifi/src/ble/controller/mod.rs +++ b/esp-wifi/src/ble/controller/mod.rs @@ -84,7 +84,7 @@ impl Write for BleConnector<'_> { /// Async Interface #[cfg(feature = "async")] -pub mod asynch { +pub(crate) mod asynch { use core::task::Poll; use bt_hci::{ @@ -95,14 +95,9 @@ pub mod asynch { WriteHci, }; use embassy_sync::waitqueue::AtomicWaker; - use embedded_io::ErrorType; - use super::{read_hci, send_hci, BleConnectorError}; - use crate::{ - ble::ble::have_hci_read_data, - hal::peripheral::{Peripheral, PeripheralRef}, - EspWifiInitialization, - }; + use super::*; + use crate::ble::ble::have_hci_read_data; static HCI_WAKER: AtomicWaker = AtomicWaker::new(); @@ -110,30 +105,6 @@ pub mod asynch { HCI_WAKER.wake(); } - /// Async HCI connector - pub struct BleConnector<'d> { - _device: PeripheralRef<'d, crate::hal::peripherals::BT>, - } - - impl<'d> BleConnector<'d> { - pub fn new( - init: &'d EspWifiController<'d>, - device: impl Peripheral

+ 'd, - ) -> BleConnector<'d> { - if !init.is_ble() { - panic!("Not initialized for BLE use"); - } - - Self { - _device: device.into_ref(), - } - } - } - - impl ErrorType for BleConnector<'_> { - type Error = BleConnectorError; - } - impl embedded_io_async::Read for BleConnector<'_> { async fn read(&mut self, buf: &mut [u8]) -> Result { if !have_hci_read_data() { diff --git a/esp-wifi/src/ble/npl.rs b/esp-wifi/src/ble/npl.rs index ecd9463eba2..b3fb2f5bf72 100644 --- a/esp-wifi/src/ble/npl.rs +++ b/esp-wifi/src/ble/npl.rs @@ -1,6 +1,7 @@ use core::{ cell::RefCell, ptr::{addr_of, addr_of_mut}, + sync::atomic::Ordering, }; use critical_section::Mutex; diff --git a/esp-wifi/src/esp_now/mod.rs b/esp-wifi/src/esp_now/mod.rs index 15317534d81..d21b14c1458 100644 --- a/esp-wifi/src/esp_now/mod.rs +++ b/esp-wifi/src/esp_now/mod.rs @@ -20,7 +20,7 @@ use crate::{ compat::queue::SimpleQueue, hal::peripheral::{Peripheral, PeripheralRef}, wifi::{Protocol, RxControlInfo}, - EspWifiInitialization, + EspWifiController, }; /// Maximum payload length @@ -465,6 +465,21 @@ impl<'d> EspNowManager<'d> { } } +impl<'d> Drop for EspNowManager<'d> { + fn drop(&mut self) { + if unwrap!( + crate::flags::WIFI.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| { + Some(x.saturating_sub(1)) + }) + ) == 0 + { + if let Err(e) = crate::wifi::wifi_deinit() { + warn!("Failed to cleanly deinit wifi: {:?}", e); + } + } + } +} + /// This is the sender part of ESP-NOW. You can get this sender by splitting /// a `EspNow` instance. /// @@ -603,10 +618,10 @@ impl<'d> Drop for EspNowRc<'d> { /// Currently this implementation (when used together with traditional Wi-Fi) /// ONLY support STA mode. pub struct EspNow<'d> { - _device: Option>, manager: EspNowManager<'d>, sender: EspNowSender<'d>, receiver: EspNowReceiver<'d>, + _phantom: PhantomData<&'d ()>, } impl<'d> EspNow<'d> { @@ -633,13 +648,15 @@ impl<'d> EspNow<'d> { inited: &'d EspWifiController<'d>, device: Option>, ) -> Result, EspNowError> { - if !inited.is_wifi() { - return Err(EspNowError::Error(Error::NotInitialized)); + if !inited.wifi() { + // if wifi isn't already enabled, and we try to coexist - panic + assert!(device.is_some()); + unwrap!(crate::wifi::wifi_init()); // TODO should we return an error + // here? } let espnow_rc = EspNowRc::new()?; let esp_now = EspNow { - _device: device, manager: EspNowManager { _rc: espnow_rc.clone(), }, @@ -647,6 +664,7 @@ impl<'d> EspNow<'d> { _rc: espnow_rc.clone(), }, receiver: EspNowReceiver { _rc: espnow_rc }, + _phantom: PhantomData, }; check_error!({ esp_wifi_set_mode(wifi_mode_t_WIFI_MODE_STA) })?; check_error!({ esp_wifi_start() })?; diff --git a/esp-wifi/src/lib.rs b/esp-wifi/src/lib.rs index 674763c3c7c..23ef7a4e9c9 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -239,10 +239,10 @@ const _: () = { type TimeBase = PeriodicTimer<'static, AnyTimer>; pub(crate) mod flags { - use portable_atomic::AtomicBool; + use portable_atomic::{AtomicBool, AtomicUsize}; pub(crate) static INITD: AtomicBool = AtomicBool::new(false); - pub(crate) static WIFI: AtomicBool = AtomicBool::new(false); + pub(crate) static WIFI: AtomicUsize = AtomicUsize::new(0); pub(crate) static BLE: AtomicBool = AtomicBool::new(false); } @@ -255,7 +255,7 @@ pub struct EspWifiController<'d> { impl<'d> EspWifiController<'d> { /// Is the WiFi part of the radio running pub fn wifi(&self) -> bool { - crate::flags::WIFI.load(Ordering::Acquire) + crate::flags::WIFI.load(Ordering::Acquire) > 0 } /// Is the BLE part of the radio running @@ -360,7 +360,7 @@ impl EspWifiTimerSource for TimeBase { /// # } /// ``` pub fn init<'d, T: EspWifiTimerSource>( - mut timer: impl Peripheral

+ 'd, + timer: impl Peripheral

+ 'd, rng: hal::rng::Rng, _radio_clocks: impl Peripheral

+ 'd, ) -> Result, InitializationError> { diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index 3cf94b7ade3..c78b8b1ed9b 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -1593,6 +1593,8 @@ pub(crate) fn wifi_init() -> Result<(), WifiError> { chip_specific::g_misc_nvs = addr_of!(NVS_STRUCT) as u32; } + crate::flags::WIFI.fetch_add(1, Ordering::SeqCst); + Ok(()) } } @@ -1601,7 +1603,6 @@ pub(crate) fn wifi_deinit() -> Result<(), crate::InitializationError> { esp_wifi_result!(unsafe { esp_wifi_stop() })?; esp_wifi_result!(unsafe { esp_wifi_deinit_internal() })?; esp_wifi_result!(unsafe { esp_supplicant_deinit() })?; - crate::flags::WIFI.store(false, Ordering::Release); Ok(()) } @@ -1901,7 +1902,7 @@ pub fn new_with_config<'d, MODE: WifiDeviceMode>( /// If you want to use AP-STA mode, use `[new_ap_sta]`. pub fn new_with_mode<'d, MODE: WifiDeviceMode>( inited: &'d EspWifiController<'d>, - device: impl crate::hal::peripheral::Peripheral

+ 'd, + device: impl Peripheral

+ 'd, _mode: MODE, ) -> Result<(WifiDevice<'d, MODE>, WifiController<'d>), WifiError> { new_with_config(inited, device, ::Config::default()) @@ -2509,19 +2510,28 @@ pub struct WifiController<'d> { impl<'d> Drop for WifiController<'d> { fn drop(&mut self) { - if let Err(e) = crate::wifi::wifi_deinit() { - warn!("Failed to cleanly deinit wifi: {:?}", e); + if unwrap!( + crate::flags::WIFI.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| { + Some(x.saturating_sub(1)) + }) + ) == 0 + { + if let Err(e) = crate::wifi::wifi_deinit() { + warn!("Failed to cleanly deinit wifi: {:?}", e); + } } } } impl<'d> WifiController<'d> { pub(crate) fn new_with_config( - _inited: &'d EspWifiController<'d>, + inited: &'d EspWifiController<'d>, _device: PeripheralRef<'d, crate::hal::peripherals::WIFI>, config: Configuration, ) -> Result { - wifi_init()?; + if !inited.wifi() { + crate::wifi::wifi_init()?; + } // We set up the controller with the default config because we need to call // `set_configuration` to apply the actual configuration, and it will update the diff --git a/esp-wifi/src/wifi/utils.rs b/esp-wifi/src/wifi/utils.rs index 3c2968c9355..e48f28f8d38 100644 --- a/esp-wifi/src/wifi/utils.rs +++ b/esp-wifi/src/wifi/utils.rs @@ -8,7 +8,7 @@ use smoltcp::{ }; use super::{WifiApDevice, WifiController, WifiDevice, WifiDeviceMode, WifiError, WifiStaDevice}; -use crate::{current_millis, EspWifiController}; +use crate::{timestamp, EspWifiController}; fn setup_iface<'a, MODE: WifiDeviceMode>( device: &mut WifiDevice<'_, MODE>, diff --git a/examples/src/bin/wifi_80211_tx.rs b/examples/src/bin/wifi_80211_tx.rs index 8cc6deb75b4..c073ce12789 100644 --- a/examples/src/bin/wifi_80211_tx.rs +++ b/examples/src/bin/wifi_80211_tx.rs @@ -13,13 +13,8 @@ use core::marker::PhantomData; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{ - delay::Delay, - prelude::*, - rng::Rng, - timer::{timg::TimerGroup, AnyTimer, PeriodicTimer}, -}; -use esp_wifi::{init, wifi, EspWifiInitFor}; +use esp_hal::{delay::Delay, prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_wifi::{init, wifi}; use ieee80211::{ common::{CapabilitiesInformation, FCFFlags}, element_chain, @@ -47,12 +42,9 @@ fn main() -> ! { let delay = Delay::new(); let timg0 = TimerGroup::new(peripherals.TIMG0); - let timer0: AnyTimer = timg0.timer0.into(); - let timer = PeriodicTimer::new(timer0); let init = init( - EspWifiInitFor::Wifi, - timer, + timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, ) diff --git a/examples/src/bin/wifi_access_point.rs b/examples/src/bin/wifi_access_point.rs index fec2923ff79..a4e8032851f 100644 --- a/examples/src/bin/wifi_access_point.rs +++ b/examples/src/bin/wifi_access_point.rs @@ -33,7 +33,6 @@ use esp_wifi::{ WifiApDevice, }, wifi_interface::WifiStack, - EspWifiInitFor, }; use smoltcp::iface::SocketStorage; @@ -51,7 +50,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Wifi, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_access_point_with_sta.rs b/examples/src/bin/wifi_access_point_with_sta.rs index 03f963c4c7c..36e17512864 100644 --- a/examples/src/bin/wifi_access_point_with_sta.rs +++ b/examples/src/bin/wifi_access_point_with_sta.rs @@ -34,7 +34,6 @@ use esp_wifi::{ Configuration, }, wifi_interface::WifiStack, - EspWifiInitFor, }; use smoltcp::{ iface::SocketStorage, @@ -58,7 +57,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Wifi, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_bench.rs b/examples/src/bin/wifi_bench.rs index 4ab9e4fa17c..06a474b8f70 100644 --- a/examples/src/bin/wifi_bench.rs +++ b/examples/src/bin/wifi_bench.rs @@ -36,7 +36,6 @@ use esp_wifi::{ WifiStaDevice, }, wifi_interface::WifiStack, - EspWifiInitFor, }; use smoltcp::{ iface::SocketStorage, @@ -71,7 +70,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Wifi, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_ble.rs b/examples/src/bin/wifi_ble.rs index 4ba170f332a..26ac4f4becf 100644 --- a/examples/src/bin/wifi_ble.rs +++ b/examples/src/bin/wifi_ble.rs @@ -32,7 +32,7 @@ use esp_hal::{ timer::timg::TimerGroup, }; use esp_println::println; -use esp_wifi::{ble::controller::BleConnector, init, EspWifiInitFor}; +use esp_wifi::{ble::controller::BleConnector, init}; #[entry] fn main() -> ! { @@ -48,7 +48,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Ble, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_coex.rs b/examples/src/bin/wifi_coex.rs index 02aa7c8f8f3..5229c2089ec 100644 --- a/examples/src/bin/wifi_coex.rs +++ b/examples/src/bin/wifi_coex.rs @@ -40,7 +40,6 @@ use esp_wifi::{ init, wifi::{utils::create_network_interface, ClientConfiguration, Configuration, WifiStaDevice}, wifi_interface::WifiStack, - EspWifiInitFor, }; use smoltcp::{ iface::SocketStorage, @@ -82,7 +81,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::WifiBle, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_dhcp.rs b/examples/src/bin/wifi_dhcp.rs index c223de675e5..68f642c3d38 100644 --- a/examples/src/bin/wifi_dhcp.rs +++ b/examples/src/bin/wifi_dhcp.rs @@ -35,7 +35,6 @@ use esp_wifi::{ WifiStaDevice, }, wifi_interface::WifiStack, - EspWifiInitFor, }; use smoltcp::{ iface::SocketStorage, @@ -59,7 +58,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Wifi, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_embassy_access_point.rs b/examples/src/bin/wifi_embassy_access_point.rs index d99dd331c2c..748df8ccba5 100644 --- a/examples/src/bin/wifi_embassy_access_point.rs +++ b/examples/src/bin/wifi_embassy_access_point.rs @@ -41,7 +41,7 @@ use esp_wifi::{ WifiEvent, WifiState, }, - EspWifiInitFor, + EspWifiController, }; // When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html @@ -67,13 +67,15 @@ async fn main(spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Wifi, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let wifi = peripherals.WIFI; let (wifi_interface, controller) = diff --git a/examples/src/bin/wifi_embassy_access_point_with_sta.rs b/examples/src/bin/wifi_embassy_access_point_with_sta.rs index 8beb0f16389..82f74805509 100644 --- a/examples/src/bin/wifi_embassy_access_point_with_sta.rs +++ b/examples/src/bin/wifi_embassy_access_point_with_sta.rs @@ -46,7 +46,7 @@ use esp_wifi::{ WifiStaDevice, WifiState, }, - EspWifiInitFor, + EspWifiController, }; const SSID: &str = env!("SSID"); @@ -75,13 +75,15 @@ async fn main(spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Wifi, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let wifi = peripherals.WIFI; let (wifi_ap_interface, wifi_sta_interface, mut controller) = diff --git a/examples/src/bin/wifi_embassy_bench.rs b/examples/src/bin/wifi_embassy_bench.rs index 3214804fa88..60a207a0631 100644 --- a/examples/src/bin/wifi_embassy_bench.rs +++ b/examples/src/bin/wifi_embassy_bench.rs @@ -35,7 +35,7 @@ use esp_wifi::{ WifiStaDevice, WifiState, }, - EspWifiInitFor, + EspWifiController, }; // When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html @@ -97,13 +97,15 @@ async fn main(spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Wifi, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let wifi = peripherals.WIFI; let (wifi_interface, controller) = diff --git a/examples/src/bin/wifi_embassy_ble.rs b/examples/src/bin/wifi_embassy_ble.rs index f9ab2b322e1..3fb6d0dd181 100644 --- a/examples/src/bin/wifi_embassy_ble.rs +++ b/examples/src/bin/wifi_embassy_ble.rs @@ -35,7 +35,17 @@ use esp_hal::{ timer::timg::TimerGroup, }; use esp_println::println; -use esp_wifi::{ble::controller::asynch::BleConnector, init, EspWifiInitFor}; +use esp_wifi::{ble::controller::BleConnector, init, EspWifiController}; + +// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html +macro_rules! mk_static { + ($t:ty,$val:expr) => {{ + static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); + #[deny(unused_attributes)] + let x = STATIC_CELL.uninit().write(($val)); + x + }}; +} #[esp_hal_embassy::main] async fn main(_spawner: Spawner) -> ! { @@ -50,13 +60,15 @@ async fn main(_spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Ble, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); cfg_if::cfg_if! { diff --git a/examples/src/bin/wifi_embassy_dhcp.rs b/examples/src/bin/wifi_embassy_dhcp.rs index 65f4443cefa..5ee5bb322ba 100644 --- a/examples/src/bin/wifi_embassy_dhcp.rs +++ b/examples/src/bin/wifi_embassy_dhcp.rs @@ -32,7 +32,7 @@ use esp_wifi::{ WifiStaDevice, WifiState, }, - EspWifiInitFor, + EspWifiController, }; // When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html @@ -61,13 +61,15 @@ async fn main(spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Wifi, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let wifi = peripherals.WIFI; let (wifi_interface, controller) = diff --git a/examples/src/bin/wifi_embassy_esp_now.rs b/examples/src/bin/wifi_embassy_esp_now.rs index ada75152122..3b7aecd43e8 100644 --- a/examples/src/bin/wifi_embassy_esp_now.rs +++ b/examples/src/bin/wifi_embassy_esp_now.rs @@ -20,9 +20,19 @@ use esp_println::println; use esp_wifi::{ esp_now::{PeerInfo, BROADCAST_ADDRESS}, init, - EspWifiInitFor, + EspWifiController, }; +// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html +macro_rules! mk_static { + ($t:ty,$val:expr) => {{ + static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); + #[deny(unused_attributes)] + let x = STATIC_CELL.uninit().write(($val)); + x + }}; +} + #[esp_hal_embassy::main] async fn main(_spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); @@ -36,13 +46,15 @@ async fn main(_spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Wifi, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let wifi = peripherals.WIFI; let mut esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); diff --git a/examples/src/bin/wifi_embassy_esp_now_duplex.rs b/examples/src/bin/wifi_embassy_esp_now_duplex.rs index c8bf8e4e078..bfbee4120a9 100644 --- a/examples/src/bin/wifi_embassy_esp_now_duplex.rs +++ b/examples/src/bin/wifi_embassy_esp_now_duplex.rs @@ -20,7 +20,7 @@ use esp_println::println; use esp_wifi::{ esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}, init, - EspWifiInitFor, + EspWifiController, }; // When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html @@ -46,13 +46,15 @@ async fn main(spawner: Spawner) -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = init( - EspWifiInitFor::Wifi, - timg0.timer0, - Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); let wifi = peripherals.WIFI; let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); diff --git a/examples/src/bin/wifi_embassy_trouble.rs b/examples/src/bin/wifi_embassy_trouble.rs index 2e3d6672465..45253463ae6 100644 --- a/examples/src/bin/wifi_embassy_trouble.rs +++ b/examples/src/bin/wifi_embassy_trouble.rs @@ -18,8 +18,8 @@ use embassy_sync::blocking_mutex::raw::NoopRawMutex; use embassy_time::{Duration, Timer}; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, timer::timg::TimerGroup}; -use esp_wifi::ble::controller::asynch::BleConnector; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_wifi::{ble::controller::BleConnector, init, EspWifiController}; use log::*; use static_cell::StaticCell; use trouble_host::{ @@ -31,6 +31,16 @@ use trouble_host::{ PacketQos, }; +// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html +macro_rules! mk_static { + ($t:ty,$val:expr) => {{ + static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); + #[deny(unused_attributes)] + let x = STATIC_CELL.uninit().write(($val)); + x + }}; +} + #[esp_hal_embassy::main] async fn main(_s: Spawner) { esp_println::logger::init_logger_from_env(); @@ -44,13 +54,15 @@ async fn main(_s: Spawner) { let timg0 = TimerGroup::new(peripherals.TIMG0); - let init = esp_wifi::init( - esp_wifi::EspWifiInitFor::Ble, - timg0.timer0, - esp_hal::rng::Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = &*mk_static!( + EspWifiController<'static>, + init( + timg0.timer0, + Rng::new(peripherals.RNG), + peripherals.RADIO_CLK, + ) + .unwrap() + ); #[cfg(feature = "esp32")] { diff --git a/examples/src/bin/wifi_esp_now.rs b/examples/src/bin/wifi_esp_now.rs index 294bddc1e52..17bb9e1a309 100644 --- a/examples/src/bin/wifi_esp_now.rs +++ b/examples/src/bin/wifi_esp_now.rs @@ -20,7 +20,7 @@ use esp_println::println; use esp_wifi::{ esp_now::{PeerInfo, BROADCAST_ADDRESS}, init, - EspWifiInitFor, + EspWifiController, }; #[entry] @@ -37,7 +37,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Wifi, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, diff --git a/examples/src/bin/wifi_sniffer.rs b/examples/src/bin/wifi_sniffer.rs index 2e82c66f57a..4e56d6338ef 100644 --- a/examples/src/bin/wifi_sniffer.rs +++ b/examples/src/bin/wifi_sniffer.rs @@ -19,13 +19,9 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; -use esp_hal::{ - prelude::*, - rng::Rng, - timer::{timg::TimerGroup, AnyTimer, PeriodicTimer}, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::println; -use esp_wifi::{init, wifi, EspWifiInitFor}; +use esp_wifi::{init, wifi}; use ieee80211::{match_frames, mgmt_frame::BeaconFrame}; static KNOWN_SSIDS: Mutex>> = Mutex::new(RefCell::new(BTreeSet::new())); @@ -42,12 +38,8 @@ fn main() -> ! { esp_alloc::heap_allocator!(72 * 1024); let timg0 = TimerGroup::new(peripherals.TIMG0); - let timer0: AnyTimer = timg0.timer0.into(); - let timer = PeriodicTimer::new(timer0); - let init = init( - EspWifiInitFor::Wifi, - timer, + timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK, ) diff --git a/examples/src/bin/wifi_static_ip.rs b/examples/src/bin/wifi_static_ip.rs index 4e0742e3049..e9a91fe1fac 100644 --- a/examples/src/bin/wifi_static_ip.rs +++ b/examples/src/bin/wifi_static_ip.rs @@ -34,7 +34,6 @@ use esp_wifi::{ WifiStaDevice, }, wifi_interface::WifiStack, - EspWifiInitFor, }; use smoltcp::iface::SocketStorage; @@ -57,7 +56,6 @@ fn main() -> ! { let timg0 = TimerGroup::new(peripherals.TIMG0); let init = init( - EspWifiInitFor::Wifi, timg0.timer0, Rng::new(peripherals.RNG), peripherals.RADIO_CLK,