diff options
Diffstat (limited to 'examples/rp235x/src/bin/uart_buffered_split.rs')
| -rw-r--r-- | examples/rp235x/src/bin/uart_buffered_split.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/uart_buffered_split.rs b/examples/rp235x/src/bin/uart_buffered_split.rs new file mode 100644 index 000000000..7cad09f9b --- /dev/null +++ b/examples/rp235x/src/bin/uart_buffered_split.rs | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | //! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP235x chip. | ||
| 2 | //! | ||
| 3 | //! No specific hardware is specified in this example. If you connect pin 0 and 1 you should get the same data back. | ||
| 4 | //! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used | ||
| 5 | //! with its UART port. | ||
| 6 | |||
| 7 | #![no_std] | ||
| 8 | #![no_main] | ||
| 9 | |||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_rp::bind_interrupts; | ||
| 13 | use embassy_rp::peripherals::UART0; | ||
| 14 | use embassy_rp::uart::{BufferedInterruptHandler, BufferedUart, BufferedUartRx, Config}; | ||
| 15 | use embassy_time::Timer; | ||
| 16 | use embedded_io_async::{Read, Write}; | ||
| 17 | use static_cell::StaticCell; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | bind_interrupts!(struct Irqs { | ||
| 21 | UART0_IRQ => BufferedInterruptHandler<UART0>; | ||
| 22 | }); | ||
| 23 | |||
| 24 | #[embassy_executor::main] | ||
| 25 | async fn main(spawner: Spawner) { | ||
| 26 | let p = embassy_rp::init(Default::default()); | ||
| 27 | let (tx_pin, rx_pin, uart) = (p.PIN_0, p.PIN_1, p.UART0); | ||
| 28 | |||
| 29 | static TX_BUF: StaticCell<[u8; 16]> = StaticCell::new(); | ||
| 30 | let tx_buf = &mut TX_BUF.init([0; 16])[..]; | ||
| 31 | static RX_BUF: StaticCell<[u8; 16]> = StaticCell::new(); | ||
| 32 | let rx_buf = &mut RX_BUF.init([0; 16])[..]; | ||
| 33 | let uart = BufferedUart::new(uart, tx_pin, rx_pin, Irqs, tx_buf, rx_buf, Config::default()); | ||
| 34 | let (mut tx, rx) = uart.split(); | ||
| 35 | |||
| 36 | unwrap!(spawner.spawn(reader(rx))); | ||
| 37 | |||
| 38 | info!("Writing..."); | ||
| 39 | loop { | ||
| 40 | let data = [ | ||
| 41 | 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, | ||
| 42 | 29, 30, 31, | ||
| 43 | ]; | ||
| 44 | info!("TX {:?}", data); | ||
| 45 | tx.write_all(&data).await.unwrap(); | ||
| 46 | Timer::after_secs(1).await; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | #[embassy_executor::task] | ||
| 51 | async fn reader(mut rx: BufferedUartRx) { | ||
| 52 | info!("Reading..."); | ||
| 53 | loop { | ||
| 54 | let mut buf = [0; 31]; | ||
| 55 | rx.read_exact(&mut buf).await.unwrap(); | ||
| 56 | info!("RX {:?}", buf); | ||
| 57 | } | ||
| 58 | } | ||
