stm32/test: cleanup ringbuffer test, exit on success (transferring 100kb)

This commit is contained in:
Dario Nieuwenhuis 2023-05-01 18:17:02 +02:00
parent 1806422763
commit 7601779693

View File

@ -6,6 +6,7 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::{assert_eq, panic};
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::interrupt; use embassy_stm32::interrupt;
use embassy_stm32::usart::{Config, DataBits, Parity, RingBufferedUartRx, StopBits, Uart, UartTx}; use embassy_stm32::usart::{Config, DataBits, Parity, RingBufferedUartRx, StopBits, Uart, UartTx};
@ -34,9 +35,9 @@ mod board {
} }
#[cfg(feature = "stm32f429zi")] #[cfg(feature = "stm32f429zi")]
mod board { mod board {
pub type Uart = embassy_stm32::peripherals::USART2; pub type Uart = embassy_stm32::peripherals::USART6;
pub type TxDma = embassy_stm32::peripherals::DMA1_CH6; pub type TxDma = embassy_stm32::peripherals::DMA2_CH6;
pub type RxDma = embassy_stm32::peripherals::DMA1_CH5; pub type RxDma = embassy_stm32::peripherals::DMA2_CH1;
} }
#[cfg(feature = "stm32wb55rg")] #[cfg(feature = "stm32wb55rg")]
mod board { mod board {
@ -56,9 +57,14 @@ mod board {
pub type TxDma = embassy_stm32::peripherals::GPDMA1_CH0; pub type TxDma = embassy_stm32::peripherals::GPDMA1_CH0;
pub type RxDma = embassy_stm32::peripherals::GPDMA1_CH1; pub type RxDma = embassy_stm32::peripherals::GPDMA1_CH1;
} }
#[cfg(feature = "stm32c031c6")]
mod board {
pub type Uart = embassy_stm32::peripherals::USART1;
pub type TxDma = embassy_stm32::peripherals::DMA1_CH1;
pub type RxDma = embassy_stm32::peripherals::DMA1_CH2;
}
const ONE_BYTE_DURATION_US: u32 = 9_000_000 / 115200; const DMA_BUF_SIZE: usize = 256;
const DMA_BUF_SIZE: usize = 64;
#[embassy_executor::main] #[embassy_executor::main]
async fn main(spawner: Spawner) { async fn main(spawner: Spawner) {
@ -83,8 +89,14 @@ async fn main(spawner: Spawner) {
let (tx, rx, usart, irq, tx_dma, rx_dma) = let (tx, rx, usart, irq, tx_dma, rx_dma) =
(p.PC4, p.PC5, p.USART1, interrupt::take!(USART1), p.DMA1_CH1, p.DMA1_CH2); (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1), p.DMA1_CH1, p.DMA1_CH2);
#[cfg(feature = "stm32f429zi")] #[cfg(feature = "stm32f429zi")]
let (tx, rx, usart, irq, tx_dma, rx_dma) = let (tx, rx, usart, irq, tx_dma, rx_dma) = (
(p.PA2, p.PA3, p.USART2, interrupt::take!(USART2), p.DMA1_CH6, p.DMA1_CH5); p.PG14,
p.PG9,
p.USART6,
interrupt::take!(USART6),
p.DMA2_CH6,
p.DMA2_CH1,
);
#[cfg(feature = "stm32wb55rg")] #[cfg(feature = "stm32wb55rg")]
let (tx, rx, usart, irq, tx_dma, rx_dma) = ( let (tx, rx, usart, irq, tx_dma, rx_dma) = (
p.PA2, p.PA2,
@ -106,11 +118,16 @@ async fn main(spawner: Spawner) {
p.GPDMA1_CH0, p.GPDMA1_CH0,
p.GPDMA1_CH1, p.GPDMA1_CH1,
); );
#[cfg(feature = "stm32c031c6")]
let (tx, rx, usart, irq, tx_dma, rx_dma) =
(p.PB6, p.PB7, p.USART1, interrupt::take!(USART1), p.DMA1_CH1, p.DMA1_CH2);
// To run this test, use the saturating_serial test utility to saturate the serial port // To run this test, use the saturating_serial test utility to saturate the serial port
let mut config = Config::default(); let mut config = Config::default();
config.baudrate = 115200; // 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;
config.data_bits = DataBits::DataBits8; config.data_bits = DataBits::DataBits8;
config.stop_bits = StopBits::STOP1; config.stop_bits = StopBits::STOP1;
config.parity = Parity::ParityNone; config.parity = Parity::ParityNone;
@ -135,19 +152,14 @@ async fn transmit_task(mut tx: UartTx<'static, board::Uart, board::TxDma>) {
let mut i: u8 = 0; let mut i: u8 = 0;
loop { loop {
let mut buf = [0; 32]; let mut buf = [0; 32];
let len = 1 + (rng.next_u32() as usize % (buf.len() - 1)); let len = 1 + (rng.next_u32() as usize % buf.len());
for b in &mut buf[..len] { for b in &mut buf[..len] {
*b = i; *b = i;
i = i.wrapping_add(1); i = i.wrapping_add(1);
} }
tx.write(&buf[..len]).await.unwrap(); tx.write(&buf[..len]).await.unwrap();
Timer::after(Duration::from_micros((rng.next_u32() % 10000) as _)).await; Timer::after(Duration::from_micros((rng.next_u32() % 1000) as _)).await;
//i += 1;
//if i % 1000 == 0 {
// trace!("Wrote {} times", i);
//}
} }
} }
@ -158,44 +170,31 @@ async fn receive_task(mut rx: RingBufferedUartRx<'static, board::Uart, board::Rx
let mut rng = ChaCha8Rng::seed_from_u64(1337); let mut rng = ChaCha8Rng::seed_from_u64(1337);
let mut i = 0; let mut i = 0;
let mut expected: Option<u8> = None; let mut expected = 0;
loop { loop {
let mut buf = [0; 100]; let mut buf = [0; 100];
let max_len = 1 + (rng.next_u32() as usize % (buf.len() - 1)); let max_len = 1 + (rng.next_u32() as usize % buf.len());
let received = match rx.read(&mut buf[..max_len]).await { let received = match rx.read(&mut buf[..max_len]).await {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {
error!("Test fail! read error: {:?}", e); panic!("Test fail! read error: {:?}", e);
cortex_m::asm::bkpt();
return;
} }
}; };
if expected.is_none() {
info!("Test started");
expected = Some(buf[0]);
}
for byte in &buf[..received] { for byte in &buf[..received] {
if byte != &expected.unwrap() { assert_eq!(*byte, expected);
error!("Test fail! received {}, expected {}", *byte, expected.unwrap()); expected = expected.wrapping_add(1);
cortex_m::asm::bkpt();
return;
}
expected = Some(expected.unwrap().wrapping_add(1));
} }
if received < max_len { if received < max_len {
let byte_count = rng.next_u32() % (DMA_BUF_SIZE as u32); Timer::after(Duration::from_micros((rng.next_u32() % 1000) as _)).await;
let random_delay_us = (byte_count * ONE_BYTE_DURATION_US) as u64;
if random_delay_us > 200 {
Timer::after(Duration::from_micros(random_delay_us - 200)).await;
}
} }
i += 1; i += received;
if i % 1000 == 0 {
trace!("Read {} times", i); if i > 100000 {
info!("Test OK!");
cortex_m::asm::bkpt();
} }
} }
} }