diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-01-06 22:48:54 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-01-06 22:48:54 +0100 |
| commit | deb3c9389202c2e98e092ba7ea9b16f39af006d2 (patch) | |
| tree | f2dcdeb3b30bdba3251f508238eeb799badef8c6 | |
| parent | 77bdb5428ee47c50fc1cd24aa9005494a6f37e12 (diff) | |
Simpliify PeripheralMutex a bit.
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 14 | ||||
| -rw-r--r-- | embassy-nrf/src/util/peripheral.rs | 27 |
2 files changed, 22 insertions, 19 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 030fecf87..ceb52916f 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -61,7 +61,7 @@ struct State<'a, U: Instance> { | |||
| 61 | /// - nrf52832: Section 15.2 | 61 | /// - nrf52832: Section 15.2 |
| 62 | /// - nrf52840: Section 6.1.2 | 62 | /// - nrf52840: Section 6.1.2 |
| 63 | pub struct BufferedUarte<'a, U: Instance> { | 63 | pub struct BufferedUarte<'a, U: Instance> { |
| 64 | inner: PeripheralMutex<U::Interrupt, State<'a, U>>, | 64 | inner: PeripheralMutex<State<'a, U>>, |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | impl<'a, U: Instance> Unpin for BufferedUarte<'a, U> {} | 67 | impl<'a, U: Instance> Unpin for BufferedUarte<'a, U> {} |
| @@ -143,7 +143,6 @@ impl<'a, U: Instance> BufferedUarte<'a, U> { | |||
| 143 | 143 | ||
| 144 | BufferedUarte { | 144 | BufferedUarte { |
| 145 | inner: PeripheralMutex::new( | 145 | inner: PeripheralMutex::new( |
| 146 | irq, | ||
| 147 | State { | 146 | State { |
| 148 | inner: uarte, | 147 | inner: uarte, |
| 149 | 148 | ||
| @@ -155,11 +154,12 @@ impl<'a, U: Instance> BufferedUarte<'a, U> { | |||
| 155 | tx_state: TxState::Idle, | 154 | tx_state: TxState::Idle, |
| 156 | tx_waker: WakerRegistration::new(), | 155 | tx_waker: WakerRegistration::new(), |
| 157 | }, | 156 | }, |
| 157 | irq, | ||
| 158 | ), | 158 | ), |
| 159 | } | 159 | } |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<U::Interrupt, State<'a, U>>> { | 162 | fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<State<'a, U>>> { |
| 163 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) } | 163 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) } |
| 164 | } | 164 | } |
| 165 | } | 165 | } |
| @@ -173,7 +173,7 @@ impl<'a, U: Instance> Drop for BufferedUarte<'a, U> { | |||
| 173 | 173 | ||
| 174 | impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> { | 174 | impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> { |
| 175 | fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { | 175 | fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { |
| 176 | self.inner().with(|_irq, state| { | 176 | self.inner().with(|state, _irq| { |
| 177 | // Conservative compiler fence to prevent optimizations that do not | 177 | // Conservative compiler fence to prevent optimizations that do not |
| 178 | // take in to account actions by DMA. The fence has been placed here, | 178 | // take in to account actions by DMA. The fence has been placed here, |
| 179 | // before any DMA action has started | 179 | // before any DMA action has started |
| @@ -203,7 +203,7 @@ impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> { | |||
| 203 | } | 203 | } |
| 204 | 204 | ||
| 205 | fn consume(self: Pin<&mut Self>, amt: usize) { | 205 | fn consume(self: Pin<&mut Self>, amt: usize) { |
| 206 | self.inner().with(|irq, state| { | 206 | self.inner().with(|state, irq| { |
| 207 | trace!("consume {:?}", amt); | 207 | trace!("consume {:?}", amt); |
| 208 | state.rx.pop(amt); | 208 | state.rx.pop(amt); |
| 209 | irq.pend(); | 209 | irq.pend(); |
| @@ -213,7 +213,7 @@ impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> { | |||
| 213 | 213 | ||
| 214 | impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> { | 214 | impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> { |
| 215 | fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { | 215 | fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { |
| 216 | self.inner().with(|irq, state| { | 216 | self.inner().with(|state, irq| { |
| 217 | trace!("poll_write: {:?}", buf.len()); | 217 | trace!("poll_write: {:?}", buf.len()); |
| 218 | 218 | ||
| 219 | let tx_buf = state.tx.push_buf(); | 219 | let tx_buf = state.tx.push_buf(); |
| @@ -242,6 +242,8 @@ impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> { | |||
| 242 | } | 242 | } |
| 243 | 243 | ||
| 244 | impl<'a, U: Instance> PeripheralState for State<'a, U> { | 244 | impl<'a, U: Instance> PeripheralState for State<'a, U> { |
| 245 | type Interrupt = U::Interrupt; | ||
| 246 | |||
| 245 | fn on_interrupt(&mut self) { | 247 | fn on_interrupt(&mut self) { |
| 246 | trace!("irq: start"); | 248 | trace!("irq: start"); |
| 247 | let mut more_work = true; | 249 | let mut more_work = true; |
diff --git a/embassy-nrf/src/util/peripheral.rs b/embassy-nrf/src/util/peripheral.rs index 07dc4a7bc..e0edbf2b7 100644 --- a/embassy-nrf/src/util/peripheral.rs +++ b/embassy-nrf/src/util/peripheral.rs | |||
| @@ -6,25 +6,26 @@ use crate::fmt::*; | |||
| 6 | use crate::interrupt::OwnedInterrupt; | 6 | use crate::interrupt::OwnedInterrupt; |
| 7 | 7 | ||
| 8 | pub trait PeripheralState { | 8 | pub trait PeripheralState { |
| 9 | type Interrupt: OwnedInterrupt; | ||
| 9 | fn on_interrupt(&mut self); | 10 | fn on_interrupt(&mut self); |
| 10 | } | 11 | } |
| 11 | 12 | ||
| 12 | pub struct PeripheralMutex<I: OwnedInterrupt, S: PeripheralState> { | 13 | pub struct PeripheralMutex<S: PeripheralState> { |
| 13 | inner: Option<(I, UnsafeCell<S>)>, | 14 | inner: Option<(UnsafeCell<S>, S::Interrupt)>, |
| 14 | not_send: PhantomData<*mut ()>, | 15 | not_send: PhantomData<*mut ()>, |
| 15 | } | 16 | } |
| 16 | 17 | ||
| 17 | impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> { | 18 | impl<S: PeripheralState> PeripheralMutex<S> { |
| 18 | pub fn new(irq: I, state: S) -> Self { | 19 | pub fn new(state: S, irq: S::Interrupt) -> Self { |
| 19 | Self { | 20 | Self { |
| 20 | inner: Some((irq, UnsafeCell::new(state))), | 21 | inner: Some((UnsafeCell::new(state), irq)), |
| 21 | not_send: PhantomData, | 22 | not_send: PhantomData, |
| 22 | } | 23 | } |
| 23 | } | 24 | } |
| 24 | 25 | ||
| 25 | pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut I, &mut S) -> R) -> R { | 26 | pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R { |
| 26 | let this = unsafe { self.get_unchecked_mut() }; | 27 | let this = unsafe { self.get_unchecked_mut() }; |
| 27 | let (irq, state) = unwrap!(this.inner.as_mut()); | 28 | let (state, irq) = unwrap!(this.inner.as_mut()); |
| 28 | 29 | ||
| 29 | irq.disable(); | 30 | irq.disable(); |
| 30 | compiler_fence(Ordering::SeqCst); | 31 | compiler_fence(Ordering::SeqCst); |
| @@ -43,7 +44,7 @@ impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> { | |||
| 43 | // Safety: it's OK to get a &mut to the state, since the irq is disabled. | 44 | // Safety: it's OK to get a &mut to the state, since the irq is disabled. |
| 44 | let state = unsafe { &mut *state.get() }; | 45 | let state = unsafe { &mut *state.get() }; |
| 45 | 46 | ||
| 46 | let r = f(irq, state); | 47 | let r = f(state, irq); |
| 47 | 48 | ||
| 48 | compiler_fence(Ordering::SeqCst); | 49 | compiler_fence(Ordering::SeqCst); |
| 49 | irq.enable(); | 50 | irq.enable(); |
| @@ -51,18 +52,18 @@ impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> { | |||
| 51 | r | 52 | r |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | pub fn free(self: Pin<&mut Self>) -> (I, S) { | 55 | pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) { |
| 55 | let this = unsafe { self.get_unchecked_mut() }; | 56 | let this = unsafe { self.get_unchecked_mut() }; |
| 56 | let (irq, state) = unwrap!(this.inner.take()); | 57 | let (state, irq) = unwrap!(this.inner.take()); |
| 57 | irq.disable(); | 58 | irq.disable(); |
| 58 | irq.remove_handler(); | 59 | irq.remove_handler(); |
| 59 | (irq, state.into_inner()) | 60 | (state.into_inner(), irq) |
| 60 | } | 61 | } |
| 61 | } | 62 | } |
| 62 | 63 | ||
| 63 | impl<I: OwnedInterrupt, S: PeripheralState> Drop for PeripheralMutex<I, S> { | 64 | impl<S: PeripheralState> Drop for PeripheralMutex<S> { |
| 64 | fn drop(&mut self) { | 65 | fn drop(&mut self) { |
| 65 | if let Some((irq, state)) = &mut self.inner { | 66 | if let Some((state, irq)) = &mut self.inner { |
| 66 | irq.disable(); | 67 | irq.disable(); |
| 67 | irq.remove_handler(); | 68 | irq.remove_handler(); |
| 68 | } | 69 | } |
