diff options
Diffstat (limited to 'examples/rp/src/bin/uart_buffered_split.rs')
| -rw-r--r-- | examples/rp/src/bin/uart_buffered_split.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/examples/rp/src/bin/uart_buffered_split.rs b/examples/rp/src/bin/uart_buffered_split.rs new file mode 100644 index 000000000..735201718 --- /dev/null +++ b/examples/rp/src/bin/uart_buffered_split.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | //! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 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 | #![feature(type_alias_impl_trait)] | ||
| 10 | |||
| 11 | use defmt::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_rp::bind_interrupts; | ||
| 14 | use embassy_rp::peripherals::UART0; | ||
| 15 | use embassy_rp::uart::{BufferedInterruptHandler, BufferedUart, BufferedUartRx, Config}; | ||
| 16 | use embassy_time::{Duration, Timer}; | ||
| 17 | use embedded_io::asynch::{Read, Write}; | ||
| 18 | use static_cell::make_static; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | ||
| 20 | |||
| 21 | bind_interrupts!(struct Irqs { | ||
| 22 | UART0_IRQ => BufferedInterruptHandler<UART0>; | ||
| 23 | }); | ||
| 24 | |||
| 25 | #[embassy_executor::main] | ||
| 26 | async fn main(spawner: Spawner) { | ||
| 27 | let p = embassy_rp::init(Default::default()); | ||
| 28 | let (tx_pin, rx_pin, uart) = (p.PIN_0, p.PIN_1, p.UART0); | ||
| 29 | |||
| 30 | let tx_buf = &mut make_static!([0u8; 16])[..]; | ||
| 31 | let rx_buf = &mut make_static!([0u8; 16])[..]; | ||
| 32 | let uart = BufferedUart::new(uart, Irqs, tx_pin, rx_pin, tx_buf, rx_buf, Config::default()); | ||
| 33 | let (rx, mut tx) = uart.split(); | ||
| 34 | |||
| 35 | unwrap!(spawner.spawn(reader(rx))); | ||
| 36 | |||
| 37 | info!("Writing..."); | ||
| 38 | loop { | ||
| 39 | let data = [ | ||
| 40 | 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, | ||
| 41 | 29, 30, 31, | ||
| 42 | ]; | ||
| 43 | info!("TX {:?}", data); | ||
| 44 | tx.write_all(&data).await.unwrap(); | ||
| 45 | Timer::after(Duration::from_secs(1)).await; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | #[embassy_executor::task] | ||
| 50 | async fn reader(mut rx: BufferedUartRx<'static, UART0>) { | ||
| 51 | info!("Reading..."); | ||
| 52 | loop { | ||
| 53 | let mut buf = [0; 31]; | ||
| 54 | rx.read_exact(&mut buf).await.unwrap(); | ||
| 55 | info!("RX {:?}", buf); | ||
| 56 | } | ||
| 57 | } | ||
