diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-02-01 00:48:33 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-02-01 01:17:41 +0100 |
| commit | b5cf332cc076a0de11ce6a0563a2235c9e57eb5c (patch) | |
| tree | ce14e014dfbe8c3764040d7f9f1ffee84ab5747b /embassy-nrf/src/timer.rs | |
| parent | ca10fe7135d10084e38038f3cd433da39e505bea (diff) | |
nrf: docs.
Diffstat (limited to 'embassy-nrf/src/timer.rs')
| -rw-r--r-- | embassy-nrf/src/timer.rs | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index bc8710640..d1ae57237 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | //! Timer driver. | ||
| 2 | //! | ||
| 3 | //! Important note! This driver is very low level. For most time-related use cases, like | ||
| 4 | //! "sleep for X seconds", "do something every X seconds", or measuring time, you should | ||
| 5 | //! use [`embassy-time`](https://crates.io/crates/embassy-time) instead! | ||
| 6 | |||
| 1 | #![macro_use] | 7 | #![macro_use] |
| 2 | 8 | ||
| 3 | use core::future::poll_fn; | 9 | use core::future::poll_fn; |
| @@ -28,9 +34,13 @@ pub(crate) mod sealed { | |||
| 28 | pub trait TimerType {} | 34 | pub trait TimerType {} |
| 29 | } | 35 | } |
| 30 | 36 | ||
| 37 | /// Basic Timer instance. | ||
| 31 | pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { | 38 | pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { |
| 39 | /// Interrupt for this peripheral. | ||
| 32 | type Interrupt: Interrupt; | 40 | type Interrupt: Interrupt; |
| 33 | } | 41 | } |
| 42 | |||
| 43 | /// Extended timer instance. | ||
| 34 | pub trait ExtendedInstance: Instance + sealed::ExtendedInstance {} | 44 | pub trait ExtendedInstance: Instance + sealed::ExtendedInstance {} |
| 35 | 45 | ||
| 36 | macro_rules! impl_timer { | 46 | macro_rules! impl_timer { |
| @@ -61,18 +71,28 @@ macro_rules! impl_timer { | |||
| 61 | }; | 71 | }; |
| 62 | } | 72 | } |
| 63 | 73 | ||
| 74 | /// Timer frequency | ||
| 64 | #[repr(u8)] | 75 | #[repr(u8)] |
| 65 | pub enum Frequency { | 76 | pub enum Frequency { |
| 66 | // I'd prefer not to prefix these with `F`, but Rust identifiers can't start with digits. | 77 | /// 16MHz |
| 67 | F16MHz = 0, | 78 | F16MHz = 0, |
| 79 | /// 8MHz | ||
| 68 | F8MHz = 1, | 80 | F8MHz = 1, |
| 81 | /// 4MHz | ||
| 69 | F4MHz = 2, | 82 | F4MHz = 2, |
| 83 | /// 2MHz | ||
| 70 | F2MHz = 3, | 84 | F2MHz = 3, |
| 85 | /// 1MHz | ||
| 71 | F1MHz = 4, | 86 | F1MHz = 4, |
| 87 | /// 500kHz | ||
| 72 | F500kHz = 5, | 88 | F500kHz = 5, |
| 89 | /// 250kHz | ||
| 73 | F250kHz = 6, | 90 | F250kHz = 6, |
| 91 | /// 125kHz | ||
| 74 | F125kHz = 7, | 92 | F125kHz = 7, |
| 93 | /// 62500Hz | ||
| 75 | F62500Hz = 8, | 94 | F62500Hz = 8, |
| 95 | /// 31250Hz | ||
| 76 | F31250Hz = 9, | 96 | F31250Hz = 9, |
| 77 | } | 97 | } |
| 78 | 98 | ||
| @@ -86,7 +106,10 @@ pub enum Frequency { | |||
| 86 | 106 | ||
| 87 | pub trait TimerType: sealed::TimerType {} | 107 | pub trait TimerType: sealed::TimerType {} |
| 88 | 108 | ||
| 109 | /// Marker type indicating the timer driver can await expiration (it owns the timer interrupt). | ||
| 89 | pub enum Awaitable {} | 110 | pub enum Awaitable {} |
| 111 | |||
| 112 | /// Marker type indicating the timer driver cannot await expiration (it does not own the timer interrupt). | ||
| 90 | pub enum NotAwaitable {} | 113 | pub enum NotAwaitable {} |
| 91 | 114 | ||
| 92 | impl sealed::TimerType for Awaitable {} | 115 | impl sealed::TimerType for Awaitable {} |
| @@ -94,12 +117,14 @@ impl sealed::TimerType for NotAwaitable {} | |||
| 94 | impl TimerType for Awaitable {} | 117 | impl TimerType for Awaitable {} |
| 95 | impl TimerType for NotAwaitable {} | 118 | impl TimerType for NotAwaitable {} |
| 96 | 119 | ||
| 120 | /// Timer driver. | ||
| 97 | pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> { | 121 | pub struct Timer<'d, T: Instance, I: TimerType = NotAwaitable> { |
| 98 | _p: PeripheralRef<'d, T>, | 122 | _p: PeripheralRef<'d, T>, |
| 99 | _i: PhantomData<I>, | 123 | _i: PhantomData<I>, |
| 100 | } | 124 | } |
| 101 | 125 | ||
| 102 | impl<'d, T: Instance> Timer<'d, T, Awaitable> { | 126 | impl<'d, T: Instance> Timer<'d, T, Awaitable> { |
| 127 | /// Create a new async-capable timer driver. | ||
| 103 | pub fn new_awaitable(timer: impl Peripheral<P = T> + 'd, irq: impl Peripheral<P = T::Interrupt> + 'd) -> Self { | 128 | pub fn new_awaitable(timer: impl Peripheral<P = T> + 'd, irq: impl Peripheral<P = T::Interrupt> + 'd) -> Self { |
| 104 | into_ref!(irq); | 129 | into_ref!(irq); |
| 105 | 130 | ||
| @@ -107,16 +132,17 @@ impl<'d, T: Instance> Timer<'d, T, Awaitable> { | |||
| 107 | irq.unpend(); | 132 | irq.unpend(); |
| 108 | irq.enable(); | 133 | irq.enable(); |
| 109 | 134 | ||
| 110 | Self::new_irqless(timer) | 135 | Self::new_inner(timer) |
| 111 | } | 136 | } |
| 112 | } | 137 | } |
| 138 | |||
| 113 | impl<'d, T: Instance> Timer<'d, T, NotAwaitable> { | 139 | impl<'d, T: Instance> Timer<'d, T, NotAwaitable> { |
| 114 | /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. | 140 | /// Create a `Timer` driver without an interrupt, meaning `Cc::wait` won't work. |
| 115 | /// | 141 | /// |
| 116 | /// This can be useful for triggering tasks via PPI | 142 | /// This can be useful for triggering tasks via PPI |
| 117 | /// `Uarte` uses this internally. | 143 | /// `Uarte` uses this internally. |
| 118 | pub fn new(timer: impl Peripheral<P = T> + 'd) -> Self { | 144 | pub fn new(timer: impl Peripheral<P = T> + 'd) -> Self { |
| 119 | Self::new_irqless(timer) | 145 | Self::new_inner(timer) |
| 120 | } | 146 | } |
| 121 | } | 147 | } |
| 122 | 148 | ||
| @@ -124,7 +150,7 @@ impl<'d, T: Instance, I: TimerType> Timer<'d, T, I> { | |||
| 124 | /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. | 150 | /// Create a `Timer` without an interrupt, meaning `Cc::wait` won't work. |
| 125 | /// | 151 | /// |
| 126 | /// This is used by the public constructors. | 152 | /// This is used by the public constructors. |
| 127 | fn new_irqless(timer: impl Peripheral<P = T> + 'd) -> Self { | 153 | fn new_inner(timer: impl Peripheral<P = T> + 'd) -> Self { |
| 128 | into_ref!(timer); | 154 | into_ref!(timer); |
| 129 | 155 | ||
| 130 | let regs = T::regs(); | 156 | let regs = T::regs(); |
