diff options
| -rw-r--r-- | embassy-rp/src/uart/buffered.rs | 7 | ||||
| -rw-r--r-- | examples/rp/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/uart_buffered_split.rs | 57 |
3 files changed, 65 insertions, 1 deletions
diff --git a/embassy-rp/src/uart/buffered.rs b/embassy-rp/src/uart/buffered.rs index 32029f81e..e89970d11 100644 --- a/embassy-rp/src/uart/buffered.rs +++ b/embassy-rp/src/uart/buffered.rs | |||
| @@ -126,6 +126,13 @@ impl<'d, T: Instance> BufferedUart<'d, T> { | |||
| 126 | 126 | ||
| 127 | Self { phantom: PhantomData } | 127 | Self { phantom: PhantomData } |
| 128 | } | 128 | } |
| 129 | |||
| 130 | pub fn split(&mut self) -> (BufferedUartRx<'d, T>, BufferedUartTx<'d, T>) { | ||
| 131 | ( | ||
| 132 | BufferedUartRx { phantom: PhantomData }, | ||
| 133 | BufferedUartTx { phantom: PhantomData }, | ||
| 134 | ) | ||
| 135 | } | ||
| 129 | } | 136 | } |
| 130 | 137 | ||
| 131 | impl<'d, T: Instance> BufferedUartRx<'d, T> { | 138 | impl<'d, T: Instance> BufferedUartRx<'d, T> { |
diff --git a/examples/rp/.cargo/config.toml b/examples/rp/.cargo/config.toml index 3d6051389..7880c0955 100644 --- a/examples/rp/.cargo/config.toml +++ b/examples/rp/.cargo/config.toml | |||
| @@ -5,4 +5,4 @@ runner = "probe-run --chip RP2040" | |||
| 5 | target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ | 5 | target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ |
| 6 | 6 | ||
| 7 | [env] | 7 | [env] |
| 8 | DEFMT_LOG = "trace" | 8 | DEFMT_LOG = "info" |
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..36f31c906 --- /dev/null +++ b/examples/rp/src/bin/uart_buffered_split.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_executor::_export::StaticCell; | ||
| 8 | use embassy_rp::interrupt; | ||
| 9 | use embassy_rp::peripherals::UART0; | ||
| 10 | use embassy_rp::uart::{BufferedUart, BufferedUartRx, Config}; | ||
| 11 | use embassy_time::{Duration, Timer}; | ||
| 12 | use embedded_io::asynch::{Read, Write}; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | macro_rules! singleton { | ||
| 16 | ($val:expr) => {{ | ||
| 17 | type T = impl Sized; | ||
| 18 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 19 | let (x,) = STATIC_CELL.init(($val,)); | ||
| 20 | x | ||
| 21 | }}; | ||
| 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 | let irq = interrupt::take!(UART0_IRQ); | ||
| 30 | let tx_buf = &mut singleton!([0u8; 16])[..]; | ||
| 31 | let rx_buf = &mut singleton!([0u8; 16])[..]; | ||
| 32 | let mut uart = BufferedUart::new(uart, irq, 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 | } | ||
