diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-02 02:52:37 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-05-02 20:10:09 +0200 |
| commit | 2bb6e93e86af02af97b4fb39197a1d1e87e635b9 (patch) | |
| tree | 75604794f82f36640c2dc0270751c8a1420b1d65 | |
| parent | a61701b7565536e6e1a224d64eafd373f7c18af5 (diff) | |
stm32/usart: add baudrate calc test.
| -rw-r--r-- | tests/stm32/Cargo.toml | 1 | ||||
| -rw-r--r-- | tests/stm32/build.rs | 13 | ||||
| -rw-r--r-- | tests/stm32/src/bin/usart.rs | 77 | ||||
| -rw-r--r-- | tests/stm32/src/example_common.rs | 5 |
4 files changed, 72 insertions, 24 deletions
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 5cd949661..83bf1e9c9 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -3,6 +3,7 @@ edition = "2021" | |||
| 3 | name = "embassy-stm32-tests" | 3 | name = "embassy-stm32-tests" |
| 4 | version = "0.1.0" | 4 | version = "0.1.0" |
| 5 | license = "MIT OR Apache-2.0" | 5 | license = "MIT OR Apache-2.0" |
| 6 | autobins = false | ||
| 6 | 7 | ||
| 7 | [features] | 8 | [features] |
| 8 | stm32f103c8 = ["embassy-stm32/stm32f103c8", "not-gpdma"] # Blue Pill | 9 | stm32f103c8 = ["embassy-stm32/stm32f103c8", "not-gpdma"] # Blue Pill |
diff --git a/tests/stm32/build.rs b/tests/stm32/build.rs index 3e67a7392..4c76a8eeb 100644 --- a/tests/stm32/build.rs +++ b/tests/stm32/build.rs | |||
| @@ -6,15 +6,16 @@ fn main() -> Result<(), Box<dyn Error>> { | |||
| 6 | let out = PathBuf::from(env::var("OUT_DIR").unwrap()); | 6 | let out = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 7 | fs::write(out.join("link_ram.x"), include_bytes!("link_ram.x")).unwrap(); | 7 | fs::write(out.join("link_ram.x"), include_bytes!("link_ram.x")).unwrap(); |
| 8 | println!("cargo:rustc-link-search={}", out.display()); | 8 | println!("cargo:rustc-link-search={}", out.display()); |
| 9 | println!("cargo:rerun-if-changed=link_ram.x"); | ||
| 10 | |||
| 11 | println!("cargo:rustc-link-arg-bins=--nmagic"); | 9 | println!("cargo:rustc-link-arg-bins=--nmagic"); |
| 12 | 10 | ||
| 13 | // too little RAM to run from RAM. | 11 | // too little RAM to run from RAM. |
| 14 | #[cfg(any(feature = "stm32c031c6"))] | 12 | if cfg!(any(feature = "stm32c031c6")) { |
| 15 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); | 13 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); |
| 16 | #[cfg(not(any(feature = "stm32c031c6")))] | 14 | println!("cargo:rerun-if-changed=link.x"); |
| 17 | println!("cargo:rustc-link-arg-bins=-Tlink_ram.x"); | 15 | } else { |
| 16 | println!("cargo:rustc-link-arg-bins=-Tlink_ram.x"); | ||
| 17 | println!("cargo:rerun-if-changed=link_ram.x"); | ||
| 18 | } | ||
| 18 | 19 | ||
| 19 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | 20 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); |
| 20 | 21 | ||
diff --git a/tests/stm32/src/bin/usart.rs b/tests/stm32/src/bin/usart.rs index cca8c42ee..bda2ce9c2 100644 --- a/tests/stm32/src/bin/usart.rs +++ b/tests/stm32/src/bin/usart.rs | |||
| @@ -9,6 +9,7 @@ use embassy_executor::Spawner; | |||
| 9 | use embassy_stm32::dma::NoDma; | 9 | use embassy_stm32::dma::NoDma; |
| 10 | use embassy_stm32::interrupt; | 10 | use embassy_stm32::interrupt; |
| 11 | use embassy_stm32::usart::{Config, Uart}; | 11 | use embassy_stm32::usart::{Config, Uart}; |
| 12 | use embassy_time::{Duration, Instant}; | ||
| 12 | use example_common::*; | 13 | use example_common::*; |
| 13 | 14 | ||
| 14 | #[embassy_executor::main] | 15 | #[embassy_executor::main] |
| @@ -19,36 +20,76 @@ async fn main(_spawner: Spawner) { | |||
| 19 | // Arduino pins D0 and D1 | 20 | // Arduino pins D0 and D1 |
| 20 | // They're connected together with a 1K resistor. | 21 | // They're connected together with a 1K resistor. |
| 21 | #[cfg(feature = "stm32f103c8")] | 22 | #[cfg(feature = "stm32f103c8")] |
| 22 | let (tx, rx, usart, irq) = (p.PA9, p.PA10, p.USART1, interrupt::take!(USART1)); | 23 | let (mut tx, mut rx, mut usart, mut irq) = (p.PA9, p.PA10, p.USART1, interrupt::take!(USART1)); |
| 23 | #[cfg(feature = "stm32g491re")] | 24 | #[cfg(feature = "stm32g491re")] |
| 24 | let (tx, rx, usart, irq) = (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1)); | 25 | let (mut tx, mut rx, mut usart, mut irq) = (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1)); |
| 25 | #[cfg(feature = "stm32g071rb")] | 26 | #[cfg(feature = "stm32g071rb")] |
| 26 | let (tx, rx, usart, irq) = (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1)); | 27 | let (mut tx, mut rx, mut usart, mut irq) = (p.PC4, p.PC5, p.USART1, interrupt::take!(USART1)); |
| 27 | #[cfg(feature = "stm32f429zi")] | 28 | #[cfg(feature = "stm32f429zi")] |
| 28 | let (tx, rx, usart, irq) = (p.PG14, p.PG9, p.USART6, interrupt::take!(USART6)); | 29 | let (mut tx, mut rx, mut usart, mut irq) = (p.PG14, p.PG9, p.USART6, interrupt::take!(USART6)); |
| 29 | #[cfg(feature = "stm32wb55rg")] | 30 | #[cfg(feature = "stm32wb55rg")] |
| 30 | let (tx, rx, usart, irq) = (p.PA2, p.PA3, p.LPUART1, interrupt::take!(LPUART1)); | 31 | let (mut tx, mut rx, mut usart, mut irq) = (p.PA2, p.PA3, p.LPUART1, interrupt::take!(LPUART1)); |
| 31 | #[cfg(feature = "stm32h755zi")] | 32 | #[cfg(feature = "stm32h755zi")] |
| 32 | let (tx, rx, usart, irq) = (p.PB6, p.PB7, p.USART1, interrupt::take!(USART1)); | 33 | let (mut tx, mut rx, mut usart, mut irq) = (p.PB6, p.PB7, p.USART1, interrupt::take!(USART1)); |
| 33 | #[cfg(feature = "stm32u585ai")] | 34 | #[cfg(feature = "stm32u585ai")] |
| 34 | let (tx, rx, usart, irq) = (p.PD8, p.PD9, p.USART3, interrupt::take!(USART3)); | 35 | let (mut tx, mut rx, mut usart, mut irq) = (p.PD8, p.PD9, p.USART3, interrupt::take!(USART3)); |
| 35 | #[cfg(feature = "stm32h563zi")] | 36 | #[cfg(feature = "stm32h563zi")] |
| 36 | let (tx, rx, usart, irq) = (p.PB6, p.PB7, p.LPUART1, interrupt::take!(LPUART1)); | 37 | let (mut tx, mut rx, mut usart, mut irq) = (p.PB6, p.PB7, p.LPUART1, interrupt::take!(LPUART1)); |
| 37 | #[cfg(feature = "stm32c031c6")] | 38 | #[cfg(feature = "stm32c031c6")] |
| 38 | let (tx, rx, usart, irq) = (p.PB6, p.PB7, p.USART1, interrupt::take!(USART1)); | 39 | let (mut tx, mut rx, mut usart, mut irq) = (p.PB6, p.PB7, p.USART1, interrupt::take!(USART1)); |
| 39 | 40 | ||
| 40 | let config = Config::default(); | 41 | { |
| 41 | let mut usart = Uart::new(usart, rx, tx, irq, NoDma, NoDma, config); | 42 | let config = Config::default(); |
| 43 | let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, &mut irq, NoDma, NoDma, config); | ||
| 42 | 44 | ||
| 43 | // We can't send too many bytes, they have to fit in the FIFO. | 45 | // We can't send too many bytes, they have to fit in the FIFO. |
| 44 | // This is because we aren't sending+receiving at the same time. | 46 | // This is because we aren't sending+receiving at the same time. |
| 45 | 47 | ||
| 46 | let data = [0xC0, 0xDE]; | 48 | let data = [0xC0, 0xDE]; |
| 47 | usart.blocking_write(&data).unwrap(); | 49 | usart.blocking_write(&data).unwrap(); |
| 48 | 50 | ||
| 49 | let mut buf = [0; 2]; | 51 | let mut buf = [0; 2]; |
| 50 | usart.blocking_read(&mut buf).unwrap(); | 52 | usart.blocking_read(&mut buf).unwrap(); |
| 51 | assert_eq!(buf, data); | 53 | assert_eq!(buf, data); |
| 54 | } | ||
| 55 | |||
| 56 | // Test that baudrate divider is calculated correctly. | ||
| 57 | // Do it by comparing the time it takes to send a known number of bytes. | ||
| 58 | for baudrate in [ | ||
| 59 | 300, | ||
| 60 | 9600, | ||
| 61 | 115200, | ||
| 62 | 250_000, | ||
| 63 | 337_934, | ||
| 64 | #[cfg(not(feature = "stm32f103c8"))] | ||
| 65 | 1_000_000, | ||
| 66 | #[cfg(not(feature = "stm32f103c8"))] | ||
| 67 | 2_000_000, | ||
| 68 | ] { | ||
| 69 | info!("testing baudrate {}", baudrate); | ||
| 70 | |||
| 71 | let mut config = Config::default(); | ||
| 72 | config.baudrate = baudrate; | ||
| 73 | let mut usart = Uart::new(&mut usart, &mut rx, &mut tx, &mut irq, NoDma, NoDma, config); | ||
| 74 | |||
| 75 | let n = (baudrate as usize / 100).max(64); | ||
| 76 | |||
| 77 | let start = Instant::now(); | ||
| 78 | for _ in 0..n { | ||
| 79 | usart.blocking_write(&[0x00]).unwrap(); | ||
| 80 | } | ||
| 81 | let dur = Instant::now() - start; | ||
| 82 | let want_dur = Duration::from_micros(n as u64 * 10 * 1_000_000 / (baudrate as u64)); | ||
| 83 | let fuzz = want_dur / 5; | ||
| 84 | if dur < want_dur - fuzz || dur > want_dur + fuzz { | ||
| 85 | defmt::panic!( | ||
| 86 | "bad duration for baudrate {}: got {:?} want {:?}", | ||
| 87 | baudrate, | ||
| 88 | dur, | ||
| 89 | want_dur | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | } | ||
| 52 | 93 | ||
| 53 | info!("Test OK"); | 94 | info!("Test OK"); |
| 54 | cortex_m::asm::bkpt(); | 95 | cortex_m::asm::bkpt(); |
diff --git a/tests/stm32/src/example_common.rs b/tests/stm32/src/example_common.rs index a4f8668c7..3d150da60 100644 --- a/tests/stm32/src/example_common.rs +++ b/tests/stm32/src/example_common.rs | |||
| @@ -16,5 +16,10 @@ pub fn config() -> Config { | |||
| 16 | config.rcc.pll1.q_ck = Some(Hertz(100_000_000)); | 16 | config.rcc.pll1.q_ck = Some(Hertz(100_000_000)); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | #[cfg(feature = "stm32u585ai")] | ||
| 20 | { | ||
| 21 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::MSI(embassy_stm32::rcc::MSIRange::Range48mhz); | ||
| 22 | } | ||
| 23 | |||
| 19 | config | 24 | config |
| 20 | } | 25 | } |
