diff options
| author | Cameron <[email protected]> | 2023-06-29 18:09:26 +0200 |
|---|---|---|
| committer | Cameron <[email protected]> | 2023-06-29 18:09:26 +0200 |
| commit | 24e186e6849223ee076e0fabdfed07d2cd7f7baf (patch) | |
| tree | 0d52ab5639e8bd5d1e7200c8076baa4a8500078c /embassy-nrf | |
| parent | 3f19879f41e8c39c5c38e7905a180160b24807fc (diff) | |
feature(1354): Added lifetimes to Event +
Diffstat (limited to 'embassy-nrf')
| -rw-r--r-- | embassy-nrf/src/ppi/mod.rs | 29 | ||||
| -rw-r--r-- | embassy-nrf/src/ppi/ppi.rs | 4 |
2 files changed, 20 insertions, 13 deletions
diff --git a/embassy-nrf/src/ppi/mod.rs b/embassy-nrf/src/ppi/mod.rs index 76757a248..f6771c2a3 100644 --- a/embassy-nrf/src/ppi/mod.rs +++ b/embassy-nrf/src/ppi/mod.rs | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | //! many tasks and events, but any single task or event can only be coupled with one channel. | 15 | //! many tasks and events, but any single task or event can only be coupled with one channel. |
| 16 | //! | 16 | //! |
| 17 | 17 | ||
| 18 | use core::marker::PhantomData; | ||
| 18 | use core::ptr::NonNull; | 19 | use core::ptr::NonNull; |
| 19 | 20 | ||
| 20 | use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef}; | 21 | use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef}; |
| @@ -125,16 +126,16 @@ const REGISTER_DPPI_CONFIG_OFFSET: usize = 0x80 / core::mem::size_of::<u32>(); | |||
| 125 | /// When a task is subscribed to a PPI channel, it will run when the channel is triggered by | 126 | /// When a task is subscribed to a PPI channel, it will run when the channel is triggered by |
| 126 | /// a published event. | 127 | /// a published event. |
| 127 | #[derive(PartialEq, Eq, Clone, Copy)] | 128 | #[derive(PartialEq, Eq, Clone, Copy)] |
| 128 | pub struct Task(NonNull<u32>); | 129 | pub struct Task<'d>(NonNull<u32>, PhantomData<&'d ()>); |
| 129 | 130 | ||
| 130 | impl Task { | 131 | impl<'d> Task<'_> { |
| 131 | /// Create a new `Task` from a task register pointer | 132 | /// Create a new `Task` from a task register pointer |
| 132 | /// | 133 | /// |
| 133 | /// # Safety | 134 | /// # Safety |
| 134 | /// | 135 | /// |
| 135 | /// `ptr` must be a pointer to a valid `TASKS_*` register from an nRF peripheral. | 136 | /// `ptr` must be a pointer to a valid `TASKS_*` register from an nRF peripheral. |
| 136 | pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self { | 137 | pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self { |
| 137 | Self(ptr) | 138 | Self(ptr, PhantomData) |
| 138 | } | 139 | } |
| 139 | 140 | ||
| 140 | /// Triggers this task. | 141 | /// Triggers this task. |
| @@ -143,7 +144,10 @@ impl Task { | |||
| 143 | } | 144 | } |
| 144 | 145 | ||
| 145 | pub(crate) fn from_reg<T>(reg: &T) -> Self { | 146 | pub(crate) fn from_reg<T>(reg: &T) -> Self { |
| 146 | Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) }) | 147 | Self( |
| 148 | unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) }, | ||
| 149 | PhantomData, | ||
| 150 | ) | ||
| 147 | } | 151 | } |
| 148 | 152 | ||
| 149 | /// Address of subscription register for this task. | 153 | /// Address of subscription register for this task. |
| @@ -156,26 +160,29 @@ impl Task { | |||
| 156 | /// # Safety | 160 | /// # Safety |
| 157 | /// | 161 | /// |
| 158 | /// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core. | 162 | /// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core. |
| 159 | unsafe impl Send for Task {} | 163 | unsafe impl Send for Task<'_> {} |
| 160 | 164 | ||
| 161 | /// Represents an event that a peripheral can publish. | 165 | /// Represents an event that a peripheral can publish. |
| 162 | /// | 166 | /// |
| 163 | /// An event can be set to publish on a PPI channel when the event happens. | 167 | /// An event can be set to publish on a PPI channel when the event happens. |
| 164 | #[derive(PartialEq, Eq, Clone, Copy)] | 168 | #[derive(PartialEq, Eq, Clone, Copy)] |
| 165 | pub struct Event(NonNull<u32>); | 169 | pub struct Event<'d>(NonNull<u32>, PhantomData<&'d ()>); |
| 166 | 170 | ||
| 167 | impl Event { | 171 | impl<'d> Event<'_> { |
| 168 | /// Create a new `Event` from an event register pointer | 172 | /// Create a new `Event` from an event register pointer |
| 169 | /// | 173 | /// |
| 170 | /// # Safety | 174 | /// # Safety |
| 171 | /// | 175 | /// |
| 172 | /// `ptr` must be a pointer to a valid `EVENTS_*` register from an nRF peripheral. | 176 | /// `ptr` must be a pointer to a valid `EVENTS_*` register from an nRF peripheral. |
| 173 | pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self { | 177 | pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self { |
| 174 | Self(ptr) | 178 | Self(ptr, PhantomData) |
| 175 | } | 179 | } |
| 176 | 180 | ||
| 177 | pub(crate) fn from_reg<T>(reg: &T) -> Self { | 181 | pub(crate) fn from_reg<T>(reg: &'d T) -> Self { |
| 178 | Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) }) | 182 | Self( |
| 183 | unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) }, | ||
| 184 | PhantomData, | ||
| 185 | ) | ||
| 179 | } | 186 | } |
| 180 | 187 | ||
| 181 | /// Describes whether this Event is currently in a triggered state. | 188 | /// Describes whether this Event is currently in a triggered state. |
| @@ -198,7 +205,7 @@ impl Event { | |||
| 198 | /// # Safety | 205 | /// # Safety |
| 199 | /// | 206 | /// |
| 200 | /// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core. | 207 | /// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core. |
| 201 | unsafe impl Send for Event {} | 208 | unsafe impl Send for Event<'_> {} |
| 202 | 209 | ||
| 203 | // ====================== | 210 | // ====================== |
| 204 | // traits | 211 | // traits |
diff --git a/embassy-nrf/src/ppi/ppi.rs b/embassy-nrf/src/ppi/ppi.rs index f1eeaee1e..6e8a669d3 100644 --- a/embassy-nrf/src/ppi/ppi.rs +++ b/embassy-nrf/src/ppi/ppi.rs | |||
| @@ -3,12 +3,12 @@ use embassy_hal_common::into_ref; | |||
| 3 | use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task}; | 3 | use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task}; |
| 4 | use crate::{pac, Peripheral}; | 4 | use crate::{pac, Peripheral}; |
| 5 | 5 | ||
| 6 | impl Task { | 6 | impl<'d> Task<'_> { |
| 7 | fn reg_val(&self) -> u32 { | 7 | fn reg_val(&self) -> u32 { |
| 8 | self.0.as_ptr() as _ | 8 | self.0.as_ptr() as _ |
| 9 | } | 9 | } |
| 10 | } | 10 | } |
| 11 | impl Event { | 11 | impl<'d> Event<'_> { |
| 12 | fn reg_val(&self) -> u32 { | 12 | fn reg_val(&self) -> u32 { |
| 13 | self.0.as_ptr() as _ | 13 | self.0.as_ptr() as _ |
| 14 | } | 14 | } |
