2023-05-01 18:15:46 +02:00
|
|
|
// required-features: not-gpdma
|
|
|
|
|
2023-04-26 10:51:23 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
2023-05-30 00:10:36 +02:00
|
|
|
#[path = "../common.rs"]
|
|
|
|
mod common;
|
2023-04-26 10:51:23 +02:00
|
|
|
|
2023-05-30 00:10:36 +02:00
|
|
|
use common::*;
|
2023-05-01 18:17:02 +02:00
|
|
|
use defmt::{assert_eq, panic};
|
2023-04-26 10:51:23 +02:00
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_stm32::usart::{Config, DataBits, Parity, RingBufferedUartRx, StopBits, Uart, UartTx};
|
|
|
|
use embassy_time::{Duration, Timer};
|
|
|
|
use rand_chacha::ChaCha8Rng;
|
|
|
|
use rand_core::{RngCore, SeedableRng};
|
|
|
|
|
2023-05-01 18:17:02 +02:00
|
|
|
const DMA_BUF_SIZE: usize = 256;
|
2023-04-26 10:51:23 +02:00
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
let p = embassy_stm32::init(config());
|
|
|
|
info!("Hello World!");
|
|
|
|
|
|
|
|
// Arduino pins D0 and D1
|
|
|
|
// They're connected together with a 1K resistor.
|
2023-09-25 22:34:18 +02:00
|
|
|
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);
|
2023-04-26 10:51:23 +02:00
|
|
|
|
|
|
|
// To run this test, use the saturating_serial test utility to saturate the serial port
|
|
|
|
|
|
|
|
let mut config = Config::default();
|
2023-05-01 18:17:02 +02:00
|
|
|
// this is the fastest we can go without tuning RCC
|
|
|
|
// some chips have default pclk=8mhz, and uart can run at max pclk/16
|
|
|
|
config.baudrate = 500_000;
|
2023-04-26 10:51:23 +02:00
|
|
|
config.data_bits = DataBits::DataBits8;
|
|
|
|
config.stop_bits = StopBits::STOP1;
|
|
|
|
config.parity = Parity::ParityNone;
|
|
|
|
|
2023-09-26 00:14:52 +02:00
|
|
|
let usart = Uart::new(usart, rx, tx, irq, tx_dma, rx_dma, config).unwrap();
|
2023-04-26 10:51:23 +02:00
|
|
|
let (tx, rx) = usart.split();
|
2023-04-27 10:48:38 +02:00
|
|
|
static mut DMA_BUF: [u8; DMA_BUF_SIZE] = [0; DMA_BUF_SIZE];
|
2023-04-26 10:51:23 +02:00
|
|
|
let dma_buf = unsafe { DMA_BUF.as_mut() };
|
|
|
|
let rx = rx.into_ring_buffered(dma_buf);
|
|
|
|
|
|
|
|
info!("Spawning tasks");
|
|
|
|
spawner.spawn(transmit_task(tx)).unwrap();
|
|
|
|
spawner.spawn(receive_task(rx)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2023-09-25 22:34:18 +02:00
|
|
|
async fn transmit_task(mut tx: UartTx<'static, peris::UART, peris::UART_TX_DMA>) {
|
2023-05-02 19:35:02 +02:00
|
|
|
// workaround https://github.com/embassy-rs/embassy/issues/1426
|
|
|
|
Timer::after(Duration::from_millis(100) as _).await;
|
|
|
|
|
2023-04-26 10:51:23 +02:00
|
|
|
let mut rng = ChaCha8Rng::seed_from_u64(1337);
|
|
|
|
|
|
|
|
info!("Starting random transmissions into void...");
|
|
|
|
|
|
|
|
let mut i: u8 = 0;
|
|
|
|
loop {
|
2023-05-02 19:35:02 +02:00
|
|
|
let mut buf = [0; 256];
|
2023-05-01 18:17:02 +02:00
|
|
|
let len = 1 + (rng.next_u32() as usize % buf.len());
|
2023-04-26 10:51:23 +02:00
|
|
|
for b in &mut buf[..len] {
|
|
|
|
*b = i;
|
|
|
|
i = i.wrapping_add(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.write(&buf[..len]).await.unwrap();
|
2023-05-01 18:17:02 +02:00
|
|
|
Timer::after(Duration::from_micros((rng.next_u32() % 1000) as _)).await;
|
2023-04-26 10:51:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
2023-09-25 22:34:18 +02:00
|
|
|
async fn receive_task(mut rx: RingBufferedUartRx<'static, peris::UART, peris::UART_RX_DMA>) {
|
2023-04-26 10:51:23 +02:00
|
|
|
info!("Ready to receive...");
|
|
|
|
|
|
|
|
let mut rng = ChaCha8Rng::seed_from_u64(1337);
|
|
|
|
|
|
|
|
let mut i = 0;
|
2023-05-01 18:17:02 +02:00
|
|
|
let mut expected = 0;
|
2023-04-26 10:51:23 +02:00
|
|
|
loop {
|
2023-05-02 19:35:02 +02:00
|
|
|
let mut buf = [0; 256];
|
2023-05-01 18:17:02 +02:00
|
|
|
let max_len = 1 + (rng.next_u32() as usize % buf.len());
|
2023-04-27 10:48:38 +02:00
|
|
|
let received = match rx.read(&mut buf[..max_len]).await {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
2023-05-01 18:17:02 +02:00
|
|
|
panic!("Test fail! read error: {:?}", e);
|
2023-04-27 10:48:38 +02:00
|
|
|
}
|
|
|
|
};
|
2023-04-26 10:51:23 +02:00
|
|
|
|
|
|
|
for byte in &buf[..received] {
|
2023-05-01 18:17:02 +02:00
|
|
|
assert_eq!(*byte, expected);
|
|
|
|
expected = expected.wrapping_add(1);
|
2023-04-26 10:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if received < max_len {
|
2023-05-01 18:17:02 +02:00
|
|
|
Timer::after(Duration::from_micros((rng.next_u32() % 1000) as _)).await;
|
2023-04-26 10:51:23 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 18:17:02 +02:00
|
|
|
i += received;
|
|
|
|
|
|
|
|
if i > 100000 {
|
|
|
|
info!("Test OK!");
|
|
|
|
cortex_m::asm::bkpt();
|
2023-04-26 10:51:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|