Skip to content

Commit

Permalink
Add RADIO peripheral (#397)
Browse files Browse the repository at this point in the history
* Allow the creation of peripherals _not_ from the pac in the peripherals macro

* Add RADIO peripheral

- Remove DerefMut hack, replace with proper Sealed impls
- Add RADIO peripheral for all chips

* Add RADIO peripheral with split method into each radio feature
  • Loading branch information
MabezDev authored Feb 21, 2023
1 parent 609fa2c commit 80996e3
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 332 deletions.
6 changes: 6 additions & 0 deletions esp-hal-common/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn main() {
// - 'usb_otg'
// - 'usb_serial_jtag'
// - 'aes'
// - 'radio'
//
// New symbols can be added as needed, but please be sure to update both this
// comment and the required vectors below.
Expand All @@ -65,6 +66,7 @@ fn main() {
"timg1",
"uart2",
"aes",
"radio",
]
} else if esp32c2 {
vec![
Expand All @@ -74,6 +76,7 @@ fn main() {
"gdma",
"systimer",
"timg0",
"radio",
]
} else if esp32c3 {
vec![
Expand All @@ -89,6 +92,7 @@ fn main() {
"timg1",
"usb_serial_jtag",
"aes",
"radio",
]
} else if esp32s2 {
vec![
Expand All @@ -106,6 +110,7 @@ fn main() {
"timg1",
"usb_otg",
"aes",
"radio",
]
} else if esp32s3 {
vec![
Expand All @@ -125,6 +130,7 @@ fn main() {
"usb_otg",
"usb_serial_jtag",
"aes",
"radio",
]
} else {
unreachable!(); // We've already confirmed exactly one chip was selected
Expand Down
48 changes: 9 additions & 39 deletions esp-hal-common/src/analog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,8 @@ impl crate::peripheral::Peripheral for &mut ADC1 {
}
}

impl core::ops::Deref for ADC2 {
type Target = ADC2;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for ADC2 {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}
impl crate::peripheral::sealed::Sealed for &mut ADC1 {}
impl crate::peripheral::sealed::Sealed for ADC1 {}

impl crate::peripheral::Peripheral for ADC2 {
type P = ADC2;
Expand All @@ -82,19 +71,8 @@ impl crate::peripheral::Peripheral for &mut ADC2 {
}
}

impl core::ops::Deref for DAC1 {
type Target = DAC1;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for DAC1 {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}
impl crate::peripheral::sealed::Sealed for &mut ADC2 {}
impl crate::peripheral::sealed::Sealed for ADC2 {}

impl crate::peripheral::Peripheral for DAC1 {
type P = DAC1;
Expand All @@ -112,19 +90,8 @@ impl crate::peripheral::Peripheral for &mut DAC1 {
}
}

impl core::ops::Deref for DAC2 {
type Target = DAC2;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for DAC2 {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}
impl crate::peripheral::sealed::Sealed for &mut DAC1 {}
impl crate::peripheral::sealed::Sealed for DAC1 {}

impl crate::peripheral::Peripheral for DAC2 {
type P = DAC2;
Expand All @@ -142,6 +109,9 @@ impl crate::peripheral::Peripheral for &mut DAC2 {
}
}

impl crate::peripheral::sealed::Sealed for &mut DAC2 {}
impl crate::peripheral::sealed::Sealed for DAC2 {}

cfg_if::cfg_if! {
if #[cfg(any(esp32, esp32s2, esp32s3))] {

Expand Down
10 changes: 10 additions & 0 deletions esp-hal-common/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,16 @@ where
{
}

impl<MODE, RA, IRA, PINTYPE, SIG, const GPIONUM: u8> crate::peripheral::sealed::Sealed
for &mut GpioPin<MODE, RA, IRA, PINTYPE, SIG, GPIONUM>
where
RA: BankGpioRegisterAccess,
IRA: InteruptStatusRegisterAccess,
PINTYPE: PinType,
SIG: GpioSignal,
{
}

impl<RA, IRA, PINTYPE, SIG, const GPIONUM: u8>
From<GpioPin<Unknown, RA, IRA, PINTYPE, SIG, GPIONUM>>
for GpioPin<Input<Floating>, RA, IRA, PINTYPE, SIG, GPIONUM>
Expand Down
2 changes: 2 additions & 0 deletions esp-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub mod peripheral;
pub mod prelude;
#[cfg(rmt)]
pub mod pulse_control;
#[cfg(radio)]
pub mod radio;
pub mod rng;
pub mod rom;
pub mod rtc_cntl;
Expand Down
184 changes: 116 additions & 68 deletions esp-hal-common/src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,80 +167,20 @@ pub trait Peripheral: Sized + sealed::Sealed {
}
}

impl<T: DerefMut> sealed::Sealed for T {}

pub(crate) mod sealed {
pub trait Sealed {}
}

mod peripheral_macros {
#[macro_export]
macro_rules! peripherals {
($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
($($(#[$cfg:meta])? $name:ident => $from_pac:tt),*$(,)?) => {

/// Contains the generated peripherals which implement [`Peripheral`]
mod peripherals {
pub use super::pac::*;
$(
$(#[$cfg])?
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct $name { _inner: () }

$(#[$cfg])?
impl $name {
/// Unsafely create an instance of this peripheral out of thin air.
///
/// # Safety
///
/// You must ensure that you're only using one instance of this type at a time.
#[inline]
pub unsafe fn steal() -> Self {
Self { _inner: () }
}

#[doc = r"Pointer to the register block"]
pub const PTR: *const <super::pac::$name as core::ops::Deref>::Target = super::pac::$name::PTR;

#[doc = r"Return the pointer to the register block"]
#[inline(always)]
pub const fn ptr() -> *const <super::pac::$name as core::ops::Deref>::Target {
super::pac::$name::PTR
}
}

impl core::ops::Deref for $name {
type Target = <super::pac::$name as core::ops::Deref>::Target;

fn deref(&self) -> &Self::Target {
unsafe { &*Self::PTR }
}
}

impl core::ops::DerefMut for $name {

fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(Self::PTR as *mut _) }
}
}

impl crate::peripheral::Peripheral for $name {
type P = $name;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
Self::steal()
}
}

impl crate::peripheral::Peripheral for &mut $name {
type P = $name;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
$name::steal()
}
}
crate::create_peripheral!($(#[$cfg])? $name => $from_pac);
)*
}

Expand Down Expand Up @@ -296,11 +236,119 @@ mod peripheral_macros {

#[macro_export]
macro_rules! into_ref {
($($name:ident),*) => {
$(
#[allow(unused_mut)]
let mut $name = $name.into_ref();
)*
($($name:ident),*) => {
$(
#[allow(unused_mut)]
let mut $name = $name.into_ref();
)*
}
}

#[macro_export]
macro_rules! create_peripheral {
($(#[$cfg:meta])? $name:ident => true) => {
$(#[$cfg])?
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct $name { _inner: () }

$(#[$cfg])?
impl $name {
/// Unsafely create an instance of this peripheral out of thin air.
///
/// # Safety
///
/// You must ensure that you're only using one instance of this type at a time.
#[inline]
pub unsafe fn steal() -> Self {
Self { _inner: () }
}

#[doc = r"Pointer to the register block"]
pub const PTR: *const <super::pac::$name as core::ops::Deref>::Target = super::pac::$name::PTR;

#[doc = r"Return the pointer to the register block"]
#[inline(always)]
pub const fn ptr() -> *const <super::pac::$name as core::ops::Deref>::Target {
super::pac::$name::PTR
}
}

impl core::ops::Deref for $name {
type Target = <super::pac::$name as core::ops::Deref>::Target;

fn deref(&self) -> &Self::Target {
unsafe { &*Self::PTR }
}
}

impl core::ops::DerefMut for $name {

fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(Self::PTR as *mut _) }
}
}

impl crate::peripheral::Peripheral for $name {
type P = $name;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
Self::steal()
}
}

impl crate::peripheral::Peripheral for &mut $name {
type P = $name;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
$name::steal()
}
}

impl crate::peripheral::sealed::Sealed for $name {}
impl crate::peripheral::sealed::Sealed for &mut $name {}
};
($(#[$cfg:meta])? $name:ident => false) => {
$(#[$cfg])?
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct $name { _inner: () }

$(#[$cfg])?
impl $name {
/// Unsafely create an instance of this peripheral out of thin air.
///
/// # Safety
///
/// You must ensure that you're only using one instance of this type at a time.
#[inline]
pub unsafe fn steal() -> Self {
Self { _inner: () }
}
}

impl crate::peripheral::Peripheral for $name {
type P = $name;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
Self::steal()
}
}

impl crate::peripheral::Peripheral for &mut $name {
type P = $name;

#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
$name::steal()
}
}

impl crate::peripheral::sealed::Sealed for $name {}
impl crate::peripheral::sealed::Sealed for &mut $name {}
}
}
}
}
Loading

0 comments on commit 80996e3

Please sign in to comment.