diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-03-21 21:58:59 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-03-29 00:58:57 +0200 |
| commit | a134fce122d570cfcd3837944554fff6c35e4039 (patch) | |
| tree | a6d2e80e94c6f230dd4c713406605466c1c1537c /embassy-extras | |
| parent | d5ff1a0ae30db8963eec1b7f23b90991e4f47c3d (diff) | |
nrf: nicer Peripherals api, add take()
Diffstat (limited to 'embassy-extras')
| -rw-r--r-- | embassy-extras/src/macros.rs | 72 |
1 files changed, 51 insertions, 21 deletions
diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index 151f410af..478549ac0 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs | |||
| @@ -1,41 +1,71 @@ | |||
| 1 | #[macro_export] | 1 | #[macro_export] |
| 2 | macro_rules! peripherals { | 2 | macro_rules! peripherals { |
| 3 | ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => { | 3 | ($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => { |
| 4 | $( | 4 | pub mod peripherals { |
| 5 | $(#[$cfg])? | 5 | $( |
| 6 | #[allow(non_camel_case_types)] | 6 | $(#[$cfg])? |
| 7 | pub struct $type { _private: () } | 7 | #[allow(non_camel_case_types)] |
| 8 | 8 | pub struct $type { _private: () } | |
| 9 | $(#[$cfg])? | 9 | |
| 10 | impl embassy::util::PeripheralBorrow for $type { | 10 | impl embassy::util::Steal for $type { |
| 11 | type Target = $type; | 11 | #[inline] |
| 12 | unsafe fn unborrow(self) -> $type { | 12 | unsafe fn steal() -> Self { |
| 13 | self | 13 | Self{ _private: ()} |
| 14 | } | ||
| 14 | } | 15 | } |
| 15 | } | ||
| 16 | 16 | ||
| 17 | $(#[$cfg])? | 17 | $(#[$cfg])? |
| 18 | impl embassy::util::PeripheralBorrow for &mut $type { | 18 | impl embassy::util::PeripheralBorrow for $type { |
| 19 | type Target = $type; | 19 | type Target = $type; |
| 20 | unsafe fn unborrow(self) -> $type { | 20 | #[inline] |
| 21 | ::core::ptr::read(self) | 21 | unsafe fn unborrow(self) -> $type { |
| 22 | self | ||
| 23 | } | ||
| 22 | } | 24 | } |
| 23 | } | 25 | |
| 24 | )* | 26 | $(#[$cfg])? |
| 27 | impl embassy::util::PeripheralBorrow for &mut $type { | ||
| 28 | type Target = $type; | ||
| 29 | #[inline] | ||
| 30 | unsafe fn unborrow(self) -> $type { | ||
| 31 | ::core::ptr::read(self) | ||
| 32 | } | ||
| 33 | } | ||
| 34 | )* | ||
| 35 | } | ||
| 25 | 36 | ||
| 26 | pub struct Peripherals { | 37 | pub struct Peripherals { |
| 27 | $( | 38 | $( |
| 28 | $(#[$cfg])? | 39 | $(#[$cfg])? |
| 29 | pub $name: $type, | 40 | pub $name: peripherals::$type, |
| 30 | )* | 41 | )* |
| 31 | } | 42 | } |
| 32 | 43 | ||
| 33 | impl Peripherals { | 44 | impl Peripherals { |
| 34 | pub unsafe fn steal() -> Self { | 45 | ///Returns all the peripherals *once* |
| 46 | #[inline] | ||
| 47 | pub fn take() -> Option<Self> { | ||
| 48 | |||
| 49 | #[no_mangle] | ||
| 50 | static mut _EMBASSY_DEVICE_PERIPHERALS: bool = false; | ||
| 51 | |||
| 52 | cortex_m::interrupt::free(|_| { | ||
| 53 | if unsafe { _EMBASSY_DEVICE_PERIPHERALS } { | ||
| 54 | None | ||
| 55 | } else { | ||
| 56 | Some(unsafe { <Self as embassy::util::Steal>::steal() }) | ||
| 57 | } | ||
| 58 | }) | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | impl embassy::util::Steal for Peripherals { | ||
| 63 | #[inline] | ||
| 64 | unsafe fn steal() -> Self { | ||
| 35 | Self { | 65 | Self { |
| 36 | $( | 66 | $( |
| 37 | $(#[$cfg])? | 67 | $(#[$cfg])? |
| 38 | $name: $type { _private: () }, | 68 | $name: <peripherals::$type as embassy::util::Steal>::steal(), |
| 39 | )* | 69 | )* |
| 40 | } | 70 | } |
| 41 | } | 71 | } |
