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

117 lines
4.2 KiB
Rust
Raw Normal View History

2021-12-08 05:43:39 +01:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::assert_eq;
use embassy_executor::Spawner;
2021-12-08 05:43:39 +01:00
use embassy_stm32::dma::NoDma;
use embassy_stm32::interrupt;
use embassy_stm32::usart::{Config, Error, Uart};
2023-05-02 02:52:37 +02:00
use embassy_time::{Duration, Instant};
2021-12-08 05:43:39 +01:00
use example_common::*;
#[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.
#[cfg(feature = "stm32f103c8")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PA9, p.PA10, p.USART1, interrupt::take!(USART1));
2021-12-08 05:43:39 +01:00
#[cfg(feature = "stm32g491re")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1));
2021-12-08 05:43:39 +01:00
#[cfg(feature = "stm32g071rb")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1));
2021-12-08 05:43:39 +01:00
#[cfg(feature = "stm32f429zi")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PG14, p.PG9, p.USART6, interrupt::take!(USART6));
2021-12-08 05:43:39 +01:00
#[cfg(feature = "stm32wb55rg")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PA2, p.PA3, p.LPUART1, interrupt::take!(LPUART1));
2021-12-08 05:43:39 +01:00
#[cfg(feature = "stm32h755zi")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PB6, p.PB7, p.USART1, interrupt::take!(USART1));
#[cfg(feature = "stm32u585ai")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PD8, p.PD9, p.USART3, interrupt::take!(USART3));
2023-04-10 15:12:47 +02:00
#[cfg(feature = "stm32h563zi")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PB6, p.PB7, p.LPUART1, interrupt::take!(LPUART1));
2023-04-11 13:37:10 +02:00
#[cfg(feature = "stm32c031c6")]
2023-05-02 02:52:37 +02:00
let (mut tx, mut rx, mut usart, mut irq) = (p.PB6, p.PB7, p.USART1, interrupt::take!(USART1));
2021-12-08 05:43:39 +01:00
2023-05-02 02:52:37 +02:00
{
let config = Config::default();
let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, &mut irq, NoDma, NoDma, config);
2021-12-08 05:43:39 +01:00
2023-05-02 02:52:37 +02:00
// We can't send too many bytes, they have to fit in the FIFO.
// This is because we aren't sending+receiving at the same time.
2021-12-08 05:43:39 +01:00
2023-05-02 02:52:37 +02:00
let data = [0xC0, 0xDE];
usart.blocking_write(&data).unwrap();
2021-12-08 05:43:39 +01:00
2023-05-02 02:52:37 +02:00
let mut buf = [0; 2];
usart.blocking_read(&mut buf).unwrap();
assert_eq!(buf, data);
}
// Test error handling with with an overflow error
{
let config = Config::default();
let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, &mut irq, NoDma, NoDma, config);
// Send enough bytes to fill the RX FIFOs off all USART versions.
let data = [0xC0, 0xDE, 0x12, 0x23, 0x34];
usart.blocking_write(&data).unwrap();
usart.blocking_flush().unwrap();
// The error should be reported first.
let mut buf = [0; 1];
let err = usart.blocking_read(&mut buf);
assert_eq!(err, Err(Error::Overrun));
// At least the first data byte should still be available on all USART versions.
usart.blocking_read(&mut buf).unwrap();
assert_eq!(buf[0], data[0]);
}
2023-05-02 02:52:37 +02:00
// Test that baudrate divider is calculated correctly.
// Do it by comparing the time it takes to send a known number of bytes.
for baudrate in [
300,
9600,
115200,
250_000,
337_934,
#[cfg(not(feature = "stm32f103c8"))]
1_000_000,
#[cfg(not(feature = "stm32f103c8"))]
2_000_000,
] {
info!("testing baudrate {}", baudrate);
let mut config = Config::default();
config.baudrate = baudrate;
let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, &mut irq, NoDma, NoDma, config);
let n = (baudrate as usize / 100).max(64);
let start = Instant::now();
for _ in 0..n {
usart.blocking_write(&[0x00]).unwrap();
}
let dur = Instant::now() - start;
let want_dur = Duration::from_micros(n as u64 * 10 * 1_000_000 / (baudrate as u64));
let fuzz = want_dur / 5;
if dur < want_dur - fuzz || dur > want_dur + fuzz {
defmt::panic!(
"bad duration for baudrate {}: got {:?} want {:?}",
baudrate,
dur,
want_dur
);
}
}
2021-12-08 05:43:39 +01:00
info!("Test OK");
cortex_m::asm::bkpt();
}