diff options
| -rw-r--r-- | embassy-hal-common/src/atomic_ring_buffer.rs | 4 | ||||
| -rw-r--r-- | embassy-rp/src/uart/buffered.rs | 51 | ||||
| -rw-r--r-- | examples/rp/src/bin/uart_buffered_split.rs | 2 |
3 files changed, 31 insertions, 26 deletions
diff --git a/embassy-hal-common/src/atomic_ring_buffer.rs b/embassy-hal-common/src/atomic_ring_buffer.rs index a8a6a2166..4c944d763 100644 --- a/embassy-hal-common/src/atomic_ring_buffer.rs +++ b/embassy-hal-common/src/atomic_ring_buffer.rs | |||
| @@ -81,6 +81,10 @@ impl RingBuffer { | |||
| 81 | Writer(self) | 81 | Writer(self) |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | pub fn len(&self) -> usize { | ||
| 85 | self.len.load(Ordering::Relaxed) | ||
| 86 | } | ||
| 87 | |||
| 84 | pub fn is_full(&self) -> bool { | 88 | pub fn is_full(&self) -> bool { |
| 85 | let len = self.len.load(Ordering::Relaxed); | 89 | let len = self.len.load(Ordering::Relaxed); |
| 86 | let start = self.start.load(Ordering::Relaxed); | 90 | let start = self.start.load(Ordering::Relaxed); |
diff --git a/embassy-rp/src/uart/buffered.rs b/embassy-rp/src/uart/buffered.rs index d853618a0..9164794b1 100644 --- a/embassy-rp/src/uart/buffered.rs +++ b/embassy-rp/src/uart/buffered.rs | |||
| @@ -27,7 +27,8 @@ impl State { | |||
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | pub struct BufferedUart<'d, T: Instance> { | 29 | pub struct BufferedUart<'d, T: Instance> { |
| 30 | phantom: PhantomData<&'d mut T>, | 30 | rx: BufferedUartRx<'d, T>, |
| 31 | tx: BufferedUartTx<'d, T>, | ||
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | pub struct BufferedUartRx<'d, T: Instance> { | 34 | pub struct BufferedUartRx<'d, T: Instance> { |
| @@ -91,7 +92,10 @@ impl<'d, T: Instance> BufferedUart<'d, T> { | |||
| 91 | rx_buffer, | 92 | rx_buffer, |
| 92 | config, | 93 | config, |
| 93 | ); | 94 | ); |
| 94 | Self { phantom: PhantomData } | 95 | Self { |
| 96 | rx: BufferedUartRx { phantom: PhantomData }, | ||
| 97 | tx: BufferedUartTx { phantom: PhantomData }, | ||
| 98 | } | ||
| 95 | } | 99 | } |
| 96 | 100 | ||
| 97 | pub fn new_with_rtscts( | 101 | pub fn new_with_rtscts( |
| @@ -116,14 +120,14 @@ impl<'d, T: Instance> BufferedUart<'d, T> { | |||
| 116 | rx_buffer, | 120 | rx_buffer, |
| 117 | config, | 121 | config, |
| 118 | ); | 122 | ); |
| 119 | Self { phantom: PhantomData } | 123 | Self { |
| 124 | rx: BufferedUartRx { phantom: PhantomData }, | ||
| 125 | tx: BufferedUartTx { phantom: PhantomData }, | ||
| 126 | } | ||
| 120 | } | 127 | } |
| 121 | 128 | ||
| 122 | pub fn split(&mut self) -> (BufferedUartRx<'d, T>, BufferedUartTx<'d, T>) { | 129 | pub fn split(self) -> (BufferedUartRx<'d, T>, BufferedUartTx<'d, T>) { |
| 123 | ( | 130 | (self.rx, self.tx) |
| 124 | BufferedUartRx { phantom: PhantomData }, | ||
| 125 | BufferedUartTx { phantom: PhantomData }, | ||
| 126 | ) | ||
| 127 | } | 131 | } |
| 128 | } | 132 | } |
| 129 | 133 | ||
| @@ -269,35 +273,32 @@ impl<'d, T: Instance> BufferedUartTx<'d, T> { | |||
| 269 | } | 273 | } |
| 270 | } | 274 | } |
| 271 | 275 | ||
| 272 | impl<'d, T: Instance> Drop for BufferedUart<'d, T> { | ||
| 273 | fn drop(&mut self) { | ||
| 274 | unsafe { | ||
| 275 | T::Interrupt::steal().disable(); | ||
| 276 | let state = T::state(); | ||
| 277 | state.tx_buf.deinit(); | ||
| 278 | state.rx_buf.deinit(); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | impl<'d, T: Instance> Drop for BufferedUartRx<'d, T> { | 276 | impl<'d, T: Instance> Drop for BufferedUartRx<'d, T> { |
| 284 | fn drop(&mut self) { | 277 | fn drop(&mut self) { |
| 278 | let state = T::state(); | ||
| 285 | unsafe { | 279 | unsafe { |
| 286 | T::Interrupt::steal().disable(); | ||
| 287 | let state = T::state(); | ||
| 288 | state.tx_buf.deinit(); | ||
| 289 | state.rx_buf.deinit(); | 280 | state.rx_buf.deinit(); |
| 281 | |||
| 282 | // TX is inactive if the the buffer is not available. | ||
| 283 | // We can now unregister the interrupt handler | ||
| 284 | if state.tx_buf.len() == 0 { | ||
| 285 | T::Interrupt::steal().disable(); | ||
| 286 | } | ||
| 290 | } | 287 | } |
| 291 | } | 288 | } |
| 292 | } | 289 | } |
| 293 | 290 | ||
| 294 | impl<'d, T: Instance> Drop for BufferedUartTx<'d, T> { | 291 | impl<'d, T: Instance> Drop for BufferedUartTx<'d, T> { |
| 295 | fn drop(&mut self) { | 292 | fn drop(&mut self) { |
| 293 | let state = T::state(); | ||
| 296 | unsafe { | 294 | unsafe { |
| 297 | T::Interrupt::steal().disable(); | ||
| 298 | let state = T::state(); | ||
| 299 | state.tx_buf.deinit(); | 295 | state.tx_buf.deinit(); |
| 300 | state.rx_buf.deinit(); | 296 | |
| 297 | // RX is inactive if the the buffer is not available. | ||
| 298 | // We can now unregister the interrupt handler | ||
| 299 | if state.rx_buf.len() == 0 { | ||
| 300 | T::Interrupt::steal().disable(); | ||
| 301 | } | ||
| 301 | } | 302 | } |
| 302 | } | 303 | } |
| 303 | } | 304 | } |
diff --git a/examples/rp/src/bin/uart_buffered_split.rs b/examples/rp/src/bin/uart_buffered_split.rs index 36f31c906..a8a682274 100644 --- a/examples/rp/src/bin/uart_buffered_split.rs +++ b/examples/rp/src/bin/uart_buffered_split.rs | |||
| @@ -29,7 +29,7 @@ async fn main(spawner: Spawner) { | |||
| 29 | let irq = interrupt::take!(UART0_IRQ); | 29 | let irq = interrupt::take!(UART0_IRQ); |
| 30 | let tx_buf = &mut singleton!([0u8; 16])[..]; | 30 | let tx_buf = &mut singleton!([0u8; 16])[..]; |
| 31 | let rx_buf = &mut singleton!([0u8; 16])[..]; | 31 | let rx_buf = &mut singleton!([0u8; 16])[..]; |
| 32 | let mut uart = BufferedUart::new(uart, irq, tx_pin, rx_pin, tx_buf, rx_buf, Config::default()); | 32 | let uart = BufferedUart::new(uart, irq, tx_pin, rx_pin, tx_buf, rx_buf, Config::default()); |
| 33 | let (rx, mut tx) = uart.split(); | 33 | let (rx, mut tx) = uart.split(); |
| 34 | 34 | ||
| 35 | unwrap!(spawner.spawn(reader(rx))); | 35 | unwrap!(spawner.spawn(reader(rx))); |
