diff --git a/esp-hal/src/rng.rs b/esp-hal/src/rng.rs index 406a99808..d0875012a 100644 --- a/esp-hal/src/rng.rs +++ b/esp-hal/src/rng.rs @@ -130,10 +130,7 @@ impl rand_core::RngCore for Rng { /// the randomness from the hardware RNG and an ADC. This struct provides /// methods to generate random numbers and fill buffers with random bytes. /// Due to pulling the entropy source from the ADC, it uses the associated -/// regiters, so to use TRNG we need to "occupy" the ADC peripheral. -/// -/// For now, even after calling `core::mem::drop()` on `TRNG` ADC1 will not be -/// usable (details in esp-hal/#1750) +/// registers, so to use TRNG we need to "occupy" the ADC peripheral. /// /// ```rust, no_run #[doc = crate::before_snippet!()] diff --git a/esp-wifi/CHANGELOG.md b/esp-wifi/CHANGELOG.md index aa932a028..24189dc5a 100644 --- a/esp-wifi/CHANGELOG.md +++ b/esp-wifi/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- `esp_wifi::init` no longer requires `EspWifiInitFor`, and now returns `EspWifiController`, see the migration guide for more details (#2301) - No need to add `rom_functions.x` manually anymore (#2374) - esp-now: Data is now private in `ReceivedData` - use `data()`(#2396) diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index d6d92be54..0b448583c 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -39,6 +39,7 @@ libm = "0.2.8" cfg-if = "1.0.0" portable-atomic = { version = "1.9.0", default-features = false } portable_atomic_enum = { version = "0.3.1", features = ["portable-atomic"] } +rand_core = "0.6.4" bt-hci = { version = "0.1.1", optional = true } esp-config = { version = "0.1.0", path = "../esp-config" } diff --git a/esp-wifi/MIGRATING-0.10.md b/esp-wifi/MIGRATING-0.10.md index a8c7414dc..0bf1d4c5c 100644 --- a/esp-wifi/MIGRATING-0.10.md +++ b/esp-wifi/MIGRATING-0.10.md @@ -1,5 +1,22 @@ # Migration Guide from 0.10.x to v0.11.x +## Initialization changes + +`EspWifiInitFor` has been removed, individual drivers such as `Wifi` and `BleConnector` handle the initialization and de-initialization of the radio stack. + +`EspWifiInit` has been removed in favour of `EspWifiController`, each radio driver takes reference to this object. If no driver is borrowing `EspWifiController`, +you can safely call `EspWifiController::deinit()` to completely deinit the stack and return the peripherals used in `esp_wifi::init`. + +`esp-wifi::init` now takes all peripherals using the `PeripheralRef` pattern, with the exception of the rng source. + +`esp_wifi::init` now accepts `esp_hal::rng::Rng` or `esp_hal::rng::Trng`. + +The following error enum variants have been removed from `InitializationError`: + +- `Timer(hal::timer::Error)` +- `TimerUnavailable` +- `RadioClockUnavailable` + ## No need to include `rom_functions.x` manually Don't include `rom_functions.x` from esp-wifi diff --git a/esp-wifi/src/ble/btdm.rs b/esp-wifi/src/ble/btdm.rs index aa601f69d..c6ca321db 100644 --- a/esp-wifi/src/ble/btdm.rs +++ b/esp-wifi/src/ble/btdm.rs @@ -442,6 +442,7 @@ pub(crate) fn ble_init() { API_vhci_host_register_callback(&VHCI_HOST_CALLBACK); } + crate::flags::BLE.store(true, Ordering::Release); } pub(crate) fn ble_deinit() { @@ -453,6 +454,7 @@ pub(crate) fn ble_deinit() { btdm_controller_deinit(); crate::common_adapter::chip_specific::phy_disable(); } + crate::flags::BLE.store(false, Ordering::Release); } pub fn send_hci(data: &[u8]) { diff --git a/esp-wifi/src/ble/controller/mod.rs b/esp-wifi/src/ble/controller/mod.rs index de26dfb51..bd2b58c92 100644 --- a/esp-wifi/src/ble/controller/mod.rs +++ b/esp-wifi/src/ble/controller/mod.rs @@ -3,7 +3,7 @@ use embedded_io::{Error, ErrorType, Read, Write}; use super::{read_hci, read_next, send_hci}; use crate::{ hal::peripheral::{Peripheral, PeripheralRef}, - EspWifiInitialization, + EspWifiController, }; /// A blocking HCI connector @@ -11,14 +11,18 @@ pub struct BleConnector<'d> { _device: PeripheralRef<'d, crate::hal::peripherals::BT>, } +impl<'d> Drop for BleConnector<'d> { + fn drop(&mut self) { + crate::ble::ble_deinit(); + } +} + impl<'d> BleConnector<'d> { pub fn new( - init: &EspWifiInitialization, + _init: &'d EspWifiController<'d>, device: impl Peripheral
+ 'd, ) -> BleConnector<'d> { - if !init.is_ble() { - panic!("Not initialized for BLE use"); - } + crate::ble::ble_init(); Self { _device: device.into_ref(), @@ -80,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::{ @@ -91,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::have_hci_read_data, - hal::peripheral::{Peripheral, PeripheralRef}, - EspWifiInitialization, - }; + use super::*; + use crate::ble::have_hci_read_data; static HCI_WAKER: AtomicWaker = AtomicWaker::new(); @@ -106,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: &EspWifiInitialization, - 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 + 'd,
) -> Result + 'd,
+ _rng: impl EspWifiRngSource,
+ _radio_clocks: impl Peripheral + 'd,
+) -> Result + 'd,
config: MODE::Config,
) -> Result<(WifiDevice<'d, MODE>, WifiController<'d>), WifiError> {
@@ -1896,8 +1907,8 @@ pub fn new_with_config<'d, MODE: WifiDeviceMode>(
/// This function will panic if the mode is [`WifiMode::ApSta`].
/// If you want to use AP-STA mode, use `[new_ap_sta]`.
pub fn new_with_mode<'d, MODE: WifiDeviceMode>(
- inited: &EspWifiInitialization,
- device: impl crate::hal::peripheral::Peripheral + 'd,
+ inited: &'d EspWifiController<'d>,
+ device: impl Peripheral + 'd,
_mode: MODE,
) -> Result<(WifiDevice<'d, MODE>, WifiController<'d>), WifiError> {
new_with_config(inited, device, + 'd,
) -> Result<
(
@@ -1925,7 +1936,7 @@ pub fn new_ap_sta<'d>(
///
/// Returns a tuple of `(AP device, STA device, controller)`.
pub fn new_ap_sta_with_config<'d>(
- inited: &EspWifiInitialization,
+ inited: &'d EspWifiController<'d>,
device: impl Peripheral + 'd,
sta_config: crate::wifi::ClientConfiguration,
ap_config: crate::wifi::AccessPointConfiguration,
@@ -2448,8 +2459,9 @@ impl Sniffer {
pub(crate) fn new() -> Self {
// This shouldn't fail, since the way this is created, means that wifi will
// always be initialized.
- esp_wifi_result!(unsafe { esp_wifi_set_promiscuous_rx_cb(Some(promiscuous_rx_cb)) })
- .unwrap();
+ unwrap!(esp_wifi_result!(unsafe {
+ esp_wifi_set_promiscuous_rx_cb(Some(promiscuous_rx_cb))
+ }));
Self {
promiscuous_mode_enabled: AtomicBool::new(false),
}
@@ -2493,14 +2505,29 @@ pub struct WifiController<'d> {
sniffer_taken: AtomicBool,
}
+impl<'d> Drop for WifiController<'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);
+ }
+ }
+ }
+}
+
impl<'d> WifiController<'d> {
pub(crate) fn new_with_config(
- inited: &EspWifiInitialization,
+ inited: &'d EspWifiController<'d>,
_device: PeripheralRef<'d, crate::hal::peripherals::WIFI>,
config: Configuration,
) -> Result + 'd,
mode: MODE,
storage: &'a mut [SocketStorage<'a>],
@@ -69,7 +69,7 @@ pub struct ApStaInterface<'a, 'd> {
}
pub fn create_ap_sta_network_interface<'a, 'd>(
- inited: &EspWifiInitialization,
+ inited: &'d EspWifiController<'d>,
device: impl crate::hal::peripheral::Peripheral + 'd,
ap_storage: &'a mut [SocketStorage<'a>],
sta_storage: &'a mut [SocketStorage<'a>],
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index d3aeba474..c9af37737 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -30,7 +30,7 @@ esp-hal-embassy = { path = "../esp-hal-embassy", optional = true }
esp-ieee802154 = { path = "../esp-ieee802154", optional = true }
esp-println = { path = "../esp-println", features = ["log"] }
esp-storage = { path = "../esp-storage", optional = true }
-esp-wifi = { path = "../esp-wifi", optional = true }
+esp-wifi = { path = "../esp-wifi", features = ["log"], optional = true }
fugit = "0.3.7"
heapless = "0.8.0"
hmac = { version = "0.12.1", default-features = false }
diff --git a/examples/src/bin/wifi_80211_tx.rs b/examples/src/bin/wifi_80211_tx.rs
index 8cc6deb75..c073ce127 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 fec2923ff..a4e803285 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 03f963c4c..36e175128 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 4ab9e4fa1..06a474b8f 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 4ba170f33..26ac4f4be 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 ad0e92e81..a8c779d11 100644
--- a/examples/src/bin/wifi_coex.rs
+++ b/examples/src/bin/wifi_coex.rs
@@ -41,7 +41,6 @@ use esp_wifi::{
init,
wifi::{utils::create_network_interface, ClientConfiguration, Configuration, WifiStaDevice},
wifi_interface::WifiStack,
- EspWifiInitFor,
};
use smoltcp::{
iface::SocketStorage,
@@ -83,7 +82,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 c223de675..68f642c3d 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 d99dd331c..748df8ccb 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 8beb0f163..82f748055 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 6659ed604..80334b862 100644
--- a/examples/src/bin/wifi_embassy_bench.rs
+++ b/examples/src/bin/wifi_embassy_bench.rs
@@ -36,7 +36,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
@@ -98,13 +98,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 f9ab2b322..3fb6d0dd1 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 65f4443ce..5ee5bb322 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 ada751521..3b7aecd43 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 270a68a7a..1eeec3a88 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 2e3d66724..45253463a 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 294bddc1e..0b3a268db 100644
--- a/examples/src/bin/wifi_esp_now.rs
+++ b/examples/src/bin/wifi_esp_now.rs
@@ -20,7 +20,6 @@ use esp_println::println;
use esp_wifi::{
esp_now::{PeerInfo, BROADCAST_ADDRESS},
init,
- EspWifiInitFor,
};
#[entry]
@@ -37,7 +36,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 2e82c66f5..4e56d6338 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