diff options
| -rw-r--r-- | embassy-extras/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-extras/src/peripheral.rs | 50 | ||||
| -rw-r--r-- | embassy-extras/src/peripheral_shared.rs | 45 | ||||
| -rw-r--r-- | embassy-extras/src/usb/mod.rs | 13 | ||||
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 5 |
5 files changed, 86 insertions, 28 deletions
diff --git a/embassy-extras/Cargo.toml b/embassy-extras/Cargo.toml index 5d07901a9..8415e32ef 100644 --- a/embassy-extras/Cargo.toml +++ b/embassy-extras/Cargo.toml | |||
| @@ -17,4 +17,5 @@ embassy = { version = "0.1.0", path = "../embassy" } | |||
| 17 | defmt = { version = "0.2.0", optional = true } | 17 | defmt = { version = "0.2.0", optional = true } |
| 18 | log = { version = "0.4.11", optional = true } | 18 | log = { version = "0.4.11", optional = true } |
| 19 | cortex-m = "0.7.1" | 19 | cortex-m = "0.7.1" |
| 20 | critical-section = "0.2.1" | ||
| 20 | usb-device = "0.2.7" | 21 | usb-device = "0.2.7" |
diff --git a/embassy-extras/src/peripheral.rs b/embassy-extras/src/peripheral.rs index 68972c543..e3e06d692 100644 --- a/embassy-extras/src/peripheral.rs +++ b/embassy-extras/src/peripheral.rs | |||
| @@ -1,15 +1,38 @@ | |||
| 1 | use core::cell::UnsafeCell; | 1 | 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 | use core::ptr; | ||
| 4 | 5 | ||
| 5 | use embassy::interrupt::{Interrupt, InterruptExt}; | 6 | use embassy::interrupt::{Interrupt, InterruptExt}; |
| 6 | 7 | ||
| 7 | pub trait PeripheralState { | 8 | /// # Safety |
| 9 | /// When types implementing this trait are used with `Peripheral` or `PeripheralMutex`, | ||
| 10 | /// their lifetime must not end without first calling `Drop` on the `Peripheral` or `PeripheralMutex`. | ||
| 11 | pub unsafe trait PeripheralStateUnchecked { | ||
| 8 | type Interrupt: Interrupt; | 12 | type Interrupt: Interrupt; |
| 9 | fn on_interrupt(&mut self); | 13 | fn on_interrupt(&mut self); |
| 10 | } | 14 | } |
| 11 | 15 | ||
| 12 | pub struct PeripheralMutex<S: PeripheralState> { | 16 | // `PeripheralMutex` is safe because `Pin` guarantees that the memory it references will not be invalidated or reused |
| 17 | // without calling `Drop`. However, it provides no guarantees about references contained within the state still being valid, | ||
| 18 | // so this `'static` bound is necessary. | ||
| 19 | pub trait PeripheralState: 'static { | ||
| 20 | type Interrupt: Interrupt; | ||
| 21 | fn on_interrupt(&mut self); | ||
| 22 | } | ||
| 23 | |||
| 24 | // SAFETY: `T` has to live for `'static` to implement `PeripheralState`, thus its lifetime cannot end. | ||
| 25 | unsafe impl<T> PeripheralStateUnchecked for T | ||
| 26 | where | ||
| 27 | T: PeripheralState, | ||
| 28 | { | ||
| 29 | type Interrupt = T::Interrupt; | ||
| 30 | fn on_interrupt(&mut self) { | ||
| 31 | self.on_interrupt() | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | pub struct PeripheralMutex<S: PeripheralStateUnchecked> { | ||
| 13 | state: UnsafeCell<S>, | 36 | state: UnsafeCell<S>, |
| 14 | 37 | ||
| 15 | irq_setup_done: bool, | 38 | irq_setup_done: bool, |
| @@ -19,7 +42,7 @@ pub struct PeripheralMutex<S: PeripheralState> { | |||
| 19 | _pinned: PhantomPinned, | 42 | _pinned: PhantomPinned, |
| 20 | } | 43 | } |
| 21 | 44 | ||
| 22 | impl<S: PeripheralState> PeripheralMutex<S> { | 45 | impl<S: PeripheralStateUnchecked> PeripheralMutex<S> { |
| 23 | pub fn new(state: S, irq: S::Interrupt) -> Self { | 46 | pub fn new(state: S, irq: S::Interrupt) -> Self { |
| 24 | Self { | 47 | Self { |
| 25 | irq, | 48 | irq, |
| @@ -39,11 +62,17 @@ impl<S: PeripheralState> PeripheralMutex<S> { | |||
| 39 | 62 | ||
| 40 | this.irq.disable(); | 63 | this.irq.disable(); |
| 41 | this.irq.set_handler(|p| { | 64 | this.irq.set_handler(|p| { |
| 42 | // Safety: it's OK to get a &mut to the state, since | 65 | critical_section::with(|_| { |
| 43 | // - We're in the IRQ, no one else can't preempt us | 66 | if p.is_null() { |
| 44 | // - We can't have preempted a with() call because the irq is disabled during it. | 67 | // The state was dropped, so we can't operate on it. |
| 45 | let state = unsafe { &mut *(p as *mut S) }; | 68 | return; |
| 46 | state.on_interrupt(); | 69 | } |
| 70 | // Safety: it's OK to get a &mut to the state, since | ||
| 71 | // - We're in a critical section, no one can preempt us (and call with()) | ||
| 72 | // - We can't have preempted a with() call because the irq is disabled during it. | ||
| 73 | let state = unsafe { &mut *(p as *mut S) }; | ||
| 74 | state.on_interrupt(); | ||
| 75 | }) | ||
| 47 | }); | 76 | }); |
| 48 | this.irq | 77 | this.irq |
| 49 | .set_handler_context((&mut this.state) as *mut _ as *mut ()); | 78 | .set_handler_context((&mut this.state) as *mut _ as *mut ()); |
| @@ -67,9 +96,12 @@ impl<S: PeripheralState> PeripheralMutex<S> { | |||
| 67 | } | 96 | } |
| 68 | } | 97 | } |
| 69 | 98 | ||
| 70 | impl<S: PeripheralState> Drop for PeripheralMutex<S> { | 99 | impl<S: PeripheralStateUnchecked> Drop for PeripheralMutex<S> { |
| 71 | fn drop(&mut self) { | 100 | fn drop(&mut self) { |
| 72 | self.irq.disable(); | 101 | self.irq.disable(); |
| 73 | self.irq.remove_handler(); | 102 | self.irq.remove_handler(); |
| 103 | // Set the context to null so that the interrupt will know we're dropped | ||
| 104 | // if we pre-empted it before it entered a critical section. | ||
| 105 | self.irq.set_handler_context(ptr::null_mut()); | ||
| 74 | } | 106 | } |
| 75 | } | 107 | } |
diff --git a/embassy-extras/src/peripheral_shared.rs b/embassy-extras/src/peripheral_shared.rs index c62113396..a9fca8ca6 100644 --- a/embassy-extras/src/peripheral_shared.rs +++ b/embassy-extras/src/peripheral_shared.rs | |||
| @@ -1,16 +1,27 @@ | |||
| 1 | use core::cell::UnsafeCell; | ||
| 2 | use core::marker::{PhantomData, PhantomPinned}; | 1 | use core::marker::{PhantomData, PhantomPinned}; |
| 3 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::ptr; | ||
| 4 | 4 | ||
| 5 | use embassy::interrupt::{Interrupt, InterruptExt}; | 5 | use embassy::interrupt::{Interrupt, InterruptExt}; |
| 6 | 6 | ||
| 7 | pub trait PeripheralState { | 7 | /// # Safety |
| 8 | /// When types implementing this trait are used with `Peripheral` or `PeripheralMutex`, | ||
| 9 | /// their lifetime must not end without first calling `Drop` on the `Peripheral` or `PeripheralMutex`. | ||
| 10 | pub unsafe trait PeripheralStateUnchecked { | ||
| 8 | type Interrupt: Interrupt; | 11 | type Interrupt: Interrupt; |
| 9 | fn on_interrupt(&self); | 12 | fn on_interrupt(&self); |
| 10 | } | 13 | } |
| 11 | 14 | ||
| 12 | pub struct Peripheral<S: PeripheralState> { | 15 | // `Peripheral` is safe because `Pin` guarantees that the memory it references will not be invalidated or reused |
| 13 | state: UnsafeCell<S>, | 16 | // without calling `Drop`. However, it provides no guarantees about references contained within the state still being valid, |
| 17 | // so this `'static` bound is necessary. | ||
| 18 | pub trait PeripheralState: 'static { | ||
| 19 | type Interrupt: Interrupt; | ||
| 20 | fn on_interrupt(&self); | ||
| 21 | } | ||
| 22 | |||
| 23 | pub struct Peripheral<S: PeripheralStateUnchecked> { | ||
| 24 | state: S, | ||
| 14 | 25 | ||
| 15 | irq_setup_done: bool, | 26 | irq_setup_done: bool, |
| 16 | irq: S::Interrupt, | 27 | irq: S::Interrupt, |
| @@ -19,13 +30,13 @@ pub struct Peripheral<S: PeripheralState> { | |||
| 19 | _pinned: PhantomPinned, | 30 | _pinned: PhantomPinned, |
| 20 | } | 31 | } |
| 21 | 32 | ||
| 22 | impl<S: PeripheralState> Peripheral<S> { | 33 | impl<S: PeripheralStateUnchecked> Peripheral<S> { |
| 23 | pub fn new(irq: S::Interrupt, state: S) -> Self { | 34 | pub fn new(irq: S::Interrupt, state: S) -> Self { |
| 24 | Self { | 35 | Self { |
| 25 | irq, | 36 | irq, |
| 26 | irq_setup_done: false, | 37 | irq_setup_done: false, |
| 27 | 38 | ||
| 28 | state: UnsafeCell::new(state), | 39 | state, |
| 29 | _not_send: PhantomData, | 40 | _not_send: PhantomData, |
| 30 | _pinned: PhantomPinned, | 41 | _pinned: PhantomPinned, |
| 31 | } | 42 | } |
| @@ -39,8 +50,16 @@ impl<S: PeripheralState> Peripheral<S> { | |||
| 39 | 50 | ||
| 40 | this.irq.disable(); | 51 | this.irq.disable(); |
| 41 | this.irq.set_handler(|p| { | 52 | this.irq.set_handler(|p| { |
| 42 | let state = unsafe { &*(p as *const S) }; | 53 | // We need to be in a critical section so that no one can preempt us |
| 43 | state.on_interrupt(); | 54 | // and drop the state after we check whether `p.is_null()`. |
| 55 | critical_section::with(|_| { | ||
| 56 | if p.is_null() { | ||
| 57 | // The state was dropped, so we can't operate on it. | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | let state = unsafe { &*(p as *const S) }; | ||
| 61 | state.on_interrupt(); | ||
| 62 | }); | ||
| 44 | }); | 63 | }); |
| 45 | this.irq | 64 | this.irq |
| 46 | .set_handler_context((&this.state) as *const _ as *mut ()); | 65 | .set_handler_context((&this.state) as *const _ as *mut ()); |
| @@ -49,15 +68,17 @@ impl<S: PeripheralState> Peripheral<S> { | |||
| 49 | this.irq_setup_done = true; | 68 | this.irq_setup_done = true; |
| 50 | } | 69 | } |
| 51 | 70 | ||
| 52 | pub fn state(self: Pin<&mut Self>) -> &S { | 71 | pub fn state<'a>(self: Pin<&'a mut Self>) -> &'a S { |
| 53 | let this = unsafe { self.get_unchecked_mut() }; | 72 | &self.into_ref().get_ref().state |
| 54 | unsafe { &*this.state.get() } | ||
| 55 | } | 73 | } |
| 56 | } | 74 | } |
| 57 | 75 | ||
| 58 | impl<S: PeripheralState> Drop for Peripheral<S> { | 76 | impl<S: PeripheralStateUnchecked> Drop for Peripheral<S> { |
| 59 | fn drop(&mut self) { | 77 | fn drop(&mut self) { |
| 60 | self.irq.disable(); | 78 | self.irq.disable(); |
| 61 | self.irq.remove_handler(); | 79 | self.irq.remove_handler(); |
| 80 | // Set the context to null so that the interrupt will know we're dropped | ||
| 81 | // if we pre-empted it before it entered a critical section. | ||
| 82 | self.irq.set_handler_context(ptr::null_mut()); | ||
| 62 | } | 83 | } |
| 63 | } | 84 | } |
diff --git a/embassy-extras/src/usb/mod.rs b/embassy-extras/src/usb/mod.rs index 182cd87d0..330eb9220 100644 --- a/embassy-extras/src/usb/mod.rs +++ b/embassy-extras/src/usb/mod.rs | |||
| @@ -9,7 +9,7 @@ use usb_device::device::UsbDevice; | |||
| 9 | mod cdc_acm; | 9 | mod cdc_acm; |
| 10 | pub mod usb_serial; | 10 | pub mod usb_serial; |
| 11 | 11 | ||
| 12 | use crate::peripheral::{PeripheralMutex, PeripheralState}; | 12 | use crate::peripheral::{PeripheralMutex, PeripheralStateUnchecked}; |
| 13 | use embassy::interrupt::Interrupt; | 13 | use embassy::interrupt::Interrupt; |
| 14 | use usb_serial::{ReadInterface, UsbSerial, WriteInterface}; | 14 | use usb_serial::{ReadInterface, UsbSerial, WriteInterface}; |
| 15 | 15 | ||
| @@ -55,10 +55,12 @@ where | |||
| 55 | } | 55 | } |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | pub fn start(self: Pin<&mut Self>) { | 58 | /// # Safety |
| 59 | let this = unsafe { self.get_unchecked_mut() }; | 59 | /// The `UsbDevice` passed to `Self::new` must not be dropped without calling `Drop` on this `Usb` first. |
| 60 | pub unsafe fn start(self: Pin<&mut Self>) { | ||
| 61 | let this = self.get_unchecked_mut(); | ||
| 60 | let mut mutex = this.inner.borrow_mut(); | 62 | let mut mutex = this.inner.borrow_mut(); |
| 61 | let mutex = unsafe { Pin::new_unchecked(&mut *mutex) }; | 63 | let mutex = Pin::new_unchecked(&mut *mutex); |
| 62 | 64 | ||
| 63 | // Use inner to register the irq | 65 | // Use inner to register the irq |
| 64 | mutex.register_interrupt(); | 66 | mutex.register_interrupt(); |
| @@ -125,7 +127,8 @@ where | |||
| 125 | } | 127 | } |
| 126 | } | 128 | } |
| 127 | 129 | ||
| 128 | impl<'bus, B, T, I> PeripheralState for State<'bus, B, T, I> | 130 | // SAFETY: The safety contract of `PeripheralStateUnchecked` is forwarded to `Usb::start`. |
| 131 | unsafe impl<'bus, B, T, I> PeripheralStateUnchecked for State<'bus, B, T, I> | ||
| 129 | where | 132 | where |
| 130 | B: UsbBus, | 133 | B: UsbBus, |
| 131 | T: ClassSet<B>, | 134 | T: ClassSet<B>, |
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index a5a37b982..1fa98a6b2 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -7,7 +7,7 @@ use core::task::{Context, Poll}; | |||
| 7 | use embassy::interrupt::InterruptExt; | 7 | use embassy::interrupt::InterruptExt; |
| 8 | use embassy::io::{AsyncBufRead, AsyncWrite, Result}; | 8 | use embassy::io::{AsyncBufRead, AsyncWrite, Result}; |
| 9 | use embassy::util::{Unborrow, WakerRegistration}; | 9 | use embassy::util::{Unborrow, WakerRegistration}; |
| 10 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; | 10 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralStateUnchecked}; |
| 11 | use embassy_extras::ring_buffer::RingBuffer; | 11 | use embassy_extras::ring_buffer::RingBuffer; |
| 12 | use embassy_extras::{low_power_wait_until, unborrow}; | 12 | use embassy_extras::{low_power_wait_until, unborrow}; |
| 13 | 13 | ||
| @@ -283,7 +283,8 @@ impl<'a, U: UarteInstance, T: TimerInstance> Drop for State<'a, U, T> { | |||
| 283 | } | 283 | } |
| 284 | } | 284 | } |
| 285 | 285 | ||
| 286 | impl<'a, U: UarteInstance, T: TimerInstance> PeripheralState for State<'a, U, T> { | 286 | // SAFETY: the safety contract of `PeripheralStateUnchecked` is forwarded to `BufferedUarte::new`. |
| 287 | unsafe impl<'a, U: UarteInstance, T: TimerInstance> PeripheralStateUnchecked for State<'a, U, T> { | ||
| 287 | type Interrupt = U::Interrupt; | 288 | type Interrupt = U::Interrupt; |
| 288 | fn on_interrupt(&mut self) { | 289 | fn on_interrupt(&mut self) { |
| 289 | trace!("irq: start"); | 290 | trace!("irq: start"); |
