use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
pub struct PeripheralRef<'a, T> {
inner: T,
_lifetime: PhantomData<&'a mut T>,
}
impl<'a, T> PeripheralRef<'a, T> {
#[inline]
pub fn new(inner: T) -> Self {
Self {
inner,
_lifetime: PhantomData,
}
}
pub unsafe fn clone_unchecked(&mut self) -> PeripheralRef<'a, T>
where
T: Peripheral<P = T>,
{
PeripheralRef::new(self.inner.clone_unchecked())
}
pub fn reborrow(&mut self) -> PeripheralRef<'_, T>
where
T: Peripheral<P = T>,
{
PeripheralRef::new(unsafe { self.inner.clone_unchecked() })
}
#[inline]
pub fn map_into<U>(self) -> PeripheralRef<'a, U>
where
T: Into<U>,
{
PeripheralRef {
inner: self.inner.into(),
_lifetime: PhantomData,
}
}
}
impl<'a, T> Deref for PeripheralRef<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'a, T> DerefMut for PeripheralRef<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
pub trait Peripheral: Sized + crate::private::Sealed {
type P;
unsafe fn clone_unchecked(&mut self) -> Self::P;
#[inline]
fn into_ref<'a>(mut self) -> PeripheralRef<'a, Self::P>
where
Self: 'a,
{
PeripheralRef::new(unsafe { self.clone_unchecked() })
}
}
impl<T> Peripheral for &mut T
where
T: Peripheral<P = T>,
{
type P = T;
unsafe fn clone_unchecked(&mut self) -> Self::P {
T::clone_unchecked(self)
}
}
impl<T> crate::private::Sealed for &mut T where T: crate::private::Sealed {}
mod peripheral_macros {
#[doc(hidden)]
#[macro_export]
macro_rules! peripherals {
($($(#[$cfg:meta])? $name:ident <= $from_pac:tt),*$(,)?) => {
mod peripherals {
pub use super::pac::*;
$(
$crate::create_peripheral!($(#[$cfg])? $name <= $from_pac);
)*
}
#[allow(non_snake_case)]
pub struct Peripherals {
$(
$(#[$cfg])?
pub $name: peripherals::$name,
)*
}
impl Peripherals {
#[inline]
pub fn take() -> Self {
#[no_mangle]
static mut _ESP_HAL_DEVICE_PERIPHERALS: bool = false;
critical_section::with(|_| unsafe {
if _ESP_HAL_DEVICE_PERIPHERALS {
panic!("init called more than once!")
}
_ESP_HAL_DEVICE_PERIPHERALS = true;
Self::steal()
})
}
}
impl Peripherals {
#[inline]
pub unsafe fn steal() -> Self {
Self {
$(
$(#[$cfg])?
$name: peripherals::$name::steal(),
)*
}
}
}
$(
pub use peripherals::$name;
)*
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! into_ref {
($($name:ident),*) => {
$(
#[allow(unused_mut)]
let mut $name = $name.into_ref();
)*
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! create_peripheral {
($(#[$cfg:meta])? $name:ident <= virtual) => {
$(#[$cfg])?
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct $name { _inner: () }
$(#[$cfg])?
impl $name {
#[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::private::Sealed for $name {}
};
($(#[$cfg:meta])? $name:ident <= $base:ident) => {
$(#[$cfg])?
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct $name { _inner: () }
$(#[$cfg])?
impl $name {
#[inline]
pub unsafe fn steal() -> Self {
Self { _inner: () }
}
#[doc = r"Pointer to the register block"]
pub const PTR: *const <super::pac::$base as core::ops::Deref>::Target = super::pac::$base::PTR;
#[doc = r"Return the pointer to the register block"]
#[inline(always)]
pub const fn ptr() -> *const <super::pac::$base as core::ops::Deref>::Target {
super::pac::$base::PTR
}
}
impl core::ops::Deref for $name {
type Target = <super::pac::$base 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::private::Sealed for $name {}
};
}
}