diff options
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 37 | ||||
| -rw-r--r-- | embassy-nrf/src/uarte.rs | 20 | ||||
| -rw-r--r-- | examples/nrf/src/bin/buffered_uart.rs | 32 | ||||
| -rw-r--r-- | examples/nrf/src/bin/uart.rs | 3 |
4 files changed, 49 insertions, 43 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 717ada78d..5468f6c0b 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | //! Async buffered UART | ||
| 2 | //! | ||
| 3 | //! Please ee [uarte] to understand when [BufferedUarte] should be used. | ||
| 4 | |||
| 1 | use core::cmp::min; | 5 | use core::cmp::min; |
| 2 | use core::marker::PhantomData; | 6 | use core::marker::PhantomData; |
| 3 | use core::mem; | 7 | use core::mem; |
| @@ -65,8 +69,7 @@ pub struct BufferedUarte<'d, U: UarteInstance, T: TimerInstance> { | |||
| 65 | impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> {} | 69 | impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> {} |
| 66 | 70 | ||
| 67 | impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | 71 | impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { |
| 68 | /// unsafe: may not leak self or futures | 72 | pub fn new( |
| 69 | pub unsafe fn new( | ||
| 70 | state: &'d mut State<'d, U, T>, | 73 | state: &'d mut State<'d, U, T>, |
| 71 | _uarte: impl Unborrow<Target = U> + 'd, | 74 | _uarte: impl Unborrow<Target = U> + 'd, |
| 72 | timer: impl Unborrow<Target = T> + 'd, | 75 | timer: impl Unborrow<Target = T> + 'd, |
| @@ -160,20 +163,22 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 160 | ppi_ch2.enable(); | 163 | ppi_ch2.enable(); |
| 161 | 164 | ||
| 162 | Self { | 165 | Self { |
| 163 | inner: PeripheralMutex::new_unchecked(irq, &mut state.0, move || StateInner { | 166 | inner: unsafe { |
| 164 | phantom: PhantomData, | 167 | PeripheralMutex::new_unchecked(irq, &mut state.0, move || StateInner { |
| 165 | timer, | 168 | phantom: PhantomData, |
| 166 | _ppi_ch1: ppi_ch1, | 169 | timer, |
| 167 | _ppi_ch2: ppi_ch2, | 170 | _ppi_ch1: ppi_ch1, |
| 168 | 171 | _ppi_ch2: ppi_ch2, | |
| 169 | rx: RingBuffer::new(rx_buffer), | 172 | |
| 170 | rx_state: RxState::Idle, | 173 | rx: RingBuffer::new(rx_buffer), |
| 171 | rx_waker: WakerRegistration::new(), | 174 | rx_state: RxState::Idle, |
| 172 | 175 | rx_waker: WakerRegistration::new(), | |
| 173 | tx: RingBuffer::new(tx_buffer), | 176 | |
| 174 | tx_state: TxState::Idle, | 177 | tx: RingBuffer::new(tx_buffer), |
| 175 | tx_waker: WakerRegistration::new(), | 178 | tx_state: TxState::Idle, |
| 176 | }), | 179 | tx_waker: WakerRegistration::new(), |
| 180 | }) | ||
| 181 | }, | ||
| 177 | } | 182 | } |
| 178 | } | 183 | } |
| 179 | 184 | ||
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 459bc8436..60e69e032 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs | |||
| @@ -1,6 +1,17 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | //! Async UART | 3 | //! Async UART |
| 4 | //! | ||
| 5 | //! Async UART is provided in two flavors - this one and also [buffered_uarte::BufferedUarte]. | ||
| 6 | //! The [Uarte] here is useful for those use-cases where reading the UARTE peripheral is | ||
| 7 | //! exclusively awaited on. If the [Uarte] is required to be awaited on with some other future, | ||
| 8 | //! for example when using `futures_util::future::select`, then you should consider | ||
| 9 | //! [buffered_uarte::BufferedUarte] so that reads may continue while processing these | ||
| 10 | //! other futures. If you do not then you may lose data between reads. | ||
| 11 | //! | ||
| 12 | //! An advantage of the [Uarte] has over [buffered_uarte::BufferedUarte] is that less | ||
| 13 | //! memory may be used given that buffers are passed in directly to its read and write | ||
| 14 | //! methods. | ||
| 4 | 15 | ||
| 5 | use core::future::Future; | 16 | use core::future::Future; |
| 6 | use core::marker::PhantomData; | 17 | use core::marker::PhantomData; |
| @@ -48,14 +59,7 @@ pub struct Uarte<'d, T: Instance> { | |||
| 48 | impl<'d, T: Instance> Uarte<'d, T> { | 59 | impl<'d, T: Instance> Uarte<'d, T> { |
| 49 | /// Creates the interface to a UARTE instance. | 60 | /// Creates the interface to a UARTE instance. |
| 50 | /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. | 61 | /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. |
| 51 | /// | 62 | pub fn new( |
| 52 | /// # Safety | ||
| 53 | /// | ||
| 54 | /// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms) | ||
| 55 | /// on stack allocated buffers which which have been passed to [`send()`](Uarte::send) | ||
| 56 | /// or [`receive`](Uarte::receive). | ||
| 57 | #[allow(unused_unsafe)] | ||
| 58 | pub unsafe fn new( | ||
| 59 | _uarte: impl Unborrow<Target = T> + 'd, | 63 | _uarte: impl Unborrow<Target = T> + 'd, |
| 60 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | 64 | irq: impl Unborrow<Target = T::Interrupt> + 'd, |
| 61 | rxd: impl Unborrow<Target = impl GpioPin> + 'd, | 65 | rxd: impl Unborrow<Target = impl GpioPin> + 'd, |
diff --git a/examples/nrf/src/bin/buffered_uart.rs b/examples/nrf/src/bin/buffered_uart.rs index 90d288780..5d9075edf 100644 --- a/examples/nrf/src/bin/buffered_uart.rs +++ b/examples/nrf/src/bin/buffered_uart.rs | |||
| @@ -24,23 +24,21 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 24 | 24 | ||
| 25 | let irq = interrupt::take!(UARTE0_UART0); | 25 | let irq = interrupt::take!(UARTE0_UART0); |
| 26 | let mut state = State::new(); | 26 | let mut state = State::new(); |
| 27 | let u = unsafe { | 27 | let u = BufferedUarte::new( |
| 28 | BufferedUarte::new( | 28 | &mut state, |
| 29 | &mut state, | 29 | p.UARTE0, |
| 30 | p.UARTE0, | 30 | p.TIMER0, |
| 31 | p.TIMER0, | 31 | p.PPI_CH0, |
| 32 | p.PPI_CH0, | 32 | p.PPI_CH1, |
| 33 | p.PPI_CH1, | 33 | irq, |
| 34 | irq, | 34 | p.P0_08, |
| 35 | p.P0_08, | 35 | p.P0_06, |
| 36 | p.P0_06, | 36 | NoPin, |
| 37 | NoPin, | 37 | NoPin, |
| 38 | NoPin, | 38 | config, |
| 39 | config, | 39 | &mut rx_buffer, |
| 40 | &mut rx_buffer, | 40 | &mut tx_buffer, |
| 41 | &mut tx_buffer, | 41 | ); |
| 42 | ) | ||
| 43 | }; | ||
| 44 | pin_mut!(u); | 42 | pin_mut!(u); |
| 45 | 43 | ||
| 46 | info!("uarte initialized!"); | 44 | info!("uarte initialized!"); |
diff --git a/examples/nrf/src/bin/uart.rs b/examples/nrf/src/bin/uart.rs index 6b89e083d..208961c8b 100644 --- a/examples/nrf/src/bin/uart.rs +++ b/examples/nrf/src/bin/uart.rs | |||
| @@ -18,8 +18,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 18 | config.baudrate = uarte::Baudrate::BAUD115200; | 18 | config.baudrate = uarte::Baudrate::BAUD115200; |
| 19 | 19 | ||
| 20 | let irq = interrupt::take!(UARTE0_UART0); | 20 | let irq = interrupt::take!(UARTE0_UART0); |
| 21 | let mut uart = | 21 | let mut uart = uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config); |
| 22 | unsafe { uarte::Uarte::new(p.UARTE0, irq, p.P0_08, p.P0_06, NoPin, NoPin, config) }; | ||
| 23 | 22 | ||
| 24 | info!("uarte initialized!"); | 23 | info!("uarte initialized!"); |
| 25 | 24 | ||
