diff options
| author | Kenneth Knudsen <[email protected]> | 2024-11-06 10:52:03 +0100 |
|---|---|---|
| committer | Kenneth Knudsen <[email protected]> | 2024-11-06 10:52:03 +0100 |
| commit | 72109a7bda5cf1568b1ff2b1b4b7de06f73d3342 (patch) | |
| tree | e90fa12a12b139e159822b93fd25d714cc09a3e2 | |
| parent | aa453caa79a0f612698e0bcb3bfff635a172eb5f (diff) | |
Split_ref with shortened lifetime. When borrowed skip drop on rx and tx
| -rw-r--r-- | embassy-stm32/src/usart/buffered.rs | 78 |
1 files changed, 52 insertions, 26 deletions
diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index d7ac33d07..f7b2bf4b4 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs | |||
| @@ -157,6 +157,7 @@ pub struct BufferedUartTx<'d> { | |||
| 157 | tx: Option<PeripheralRef<'d, AnyPin>>, | 157 | tx: Option<PeripheralRef<'d, AnyPin>>, |
| 158 | cts: Option<PeripheralRef<'d, AnyPin>>, | 158 | cts: Option<PeripheralRef<'d, AnyPin>>, |
| 159 | de: Option<PeripheralRef<'d, AnyPin>>, | 159 | de: Option<PeripheralRef<'d, AnyPin>>, |
| 160 | is_borrowed: bool, | ||
| 160 | } | 161 | } |
| 161 | 162 | ||
| 162 | /// Rx-only buffered UART | 163 | /// Rx-only buffered UART |
| @@ -168,6 +169,7 @@ pub struct BufferedUartRx<'d> { | |||
| 168 | kernel_clock: Hertz, | 169 | kernel_clock: Hertz, |
| 169 | rx: Option<PeripheralRef<'d, AnyPin>>, | 170 | rx: Option<PeripheralRef<'d, AnyPin>>, |
| 170 | rts: Option<PeripheralRef<'d, AnyPin>>, | 171 | rts: Option<PeripheralRef<'d, AnyPin>>, |
| 172 | is_borrowed: bool, | ||
| 171 | } | 173 | } |
| 172 | 174 | ||
| 173 | impl<'d> SetConfig for BufferedUart<'d> { | 175 | impl<'d> SetConfig for BufferedUart<'d> { |
| @@ -341,6 +343,7 @@ impl<'d> BufferedUart<'d> { | |||
| 341 | kernel_clock, | 343 | kernel_clock, |
| 342 | rx, | 344 | rx, |
| 343 | rts, | 345 | rts, |
| 346 | is_borrowed: false, | ||
| 344 | }, | 347 | }, |
| 345 | tx: BufferedUartTx { | 348 | tx: BufferedUartTx { |
| 346 | info, | 349 | info, |
| @@ -349,6 +352,7 @@ impl<'d> BufferedUart<'d> { | |||
| 349 | tx, | 352 | tx, |
| 350 | cts, | 353 | cts, |
| 351 | de, | 354 | de, |
| 355 | is_borrowed: false, | ||
| 352 | }, | 356 | }, |
| 353 | }; | 357 | }; |
| 354 | this.enable_and_configure(tx_buffer, rx_buffer, &config)?; | 358 | this.enable_and_configure(tx_buffer, rx_buffer, &config)?; |
| @@ -396,11 +400,29 @@ impl<'d> BufferedUart<'d> { | |||
| 396 | (self.tx, self.rx) | 400 | (self.tx, self.rx) |
| 397 | } | 401 | } |
| 398 | 402 | ||
| 399 | /// Split the Uart into a transmitter and receiver by mutable reference, | 403 | /// Split the Uart into a transmitter and receiver, |
| 400 | /// which is particularly useful when having two tasks correlating to | 404 | /// which is particularly useful when having two tasks correlating to |
| 401 | /// transmitting and receiving. | 405 | /// transmitting and receiving. |
| 402 | pub fn split_ref(&mut self) -> (&mut BufferedUartTx<'d>, &mut BufferedUartRx<'d>) { | 406 | pub fn split_ref(&mut self) -> (BufferedUartTx<'_>, BufferedUartRx<'_>) { |
| 403 | (&mut self.tx, &mut self.rx) | 407 | ( |
| 408 | BufferedUartTx { | ||
| 409 | info: self.tx.info, | ||
| 410 | state: self.tx.state, | ||
| 411 | kernel_clock: self.tx.kernel_clock, | ||
| 412 | tx: self.tx.tx.as_mut().map(PeripheralRef::reborrow), | ||
| 413 | cts: self.tx.cts.as_mut().map(PeripheralRef::reborrow), | ||
| 414 | de: self.tx.de.as_mut().map(PeripheralRef::reborrow), | ||
| 415 | is_borrowed: true, | ||
| 416 | }, | ||
| 417 | BufferedUartRx { | ||
| 418 | info: self.rx.info, | ||
| 419 | state: self.rx.state, | ||
| 420 | kernel_clock: self.rx.kernel_clock, | ||
| 421 | rx: self.rx.rx.as_mut().map(PeripheralRef::reborrow), | ||
| 422 | rts: self.rx.rts.as_mut().map(PeripheralRef::reborrow), | ||
| 423 | is_borrowed: true, | ||
| 424 | }, | ||
| 425 | ) | ||
| 404 | } | 426 | } |
| 405 | 427 | ||
| 406 | /// Reconfigure the driver | 428 | /// Reconfigure the driver |
| @@ -607,40 +629,44 @@ impl<'d> BufferedUartTx<'d> { | |||
| 607 | 629 | ||
| 608 | impl<'d> Drop for BufferedUartRx<'d> { | 630 | impl<'d> Drop for BufferedUartRx<'d> { |
| 609 | fn drop(&mut self) { | 631 | fn drop(&mut self) { |
| 610 | let state = self.state; | 632 | if !self.is_borrowed { |
| 611 | unsafe { | 633 | let state = self.state; |
| 612 | state.rx_buf.deinit(); | 634 | unsafe { |
| 635 | state.rx_buf.deinit(); | ||
| 613 | 636 | ||
| 614 | // TX is inactive if the the buffer is not available. | 637 | // TX is inactive if the the buffer is not available. |
| 615 | // We can now unregister the interrupt handler | 638 | // We can now unregister the interrupt handler |
| 616 | if state.tx_buf.len() == 0 { | 639 | if state.tx_buf.len() == 0 { |
| 617 | self.info.interrupt.disable(); | 640 | self.info.interrupt.disable(); |
| 641 | } | ||
| 618 | } | 642 | } |
| 619 | } | ||
| 620 | 643 | ||
| 621 | self.rx.as_ref().map(|x| x.set_as_disconnected()); | 644 | self.rx.as_ref().map(|x| x.set_as_disconnected()); |
| 622 | self.rts.as_ref().map(|x| x.set_as_disconnected()); | 645 | self.rts.as_ref().map(|x| x.set_as_disconnected()); |
| 623 | drop_tx_rx(self.info, state); | 646 | drop_tx_rx(self.info, state); |
| 647 | } | ||
| 624 | } | 648 | } |
| 625 | } | 649 | } |
| 626 | 650 | ||
| 627 | impl<'d> Drop for BufferedUartTx<'d> { | 651 | impl<'d> Drop for BufferedUartTx<'d> { |
| 628 | fn drop(&mut self) { | 652 | fn drop(&mut self) { |
| 629 | let state = self.state; | 653 | if !self.is_borrowed { |
| 630 | unsafe { | 654 | let state = self.state; |
| 631 | state.tx_buf.deinit(); | 655 | unsafe { |
| 656 | state.tx_buf.deinit(); | ||
| 632 | 657 | ||
| 633 | // RX is inactive if the the buffer is not available. | 658 | // RX is inactive if the the buffer is not available. |
| 634 | // We can now unregister the interrupt handler | 659 | // We can now unregister the interrupt handler |
| 635 | if state.rx_buf.len() == 0 { | 660 | if state.rx_buf.len() == 0 { |
| 636 | self.info.interrupt.disable(); | 661 | self.info.interrupt.disable(); |
| 662 | } | ||
| 637 | } | 663 | } |
| 638 | } | ||
| 639 | 664 | ||
| 640 | self.tx.as_ref().map(|x| x.set_as_disconnected()); | 665 | self.tx.as_ref().map(|x| x.set_as_disconnected()); |
| 641 | self.cts.as_ref().map(|x| x.set_as_disconnected()); | 666 | self.cts.as_ref().map(|x| x.set_as_disconnected()); |
| 642 | self.de.as_ref().map(|x| x.set_as_disconnected()); | 667 | self.de.as_ref().map(|x| x.set_as_disconnected()); |
| 643 | drop_tx_rx(self.info, state); | 668 | drop_tx_rx(self.info, state); |
| 669 | } | ||
| 644 | } | 670 | } |
| 645 | } | 671 | } |
| 646 | 672 | ||
