diff options
Diffstat (limited to 'embassy-extras/src/peripheral.rs')
| -rw-r--r-- | embassy-extras/src/peripheral.rs | 97 |
1 files changed, 91 insertions, 6 deletions
diff --git a/embassy-extras/src/peripheral.rs b/embassy-extras/src/peripheral.rs index 68972c543..92512a0f6 100644 --- a/embassy-extras/src/peripheral.rs +++ b/embassy-extras/src/peripheral.rs | |||
| @@ -2,9 +2,19 @@ use core::cell::UnsafeCell; | |||
| 2 | use core::marker::{PhantomData, PhantomPinned}; | 2 | use core::marker::{PhantomData, PhantomPinned}; |
| 3 | use core::pin::Pin; | 3 | use core::pin::Pin; |
| 4 | 4 | ||
| 5 | use cortex_m::peripheral::scb::VectActive; | ||
| 6 | use cortex_m::peripheral::{NVIC, SCB}; | ||
| 5 | use embassy::interrupt::{Interrupt, InterruptExt}; | 7 | use embassy::interrupt::{Interrupt, InterruptExt}; |
| 6 | 8 | ||
| 7 | pub trait PeripheralState { | 9 | /// A type which can be used as state with `PeripheralMutex`. |
| 10 | /// | ||
| 11 | /// It needs to be `Send` because `&mut` references are sent back and forth between the 'thread' which owns the `PeripheralMutex` and the interrupt, | ||
| 12 | /// and `&mut T` is only `Send` where `T: Send`. | ||
| 13 | /// | ||
| 14 | /// It also requires `'static` to be used safely with `PeripheralMutex::register_interrupt`, | ||
| 15 | /// because although `Pin` guarantees that the memory of the state won't be invalidated, | ||
| 16 | /// it doesn't guarantee that the lifetime will last. | ||
| 17 | pub trait PeripheralState: Send { | ||
| 8 | type Interrupt: Interrupt; | 18 | type Interrupt: Interrupt; |
| 9 | fn on_interrupt(&mut self); | 19 | fn on_interrupt(&mut self); |
| 10 | } | 20 | } |
| @@ -19,8 +29,51 @@ pub struct PeripheralMutex<S: PeripheralState> { | |||
| 19 | _pinned: PhantomPinned, | 29 | _pinned: PhantomPinned, |
| 20 | } | 30 | } |
| 21 | 31 | ||
| 32 | /// Whether `irq` can be preempted by the current interrupt. | ||
| 33 | pub(crate) fn can_be_preempted(irq: &impl Interrupt) -> bool { | ||
| 34 | match SCB::vect_active() { | ||
| 35 | // Thread mode can't preempt anything. | ||
| 36 | VectActive::ThreadMode => false, | ||
| 37 | // Exceptions don't always preempt interrupts, | ||
| 38 | // but there isn't much of a good reason to be keeping a `PeripheralMutex` in an exception anyway. | ||
| 39 | VectActive::Exception(_) => true, | ||
| 40 | VectActive::Interrupt { irqn } => { | ||
| 41 | #[derive(Clone, Copy)] | ||
| 42 | struct NrWrap(u16); | ||
| 43 | unsafe impl cortex_m::interrupt::InterruptNumber for NrWrap { | ||
| 44 | fn number(self) -> u16 { | ||
| 45 | self.0 | ||
| 46 | } | ||
| 47 | } | ||
| 48 | NVIC::get_priority(NrWrap(irqn.into())) < irq.get_priority().into() | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | impl<S: PeripheralState + 'static> PeripheralMutex<S> { | ||
| 54 | /// Registers `on_interrupt` as the wrapped interrupt's interrupt handler and enables it. | ||
| 55 | /// | ||
| 56 | /// This requires this `PeripheralMutex`'s `PeripheralState` to live for `'static`, | ||
| 57 | /// because `Pin` only guarantees that it's memory won't be repurposed, | ||
| 58 | /// not that it's lifetime will last. | ||
| 59 | /// | ||
| 60 | /// To use non-`'static` `PeripheralState`, use the unsafe `register_interrupt_unchecked`. | ||
| 61 | /// | ||
| 62 | /// Note: `'static` doesn't mean it _has_ to live for the entire program, like an `&'static T`; | ||
| 63 | /// it just means it _can_ live for the entire program - for example, `u8` lives for `'static`. | ||
| 64 | pub fn register_interrupt(self: Pin<&mut Self>) { | ||
| 65 | // SAFETY: `S: 'static`, so there's no way it's lifetime can expire. | ||
| 66 | unsafe { self.register_interrupt_unchecked() } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 22 | impl<S: PeripheralState> PeripheralMutex<S> { | 70 | impl<S: PeripheralState> PeripheralMutex<S> { |
| 71 | /// Create a new `PeripheralMutex` wrapping `irq`, with the initial state `state`. | ||
| 23 | pub fn new(state: S, irq: S::Interrupt) -> Self { | 72 | pub fn new(state: S, irq: S::Interrupt) -> Self { |
| 73 | if can_be_preempted(&irq) { | ||
| 74 | panic!("`PeripheralMutex` cannot be created in an interrupt with higher priority than the interrupt it wraps"); | ||
| 75 | } | ||
| 76 | |||
| 24 | Self { | 77 | Self { |
| 25 | irq, | 78 | irq, |
| 26 | irq_setup_done: false, | 79 | irq_setup_done: false, |
| @@ -31,8 +84,18 @@ impl<S: PeripheralState> PeripheralMutex<S> { | |||
| 31 | } | 84 | } |
| 32 | } | 85 | } |
| 33 | 86 | ||
| 34 | pub fn register_interrupt(self: Pin<&mut Self>) { | 87 | /// Registers `on_interrupt` as the wrapped interrupt's interrupt handler and enables it. |
| 35 | let this = unsafe { self.get_unchecked_mut() }; | 88 | /// |
| 89 | /// # Safety | ||
| 90 | /// The lifetime of any data in `PeripheralState` that is accessed by the interrupt handler | ||
| 91 | /// must not end without `Drop` being called on this `PeripheralMutex`. | ||
| 92 | /// | ||
| 93 | /// This can be accomplished by either not accessing any data with a lifetime in `on_interrupt`, | ||
| 94 | /// or making sure that nothing like `mem::forget` is used on the `PeripheralMutex`. | ||
| 95 | |||
| 96 | // TODO: this name isn't the best. | ||
| 97 | pub unsafe fn register_interrupt_unchecked(self: Pin<&mut Self>) { | ||
| 98 | let this = self.get_unchecked_mut(); | ||
| 36 | if this.irq_setup_done { | 99 | if this.irq_setup_done { |
| 37 | return; | 100 | return; |
| 38 | } | 101 | } |
| @@ -40,7 +103,9 @@ impl<S: PeripheralState> PeripheralMutex<S> { | |||
| 40 | this.irq.disable(); | 103 | this.irq.disable(); |
| 41 | this.irq.set_handler(|p| { | 104 | this.irq.set_handler(|p| { |
| 42 | // Safety: it's OK to get a &mut to the state, since | 105 | // Safety: it's OK to get a &mut to the state, since |
| 43 | // - We're in the IRQ, no one else can't preempt us | 106 | // - We checked that the thread owning the `PeripheralMutex` can't preempt us in `new`. |
| 107 | // Interrupts' priorities can only be changed with raw embassy `Interrupts`, | ||
| 108 | // which can't safely store a `PeripheralMutex` across invocations. | ||
| 44 | // - We can't have preempted a with() call because the irq is disabled during it. | 109 | // - We can't have preempted a with() call because the irq is disabled during it. |
| 45 | let state = unsafe { &mut *(p as *mut S) }; | 110 | let state = unsafe { &mut *(p as *mut S) }; |
| 46 | state.on_interrupt(); | 111 | state.on_interrupt(); |
| @@ -52,19 +117,39 @@ impl<S: PeripheralState> PeripheralMutex<S> { | |||
| 52 | this.irq_setup_done = true; | 117 | this.irq_setup_done = true; |
| 53 | } | 118 | } |
| 54 | 119 | ||
| 55 | pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R { | 120 | pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S) -> R) -> R { |
| 56 | let this = unsafe { self.get_unchecked_mut() }; | 121 | let this = unsafe { self.get_unchecked_mut() }; |
| 57 | 122 | ||
| 58 | this.irq.disable(); | 123 | this.irq.disable(); |
| 59 | 124 | ||
| 60 | // Safety: it's OK to get a &mut to the state, since the irq is disabled. | 125 | // Safety: it's OK to get a &mut to the state, since the irq is disabled. |
| 61 | let state = unsafe { &mut *this.state.get() }; | 126 | let state = unsafe { &mut *this.state.get() }; |
| 62 | let r = f(state, &mut this.irq); | 127 | let r = f(state); |
| 63 | 128 | ||
| 64 | this.irq.enable(); | 129 | this.irq.enable(); |
| 65 | 130 | ||
| 66 | r | 131 | r |
| 67 | } | 132 | } |
| 133 | |||
| 134 | /// Returns whether the wrapped interrupt is currently in a pending state. | ||
| 135 | pub fn is_pending(&self) -> bool { | ||
| 136 | self.irq.is_pending() | ||
| 137 | } | ||
| 138 | |||
| 139 | /// Forces the wrapped interrupt into a pending state. | ||
| 140 | pub fn pend(&self) { | ||
| 141 | self.irq.pend() | ||
| 142 | } | ||
| 143 | |||
| 144 | /// Forces the wrapped interrupt out of a pending state. | ||
| 145 | pub fn unpend(&self) { | ||
| 146 | self.irq.unpend() | ||
| 147 | } | ||
| 148 | |||
| 149 | /// Gets the priority of the wrapped interrupt. | ||
| 150 | pub fn priority(&self) -> <S::Interrupt as Interrupt>::Priority { | ||
| 151 | self.irq.get_priority() | ||
| 152 | } | ||
| 68 | } | 153 | } |
| 69 | 154 | ||
| 70 | impl<S: PeripheralState> Drop for PeripheralMutex<S> { | 155 | impl<S: PeripheralState> Drop for PeripheralMutex<S> { |
