diff options
Diffstat (limited to 'embassy-nrf/src/timer.rs')
| -rw-r--r-- | embassy-nrf/src/timer.rs | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index d7a7c4d70..8deecdc03 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs | |||
| @@ -5,7 +5,7 @@ use core::task::Poll; | |||
| 5 | 5 | ||
| 6 | use embassy::waitqueue::AtomicWaker; | 6 | use embassy::waitqueue::AtomicWaker; |
| 7 | use embassy_hal_common::drop::OnDrop; | 7 | use embassy_hal_common::drop::OnDrop; |
| 8 | use embassy_hal_common::into_ref; | 8 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 9 | use futures::future::poll_fn; | 9 | use futures::future::poll_fn; |
| 10 | 10 | ||
| 11 | use crate::interrupt::{Interrupt, InterruptExt}; | 11 | use crate::interrupt::{Interrupt, InterruptExt}; |
| @@ -95,7 +95,8 @@ impl TimerType for Awaitable {} | |||
| 95 | impl TimerType for NotAwaitable {} | 95 | impl TimerType for NotAwaitable {} |
| 96 | 96 | ||
| 97 | pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> { | 97 | pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> { |
| 98 | phantom: PhantomData<(&'d mut T, I)>, | 98 | _p: PeripheralRef<'d, T>, |
| 99 | _i: PhantomData<I>, | ||
| 99 | } | 100 | } |
| 100 | 101 | ||
| 101 | impl<'d, T: Instance> Timer<'d, T, Awaitable> { | 102 | impl<'d, T: Instance> Timer<'d, T, Awaitable> { |
| @@ -123,10 +124,15 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> { | |||
| 123 | /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. | 124 | /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. |
| 124 | /// | 125 | /// |
| 125 | /// This is used by the public constructors. | 126 | /// This is used by the public constructors. |
| 126 | fn new_irqless(_timer: impl Peripheral<P = T> + 'd) -> Self { | 127 | fn new_irqless(timer: impl Peripheral<P = T> + 'd) -> Self { |
| 128 | into_ref!(timer); | ||
| 129 | |||
| 127 | let regs = T::regs(); | 130 | let regs = T::regs(); |
| 128 | 131 | ||
| 129 | let mut this = Self { phantom: PhantomData }; | 132 | let mut this = Self { |
| 133 | _p: timer, | ||
| 134 | _i: PhantomData, | ||
| 135 | }; | ||
| 130 | 136 | ||
| 131 | // Stop the timer before doing anything else, | 137 | // Stop the timer before doing anything else, |
| 132 | // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. | 138 | // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. |
| @@ -230,7 +236,8 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> { | |||
| 230 | } | 236 | } |
| 231 | Cc { | 237 | Cc { |
| 232 | n, | 238 | n, |
| 233 | phantom: PhantomData, | 239 | _p: self._p.reborrow(), |
| 240 | _i: PhantomData, | ||
| 234 | } | 241 | } |
| 235 | } | 242 | } |
| 236 | } | 243 | } |
| @@ -242,12 +249,13 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> { | |||
| 242 | /// | 249 | /// |
| 243 | /// The timer will fire the register's COMPARE event when its counter reaches the value stored in the register. | 250 | /// The timer will fire the register's COMPARE event when its counter reaches the value stored in the register. |
| 244 | /// When the register's CAPTURE task is triggered, the timer will store the current value of its counter in the register | 251 | /// When the register's CAPTURE task is triggered, the timer will store the current value of its counter in the register |
| 245 | pub struct Cc<'a, T: Instance, I: TimerType = NotAwaitable> { | 252 | pub struct Cc<'d, T: Instance, I: TimerType = NotAwaitable> { |
| 246 | n: usize, | 253 | n: usize, |
| 247 | phantom: PhantomData<(&'a mut T, I)>, | 254 | _p: PeripheralRef<'d, T>, |
| 255 | _i: PhantomData<I>, | ||
| 248 | } | 256 | } |
| 249 | 257 | ||
| 250 | impl<'a, T: Instance> Cc<'a, T, Awaitable> { | 258 | impl<'d, T: Instance> Cc<'d, T, Awaitable> { |
| 251 | /// Wait until the timer's counter reaches the value stored in this register. | 259 | /// Wait until the timer's counter reaches the value stored in this register. |
| 252 | /// | 260 | /// |
| 253 | /// This requires a mutable reference so that this task's waker cannot be overwritten by a second call to `wait`. | 261 | /// This requires a mutable reference so that this task's waker cannot be overwritten by a second call to `wait`. |
| @@ -281,9 +289,9 @@ impl<'a, T: Instance> Cc<'a, T, Awaitable> { | |||
| 281 | on_drop.defuse(); | 289 | on_drop.defuse(); |
| 282 | } | 290 | } |
| 283 | } | 291 | } |
| 284 | impl<'a, T: Instance> Cc<'a, T, NotAwaitable> {} | 292 | impl<'d, T: Instance> Cc<'d, T, NotAwaitable> {} |
| 285 | 293 | ||
| 286 | impl<'a, T: Instance, I: TimerType> Cc<'a, T, I> { | 294 | impl<'d, T: Instance, I: TimerType> Cc<'d, T, I> { |
| 287 | /// Get the current value stored in the register. | 295 | /// Get the current value stored in the register. |
| 288 | pub fn read(&self) -> u32 { | 296 | pub fn read(&self) -> u32 { |
| 289 | T::regs().cc[self.n].read().cc().bits() | 297 | T::regs().cc[self.n].read().cc().bits() |
