Skip to content

Commit

Permalink
Finish PeripheralRef (#334)
Browse files Browse the repository at this point in the history
- Remove the mixed pac and generated peripherals, instead all are
  generated.
  • Loading branch information
MabezDev authored Jan 6, 2023
1 parent fe6ea53 commit f618490
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 213 deletions.
139 changes: 68 additions & 71 deletions esp-hal-common/src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,73 @@ mod peripheral_macros {
#[macro_export]
macro_rules! peripherals {
($($(#[$cfg:meta])? $name:ident),*$(,)?) => {

/// 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()
}
}
)*
}

#[allow(non_snake_case)]
pub struct Peripherals {
$(
Expand Down Expand Up @@ -214,8 +281,7 @@ mod peripheral_macros {
Self {
$(
$(#[$cfg])?
// $name: peripherals::$name::steal(), // FIXME add this back once we have removed pac::Peripherals completely
$name: unsafe { core::mem::zeroed() }, // this is well defined for zero sized types: https://github.com/rust-lang/unsafe-code-guidelines/issues/250
$name: peripherals::$name::steal(),
)*
}
}
Expand All @@ -228,75 +294,6 @@ mod peripheral_macros {
}
}

#[macro_export]
macro_rules! create_peripherals {
($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
$(
$(#[$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()
}
}


)*
}
}

#[macro_export]
macro_rules! into_ref {
($($name:ident),*) => {
Expand Down
29 changes: 0 additions & 29 deletions esp-hal-common/src/peripherals/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,3 @@ crate::peripherals! {
UHCI0,
UHCI1,
}

mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
I2C0,
I2C1,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
SPI3,
UART0,
UART1,
UART2,
DPORT,
LEDC,
RMT,
I2S0,
I2S1,
PWM0,
PWM1,
RTC_CNTL,
TIMG0,
TIMG1,
SENS,
}
}
22 changes: 0 additions & 22 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,3 @@ crate::peripherals! {
UART1,
XTS_AES,
}

mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
I2C0,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
SYSTIMER,
UART0,
UART1,
SYSTEM,
LEDC,
DMA,
RTC_CNTL,
TIMG0,
APB_SARADC,
}
}
27 changes: 0 additions & 27 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,3 @@ crate::peripherals! {
USB_DEVICE,
XTS_AES,
}

mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
I2C0,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
SYSTIMER,
UART0,
UART1,
USB_DEVICE,
SYSTEM,
LEDC,
RMT,
I2S,
DMA,
RTC_CNTL,
TIMG0,
TIMG1,
APB_SARADC,
TWAI,
}
}
29 changes: 0 additions & 29 deletions esp-hal-common/src/peripherals/esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,3 @@ crate::peripherals! {
USB_WRAP,
XTS_AES,
}

mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
I2C0,
I2C1,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
SPI3,
SPI4,
SYSTIMER,
UART0,
UART1,
SYSTEM,
LEDC,
RMT,
I2S,
USB0,
RTC_CNTL,
TIMG0,
TIMG1,
APB_SARADC,
SENS,
}
}
35 changes: 0 additions & 35 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,3 @@ crate::peripherals! {
WCL,
XTS_AES,
}

mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
I2C0,
I2C1,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
SPI3,
SYSTIMER,
UART0,
UART1,
UART2,
USB_DEVICE,
SYSTEM,
LEDC,
RMT,
I2S0,
I2S1,
DMA,
PWM0,
PWM1,
USB0,
RTC_CNTL,
TIMG0,
TIMG1,
TWAI,
APB_SARADC,
SENS,
}
}

0 comments on commit f618490

Please sign in to comment.