diff options
| author | i509VCB <[email protected]> | 2025-04-06 21:13:49 -0500 |
|---|---|---|
| committer | i509VCB <[email protected]> | 2025-04-06 21:15:42 -0500 |
| commit | 1e23b8114bb1f4b9e092bc50b3cfe4bd2f7ebdb6 (patch) | |
| tree | 78f39de51ef1e399b9e4b0d3786a095134838790 | |
| parent | 717fbc1cd9a038d6601721a6e84f58be264ee624 (diff) | |
mspm0: add uart tests
This also fixes a bug in the uart clock calculation where it could select an oversampling faster than what the hardware is providing.
| -rwxr-xr-x | ci.sh | 4 | ||||
| -rw-r--r-- | embassy-mspm0/src/uart.rs | 32 | ||||
| -rw-r--r-- | tests/mspm0/.cargo/config.toml | 8 | ||||
| -rw-r--r-- | tests/mspm0/Cargo.toml | 58 | ||||
| -rw-r--r-- | tests/mspm0/build.rs | 24 | ||||
| -rw-r--r-- | tests/mspm0/memory_g3507.x | 6 | ||||
| -rw-r--r-- | tests/mspm0/src/bin/uart.rs | 83 |
7 files changed, 212 insertions, 3 deletions
| @@ -308,6 +308,7 @@ cargo batch \ | |||
| 308 | --- build --release --manifest-path tests/nrf/Cargo.toml --target thumbv7em-none-eabi --features nrf52840 --artifact-dir out/tests/nrf52840-dk \ | 308 | --- build --release --manifest-path tests/nrf/Cargo.toml --target thumbv7em-none-eabi --features nrf52840 --artifact-dir out/tests/nrf52840-dk \ |
| 309 | --- build --release --manifest-path tests/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nrf5340 --artifact-dir out/tests/nrf5340-dk \ | 309 | --- build --release --manifest-path tests/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nrf5340 --artifact-dir out/tests/nrf5340-dk \ |
| 310 | --- build --release --manifest-path tests/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nrf9160 --artifact-dir out/tests/nrf9160-dk \ | 310 | --- build --release --manifest-path tests/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nrf9160 --artifact-dir out/tests/nrf9160-dk \ |
| 311 | --- build --release --manifest-path tests/mspm0/Cargo.toml --target thumbv6m-none-eabi --features mspm0g3507 --artifact-dir out/tests/mspm0g3507 \ | ||
| 311 | --- build --release --manifest-path tests/riscv32/Cargo.toml --target riscv32imac-unknown-none-elf \ | 312 | --- build --release --manifest-path tests/riscv32/Cargo.toml --target riscv32imac-unknown-none-elf \ |
| 312 | $BUILD_EXTRA | 313 | $BUILD_EXTRA |
| 313 | 314 | ||
| @@ -322,6 +323,9 @@ rm -rf out/tests/stm32f103c8 | |||
| 322 | rm -rf out/tests/nrf52840-dk | 323 | rm -rf out/tests/nrf52840-dk |
| 323 | rm -rf out/tests/nrf52833-dk | 324 | rm -rf out/tests/nrf52833-dk |
| 324 | 325 | ||
| 326 | # disabled because these boards are not on the shelf | ||
| 327 | rm -rf out/tests/mspm0g3507 | ||
| 328 | |||
| 325 | rm out/tests/stm32wb55rg/wpan_mac | 329 | rm out/tests/stm32wb55rg/wpan_mac |
| 326 | rm out/tests/stm32wb55rg/wpan_ble | 330 | rm out/tests/stm32wb55rg/wpan_ble |
| 327 | 331 | ||
diff --git a/embassy-mspm0/src/uart.rs b/embassy-mspm0/src/uart.rs index 45094a000..bc1d2e343 100644 --- a/embassy-mspm0/src/uart.rs +++ b/embassy-mspm0/src/uart.rs | |||
| @@ -869,7 +869,7 @@ fn set_baudrate_inner(regs: Regs, clock: u32, baudrate: u32) -> Result<(), Confi | |||
| 869 | // maximum speed is limited to UARTclk/16." | 869 | // maximum speed is limited to UARTclk/16." |
| 870 | // | 870 | // |
| 871 | // Based on these requirements, prioritize higher oversampling first to increase tolerance to clock | 871 | // Based on these requirements, prioritize higher oversampling first to increase tolerance to clock |
| 872 | // deviation. If no valid BRD valud can be found satisifying the highest sample rate, then reduce | 872 | // deviation. If no valid BRD value can be found satisifying the highest sample rate, then reduce |
| 873 | // sample rate until valid parameters are found. | 873 | // sample rate until valid parameters are found. |
| 874 | const OVS: [(u8, vals::Hse); 3] = [(16, vals::Hse::OVS16), (8, vals::Hse::OVS8), (3, vals::Hse::OVS3)]; | 874 | const OVS: [(u8, vals::Hse); 3] = [(16, vals::Hse::OVS16), (8, vals::Hse::OVS8), (3, vals::Hse::OVS3)]; |
| 875 | 875 | ||
| @@ -882,21 +882,47 @@ fn set_baudrate_inner(regs: Regs, clock: u32, baudrate: u32) -> Result<(), Confi | |||
| 882 | }; | 882 | }; |
| 883 | let mut found = None; | 883 | let mut found = None; |
| 884 | 884 | ||
| 885 | for &(oversampling, hse_value) in &OVS { | 885 | 'outer: for &(oversampling, hse_value) in &OVS { |
| 886 | if matches!(hse_value, vals::Hse::OVS3) && !x3_invalid { | 886 | if matches!(hse_value, vals::Hse::OVS3) && x3_invalid { |
| 887 | continue; | ||
| 888 | } | ||
| 889 | |||
| 890 | // Verify that the selected oversampling does not require a clock faster than what the hardware | ||
| 891 | // is provided. | ||
| 892 | let Some(min_clock) = baudrate.checked_mul(oversampling as u32) else { | ||
| 893 | trace!( | ||
| 894 | "{}x oversampling would cause overflow for clock: {} Hz", | ||
| 895 | oversampling, | ||
| 896 | clock | ||
| 897 | ); | ||
| 898 | continue; | ||
| 899 | }; | ||
| 900 | |||
| 901 | if min_clock > clock { | ||
| 902 | trace!("{} oversampling is too high for clock: {} Hz", oversampling, clock); | ||
| 887 | continue; | 903 | continue; |
| 888 | } | 904 | } |
| 889 | 905 | ||
| 890 | for &(div, div_value) in &DIVS { | 906 | for &(div, div_value) in &DIVS { |
| 907 | trace!( | ||
| 908 | "Trying div: {}, oversampling {} for {} baud", | ||
| 909 | div, | ||
| 910 | oversampling, | ||
| 911 | baudrate | ||
| 912 | ); | ||
| 913 | |||
| 891 | let Some((ibrd, fbrd)) = calculate_brd(clock, div, baudrate, oversampling) else { | 914 | let Some((ibrd, fbrd)) = calculate_brd(clock, div, baudrate, oversampling) else { |
| 915 | trace!("Calculating BRD overflowed: trying another divider"); | ||
| 892 | continue; | 916 | continue; |
| 893 | }; | 917 | }; |
| 894 | 918 | ||
| 895 | if ibrd < MIN_IBRD || fbrd > MAX_FBRD { | 919 | if ibrd < MIN_IBRD || fbrd > MAX_FBRD { |
| 920 | trace!("BRD was invalid: trying another divider"); | ||
| 896 | continue; | 921 | continue; |
| 897 | } | 922 | } |
| 898 | 923 | ||
| 899 | found = Some((hse_value, div_value, ibrd, fbrd)); | 924 | found = Some((hse_value, div_value, ibrd, fbrd)); |
| 925 | break 'outer; | ||
| 900 | } | 926 | } |
| 901 | } | 927 | } |
| 902 | 928 | ||
diff --git a/tests/mspm0/.cargo/config.toml b/tests/mspm0/.cargo/config.toml new file mode 100644 index 000000000..825bf3ae9 --- /dev/null +++ b/tests/mspm0/.cargo/config.toml | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | runner = "teleprobe local run --chip MSPM0G3507 --protocol swd --elf" | ||
| 3 | |||
| 4 | [build] | ||
| 5 | target = "thumbv6m-none-eabi" | ||
| 6 | |||
| 7 | [env] | ||
| 8 | DEFMT_LOG = "trace,embassy_hal_internal=debug" | ||
diff --git a/tests/mspm0/Cargo.toml b/tests/mspm0/Cargo.toml new file mode 100644 index 000000000..0566807d7 --- /dev/null +++ b/tests/mspm0/Cargo.toml | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "embassy-mspm0-tests" | ||
| 4 | version = "0.1.0" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [features] | ||
| 8 | mspm0g3507 = [ "embassy-mspm0/mspm0g350x" ] | ||
| 9 | |||
| 10 | [dependencies] | ||
| 11 | teleprobe-meta = "1.1" | ||
| 12 | |||
| 13 | embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = [ "defmt" ] } | ||
| 14 | embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = [ "arch-cortex-m", "executor-thread", "defmt" ] } | ||
| 15 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = [ "defmt" ] } | ||
| 16 | embassy-mspm0 = { version = "0.1.0", path = "../../embassy-mspm0", features = [ "rt", "defmt", "unstable-pac", "time-driver-any" ] } | ||
| 17 | embassy-embedded-hal = { version = "0.3.0", path = "../../embassy-embedded-hal/"} | ||
| 18 | |||
| 19 | defmt = "1.0.1" | ||
| 20 | defmt-rtt = "1.0.0" | ||
| 21 | |||
| 22 | cortex-m = { version = "0.7.6", features = [ "inline-asm", "critical-section-single-core" ]} | ||
| 23 | cortex-m-rt = "0.7.0" | ||
| 24 | embedded-hal = { package = "embedded-hal", version = "1.0" } | ||
| 25 | embedded-hal-async = { version = "1.0" } | ||
| 26 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
| 27 | static_cell = "2" | ||
| 28 | portable-atomic = { version = "1.5", features = ["critical-section"] } | ||
| 29 | |||
| 30 | [profile.dev] | ||
| 31 | debug = 2 | ||
| 32 | debug-assertions = true | ||
| 33 | opt-level = 's' | ||
| 34 | overflow-checks = true | ||
| 35 | |||
| 36 | [profile.release] | ||
| 37 | codegen-units = 1 | ||
| 38 | debug = 2 | ||
| 39 | debug-assertions = false | ||
| 40 | incremental = false | ||
| 41 | lto = "fat" | ||
| 42 | opt-level = 's' | ||
| 43 | overflow-checks = false | ||
| 44 | |||
| 45 | # do not optimize proc-macro crates = faster builds from scratch | ||
| 46 | [profile.dev.build-override] | ||
| 47 | codegen-units = 8 | ||
| 48 | debug = false | ||
| 49 | debug-assertions = false | ||
| 50 | opt-level = 0 | ||
| 51 | overflow-checks = false | ||
| 52 | |||
| 53 | [profile.release.build-override] | ||
| 54 | codegen-units = 8 | ||
| 55 | debug = false | ||
| 56 | debug-assertions = false | ||
| 57 | opt-level = 0 | ||
| 58 | overflow-checks = false | ||
diff --git a/tests/mspm0/build.rs b/tests/mspm0/build.rs new file mode 100644 index 000000000..57b592abf --- /dev/null +++ b/tests/mspm0/build.rs | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | use std::error::Error; | ||
| 2 | use std::path::PathBuf; | ||
| 3 | use std::{env, fs}; | ||
| 4 | |||
| 5 | fn main() -> Result<(), Box<dyn Error>> { | ||
| 6 | let out = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
| 7 | |||
| 8 | #[cfg(feature = "mspm0g3507")] | ||
| 9 | let memory_x = include_bytes!("memory_g3507.x"); | ||
| 10 | |||
| 11 | fs::write(out.join("memory.x"), memory_x).unwrap(); | ||
| 12 | |||
| 13 | println!("cargo:rustc-link-search={}", out.display()); | ||
| 14 | println!("cargo:rerun-if-changed=link_ram.x"); | ||
| 15 | // copy main linker script. | ||
| 16 | fs::write(out.join("link_ram.x"), include_bytes!("../link_ram_cortex_m.x")).unwrap(); | ||
| 17 | |||
| 18 | println!("cargo:rustc-link-arg-bins=--nmagic"); | ||
| 19 | println!("cargo:rustc-link-arg-bins=-Tlink_ram.x"); | ||
| 20 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 21 | println!("cargo:rustc-link-arg-bins=-Tteleprobe.x"); | ||
| 22 | |||
| 23 | Ok(()) | ||
| 24 | } | ||
diff --git a/tests/mspm0/memory_g3507.x b/tests/mspm0/memory_g3507.x new file mode 100644 index 000000000..37e381fbd --- /dev/null +++ b/tests/mspm0/memory_g3507.x | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | FLASH : ORIGIN = 0x00000000, LENGTH = 128K | ||
| 4 | /* Select non-parity range of SRAM due to SRAM_ERR_01 errata in SLAZ758 */ | ||
| 5 | RAM : ORIGIN = 0x20200000, LENGTH = 32K | ||
| 6 | } | ||
diff --git a/tests/mspm0/src/bin/uart.rs b/tests/mspm0/src/bin/uart.rs new file mode 100644 index 000000000..458129d44 --- /dev/null +++ b/tests/mspm0/src/bin/uart.rs | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | #[cfg(feature = "mspm0g3507")] | ||
| 5 | teleprobe_meta::target!(b"lp-mspm0g3507"); | ||
| 6 | |||
| 7 | use defmt::{assert_eq, unwrap, *}; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_mspm0::mode::Blocking; | ||
| 10 | use embassy_mspm0::uart::{ClockSel, Config, Error, Uart}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | fn read<const N: usize>(uart: &mut Uart<'_, Blocking>) -> Result<[u8; N], Error> { | ||
| 14 | let mut buf = [255; N]; | ||
| 15 | uart.blocking_read(&mut buf)?; | ||
| 16 | Ok(buf) | ||
| 17 | } | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async fn main(_spawner: Spawner) { | ||
| 21 | let p = embassy_mspm0::init(Default::default()); | ||
| 22 | info!("Hello World!"); | ||
| 23 | |||
| 24 | // TODO: Allow creating a looped-back UART (so pins are not needed). | ||
| 25 | // Do not select default UART since the virtual COM port is attached to UART0. | ||
| 26 | #[cfg(feature = "mspm0g3507")] | ||
| 27 | let (mut tx, mut rx, mut uart) = (p.PA8, p.PA9, p.UART1); | ||
| 28 | |||
| 29 | const MFCLK_BUAD_RATES: &[u32] = &[1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]; | ||
| 30 | |||
| 31 | for &rate in MFCLK_BUAD_RATES { | ||
| 32 | info!("{} baud using MFCLK", rate); | ||
| 33 | |||
| 34 | let mut config = Config::default(); | ||
| 35 | // MSPM0 hardware supports a loopback mode to allow self test. | ||
| 36 | config.loop_back_enable = true; | ||
| 37 | config.baudrate = rate; | ||
| 38 | |||
| 39 | let mut uart = unwrap!(Uart::new_blocking( | ||
| 40 | uart.reborrow(), | ||
| 41 | rx.reborrow(), | ||
| 42 | tx.reborrow(), | ||
| 43 | config | ||
| 44 | )); | ||
| 45 | |||
| 46 | // We can't send too many bytes, they have to fit in the FIFO. | ||
| 47 | // This is because we aren't sending+receiving at the same time. | ||
| 48 | |||
| 49 | let data = [0xC0, 0xDE]; | ||
| 50 | unwrap!(uart.blocking_write(&data)); | ||
| 51 | assert_eq!(unwrap!(read(&mut uart)), data); | ||
| 52 | } | ||
| 53 | |||
| 54 | // 9600 is the maximum possible value for 32.768 kHz. | ||
| 55 | const LFCLK_BAUD_RATES: &[u32] = &[1200, 2400, 4800, 9600]; | ||
| 56 | |||
| 57 | for &rate in LFCLK_BAUD_RATES { | ||
| 58 | info!("{} baud using LFCLK", rate); | ||
| 59 | |||
| 60 | let mut config = Config::default(); | ||
| 61 | // MSPM0 hardware supports a loopback mode to allow self test. | ||
| 62 | config.loop_back_enable = true; | ||
| 63 | config.baudrate = rate; | ||
| 64 | config.clock_source = ClockSel::LfClk; | ||
| 65 | |||
| 66 | let mut uart = expect!(Uart::new_blocking( | ||
| 67 | uart.reborrow(), | ||
| 68 | rx.reborrow(), | ||
| 69 | tx.reborrow(), | ||
| 70 | config, | ||
| 71 | )); | ||
| 72 | |||
| 73 | // We can't send too many bytes, they have to fit in the FIFO. | ||
| 74 | // This is because we aren't sending+receiving at the same time. | ||
| 75 | |||
| 76 | let data = [0xC0, 0xDE]; | ||
| 77 | unwrap!(uart.blocking_write(&data)); | ||
| 78 | assert_eq!(unwrap!(read(&mut uart)), data); | ||
| 79 | } | ||
| 80 | |||
| 81 | info!("Test OK"); | ||
| 82 | cortex_m::asm::bkpt(); | ||
| 83 | } | ||
