nrf: nicer Peripherals api, add take()
This commit is contained in:
		@@ -1,41 +1,71 @@
 | 
			
		||||
#[macro_export]
 | 
			
		||||
macro_rules! peripherals {
 | 
			
		||||
    ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => {
 | 
			
		||||
        $(
 | 
			
		||||
            $(#[$cfg])?
 | 
			
		||||
            #[allow(non_camel_case_types)]
 | 
			
		||||
            pub struct $type { _private: () }
 | 
			
		||||
        pub mod peripherals {
 | 
			
		||||
            $(
 | 
			
		||||
                $(#[$cfg])?
 | 
			
		||||
                #[allow(non_camel_case_types)]
 | 
			
		||||
                pub struct $type { _private: () }
 | 
			
		||||
 | 
			
		||||
            $(#[$cfg])?
 | 
			
		||||
            impl embassy::util::PeripheralBorrow for $type {
 | 
			
		||||
                type Target = $type;
 | 
			
		||||
                unsafe fn unborrow(self) -> $type {
 | 
			
		||||
                    self
 | 
			
		||||
                impl embassy::util::Steal for $type {
 | 
			
		||||
                    #[inline]
 | 
			
		||||
                    unsafe fn steal() -> Self {
 | 
			
		||||
                        Self{ _private: ()}
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $(#[$cfg])?
 | 
			
		||||
            impl embassy::util::PeripheralBorrow for &mut $type {
 | 
			
		||||
                type Target = $type;
 | 
			
		||||
                unsafe fn unborrow(self) -> $type {
 | 
			
		||||
                    ::core::ptr::read(self)
 | 
			
		||||
                $(#[$cfg])?
 | 
			
		||||
                impl embassy::util::PeripheralBorrow for $type {
 | 
			
		||||
                    type Target = $type;
 | 
			
		||||
                    #[inline]
 | 
			
		||||
                    unsafe fn unborrow(self) -> $type {
 | 
			
		||||
                        self
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        )*
 | 
			
		||||
 | 
			
		||||
                $(#[$cfg])?
 | 
			
		||||
                impl embassy::util::PeripheralBorrow for &mut $type {
 | 
			
		||||
                    type Target = $type;
 | 
			
		||||
                    #[inline]
 | 
			
		||||
                    unsafe fn unborrow(self) -> $type {
 | 
			
		||||
                        ::core::ptr::read(self)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            )*
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        pub struct Peripherals {
 | 
			
		||||
            $(
 | 
			
		||||
                $(#[$cfg])?
 | 
			
		||||
                pub $name: $type,
 | 
			
		||||
                pub $name: peripherals::$type,
 | 
			
		||||
            )*
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        impl Peripherals {
 | 
			
		||||
            pub unsafe fn steal() -> Self {
 | 
			
		||||
            ///Returns all the peripherals *once*
 | 
			
		||||
            #[inline]
 | 
			
		||||
            pub fn take() -> Option<Self> {
 | 
			
		||||
 | 
			
		||||
                #[no_mangle]
 | 
			
		||||
                static mut _EMBASSY_DEVICE_PERIPHERALS: bool = false;
 | 
			
		||||
 | 
			
		||||
                cortex_m::interrupt::free(|_| {
 | 
			
		||||
                    if unsafe { _EMBASSY_DEVICE_PERIPHERALS } {
 | 
			
		||||
                        None
 | 
			
		||||
                    } else {
 | 
			
		||||
                        Some(unsafe { <Self as embassy::util::Steal>::steal() })
 | 
			
		||||
                    }
 | 
			
		||||
                })
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        impl embassy::util::Steal for Peripherals {
 | 
			
		||||
            #[inline]
 | 
			
		||||
            unsafe fn steal() -> Self {
 | 
			
		||||
                Self {
 | 
			
		||||
                    $(
 | 
			
		||||
                        $(#[$cfg])?
 | 
			
		||||
                        $name: $type { _private: () },
 | 
			
		||||
                        $name: <peripherals::$type as embassy::util::Steal>::steal(),
 | 
			
		||||
                    )*
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user