From 633c4da57f12151adedcb8d83279cd9c6950bed8 Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Fri, 6 Jan 2023 00:52:50 +0000 Subject: [PATCH] Finish PeripheralRef - Remove the mixed pac and generated peripherals, instead all are generated. --- esp-hal-common/src/peripheral.rs | 139 +++++++++++----------- esp-hal-common/src/peripherals/esp32.rs | 29 ----- esp-hal-common/src/peripherals/esp32c2.rs | 22 ---- esp-hal-common/src/peripherals/esp32c3.rs | 27 ----- esp-hal-common/src/peripherals/esp32s2.rs | 29 ----- esp-hal-common/src/peripherals/esp32s3.rs | 35 ------ 6 files changed, 68 insertions(+), 213 deletions(-) diff --git a/esp-hal-common/src/peripheral.rs b/esp-hal-common/src/peripheral.rs index b2332042b83..6019978b21a 100644 --- a/esp-hal-common/src/peripheral.rs +++ b/esp-hal-common/src/peripheral.rs @@ -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 ::Target = super::pac::$name::PTR; + + #[doc = r"Return the pointer to the register block"] + #[inline(always)] + pub const fn ptr() -> *const ::Target { + super::pac::$name::PTR + } + } + + impl core::ops::Deref for $name { + type Target = ::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 { $( @@ -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(), )* } } @@ -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 ::Target = super::pac::$name::PTR; - - #[doc = r"Return the pointer to the register block"] - #[inline(always)] - pub const fn ptr() -> *const ::Target { - super::pac::$name::PTR - } - } - - impl core::ops::Deref for $name { - type Target = ::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),*) => { diff --git a/esp-hal-common/src/peripherals/esp32.rs b/esp-hal-common/src/peripherals/esp32.rs index 9b213653bbd..980f409120d 100644 --- a/esp-hal-common/src/peripherals/esp32.rs +++ b/esp-hal-common/src/peripherals/esp32.rs @@ -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, - } -} diff --git a/esp-hal-common/src/peripherals/esp32c2.rs b/esp-hal-common/src/peripherals/esp32c2.rs index 1613e05427e..f09b8cb07ee 100644 --- a/esp-hal-common/src/peripherals/esp32c2.rs +++ b/esp-hal-common/src/peripherals/esp32c2.rs @@ -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, - } -} diff --git a/esp-hal-common/src/peripherals/esp32c3.rs b/esp-hal-common/src/peripherals/esp32c3.rs index d0f2459c249..ede7dfb369e 100644 --- a/esp-hal-common/src/peripherals/esp32c3.rs +++ b/esp-hal-common/src/peripherals/esp32c3.rs @@ -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, - } -} diff --git a/esp-hal-common/src/peripherals/esp32s2.rs b/esp-hal-common/src/peripherals/esp32s2.rs index 7c4d42c03df..1110b20450d 100644 --- a/esp-hal-common/src/peripherals/esp32s2.rs +++ b/esp-hal-common/src/peripherals/esp32s2.rs @@ -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, - } -} diff --git a/esp-hal-common/src/peripherals/esp32s3.rs b/esp-hal-common/src/peripherals/esp32s3.rs index 035b1cac305..31da405d082 100644 --- a/esp-hal-common/src/peripherals/esp32s3.rs +++ b/esp-hal-common/src/peripherals/esp32s3.rs @@ -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, - } -}