embassy/tests/stm32/src/bin/usart_dma.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

2021-12-08 05:43:39 +01:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
2023-05-30 00:10:36 +02:00
#[path = "../common.rs"]
mod common;
2021-12-08 05:43:39 +01:00
2023-05-30 00:10:36 +02:00
use common::*;
2021-12-08 05:43:39 +01:00
use defmt::assert_eq;
use embassy_executor::Spawner;
use embassy_futures::join::join;
2021-12-08 05:43:39 +01:00
use embassy_stm32::usart::{Config, Uart};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(config());
2021-12-08 05:43:39 +01:00
info!("Hello World!");
// Arduino pins D0 and D1
// They're connected together with a 1K resistor.
let usart = peri!(p, UART);
let rx = peri!(p, UART_RX);
let tx = peri!(p, UART_TX);
let rx_dma = peri!(p, UART_RX_DMA);
let tx_dma = peri!(p, UART_TX_DMA);
let irq = irqs!(UART);
2021-12-08 05:43:39 +01:00
let config = Config::default();
let usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config).unwrap();
2021-12-08 05:43:39 +01:00
const LEN: usize = 128;
let mut tx_buf = [0; LEN];
let mut rx_buf = [0; LEN];
2021-12-08 05:43:39 +01:00
let (mut tx, mut rx) = usart.split();
2021-12-08 05:43:39 +01:00
for n in 0..42 {
for i in 0..LEN {
tx_buf[i] = (i ^ n) as u8;
}
let tx_fut = async {
tx.write(&tx_buf).await.unwrap();
};
let rx_fut = async {
rx.read(&mut rx_buf).await.unwrap();
};
2023-05-02 19:35:02 +02:00
// note: rx needs to be polled first, to workaround this bug:
// https://github.com/embassy-rs/embassy/issues/1426
join(rx_fut, tx_fut).await;
assert_eq!(tx_buf, rx_buf);
}
2021-12-08 05:43:39 +01:00
info!("Test OK");
cortex_m::asm::bkpt();
}