Skip to content

Commit

Permalink
Re-work and re-organize the radio module, eliminate the RadioExt
Browse files Browse the repository at this point in the history
…trait
  • Loading branch information
jessebraham committed Oct 16, 2023
1 parent 0c8dd59 commit 51a045b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
2 changes: 0 additions & 2 deletions esp-hal-common/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ pub use crate::ledc::{
},
timer::{TimerHW as _esp_hal_ledc_timer_TimerHW, TimerIFace as _esp_hal_ledc_timer_TimerIFace},
};
#[cfg(radio)]
pub use crate::radio::RadioExt as _esp_hal_RadioExt;
#[cfg(any(esp32, esp32s2, esp32s3))]
pub use crate::spi::master::dma::WithDmaSpi3 as _esp_hal_spi_dma_WithDmaSpi3;
#[cfg(any(spi0, spi1, spi2, spi3))]
Expand Down
81 changes: 38 additions & 43 deletions esp-hal-common/src/radio.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
//! # Wireless communication peripheral implementations
//!
//! ## Overview
//! The radio module provides implementations for different wireless
//! communication peripherals, including WiFi, Bluetooth and
//! IEEE 802.15.4 Low Rate wireless personal area radio.
//!
//! In addition to the structures defined in this module, the module also
//! defines the `RadioExt` trait, which provides a `split` method. This method
//! allows splitting the general `Radio` peripheral into its individual
//! components.
//!
//! Additionally, the module includes implementation blocks for each wireless
//! communication peripheral, providing necessary functions and traits for each
//! peripheral.
pub trait RadioExt {
type Components;

fn split(self) -> Self::Components;
}
//! The radio module provides the individual peripheral structs for different
//! wireless communication peripherals, collectively represented by the virtual
//! `RADIO` peripheral. This includes Wi-Fi, Bluetooth, and IEEE 802.15.4
//! low-rate radios, depending on the device.
/// WiFi radio
/// Wi-Fi radio
#[cfg(wifi)]
pub struct Wifi {
_private: (),
}

/// Bluetooth radio
pub struct Bluetooth {
_private: (),
}

/// IEEE 802.15.4 Low rate wireless personal area radio
pub struct LowRate {
_private: (),
}

#[cfg(wifi)]
impl Wifi {
pub const unsafe fn steal() -> Self {
Self { _private: () }
}
}

#[cfg(wifi)]
impl crate::peripheral::Peripheral for Wifi {
type P = Self;

Expand All @@ -48,14 +29,23 @@ impl crate::peripheral::Peripheral for Wifi {
}
}

#[cfg(wifi)]
impl crate::peripheral::sealed::Sealed for Wifi {}

/// Bluetooth radio
#[cfg(bt)]
pub struct Bluetooth {
_private: (),
}

#[cfg(bt)]
impl Bluetooth {
pub const unsafe fn steal() -> Self {
Self { _private: () }
}
}

#[cfg(bt)]
impl crate::peripheral::Peripheral for Bluetooth {
type P = Self;

Expand All @@ -64,14 +54,23 @@ impl crate::peripheral::Peripheral for Bluetooth {
}
}

#[cfg(bt)]
impl crate::peripheral::sealed::Sealed for Bluetooth {}

/// IEEE 802.15.4 low-rate radio
#[cfg(ieee802154)]
pub struct LowRate {
_private: (),
}

#[cfg(ieee802154)]
impl LowRate {
pub const unsafe fn steal() -> Self {
Self { _private: () }
}
}

#[cfg(ieee802154)]
impl crate::peripheral::Peripheral for LowRate {
type P = Self;

Expand All @@ -80,44 +79,40 @@ impl crate::peripheral::Peripheral for LowRate {
}
}

#[cfg(ieee802154)]
impl crate::peripheral::sealed::Sealed for LowRate {}

// Since each device has a different set of wireless radios available, we need
// per-device implementations of the `split` funciton on the `RADIO` peripheral
// singleton.
cfg_if::cfg_if! {
if #[cfg(all(bt, ieee802154, wifi))] {
impl RadioExt for crate::peripherals::RADIO {
type Components = (Wifi, Bluetooth, LowRate);

fn split(self) -> Self::Components {
impl crate::peripherals::RADIO {
pub fn split(self) -> (Wifi, Bluetooth, LowRate) {
unsafe {
(Wifi::steal(), Bluetooth::steal(), LowRate::steal())
}
}
}
} else if #[cfg(all(bt, ieee802154))] {
impl RadioExt for crate::peripherals::RADIO {
type Components = (Bluetooth, LowRate);

fn split(self) -> Self::Components {
impl crate::peripherals::RADIO {
pub fn split(self) -> (Bluetooth, LowRate) {
unsafe {
(Bluetooth::steal(), LowRate::steal())
}
}
}
} else if #[cfg(all(bt, wifi))] {
impl RadioExt for crate::peripherals::RADIO {
type Components = (Wifi, Bluetooth);

fn split(self) -> Self::Components {
impl crate::peripherals::RADIO {
pub fn split(self) -> (Wifi, Bluetooth) {
unsafe {
(Wifi::steal(), Bluetooth::steal())
}
}
}
} else if #[cfg(wifi)] {
impl RadioExt for crate::peripherals::RADIO {
type Components = Wifi;

fn split(self) -> Self::Components {
impl crate::peripherals::RADIO {
pub fn split(self) -> Wifi {
unsafe {
Wifi::steal()
}
Expand Down

0 comments on commit 51a045b

Please sign in to comment.