diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-03-26 16:01:37 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-03-27 15:18:06 +0100 |
| commit | d41eeeae79388f219bf6a84e2f7bde9f6b532516 (patch) | |
| tree | 678b6fc732216e529dc38e6f65b72a309917ac32 /embassy-hal-internal/src | |
| parent | 9edf5b7f049f95742b60b041e4443967d8a6b708 (diff) | |
Remove Peripheral trait, rename PeripheralRef->Peri.
Diffstat (limited to 'embassy-hal-internal/src')
| -rw-r--r-- | embassy-hal-internal/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-hal-internal/src/macros.rs | 38 | ||||
| -rw-r--r-- | embassy-hal-internal/src/peripheral.rs | 151 |
3 files changed, 50 insertions, 141 deletions
diff --git a/embassy-hal-internal/src/lib.rs b/embassy-hal-internal/src/lib.rs index 89f20e993..7addb71e2 100644 --- a/embassy-hal-internal/src/lib.rs +++ b/embassy-hal-internal/src/lib.rs | |||
| @@ -11,7 +11,7 @@ pub mod drop; | |||
| 11 | mod macros; | 11 | mod macros; |
| 12 | mod peripheral; | 12 | mod peripheral; |
| 13 | pub mod ratio; | 13 | pub mod ratio; |
| 14 | pub use peripheral::{Peripheral, PeripheralRef}; | 14 | pub use peripheral::{Peri, PeripheralType}; |
| 15 | 15 | ||
| 16 | #[cfg(feature = "cortex-m")] | 16 | #[cfg(feature = "cortex-m")] |
| 17 | pub mod interrupt; | 17 | pub mod interrupt; |
diff --git a/embassy-hal-internal/src/macros.rs b/embassy-hal-internal/src/macros.rs index 07cd89487..cd2bc3cab 100644 --- a/embassy-hal-internal/src/macros.rs +++ b/embassy-hal-internal/src/macros.rs | |||
| @@ -18,8 +18,8 @@ macro_rules! peripherals_definition { | |||
| 18 | /// | 18 | /// |
| 19 | /// You must ensure that you're only using one instance of this type at a time. | 19 | /// You must ensure that you're only using one instance of this type at a time. |
| 20 | #[inline] | 20 | #[inline] |
| 21 | pub unsafe fn steal() -> Self { | 21 | pub unsafe fn steal() -> $crate::Peri<'static, Self> { |
| 22 | Self{ _private: ()} | 22 | $crate::Peri::new_unchecked(Self{ _private: ()}) |
| 23 | } | 23 | } |
| 24 | } | 24 | } |
| 25 | 25 | ||
| @@ -42,7 +42,7 @@ macro_rules! peripherals_struct { | |||
| 42 | $( | 42 | $( |
| 43 | #[doc = concat!(stringify!($name), " peripheral")] | 43 | #[doc = concat!(stringify!($name), " peripheral")] |
| 44 | $(#[$cfg])? | 44 | $(#[$cfg])? |
| 45 | pub $name: peripherals::$name, | 45 | pub $name: $crate::Peri<'static, peripherals::$name>, |
| 46 | )* | 46 | )* |
| 47 | } | 47 | } |
| 48 | 48 | ||
| @@ -108,28 +108,26 @@ macro_rules! peripherals { | |||
| 108 | }; | 108 | }; |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | /// Convenience converting into reference. | ||
| 112 | #[macro_export] | ||
| 113 | macro_rules! into_ref { | ||
| 114 | ($($name:ident),*) => { | ||
| 115 | $( | ||
| 116 | let mut $name = $name.into_ref(); | ||
| 117 | )* | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | /// Implement the peripheral trait. | 111 | /// Implement the peripheral trait. |
| 122 | #[macro_export] | 112 | #[macro_export] |
| 123 | macro_rules! impl_peripheral { | 113 | macro_rules! impl_peripheral { |
| 124 | ($type:ident) => { | 114 | ($type:ident<$($T:ident $(: $bound:tt $(+ $others:tt )*)?),*>) => { |
| 125 | impl $crate::Peripheral for $type { | 115 | impl<$($T: $($bound $(+$others)*)?),*> Copy for $type <$($T),*> {} |
| 126 | type P = $type; | 116 | impl<$($T: $($bound $(+$others)*)?),*> Clone for $type <$($T),*> { |
| 117 | fn clone(&self) -> Self { | ||
| 118 | *self | ||
| 119 | } | ||
| 120 | } | ||
| 121 | impl<$($T: $($bound $(+$others)*)?),*> PeripheralType for $type <$($T),*> {} | ||
| 122 | }; | ||
| 127 | 123 | ||
| 128 | #[inline] | 124 | ($type:ident) => { |
| 129 | unsafe fn clone_unchecked(&self) -> Self::P { | 125 | impl Copy for $type {} |
| 130 | #[allow(clippy::needless_update)] | 126 | impl Clone for $type { |
| 131 | $type { ..*self } | 127 | fn clone(&self) -> Self { |
| 128 | *self | ||
| 132 | } | 129 | } |
| 133 | } | 130 | } |
| 131 | impl $crate::PeripheralType for $type {} | ||
| 134 | }; | 132 | }; |
| 135 | } | 133 | } |
diff --git a/embassy-hal-internal/src/peripheral.rs b/embassy-hal-internal/src/peripheral.rs index 0b0f13338..803259bb8 100644 --- a/embassy-hal-internal/src/peripheral.rs +++ b/embassy-hal-internal/src/peripheral.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | use core::ops::{Deref, DerefMut}; | 2 | use core::ops::Deref; |
| 3 | 3 | ||
| 4 | /// An exclusive reference to a peripheral. | 4 | /// An exclusive reference to a peripheral. |
| 5 | /// | 5 | /// |
| @@ -9,20 +9,26 @@ use core::ops::{Deref, DerefMut}; | |||
| 9 | /// - Memory efficiency: Peripheral singletons are typically either zero-sized (for concrete | 9 | /// - Memory efficiency: Peripheral singletons are typically either zero-sized (for concrete |
| 10 | /// peripherals like `PA9` or `SPI4`) or very small (for example `AnyPin`, which is 1 byte). | 10 | /// peripherals like `PA9` or `SPI4`) or very small (for example `AnyPin`, which is 1 byte). |
| 11 | /// However `&mut T` is always 4 bytes for 32-bit targets, even if T is zero-sized. | 11 | /// However `&mut T` is always 4 bytes for 32-bit targets, even if T is zero-sized. |
| 12 | /// PeripheralRef stores a copy of `T` instead, so it's the same size. | 12 | /// Peripheral stores a copy of `T` instead, so it's the same size. |
| 13 | /// - Code size efficiency. If the user uses the same driver with both `SPI4` and `&mut SPI4`, | 13 | /// - Code size efficiency. If the user uses the same driver with both `SPI4` and `&mut SPI4`, |
| 14 | /// the driver code would be monomorphized two times. With PeripheralRef, the driver is generic | 14 | /// the driver code would be monomorphized two times. With Peri, the driver is generic |
| 15 | /// over a lifetime only. `SPI4` becomes `PeripheralRef<'static, SPI4>`, and `&mut SPI4` becomes | 15 | /// over a lifetime only. `SPI4` becomes `Peri<'static, SPI4>`, and `&mut SPI4` becomes |
| 16 | /// `PeripheralRef<'a, SPI4>`. Lifetimes don't cause monomorphization. | 16 | /// `Peri<'a, SPI4>`. Lifetimes don't cause monomorphization. |
| 17 | pub struct PeripheralRef<'a, T> { | 17 | pub struct Peri<'a, T: PeripheralType> { |
| 18 | inner: T, | 18 | inner: T, |
| 19 | _lifetime: PhantomData<&'a mut T>, | 19 | _lifetime: PhantomData<&'a mut T>, |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | impl<'a, T> PeripheralRef<'a, T> { | 22 | impl<'a, T: PeripheralType> Peri<'a, T> { |
| 23 | /// Create a new reference to a peripheral. | 23 | /// Create a new owned a peripheral. |
| 24 | /// | ||
| 25 | /// For use by HALs only. | ||
| 26 | /// | ||
| 27 | /// If you're an end user you shouldn't use this, you should use `steal()` | ||
| 28 | /// on the actual peripheral types instead. | ||
| 24 | #[inline] | 29 | #[inline] |
| 25 | pub fn new(inner: T) -> Self { | 30 | #[doc(hidden)] |
| 31 | pub unsafe fn new_unchecked(inner: T) -> Self { | ||
| 26 | Self { | 32 | Self { |
| 27 | inner, | 33 | inner, |
| 28 | _lifetime: PhantomData, | 34 | _lifetime: PhantomData, |
| @@ -38,46 +44,38 @@ impl<'a, T> PeripheralRef<'a, T> { | |||
| 38 | /// create two SPI drivers on `SPI1`, because they will "fight" each other. | 44 | /// create two SPI drivers on `SPI1`, because they will "fight" each other. |
| 39 | /// | 45 | /// |
| 40 | /// You should strongly prefer using `reborrow()` instead. It returns a | 46 | /// You should strongly prefer using `reborrow()` instead. It returns a |
| 41 | /// `PeripheralRef` that borrows `self`, which allows the borrow checker | 47 | /// `Peri` that borrows `self`, which allows the borrow checker |
| 42 | /// to enforce this at compile time. | 48 | /// to enforce this at compile time. |
| 43 | pub unsafe fn clone_unchecked(&self) -> PeripheralRef<'a, T> | 49 | pub unsafe fn clone_unchecked(&self) -> Peri<'a, T> { |
| 44 | where | 50 | Peri::new_unchecked(self.inner) |
| 45 | T: Peripheral<P = T>, | ||
| 46 | { | ||
| 47 | PeripheralRef::new(self.inner.clone_unchecked()) | ||
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | /// Reborrow into a "child" PeripheralRef. | 53 | /// Reborrow into a "child" Peri. |
| 51 | /// | 54 | /// |
| 52 | /// `self` will stay borrowed until the child PeripheralRef is dropped. | 55 | /// `self` will stay borrowed until the child Peripheral is dropped. |
| 53 | pub fn reborrow(&mut self) -> PeripheralRef<'_, T> | 56 | pub fn reborrow(&mut self) -> Peri<'_, T> { |
| 54 | where | 57 | // safety: we're returning the clone inside a new Peripheral that borrows |
| 55 | T: Peripheral<P = T>, | ||
| 56 | { | ||
| 57 | // safety: we're returning the clone inside a new PeripheralRef that borrows | ||
| 58 | // self, so user code can't use both at the same time. | 58 | // self, so user code can't use both at the same time. |
| 59 | PeripheralRef::new(unsafe { self.inner.clone_unchecked() }) | 59 | unsafe { self.clone_unchecked() } |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | /// Map the inner peripheral using `Into`. | 62 | /// Map the inner peripheral using `Into`. |
| 63 | /// | 63 | /// |
| 64 | /// This converts from `PeripheralRef<'a, T>` to `PeripheralRef<'a, U>`, using an | 64 | /// This converts from `Peri<'a, T>` to `Peri<'a, U>`, using an |
| 65 | /// `Into` impl to convert from `T` to `U`. | 65 | /// `Into` impl to convert from `T` to `U`. |
| 66 | /// | 66 | /// |
| 67 | /// For example, this can be useful to degrade GPIO pins: converting from PeripheralRef<'a, PB11>` to `PeripheralRef<'a, AnyPin>`. | 67 | /// For example, this can be useful to.into() GPIO pins: converting from Peri<'a, PB11>` to `Peri<'a, AnyPin>`. |
| 68 | #[inline] | 68 | #[inline] |
| 69 | pub fn map_into<U>(self) -> PeripheralRef<'a, U> | 69 | pub fn into<U>(self) -> Peri<'a, U> |
| 70 | where | 70 | where |
| 71 | T: Into<U>, | 71 | T: Into<U>, |
| 72 | U: PeripheralType, | ||
| 72 | { | 73 | { |
| 73 | PeripheralRef { | 74 | unsafe { Peri::new_unchecked(self.inner.into()) } |
| 74 | inner: self.inner.into(), | ||
| 75 | _lifetime: PhantomData, | ||
| 76 | } | ||
| 77 | } | 75 | } |
| 78 | } | 76 | } |
| 79 | 77 | ||
| 80 | impl<'a, T> Deref for PeripheralRef<'a, T> { | 78 | impl<'a, T: PeripheralType> Deref for Peri<'a, T> { |
| 81 | type Target = T; | 79 | type Target = T; |
| 82 | 80 | ||
| 83 | #[inline] | 81 | #[inline] |
| @@ -86,92 +84,5 @@ impl<'a, T> Deref for PeripheralRef<'a, T> { | |||
| 86 | } | 84 | } |
| 87 | } | 85 | } |
| 88 | 86 | ||
| 89 | /// Trait for any type that can be used as a peripheral of type `P`. | 87 | /// Marker trait for peripheral types. |
| 90 | /// | 88 | pub trait PeripheralType: Copy + Sized {} |
| 91 | /// This is used in driver constructors, to allow passing either owned peripherals (e.g. `TWISPI0`), | ||
| 92 | /// or borrowed peripherals (e.g. `&mut TWISPI0`). | ||
| 93 | /// | ||
| 94 | /// For example, if you have a driver with a constructor like this: | ||
| 95 | /// | ||
| 96 | /// ```ignore | ||
| 97 | /// impl<'d, T: Instance> Twim<'d, T> { | ||
| 98 | /// pub fn new( | ||
| 99 | /// twim: impl Peripheral<P = T> + 'd, | ||
| 100 | /// irq: impl Peripheral<P = T::Interrupt> + 'd, | ||
| 101 | /// sda: impl Peripheral<P = impl GpioPin> + 'd, | ||
| 102 | /// scl: impl Peripheral<P = impl GpioPin> + 'd, | ||
| 103 | /// config: Config, | ||
| 104 | /// ) -> Self { .. } | ||
| 105 | /// } | ||
| 106 | /// ``` | ||
| 107 | /// | ||
| 108 | /// You may call it with owned peripherals, which yields an instance that can live forever (`'static`): | ||
| 109 | /// | ||
| 110 | /// ```ignore | ||
| 111 | /// let mut twi: Twim<'static, ...> = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); | ||
| 112 | /// ``` | ||
| 113 | /// | ||
| 114 | /// Or you may call it with borrowed peripherals, which yields an instance that can only live for as long | ||
| 115 | /// as the borrows last: | ||
| 116 | /// | ||
| 117 | /// ```ignore | ||
| 118 | /// let mut twi: Twim<'_, ...> = Twim::new(&mut p.TWISPI0, &mut irq, &mut p.P0_03, &mut p.P0_04, config); | ||
| 119 | /// ``` | ||
| 120 | /// | ||
| 121 | /// # Implementation details, for HAL authors | ||
| 122 | /// | ||
| 123 | /// When writing a HAL, the intended way to use this trait is to take `impl Peripheral<P = ..>` in | ||
| 124 | /// the HAL's public API (such as driver constructors), calling `.into_ref()` to obtain a `PeripheralRef`, | ||
| 125 | /// and storing that in the driver struct. | ||
| 126 | /// | ||
| 127 | /// `.into_ref()` on an owned `T` yields a `PeripheralRef<'static, T>`. | ||
| 128 | /// `.into_ref()` on an `&'a mut T` yields a `PeripheralRef<'a, T>`. | ||
| 129 | pub trait Peripheral: Sized { | ||
| 130 | /// Peripheral singleton type | ||
| 131 | type P; | ||
| 132 | |||
| 133 | /// Unsafely clone (duplicate) a peripheral singleton. | ||
| 134 | /// | ||
| 135 | /// # Safety | ||
| 136 | /// | ||
| 137 | /// This returns an owned clone of the peripheral. You must manually ensure | ||
| 138 | /// only one copy of the peripheral is in use at a time. For example, don't | ||
| 139 | /// create two SPI drivers on `SPI1`, because they will "fight" each other. | ||
| 140 | /// | ||
| 141 | /// You should strongly prefer using `into_ref()` instead. It returns a | ||
| 142 | /// `PeripheralRef`, which allows the borrow checker to enforce this at compile time. | ||
| 143 | unsafe fn clone_unchecked(&self) -> Self::P; | ||
| 144 | |||
| 145 | /// Convert a value into a `PeripheralRef`. | ||
| 146 | /// | ||
| 147 | /// When called on an owned `T`, yields a `PeripheralRef<'static, T>`. | ||
| 148 | /// When called on an `&'a mut T`, yields a `PeripheralRef<'a, T>`. | ||
| 149 | #[inline] | ||
| 150 | fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P> | ||
| 151 | where | ||
| 152 | Self: 'a, | ||
| 153 | { | ||
| 154 | PeripheralRef::new(unsafe { self.clone_unchecked() }) | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | impl<'b, T: DerefMut> Peripheral for T | ||
| 159 | where | ||
| 160 | T::Target: Peripheral, | ||
| 161 | { | ||
| 162 | type P = <T::Target as Peripheral>::P; | ||
| 163 | |||
| 164 | #[inline] | ||
| 165 | unsafe fn clone_unchecked(&self) -> Self::P { | ||
| 166 | T::Target::clone_unchecked(self) | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | impl<'b, T: Peripheral> Peripheral for PeripheralRef<'_, T> { | ||
| 171 | type P = T::P; | ||
| 172 | |||
| 173 | #[inline] | ||
| 174 | unsafe fn clone_unchecked(&self) -> Self::P { | ||
| 175 | T::clone_unchecked(self) | ||
| 176 | } | ||
| 177 | } | ||
