diff options
| author | Pedro Ferreira <[email protected]> | 2023-01-04 20:27:07 +0100 |
|---|---|---|
| committer | Pedro Ferreira <[email protected]> | 2023-01-04 21:11:19 +0100 |
| commit | 6d4c6e0481f0738b8306172e7c5d54aa2758b74e (patch) | |
| tree | ae99756e87b98f62c78d062c672996a12ed5c465 /examples | |
| parent | bf4c0de16a119b9e3a42daf76c4bc60face3c2a1 (diff) | |
rp2040: add {tx,rx}-only constructors to UART
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/src/bin/uart_unidir.rs | 42 |
1 files changed, 42 insertions, 0 deletions
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 | } | ||
