diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-01-04 20:18:02 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-01-04 20:18:02 +0000 |
| commit | f339e8518f7ec11da93dfb615ed8aa0ed009358f (patch) | |
| tree | ae99756e87b98f62c78d062c672996a12ed5c465 | |
| parent | bf4c0de16a119b9e3a42daf76c4bc60face3c2a1 (diff) | |
| parent | 6d4c6e0481f0738b8306172e7c5d54aa2758b74e (diff) | |
Merge #1143
1143: rp2040: add {tx,rx}-only constructors to UART r=Dirbaio a=pferreir
As discussed with `@henrik-alser` on Matrix. I also added an example, feel free to remove it if it's too much.
Co-authored-by: Pedro Ferreira <[email protected]>
| -rw-r--r-- | embassy-rp/src/uart/mod.rs | 32 | ||||
| -rw-r--r-- | examples/rp/src/bin/uart_unidir.rs | 42 |
2 files changed, 70 insertions, 4 deletions
diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index 7e7bcaf30..bbbf97c01 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs | |||
| @@ -98,7 +98,19 @@ pub struct UartRx<'d, T: Instance, M: Mode> { | |||
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | impl<'d, T: Instance, M: Mode> UartTx<'d, T, M> { | 100 | impl<'d, T: Instance, M: Mode> UartTx<'d, T, M> { |
| 101 | fn new(tx_dma: Option<PeripheralRef<'d, AnyChannel>>) -> Self { | 101 | /// Create a new DMA-enabled UART which can only send data |
| 102 | pub fn new( | ||
| 103 | _uart: impl Peripheral<P = T> + 'd, | ||
| 104 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | ||
| 105 | tx_dma: impl Peripheral<P = impl Channel> + 'd, | ||
| 106 | config: Config, | ||
| 107 | ) -> Self { | ||
| 108 | into_ref!(tx, tx_dma); | ||
| 109 | Uart::<T, M>::init(Some(tx.map_into()), None, None, None, config); | ||
| 110 | Self::new_inner(Some(tx_dma.map_into())) | ||
| 111 | } | ||
| 112 | |||
| 113 | fn new_inner(tx_dma: Option<PeripheralRef<'d, AnyChannel>>) -> Self { | ||
| 102 | Self { | 114 | Self { |
| 103 | tx_dma, | 115 | tx_dma, |
| 104 | phantom: PhantomData, | 116 | phantom: PhantomData, |
| @@ -140,7 +152,19 @@ impl<'d, T: Instance> UartTx<'d, T, Async> { | |||
| 140 | } | 152 | } |
| 141 | 153 | ||
| 142 | impl<'d, T: Instance, M: Mode> UartRx<'d, T, M> { | 154 | impl<'d, T: Instance, M: Mode> UartRx<'d, T, M> { |
| 143 | fn new(rx_dma: Option<PeripheralRef<'d, AnyChannel>>) -> Self { | 155 | /// Create a new DMA-enabled UART which can only send data |
| 156 | pub fn new( | ||
| 157 | _uart: impl Peripheral<P = T> + 'd, | ||
| 158 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | ||
| 159 | rx_dma: impl Peripheral<P = impl Channel> + 'd, | ||
| 160 | config: Config, | ||
| 161 | ) -> Self { | ||
| 162 | into_ref!(rx, rx_dma); | ||
| 163 | Uart::<T, M>::init(Some(rx.map_into()), None, None, None, config); | ||
| 164 | Self::new_inner(Some(rx_dma.map_into())) | ||
| 165 | } | ||
| 166 | |||
| 167 | fn new_inner(rx_dma: Option<PeripheralRef<'d, AnyChannel>>) -> Self { | ||
| 144 | Self { | 168 | Self { |
| 145 | rx_dma, | 169 | rx_dma, |
| 146 | phantom: PhantomData, | 170 | phantom: PhantomData, |
| @@ -295,8 +319,8 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { | |||
| 295 | ); | 319 | ); |
| 296 | 320 | ||
| 297 | Self { | 321 | Self { |
| 298 | tx: UartTx::new(tx_dma), | 322 | tx: UartTx::new_inner(tx_dma), |
| 299 | rx: UartRx::new(rx_dma), | 323 | rx: UartRx::new_inner(rx_dma), |
| 300 | } | 324 | } |
| 301 | } | 325 | } |
| 302 | 326 | ||
diff --git a/examples/rp/src/bin/uart_unidir.rs b/examples/rp/src/bin/uart_unidir.rs new file mode 100644 index 000000000..f56e7009f --- /dev/null +++ b/examples/rp/src/bin/uart_unidir.rs | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | //! test TX-only and RX-only UARTs. You need to connect GPIO0 to GPIO5 for | ||
| 2 | //! this to work | ||
| 3 | |||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_rp::peripherals::UART1; | ||
| 11 | use embassy_rp::uart::{Async, Config, UartRx, UartTx}; | ||
| 12 | use embassy_time::{Duration, Timer}; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(spawner: Spawner) { | ||
| 17 | let p = embassy_rp::init(Default::default()); | ||
| 18 | |||
| 19 | let mut uart_tx = UartTx::new(p.UART0, p.PIN_0, p.DMA_CH0, Config::default()); | ||
| 20 | let uart_rx = UartRx::new(p.UART1, p.PIN_5, p.DMA_CH1, Config::default()); | ||
| 21 | |||
| 22 | unwrap!(spawner.spawn(reader(uart_rx))); | ||
| 23 | |||
| 24 | info!("Writing..."); | ||
| 25 | loop { | ||
| 26 | let data = [1u8, 2, 3, 4, 5, 6, 7, 8]; | ||
| 27 | info!("TX {:?}", data); | ||
| 28 | uart_tx.write(&data).await.unwrap(); | ||
| 29 | Timer::after(Duration::from_secs(1)).await; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | #[embassy_executor::task] | ||
| 34 | async fn reader(mut rx: UartRx<'static, UART1, Async>) { | ||
| 35 | info!("Reading..."); | ||
| 36 | loop { | ||
| 37 | // read a total of 4 transmissions (32 / 8) and then print the result | ||
| 38 | let mut buf = [0; 32]; | ||
| 39 | rx.read(&mut buf).await.unwrap(); | ||
| 40 | info!("RX {:?}", buf); | ||
| 41 | } | ||
| 42 | } | ||
