diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/src/bin/usart_split.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/stm32h7/src/bin/usart_split.rs b/examples/stm32h7/src/bin/usart_split.rs new file mode 100644 index 000000000..b112290f9 --- /dev/null +++ b/examples/stm32h7/src/bin/usart_split.rs | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use embassy::blocking_mutex::raw::NoopRawMutex; | ||
| 8 | use embassy::channel::mpsc::{self, Channel, Sender}; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy::util::Forever; | ||
| 11 | use embassy_stm32::dma::NoDma; | ||
| 12 | use embassy_stm32::{ | ||
| 13 | peripherals::{DMA1_CH1, UART7}, | ||
| 14 | usart::{Config, Uart, UartRx}, | ||
| 15 | Peripherals, | ||
| 16 | }; | ||
| 17 | use example_common::*; | ||
| 18 | |||
| 19 | #[embassy::task] | ||
| 20 | async fn writer(mut usart: Uart<'static, UART7, NoDma, NoDma>) { | ||
| 21 | unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n")); | ||
| 22 | info!("wrote Hello, starting echo"); | ||
| 23 | |||
| 24 | let mut buf = [0u8; 1]; | ||
| 25 | loop { | ||
| 26 | unwrap!(usart.blocking_read(&mut buf)); | ||
| 27 | unwrap!(usart.blocking_write(&buf)); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | static CHANNEL: Forever<Channel<NoopRawMutex, [u8; 8], 1>> = Forever::new(); | ||
| 32 | |||
| 33 | #[embassy::main] | ||
| 34 | async fn main(spawner: Spawner, p: Peripherals) -> ! { | ||
| 35 | info!("Hello World!"); | ||
| 36 | |||
| 37 | let config = Config::default(); | ||
| 38 | let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, p.DMA1_CH0, p.DMA1_CH1, config); | ||
| 39 | unwrap!(usart.blocking_write(b"Type 8 chars to echo!\r\n")); | ||
| 40 | |||
| 41 | let (mut tx, rx) = usart.split(); | ||
| 42 | |||
| 43 | let c = CHANNEL.put(Channel::new()); | ||
| 44 | let (s, mut r) = mpsc::split(c); | ||
| 45 | |||
| 46 | unwrap!(spawner.spawn(reader(rx, s))); | ||
| 47 | |||
| 48 | loop { | ||
| 49 | if let Some(buf) = r.recv().await { | ||
| 50 | info!("writing..."); | ||
| 51 | unwrap!(tx.write(&buf).await); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | #[embassy::task] | ||
| 57 | async fn reader( | ||
| 58 | mut rx: UartRx<'static, UART7, DMA1_CH1>, | ||
| 59 | s: Sender<'static, NoopRawMutex, [u8; 8], 1>, | ||
| 60 | ) { | ||
| 61 | let mut buf = [0; 8]; | ||
| 62 | loop { | ||
| 63 | info!("reading..."); | ||
| 64 | unwrap!(rx.read(&mut buf).await); | ||
| 65 | unwrap!(s.send(buf).await); | ||
| 66 | } | ||
| 67 | } | ||
