diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-01-05 01:57:05 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-01-05 01:57:05 +0100 |
| commit | 3a4dbfa52ece1d11ec4ded71074fb1e914671571 (patch) | |
| tree | 7a7457dcc50968f0d953fa1e51bcf40523bd3b2a /embassy-nrf/src/buffered_uarte.rs | |
| parent | 9e88718fbdd27a33e0182c8430bc51ff314f4e48 (diff) | |
Massicely simplify peripheral abstraction
Diffstat (limited to 'embassy-nrf/src/buffered_uarte.rs')
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 42 |
1 files changed, 13 insertions, 29 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index c67b6f166..dbdb6f514 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -5,7 +5,6 @@ | |||
| 5 | //! - nrf52832: Section 35 | 5 | //! - nrf52832: Section 35 |
| 6 | //! - nrf52840: Section 6.34 | 6 | //! - nrf52840: Section 6.34 |
| 7 | use core::cmp::min; | 7 | use core::cmp::min; |
| 8 | use core::marker::PhantomData; | ||
| 9 | use core::mem; | 8 | use core::mem; |
| 10 | use core::ops::Deref; | 9 | use core::ops::Deref; |
| 11 | use core::pin::Pin; | 10 | use core::pin::Pin; |
| @@ -20,7 +19,7 @@ use crate::hal::gpio::Port as GpioPort; | |||
| 20 | use crate::interrupt::{self, OwnedInterrupt}; | 19 | use crate::interrupt::{self, OwnedInterrupt}; |
| 21 | use crate::pac; | 20 | use crate::pac; |
| 22 | use crate::pac::uarte0; | 21 | use crate::pac::uarte0; |
| 23 | use crate::util::peripheral; | 22 | use crate::util::peripheral::{PeripheralMutex, PeripheralState}; |
| 24 | use crate::util::ring_buffer::RingBuffer; | 23 | use crate::util::ring_buffer::RingBuffer; |
| 25 | 24 | ||
| 26 | // Re-export SVD variants to allow user to directly set values | 25 | // Re-export SVD variants to allow user to directly set values |
| @@ -49,8 +48,7 @@ enum TxState { | |||
| 49 | /// - nrf52832: Section 15.2 | 48 | /// - nrf52832: Section 15.2 |
| 50 | /// - nrf52840: Section 6.1.2 | 49 | /// - nrf52840: Section 6.1.2 |
| 51 | pub struct BufferedUarte<'a, T: Instance> { | 50 | pub struct BufferedUarte<'a, T: Instance> { |
| 52 | reg: peripheral::Registration<State<'a, T>>, | 51 | inner: PeripheralMutex<T::Interrupt, State<'a, T>>, |
| 53 | wtf: PhantomData<&'a ()>, | ||
| 54 | } | 52 | } |
| 55 | 53 | ||
| 56 | impl<'a, T: Instance> Unpin for BufferedUarte<'a, T> {} | 54 | impl<'a, T: Instance> Unpin for BufferedUarte<'a, T> {} |
| @@ -126,10 +124,12 @@ impl<'a, T: Instance> BufferedUarte<'a, T> { | |||
| 126 | // Configure frequency | 124 | // Configure frequency |
| 127 | uarte.baudrate.write(|w| w.baudrate().variant(baudrate)); | 125 | uarte.baudrate.write(|w| w.baudrate().variant(baudrate)); |
| 128 | 126 | ||
| 127 | // Disable the irq, let the Registration enable it when everything is set up. | ||
| 128 | irq.disable(); | ||
| 129 | irq.pend(); | 129 | irq.pend(); |
| 130 | 130 | ||
| 131 | BufferedUarte { | 131 | BufferedUarte { |
| 132 | reg: peripheral::Registration::new( | 132 | inner: PeripheralMutex::new( |
| 133 | irq, | 133 | irq, |
| 134 | State { | 134 | State { |
| 135 | inner: uarte, | 135 | inner: uarte, |
| @@ -143,7 +143,6 @@ impl<'a, T: Instance> BufferedUarte<'a, T> { | |||
| 143 | tx_waker: WakerRegistration::new(), | 143 | tx_waker: WakerRegistration::new(), |
| 144 | }, | 144 | }, |
| 145 | ), | 145 | ), |
| 146 | wtf: PhantomData, | ||
| 147 | } | 146 | } |
| 148 | } | 147 | } |
| 149 | } | 148 | } |
| @@ -158,7 +157,8 @@ impl<'a, T: Instance> Drop for BufferedUarte<'a, T> { | |||
| 158 | impl<'a, T: Instance> AsyncBufRead for BufferedUarte<'a, T> { | 157 | impl<'a, T: Instance> AsyncBufRead for BufferedUarte<'a, T> { |
| 159 | fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { | 158 | fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { |
| 160 | let this = unsafe { self.get_unchecked_mut() }; | 159 | let this = unsafe { self.get_unchecked_mut() }; |
| 161 | this.reg.with(|state, _| { | 160 | let reg = unsafe { Pin::new_unchecked(&mut this.inner) }; |
| 161 | reg.with(|_irq, state| { | ||
| 162 | let z: Poll<Result<&[u8]>> = state.poll_fill_buf(cx); | 162 | let z: Poll<Result<&[u8]>> = state.poll_fill_buf(cx); |
| 163 | let z: Poll<Result<&[u8]>> = unsafe { mem::transmute(z) }; | 163 | let z: Poll<Result<&[u8]>> = unsafe { mem::transmute(z) }; |
| 164 | z | 164 | z |
| @@ -167,14 +167,16 @@ impl<'a, T: Instance> AsyncBufRead for BufferedUarte<'a, T> { | |||
| 167 | 167 | ||
| 168 | fn consume(self: Pin<&mut Self>, amt: usize) { | 168 | fn consume(self: Pin<&mut Self>, amt: usize) { |
| 169 | let this = unsafe { self.get_unchecked_mut() }; | 169 | let this = unsafe { self.get_unchecked_mut() }; |
| 170 | this.reg.with(|state, irq| state.consume(irq, amt)) | 170 | let reg = unsafe { Pin::new_unchecked(&mut this.inner) }; |
| 171 | reg.with(|irq, state| state.consume(irq, amt)) | ||
| 171 | } | 172 | } |
| 172 | } | 173 | } |
| 173 | 174 | ||
| 174 | impl<'a, T: Instance> AsyncWrite for BufferedUarte<'a, T> { | 175 | impl<'a, T: Instance> AsyncWrite for BufferedUarte<'a, T> { |
| 175 | fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { | 176 | fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { |
| 176 | let this = unsafe { self.get_unchecked_mut() }; | 177 | let this = unsafe { self.get_unchecked_mut() }; |
| 177 | this.reg.with(|state, irq| state.poll_write(irq, cx, buf)) | 178 | let reg = unsafe { Pin::new_unchecked(&mut this.inner) }; |
| 179 | reg.with(|irq, state| state.poll_write(irq, cx, buf)) | ||
| 178 | } | 180 | } |
| 179 | } | 181 | } |
| 180 | 182 | ||
| @@ -262,12 +264,7 @@ impl<'a, T: Instance> State<'a, T> { | |||
| 262 | } | 264 | } |
| 263 | } | 265 | } |
| 264 | 266 | ||
| 265 | impl<'a, T: Instance> peripheral::State for State<'a, T> { | 267 | impl<'a, T: Instance> PeripheralState for State<'a, T> { |
| 266 | type Interrupt = T::Interrupt; | ||
| 267 | fn store<'b>() -> &'b peripheral::Store<Self> { | ||
| 268 | unsafe { mem::transmute(T::storage()) } | ||
| 269 | } | ||
| 270 | |||
| 271 | fn on_interrupt(&mut self) { | 268 | fn on_interrupt(&mut self) { |
| 272 | trace!("irq: start"); | 269 | trace!("irq: start"); |
| 273 | let mut more_work = true; | 270 | let mut more_work = true; |
| @@ -414,28 +411,15 @@ mod private { | |||
| 414 | impl Sealed for crate::pac::UARTE1 {} | 411 | impl Sealed for crate::pac::UARTE1 {} |
| 415 | } | 412 | } |
| 416 | 413 | ||
| 417 | pub trait Instance: | 414 | pub trait Instance: Deref<Target = uarte0::RegisterBlock> + private::Sealed { |
| 418 | Deref<Target = uarte0::RegisterBlock> + Sized + private::Sealed + 'static | ||
| 419 | { | ||
| 420 | type Interrupt: OwnedInterrupt; | 415 | type Interrupt: OwnedInterrupt; |
| 421 | fn storage() -> &'static peripheral::Store<State<'static, Self>>; | ||
| 422 | } | 416 | } |
| 423 | 417 | ||
| 424 | impl Instance for pac::UARTE0 { | 418 | impl Instance for pac::UARTE0 { |
| 425 | type Interrupt = interrupt::UARTE0_UART0Interrupt; | 419 | type Interrupt = interrupt::UARTE0_UART0Interrupt; |
| 426 | fn storage() -> &'static peripheral::Store<State<'static, Self>> { | ||
| 427 | static STORAGE: peripheral::Store<State<'static, crate::pac::UARTE0>> = | ||
| 428 | peripheral::Store::uninit(); | ||
| 429 | &STORAGE | ||
| 430 | } | ||
| 431 | } | 420 | } |
| 432 | 421 | ||
| 433 | #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] | 422 | #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] |
| 434 | impl Instance for pac::UARTE1 { | 423 | impl Instance for pac::UARTE1 { |
| 435 | type Interrupt = interrupt::UARTE1Interrupt; | 424 | type Interrupt = interrupt::UARTE1Interrupt; |
| 436 | fn storage() -> &'static peripheral::Store<State<'static, Self>> { | ||
| 437 | static STORAGE: peripheral::Store<State<'static, crate::pac::UARTE1>> = | ||
| 438 | peripheral::Store::uninit(); | ||
| 439 | &STORAGE | ||
| 440 | } | ||
| 441 | } | 425 | } |
