aboutsummaryrefslogtreecommitdiff
path: root/examples/rp235x/src/bin/pio_uart.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp235x/src/bin/pio_uart.rs')
-rw-r--r--examples/rp235x/src/bin/pio_uart.rs190
1 files changed, 190 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/pio_uart.rs b/examples/rp235x/src/bin/pio_uart.rs
new file mode 100644
index 000000000..9712984f9
--- /dev/null
+++ b/examples/rp235x/src/bin/pio_uart.rs
@@ -0,0 +1,190 @@
1//! This example shows how to use the PIO module in the RP2040 chip to implement a duplex UART.
2//! The PIO module is a very powerful peripheral that can be used to implement many different
3//! protocols. It is a very flexible state machine that can be programmed to do almost anything.
4//!
5//! This example opens up a USB device that implements a CDC ACM serial port. It then uses the
6//! PIO module to implement a UART that is connected to the USB serial port. This allows you to
7//! communicate with a device connected to the RP2040 over USB serial.
8
9#![no_std]
10#![no_main]
11#![allow(async_fn_in_trait)]
12
13use defmt::{info, panic, trace};
14use embassy_executor::Spawner;
15use embassy_futures::join::{join, join3};
16use embassy_rp::peripherals::{PIO0, USB};
17use embassy_rp::pio_programs::uart::{PioUartRx, PioUartRxProgram, PioUartTx, PioUartTxProgram};
18use embassy_rp::usb::{Driver, Instance, InterruptHandler};
19use embassy_rp::{bind_interrupts, pio};
20use embassy_sync::blocking_mutex::raw::NoopRawMutex;
21use embassy_sync::pipe::Pipe;
22use embassy_usb::class::cdc_acm::{CdcAcmClass, Receiver, Sender, State};
23use embassy_usb::driver::EndpointError;
24use embassy_usb::{Builder, Config};
25use embedded_io_async::{Read, Write};
26use {defmt_rtt as _, panic_probe as _};
27
28bind_interrupts!(struct Irqs {
29 USBCTRL_IRQ => InterruptHandler<USB>;
30 PIO0_IRQ_0 => pio::InterruptHandler<PIO0>;
31});
32
33#[embassy_executor::main]
34async fn main(_spawner: Spawner) {
35 info!("Hello there!");
36
37 let p = embassy_rp::init(Default::default());
38
39 // Create the driver, from the HAL.
40 let driver = Driver::new(p.USB, Irqs);
41
42 // Create embassy-usb Config
43 let mut config = Config::new(0xc0de, 0xcafe);
44 config.manufacturer = Some("Embassy");
45 config.product = Some("PIO UART example");
46 config.serial_number = Some("12345678");
47 config.max_power = 100;
48 config.max_packet_size_0 = 64;
49
50 // Create embassy-usb DeviceBuilder using the driver and config.
51 // It needs some buffers for building the descriptors.
52 let mut config_descriptor = [0; 256];
53 let mut bos_descriptor = [0; 256];
54 let mut control_buf = [0; 64];
55
56 let mut state = State::new();
57
58 let mut builder = Builder::new(
59 driver,
60 config,
61 &mut config_descriptor,
62 &mut bos_descriptor,
63 &mut [], // no msos descriptors
64 &mut control_buf,
65 );
66
67 // Create classes on the builder.
68 let class = CdcAcmClass::new(&mut builder, &mut state, 64);
69
70 // Build the builder.
71 let mut usb = builder.build();
72
73 // Run the USB device.
74 let usb_fut = usb.run();
75
76 // PIO UART setup
77 let pio::Pio {
78 mut common, sm0, sm1, ..
79 } = pio::Pio::new(p.PIO0, Irqs);
80
81 let tx_program = PioUartTxProgram::new(&mut common);
82 let mut uart_tx = PioUartTx::new(9600, &mut common, sm0, p.PIN_4, &tx_program);
83
84 let rx_program = PioUartRxProgram::new(&mut common);
85 let mut uart_rx = PioUartRx::new(9600, &mut common, sm1, p.PIN_5, &rx_program);
86
87 // Pipe setup
88 let mut usb_pipe: Pipe<NoopRawMutex, 20> = Pipe::new();
89 let (mut usb_pipe_reader, mut usb_pipe_writer) = usb_pipe.split();
90
91 let mut uart_pipe: Pipe<NoopRawMutex, 20> = Pipe::new();
92 let (mut uart_pipe_reader, mut uart_pipe_writer) = uart_pipe.split();
93
94 let (mut usb_tx, mut usb_rx) = class.split();
95
96 // Read + write from USB
97 let usb_future = async {
98 loop {
99 info!("Wait for USB connection");
100 usb_rx.wait_connection().await;
101 info!("Connected");
102 let _ = join(
103 usb_read(&mut usb_rx, &mut uart_pipe_writer),
104 usb_write(&mut usb_tx, &mut usb_pipe_reader),
105 )
106 .await;
107 info!("Disconnected");
108 }
109 };
110
111 // Read + write from UART
112 let uart_future = join(
113 uart_read(&mut uart_rx, &mut usb_pipe_writer),
114 uart_write(&mut uart_tx, &mut uart_pipe_reader),
115 );
116
117 // Run everything concurrently.
118 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
119 join3(usb_fut, usb_future, uart_future).await;
120}
121
122struct Disconnected {}
123
124impl From<EndpointError> for Disconnected {
125 fn from(val: EndpointError) -> Self {
126 match val {
127 EndpointError::BufferOverflow => panic!("Buffer overflow"),
128 EndpointError::Disabled => Disconnected {},
129 }
130 }
131}
132
133/// Read from the USB and write it to the UART TX pipe
134async fn usb_read<'d, T: Instance + 'd>(
135 usb_rx: &mut Receiver<'d, Driver<'d, T>>,
136 uart_pipe_writer: &mut embassy_sync::pipe::Writer<'_, NoopRawMutex, 20>,
137) -> Result<(), Disconnected> {
138 let mut buf = [0; 64];
139 loop {
140 let n = usb_rx.read_packet(&mut buf).await?;
141 let data = &buf[..n];
142 trace!("USB IN: {:x}", data);
143 (*uart_pipe_writer).write(data).await;
144 }
145}
146
147/// Read from the USB TX pipe and write it to the USB
148async fn usb_write<'d, T: Instance + 'd>(
149 usb_tx: &mut Sender<'d, Driver<'d, T>>,
150 usb_pipe_reader: &mut embassy_sync::pipe::Reader<'_, NoopRawMutex, 20>,
151) -> Result<(), Disconnected> {
152 let mut buf = [0; 64];
153 loop {
154 let n = (*usb_pipe_reader).read(&mut buf).await;
155 let data = &buf[..n];
156 trace!("USB OUT: {:x}", data);
157 usb_tx.write_packet(&data).await?;
158 }
159}
160
161/// Read from the UART and write it to the USB TX pipe
162async fn uart_read<PIO: pio::Instance, const SM: usize>(
163 uart_rx: &mut PioUartRx<'_, PIO, SM>,
164 usb_pipe_writer: &mut embassy_sync::pipe::Writer<'_, NoopRawMutex, 20>,
165) -> ! {
166 let mut buf = [0; 64];
167 loop {
168 let n = uart_rx.read(&mut buf).await.expect("UART read error");
169 if n == 0 {
170 continue;
171 }
172 let data = &buf[..n];
173 trace!("UART IN: {:x}", buf);
174 (*usb_pipe_writer).write(data).await;
175 }
176}
177
178/// Read from the UART TX pipe and write it to the UART
179async fn uart_write<PIO: pio::Instance, const SM: usize>(
180 uart_tx: &mut PioUartTx<'_, PIO, SM>,
181 uart_pipe_reader: &mut embassy_sync::pipe::Reader<'_, NoopRawMutex, 20>,
182) -> ! {
183 let mut buf = [0; 64];
184 loop {
185 let n = (*uart_pipe_reader).read(&mut buf).await;
186 let data = &buf[..n];
187 trace!("UART OUT: {:x}", data);
188 let _ = uart_tx.write(&data).await;
189 }
190}