Skip to content

Commit

Permalink
More granular init and deinit per driver
Browse files Browse the repository at this point in the history
- Rework EspWifiInit
- No longer require EspWifiInitFor
- Add Drop impls for each driver, and add Drop for EspWifiController to
  fully deinit the stack
  • Loading branch information
MabezDev committed Oct 29, 2024
1 parent 8a23dbe commit f014dd5
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 191 deletions.
2 changes: 2 additions & 0 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,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() {
Expand All @@ -530,6 +531,7 @@ pub(crate) fn ble_deinit() {
btdm_controller_deinit();
crate::common_adapter::chip_specific::phy_disable();
}
crate::flags::BLE.store(false, Ordering::Release);
}

static mut BLE_HCI_READ_DATA: [u8; 256] = [0u8; 256];
Expand Down
16 changes: 10 additions & 6 deletions esp-wifi/src/ble/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ 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
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<P = crate::hal::peripherals::BT> + 'd,
) -> BleConnector<'d> {
if !init.is_ble() {
panic!("Not initialized for BLE use");
}
crate::ble::ble_init();

Self {
_device: device.into_ref(),
Expand Down Expand Up @@ -113,7 +117,7 @@ pub mod asynch {

impl<'d> BleConnector<'d> {
pub fn new(
init: &EspWifiInitialization,
init: &'d EspWifiController<'d>,
device: impl Peripheral<P = crate::hal::peripherals::BT> + 'd,
) -> BleConnector<'d> {
if !init.is_ble() {
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) mod npl;

use core::mem::MaybeUninit;

pub(crate) use ble::{ble_init, read_hci, read_next, send_hci};
pub(crate) use ble::{ble_deinit, ble_init, read_hci, read_next, send_hci};

#[cfg(any(esp32, esp32c3, esp32s3))]
use self::btdm as ble;
Expand Down
2 changes: 2 additions & 0 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ pub(crate) fn ble_init() {

debug!("The ble_controller_init was initialized");
}
crate::flags::BLE.store(true, Ordering::Release);
}

pub(crate) fn ble_deinit() {
Expand Down Expand Up @@ -1252,6 +1253,7 @@ pub(crate) fn ble_deinit() {
item.take();
});
}
crate::flags::BLE.store(false, Ordering::Release);
}

#[cfg(esp32c2)]
Expand Down
6 changes: 3 additions & 3 deletions esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,15 @@ pub struct EspNow<'d> {
impl<'d> EspNow<'d> {
/// Creates an `EspNow` instance.
pub fn new(
inited: &EspWifiInitialization,
inited: &'d EspWifiController<'d>,
device: impl Peripheral<P = crate::hal::peripherals::WIFI> + 'd,
) -> Result<EspNow<'d>, EspNowError> {
EspNow::new_internal(inited, Some(device.into_ref()))
}

/// Creates an `EspNow` instance with support for Wi-Fi coexistence.
pub fn new_with_wifi(
inited: &EspWifiInitialization,
inited: &'d EspWifiController<'d>,
_token: EspNowWithWifiCreateToken,
) -> Result<EspNow<'d>, EspNowError> {
EspNow::new_internal(
Expand All @@ -630,7 +630,7 @@ impl<'d> EspNow<'d> {
}

fn new_internal(
inited: &EspWifiInitialization,
inited: &'d EspWifiController<'d>,
device: Option<PeripheralRef<'d, crate::hal::peripherals::WIFI>>,
) -> Result<EspNow<'d>, EspNowError> {
if !inited.is_wifi() {
Expand Down
Loading

0 comments on commit f014dd5

Please sign in to comment.