diff options
| author | Grant Miller <[email protected]> | 2022-07-03 16:16:10 -0500 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-07-23 01:33:22 +0200 |
| commit | 65a82d02d17fc491246eae219f416e565719c0ac (patch) | |
| tree | 1d4842d73031529f018d6b96a84c090e0fb3eb82 /embassy-nrf/src/uarte.rs | |
| parent | ffbd9363f2a52fd27c81bbfbbe8e0e605a1ece86 (diff) | |
WIP: Make unborrow safe to use
Diffstat (limited to 'embassy-nrf/src/uarte.rs')
| -rw-r--r-- | embassy-nrf/src/uarte.rs | 87 |
1 files changed, 34 insertions, 53 deletions
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 459c56c8e..d5ffb3159 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs | |||
| @@ -18,7 +18,7 @@ use core::sync::atomic::{compiler_fence, Ordering}; | |||
| 18 | use core::task::Poll; | 18 | use core::task::Poll; |
| 19 | 19 | ||
| 20 | use embassy_hal_common::drop::OnDrop; | 20 | use embassy_hal_common::drop::OnDrop; |
| 21 | use embassy_hal_common::unborrow; | 21 | use embassy_hal_common::{unborrow, Unborrowed}; |
| 22 | use futures::future::poll_fn; | 22 | use futures::future::poll_fn; |
| 23 | use pac::uarte0::RegisterBlock; | 23 | use pac::uarte0::RegisterBlock; |
| 24 | // Re-export SVD variants to allow user to directly set values. | 24 | // Re-export SVD variants to allow user to directly set values. |
| @@ -89,8 +89,8 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 89 | txd: impl Unborrow<Target = impl GpioPin> + 'd, | 89 | txd: impl Unborrow<Target = impl GpioPin> + 'd, |
| 90 | config: Config, | 90 | config: Config, |
| 91 | ) -> Self { | 91 | ) -> Self { |
| 92 | unborrow!(rxd, txd); | 92 | unborrow_and_degrade!(rxd, txd); |
| 93 | Self::new_inner(uarte, irq, rxd.degrade(), txd.degrade(), None, None, config) | 93 | Self::new_inner(uarte, irq, rxd, txd, None, None, config) |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | /// Create a new UARTE with hardware flow control (RTS/CTS) | 96 | /// Create a new UARTE with hardware flow control (RTS/CTS) |
| @@ -103,25 +103,17 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 103 | rts: impl Unborrow<Target = impl GpioPin> + 'd, | 103 | rts: impl Unborrow<Target = impl GpioPin> + 'd, |
| 104 | config: Config, | 104 | config: Config, |
| 105 | ) -> Self { | 105 | ) -> Self { |
| 106 | unborrow!(rxd, txd, cts, rts); | 106 | unborrow_and_degrade!(rxd, txd, cts, rts); |
| 107 | Self::new_inner( | 107 | Self::new_inner(uarte, irq, rxd, txd, Some(cts), Some(rts), config) |
| 108 | uarte, | ||
| 109 | irq, | ||
| 110 | rxd.degrade(), | ||
| 111 | txd.degrade(), | ||
| 112 | Some(cts.degrade()), | ||
| 113 | Some(rts.degrade()), | ||
| 114 | config, | ||
| 115 | ) | ||
| 116 | } | 108 | } |
| 117 | 109 | ||
| 118 | fn new_inner( | 110 | fn new_inner( |
| 119 | _uarte: impl Unborrow<Target = T> + 'd, | 111 | _uarte: impl Unborrow<Target = T> + 'd, |
| 120 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | 112 | irq: impl Unborrow<Target = T::Interrupt> + 'd, |
| 121 | rxd: AnyPin, | 113 | rxd: Unborrowed<'d, AnyPin>, |
| 122 | txd: AnyPin, | 114 | txd: Unborrowed<'d, AnyPin>, |
| 123 | cts: Option<AnyPin>, | 115 | cts: Option<Unborrowed<'d, AnyPin>>, |
| 124 | rts: Option<AnyPin>, | 116 | rts: Option<Unborrowed<'d, AnyPin>>, |
| 125 | config: Config, | 117 | config: Config, |
| 126 | ) -> Self { | 118 | ) -> Self { |
| 127 | unborrow!(irq); | 119 | unborrow!(irq); |
| @@ -250,8 +242,8 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 250 | txd: impl Unborrow<Target = impl GpioPin> + 'd, | 242 | txd: impl Unborrow<Target = impl GpioPin> + 'd, |
| 251 | config: Config, | 243 | config: Config, |
| 252 | ) -> Self { | 244 | ) -> Self { |
| 253 | unborrow!(txd); | 245 | unborrow_and_degrade!(txd); |
| 254 | Self::new_inner(uarte, irq, txd.degrade(), None, config) | 246 | Self::new_inner(uarte, irq, txd, None, config) |
| 255 | } | 247 | } |
| 256 | 248 | ||
| 257 | /// Create a new tx-only UARTE with hardware flow control (RTS/CTS) | 249 | /// Create a new tx-only UARTE with hardware flow control (RTS/CTS) |
| @@ -262,15 +254,15 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 262 | cts: impl Unborrow<Target = impl GpioPin> + 'd, | 254 | cts: impl Unborrow<Target = impl GpioPin> + 'd, |
| 263 | config: Config, | 255 | config: Config, |
| 264 | ) -> Self { | 256 | ) -> Self { |
| 265 | unborrow!(txd, cts); | 257 | unborrow_and_degrade!(txd, cts); |
| 266 | Self::new_inner(uarte, irq, txd.degrade(), Some(cts.degrade()), config) | 258 | Self::new_inner(uarte, irq, txd, Some(cts), config) |
| 267 | } | 259 | } |
| 268 | 260 | ||
| 269 | fn new_inner( | 261 | fn new_inner( |
| 270 | _uarte: impl Unborrow<Target = T> + 'd, | 262 | _uarte: impl Unborrow<Target = T> + 'd, |
| 271 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | 263 | irq: impl Unborrow<Target = T::Interrupt> + 'd, |
| 272 | txd: AnyPin, | 264 | txd: Unborrowed<'d, AnyPin>, |
| 273 | cts: Option<AnyPin>, | 265 | cts: Option<Unborrowed<'d, AnyPin>>, |
| 274 | config: Config, | 266 | config: Config, |
| 275 | ) -> Self { | 267 | ) -> Self { |
| 276 | unborrow!(irq); | 268 | unborrow!(irq); |
| @@ -442,8 +434,8 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 442 | rxd: impl Unborrow<Target = impl GpioPin> + 'd, | 434 | rxd: impl Unborrow<Target = impl GpioPin> + 'd, |
| 443 | config: Config, | 435 | config: Config, |
| 444 | ) -> Self { | 436 | ) -> Self { |
| 445 | unborrow!(rxd); | 437 | unborrow_and_degrade!(rxd); |
| 446 | Self::new_inner(uarte, irq, rxd.degrade(), None, config) | 438 | Self::new_inner(uarte, irq, rxd, None, config) |
| 447 | } | 439 | } |
| 448 | 440 | ||
| 449 | /// Create a new rx-only UARTE with hardware flow control (RTS/CTS) | 441 | /// Create a new rx-only UARTE with hardware flow control (RTS/CTS) |
| @@ -454,15 +446,15 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 454 | rts: impl Unborrow<Target = impl GpioPin> + 'd, | 446 | rts: impl Unborrow<Target = impl GpioPin> + 'd, |
| 455 | config: Config, | 447 | config: Config, |
| 456 | ) -> Self { | 448 | ) -> Self { |
| 457 | unborrow!(rxd, rts); | 449 | unborrow_and_degrade!(rxd, rts); |
| 458 | Self::new_inner(uarte, irq, rxd.degrade(), Some(rts.degrade()), config) | 450 | Self::new_inner(uarte, irq, rxd, Some(rts), config) |
| 459 | } | 451 | } |
| 460 | 452 | ||
| 461 | fn new_inner( | 453 | fn new_inner( |
| 462 | _uarte: impl Unborrow<Target = T> + 'd, | 454 | _uarte: impl Unborrow<Target = T> + 'd, |
| 463 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | 455 | irq: impl Unborrow<Target = T::Interrupt> + 'd, |
| 464 | rxd: AnyPin, | 456 | rxd: Unborrowed<'d, AnyPin>, |
| 465 | rts: Option<AnyPin>, | 457 | rts: Option<Unborrowed<'d, AnyPin>>, |
| 466 | config: Config, | 458 | config: Config, |
| 467 | ) -> Self { | 459 | ) -> Self { |
| 468 | unborrow!(irq); | 460 | unborrow!(irq); |
| @@ -685,19 +677,8 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { | |||
| 685 | txd: impl Unborrow<Target = impl GpioPin> + 'd, | 677 | txd: impl Unborrow<Target = impl GpioPin> + 'd, |
| 686 | config: Config, | 678 | config: Config, |
| 687 | ) -> Self { | 679 | ) -> Self { |
| 688 | unborrow!(rxd, txd); | 680 | unborrow_and_degrade!(rxd, txd); |
| 689 | Self::new_inner( | 681 | Self::new_inner(uarte, timer, ppi_ch1, ppi_ch2, irq, rxd, txd, None, None, config) |
| 690 | uarte, | ||
| 691 | timer, | ||
| 692 | ppi_ch1, | ||
| 693 | ppi_ch2, | ||
| 694 | irq, | ||
| 695 | rxd.degrade(), | ||
| 696 | txd.degrade(), | ||
| 697 | None, | ||
| 698 | None, | ||
| 699 | config, | ||
| 700 | ) | ||
| 701 | } | 682 | } |
| 702 | 683 | ||
| 703 | /// Create a new UARTE with hardware flow control (RTS/CTS) | 684 | /// Create a new UARTE with hardware flow control (RTS/CTS) |
| @@ -713,17 +694,17 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { | |||
| 713 | rts: impl Unborrow<Target = impl GpioPin> + 'd, | 694 | rts: impl Unborrow<Target = impl GpioPin> + 'd, |
| 714 | config: Config, | 695 | config: Config, |
| 715 | ) -> Self { | 696 | ) -> Self { |
| 716 | unborrow!(rxd, txd, cts, rts); | 697 | unborrow_and_degrade!(rxd, txd, cts, rts); |
| 717 | Self::new_inner( | 698 | Self::new_inner( |
| 718 | uarte, | 699 | uarte, |
| 719 | timer, | 700 | timer, |
| 720 | ppi_ch1, | 701 | ppi_ch1, |
| 721 | ppi_ch2, | 702 | ppi_ch2, |
| 722 | irq, | 703 | irq, |
| 723 | rxd.degrade(), | 704 | rxd, |
| 724 | txd.degrade(), | 705 | txd, |
| 725 | Some(cts.degrade()), | 706 | Some(cts), |
| 726 | Some(rts.degrade()), | 707 | Some(rts), |
| 727 | config, | 708 | config, |
| 728 | ) | 709 | ) |
| 729 | } | 710 | } |
| @@ -734,10 +715,10 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { | |||
| 734 | ppi_ch1: impl Unborrow<Target = impl ConfigurableChannel + 'd> + 'd, | 715 | ppi_ch1: impl Unborrow<Target = impl ConfigurableChannel + 'd> + 'd, |
| 735 | ppi_ch2: impl Unborrow<Target = impl ConfigurableChannel + 'd> + 'd, | 716 | ppi_ch2: impl Unborrow<Target = impl ConfigurableChannel + 'd> + 'd, |
| 736 | irq: impl Unborrow<Target = U::Interrupt> + 'd, | 717 | irq: impl Unborrow<Target = U::Interrupt> + 'd, |
| 737 | rxd: AnyPin, | 718 | rxd: Unborrowed<'d, AnyPin>, |
| 738 | txd: AnyPin, | 719 | txd: Unborrowed<'d, AnyPin>, |
| 739 | cts: Option<AnyPin>, | 720 | cts: Option<Unborrowed<'d, AnyPin>>, |
| 740 | rts: Option<AnyPin>, | 721 | rts: Option<Unborrowed<'d, AnyPin>>, |
| 741 | config: Config, | 722 | config: Config, |
| 742 | ) -> Self { | 723 | ) -> Self { |
| 743 | let baudrate = config.baudrate; | 724 | let baudrate = config.baudrate; |
| @@ -763,7 +744,7 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { | |||
| 763 | timer.cc(0).short_compare_stop(); | 744 | timer.cc(0).short_compare_stop(); |
| 764 | 745 | ||
| 765 | let mut ppi_ch1 = Ppi::new_one_to_two( | 746 | let mut ppi_ch1 = Ppi::new_one_to_two( |
| 766 | ppi_ch1.degrade(), | 747 | unsafe { ppi_ch1.into_inner() }.degrade(), |
| 767 | Event::from_reg(&r.events_rxdrdy), | 748 | Event::from_reg(&r.events_rxdrdy), |
| 768 | timer.task_clear(), | 749 | timer.task_clear(), |
| 769 | timer.task_start(), | 750 | timer.task_start(), |
| @@ -771,7 +752,7 @@ impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> { | |||
| 771 | ppi_ch1.enable(); | 752 | ppi_ch1.enable(); |
| 772 | 753 | ||
| 773 | let mut ppi_ch2 = Ppi::new_one_to_one( | 754 | let mut ppi_ch2 = Ppi::new_one_to_one( |
| 774 | ppi_ch2.degrade(), | 755 | unsafe { ppi_ch2.into_inner() }.degrade(), |
| 775 | timer.cc(0).event_compare(), | 756 | timer.cc(0).event_compare(), |
| 776 | Task::from_reg(&r.tasks_stoprx), | 757 | Task::from_reg(&r.tasks_stoprx), |
| 777 | ); | 758 | ); |
