diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-07-28 13:23:22 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-28 13:23:22 +0200 |
| commit | 036e6ae30c9e772ef8ef20439f121e108b9106f0 (patch) | |
| tree | 7c654de04304274a11d44733b51c5012aeec9e3c /embassy-hal-internal/src/macros.rs | |
| parent | 0ced8400d00da30abe76438ef26224c7ed649708 (diff) | |
Rename embassy-hal-common to embassy-hal-internal, document it's for internal use only. (#1700)
Diffstat (limited to 'embassy-hal-internal/src/macros.rs')
| -rw-r--r-- | embassy-hal-internal/src/macros.rs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/embassy-hal-internal/src/macros.rs b/embassy-hal-internal/src/macros.rs new file mode 100644 index 000000000..f06b46002 --- /dev/null +++ b/embassy-hal-internal/src/macros.rs | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | #[macro_export] | ||
| 2 | macro_rules! peripherals_definition { | ||
| 3 | ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { | ||
| 4 | /// Types for the peripheral singletons. | ||
| 5 | pub mod peripherals { | ||
| 6 | $( | ||
| 7 | $(#[$cfg])? | ||
| 8 | #[allow(non_camel_case_types)] | ||
| 9 | #[doc = concat!(stringify!($name), " peripheral")] | ||
| 10 | pub struct $name { _private: () } | ||
| 11 | |||
| 12 | $(#[$cfg])? | ||
| 13 | impl $name { | ||
| 14 | /// Unsafely create an instance of this peripheral out of thin air. | ||
| 15 | /// | ||
| 16 | /// # Safety | ||
| 17 | /// | ||
| 18 | /// You must ensure that you're only using one instance of this type at a time. | ||
| 19 | #[inline] | ||
| 20 | pub unsafe fn steal() -> Self { | ||
| 21 | Self{ _private: ()} | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | $(#[$cfg])? | ||
| 26 | $crate::impl_peripheral!($name); | ||
| 27 | )* | ||
| 28 | } | ||
| 29 | }; | ||
| 30 | } | ||
| 31 | |||
| 32 | #[macro_export] | ||
| 33 | macro_rules! peripherals_struct { | ||
| 34 | ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { | ||
| 35 | /// Struct containing all the peripheral singletons. | ||
| 36 | /// | ||
| 37 | /// To obtain the peripherals, you must initialize the HAL, by calling [`crate::init`]. | ||
| 38 | #[allow(non_snake_case)] | ||
| 39 | pub struct Peripherals { | ||
| 40 | $( | ||
| 41 | #[doc = concat!(stringify!($name), " peripheral")] | ||
| 42 | $(#[$cfg])? | ||
| 43 | pub $name: peripherals::$name, | ||
| 44 | )* | ||
| 45 | } | ||
| 46 | |||
| 47 | impl Peripherals { | ||
| 48 | ///Returns all the peripherals *once* | ||
| 49 | #[inline] | ||
| 50 | pub(crate) fn take() -> Self { | ||
| 51 | |||
| 52 | #[no_mangle] | ||
| 53 | static mut _EMBASSY_DEVICE_PERIPHERALS: bool = false; | ||
| 54 | |||
| 55 | critical_section::with(|_| unsafe { | ||
| 56 | if _EMBASSY_DEVICE_PERIPHERALS { | ||
| 57 | panic!("init called more than once!") | ||
| 58 | } | ||
| 59 | _EMBASSY_DEVICE_PERIPHERALS = true; | ||
| 60 | Self::steal() | ||
| 61 | }) | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | impl Peripherals { | ||
| 66 | /// Unsafely create an instance of this peripheral out of thin air. | ||
| 67 | /// | ||
| 68 | /// # Safety | ||
| 69 | /// | ||
| 70 | /// You must ensure that you're only using one instance of this type at a time. | ||
| 71 | #[inline] | ||
| 72 | pub unsafe fn steal() -> Self { | ||
| 73 | Self { | ||
| 74 | $( | ||
| 75 | $(#[$cfg])? | ||
| 76 | $name: peripherals::$name::steal(), | ||
| 77 | )* | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | |||
| 84 | #[macro_export] | ||
| 85 | macro_rules! peripherals { | ||
| 86 | ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { | ||
| 87 | $crate::peripherals_definition!( | ||
| 88 | $( | ||
| 89 | $(#[$cfg])? | ||
| 90 | $name, | ||
| 91 | )* | ||
| 92 | ); | ||
| 93 | $crate::peripherals_struct!( | ||
| 94 | $( | ||
| 95 | $(#[$cfg])? | ||
| 96 | $name, | ||
| 97 | )* | ||
| 98 | ); | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | #[macro_export] | ||
| 103 | macro_rules! into_ref { | ||
| 104 | ($($name:ident),*) => { | ||
| 105 | $( | ||
| 106 | let mut $name = $name.into_ref(); | ||
| 107 | )* | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | #[macro_export] | ||
| 112 | macro_rules! impl_peripheral { | ||
| 113 | ($type:ident) => { | ||
| 114 | impl $crate::Peripheral for $type { | ||
| 115 | type P = $type; | ||
| 116 | |||
| 117 | #[inline] | ||
| 118 | unsafe fn clone_unchecked(&self) -> Self::P { | ||
| 119 | $type { ..*self } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | } | ||
