diff options
| author | HybridChild <[email protected]> | 2025-11-13 14:36:31 +0100 |
|---|---|---|
| committer | HybridChild <[email protected]> | 2025-11-13 14:40:16 +0100 |
| commit | 2553ced205d49d2890e000069f5a170b75d267a9 (patch) | |
| tree | 14c0ecadef3d6b2bf5176f5f4eb1ead3bf79d6fc /tests | |
| parent | 00ca12b20f330101cd47ce3947aa186be8f72dd5 (diff) | |
stm32: Move i2c_master test to examples
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/stm32/Cargo.toml | 7 | ||||
| -rw-r--r-- | tests/stm32/build.rs | 1 | ||||
| -rw-r--r-- | tests/stm32/src/bin/i2c_v2_master.rs | 608 | ||||
| -rw-r--r-- | tests/stm32/src/common.rs | 25 |
4 files changed, 0 insertions, 641 deletions
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 1912a772c..b92b47be2 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -35,7 +35,6 @@ stm32wb55rg = ["embassy-stm32/stm32wb55rg", "chrono", "not-gpdma", "ble", "mac" | |||
| 35 | stm32wba52cg = ["embassy-stm32/stm32wba52cg", "spi-v345", "chrono", "rng", "hash"] | 35 | stm32wba52cg = ["embassy-stm32/stm32wba52cg", "spi-v345", "chrono", "rng", "hash"] |
| 36 | stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"] | 36 | stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"] |
| 37 | stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"] | 37 | stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"] |
| 38 | stm32f072rb = ["embassy-stm32/stm32f072rb", "cm0", "not-gpdma", "chrono"] | ||
| 39 | stm32h503rb = ["embassy-stm32/stm32h503rb", "spi-v345", "rng", "stop"] | 38 | stm32h503rb = ["embassy-stm32/stm32h503rb", "spi-v345", "rng", "stop"] |
| 40 | stm32h7s3l8 = ["embassy-stm32/stm32h7s3l8", "spi-v345", "rng", "cordic", "hash-v34"] # TODO: fdcan crashes, cryp dma hangs. | 39 | stm32h7s3l8 = ["embassy-stm32/stm32h7s3l8", "spi-v345", "rng", "cordic", "hash-v34"] # TODO: fdcan crashes, cryp dma hangs. |
| 41 | stm32u083rc = ["embassy-stm32/stm32u083rc", "cm0", "rng", "chrono"] | 40 | stm32u083rc = ["embassy-stm32/stm32u083rc", "cm0", "rng", "chrono"] |
| @@ -161,11 +160,6 @@ path = "src/bin/hash.rs" | |||
| 161 | required-features = [ "hash",] | 160 | required-features = [ "hash",] |
| 162 | 161 | ||
| 163 | [[bin]] | 162 | [[bin]] |
| 164 | name = "i2c_v2_master" | ||
| 165 | path = "src/bin/i2c_v2_master.rs" | ||
| 166 | required-features = [ "stm32f072rb",] | ||
| 167 | |||
| 168 | [[bin]] | ||
| 169 | name = "rng" | 163 | name = "rng" |
| 170 | path = "src/bin/rng.rs" | 164 | path = "src/bin/rng.rs" |
| 171 | required-features = [ "rng",] | 165 | required-features = [ "rng",] |
| @@ -291,7 +285,6 @@ build = [ | |||
| 291 | { target = "thumbv7em-none-eabi", features = ["stm32wl55jc"], artifact-dir = "out/tests/stm32wl55jc" }, | 285 | { target = "thumbv7em-none-eabi", features = ["stm32wl55jc"], artifact-dir = "out/tests/stm32wl55jc" }, |
| 292 | { target = "thumbv7em-none-eabi", features = ["stm32h7s3l8"], artifact-dir = "out/tests/stm32h7s3l8" }, | 286 | { target = "thumbv7em-none-eabi", features = ["stm32h7s3l8"], artifact-dir = "out/tests/stm32h7s3l8" }, |
| 293 | { target = "thumbv6m-none-eabi", features = ["stm32f091rc"], artifact-dir = "out/tests/stm32f091rc" }, | 287 | { target = "thumbv6m-none-eabi", features = ["stm32f091rc"], artifact-dir = "out/tests/stm32f091rc" }, |
| 294 | { target = "thumbv6m-none-eabi", features = ["stm32f072rb"], artifact-dir = "out/tests/stm32f072rb" }, | ||
| 295 | { target = "thumbv8m.main-none-eabihf", features = ["stm32h503rb"], artifact-dir = "out/tests/stm32h503rb" }, | 288 | { target = "thumbv8m.main-none-eabihf", features = ["stm32h503rb"], artifact-dir = "out/tests/stm32h503rb" }, |
| 296 | { target = "thumbv6m-none-eabi", features = ["stm32u083rc"], artifact-dir = "out/tests/stm32u083rc" } | 289 | { target = "thumbv6m-none-eabi", features = ["stm32u083rc"], artifact-dir = "out/tests/stm32u083rc" } |
| 297 | ] | 290 | ] |
diff --git a/tests/stm32/build.rs b/tests/stm32/build.rs index 34030c206..556d77a20 100644 --- a/tests/stm32/build.rs +++ b/tests/stm32/build.rs | |||
| @@ -15,7 +15,6 @@ fn main() -> Result<(), Box<dyn Error>> { | |||
| 15 | feature = "stm32c071rb", // 24 kb | 15 | feature = "stm32c071rb", // 24 kb |
| 16 | feature = "stm32l073rz", // 20 kb | 16 | feature = "stm32l073rz", // 20 kb |
| 17 | feature = "stm32h503rb", // 32 kb | 17 | feature = "stm32h503rb", // 32 kb |
| 18 | feature = "stm32f072rb", // 16 kb - I2C v2 test with async too large for RAM | ||
| 19 | // no VTOR, so interrupts can't work when running from RAM | 18 | // no VTOR, so interrupts can't work when running from RAM |
| 20 | feature = "stm32f091rc", | 19 | feature = "stm32f091rc", |
| 21 | )) { | 20 | )) { |
diff --git a/tests/stm32/src/bin/i2c_v2_master.rs b/tests/stm32/src/bin/i2c_v2_master.rs deleted file mode 100644 index 34f9b48d3..000000000 --- a/tests/stm32/src/bin/i2c_v2_master.rs +++ /dev/null | |||
| @@ -1,608 +0,0 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | // required-features: stm32f072rb | ||
| 4 | // | ||
| 5 | // Hardware Setup for NUCLEO-F072RB: | ||
| 6 | // - I2C1 pins: PB8 (SCL), PB9 (SDA) on CN5 connector | ||
| 7 | // - Connect to I2C slave device (e.g., Digilent Analog Discovery I2C slave, or EEPROM) | ||
| 8 | // - Default slave address: 0x50 | ||
| 9 | // - Pull-up resistors: 4.7kΩ on both SCL and SDA | ||
| 10 | // - CN5 Pin 10 (PB8/SCL) and CN5 Pin 9 (PB9/SDA) | ||
| 11 | // | ||
| 12 | // Analog Discovery - Waveforms Setup: | ||
| 13 | // - Increase buffer size: Settings -> Device Manager -> Option 4 | ||
| 14 | // - Run Protocol Analyzer | ||
| 15 | // - Configure as I2C Slave at address 0x50 | ||
| 16 | // - Connect and configure DIO pins for SCL and SDA | ||
| 17 | // - Frequency: 100kHz - [✓] Clock Stretching | ||
| 18 | |||
| 19 | #[path = "../common.rs"] | ||
| 20 | mod common; | ||
| 21 | |||
| 22 | use common::*; | ||
| 23 | use embassy_executor::Spawner; | ||
| 24 | use embassy_stm32::i2c::{Config, I2c, Master}; | ||
| 25 | use embassy_stm32::mode::{Async, Blocking}; | ||
| 26 | use embassy_stm32::time::Hertz; | ||
| 27 | use embassy_time::Timer; | ||
| 28 | use embedded_hal_1::i2c::Operation; | ||
| 29 | |||
| 30 | #[embassy_executor::main] | ||
| 31 | async fn main(_spawner: Spawner) { | ||
| 32 | let p = init(); | ||
| 33 | info!("Run stm32 I2C v2 Master Tests..."); | ||
| 34 | |||
| 35 | let mut i2c_peri = peri!(p, I2C); | ||
| 36 | let mut scl = peri!(p, I2C_SCL); | ||
| 37 | let mut sda = peri!(p, I2C_SDA); | ||
| 38 | |||
| 39 | let mut config = Config::default(); | ||
| 40 | config.frequency = Hertz(100_000); | ||
| 41 | |||
| 42 | // I2C slave address for Analog Discovery or test EEPROM | ||
| 43 | let slave_addr = 0x50u8; | ||
| 44 | |||
| 45 | // Wait for slave device to be ready | ||
| 46 | Timer::after_millis(100).await; | ||
| 47 | |||
| 48 | // ========== BLOCKING DIRECT API TESTS ========== | ||
| 49 | info!("========== BLOCKING DIRECT API TESTS =========="); | ||
| 50 | { | ||
| 51 | let mut i2c = I2c::new_blocking(i2c_peri.reborrow(), scl.reborrow(), sda.reborrow(), config); | ||
| 52 | |||
| 53 | info!("=== Test 1: Direct blocking_write ==="); | ||
| 54 | test_blocking_write(&mut i2c, slave_addr); | ||
| 55 | |||
| 56 | info!("=== Test 2: Direct blocking_read ==="); | ||
| 57 | test_blocking_read(&mut i2c, slave_addr); | ||
| 58 | |||
| 59 | info!("=== Test 3: Direct blocking_write_read ==="); | ||
| 60 | test_blocking_write_read(&mut i2c, slave_addr); | ||
| 61 | |||
| 62 | info!("=== Test 4: Direct blocking_write_vectored ==="); | ||
| 63 | test_blocking_write_vectored(&mut i2c, slave_addr); | ||
| 64 | |||
| 65 | info!("=== Test 5: Large buffer (>255 bytes) ==="); | ||
| 66 | test_blocking_large_buffer(&mut i2c, slave_addr); | ||
| 67 | |||
| 68 | info!("Blocking direct API tests OK"); | ||
| 69 | } | ||
| 70 | |||
| 71 | Timer::after_millis(100).await; | ||
| 72 | |||
| 73 | // ========== BLOCKING TRANSACTION TESTS ========== | ||
| 74 | info!("========== BLOCKING TRANSACTION TESTS =========="); | ||
| 75 | { | ||
| 76 | let mut i2c = I2c::new_blocking(i2c_peri.reborrow(), scl.reborrow(), sda.reborrow(), config); | ||
| 77 | |||
| 78 | info!("=== Test 6: Consecutive Writes (Should Merge) ==="); | ||
| 79 | test_consecutive_writes_blocking(&mut i2c, slave_addr); | ||
| 80 | |||
| 81 | info!("=== Test 7: Consecutive Reads (Should Merge) ==="); | ||
| 82 | test_consecutive_reads_blocking(&mut i2c, slave_addr); | ||
| 83 | |||
| 84 | info!("=== Test 8: Write then Read (RESTART) ==="); | ||
| 85 | test_write_then_read_blocking(&mut i2c, slave_addr); | ||
| 86 | |||
| 87 | info!("=== Test 9: Read then Write (RESTART) ==="); | ||
| 88 | test_read_then_write_blocking(&mut i2c, slave_addr); | ||
| 89 | |||
| 90 | info!("=== Test 10: Complex Mixed Sequence ==="); | ||
| 91 | test_mixed_sequence_blocking(&mut i2c, slave_addr); | ||
| 92 | |||
| 93 | info!("=== Test 11: Single Operations ==="); | ||
| 94 | test_single_operations_blocking(&mut i2c, slave_addr); | ||
| 95 | |||
| 96 | info!("Blocking transaction tests OK"); | ||
| 97 | } | ||
| 98 | |||
| 99 | Timer::after_millis(100).await; | ||
| 100 | |||
| 101 | // ========== ASYNC TESTS (DMA) ========== | ||
| 102 | info!("========== ASYNC TESTS (DMA) =========="); | ||
| 103 | { | ||
| 104 | let tx_dma = peri!(p, I2C_TX_DMA); | ||
| 105 | let rx_dma = peri!(p, I2C_RX_DMA); | ||
| 106 | let irq = irqs!(I2C); | ||
| 107 | |||
| 108 | let mut i2c = I2c::new(i2c_peri, scl, sda, irq, tx_dma, rx_dma, config); | ||
| 109 | |||
| 110 | // Direct API tests (reusing same I2C instance) | ||
| 111 | info!("=== Direct API Test 1: write() ==="); | ||
| 112 | test_async_write(&mut i2c, slave_addr).await; | ||
| 113 | |||
| 114 | info!("=== Direct API Test 2: read() ==="); | ||
| 115 | test_async_read(&mut i2c, slave_addr).await; | ||
| 116 | |||
| 117 | info!("=== Direct API Test 3: write_read() ==="); | ||
| 118 | test_async_write_read(&mut i2c, slave_addr).await; | ||
| 119 | |||
| 120 | info!("=== Direct API Test 4: write_vectored() ==="); | ||
| 121 | test_async_write_vectored(&mut i2c, slave_addr).await; | ||
| 122 | |||
| 123 | info!("=== Direct API Test 5: Large buffer (>255 bytes) ==="); | ||
| 124 | test_async_large_buffer(&mut i2c, slave_addr).await; | ||
| 125 | |||
| 126 | info!("Async Direct API tests OK"); | ||
| 127 | |||
| 128 | // Transaction tests | ||
| 129 | info!("=== Transaction Test 6: Consecutive Writes (Should Merge) ==="); | ||
| 130 | test_consecutive_writes_async(&mut i2c, slave_addr).await; | ||
| 131 | |||
| 132 | info!("=== Transaction Test 7: Consecutive Reads (Should Merge) ==="); | ||
| 133 | test_consecutive_reads_async(&mut i2c, slave_addr).await; | ||
| 134 | |||
| 135 | info!("=== Transaction Test 8: Write then Read (RESTART) ==="); | ||
| 136 | test_write_then_read_async(&mut i2c, slave_addr).await; | ||
| 137 | |||
| 138 | info!("=== Transaction Test 9: Read then Write (RESTART) ==="); | ||
| 139 | test_read_then_write_async(&mut i2c, slave_addr).await; | ||
| 140 | |||
| 141 | info!("=== Transaction Test 10: Complex Mixed Sequence ==="); | ||
| 142 | test_mixed_sequence_async(&mut i2c, slave_addr).await; | ||
| 143 | |||
| 144 | info!("=== Transaction Test 11: Single Operations ==="); | ||
| 145 | test_single_operations_async(&mut i2c, slave_addr).await; | ||
| 146 | |||
| 147 | info!("Async transaction tests OK"); | ||
| 148 | } | ||
| 149 | |||
| 150 | info!("All tests OK"); | ||
| 151 | cortex_m::asm::bkpt(); | ||
| 152 | } | ||
| 153 | |||
| 154 | // ==================== BLOCKING DIRECT API TEST FUNCTIONS ==================== | ||
| 155 | |||
| 156 | fn test_blocking_write(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 157 | let write_data = [0x42, 0x43, 0x44, 0x45]; | ||
| 158 | |||
| 159 | match i2c.blocking_write(addr, &write_data) { | ||
| 160 | Ok(_) => info!("✓ blocking_write succeeded: {:02x}", write_data), | ||
| 161 | Err(e) => { | ||
| 162 | error!("✗ blocking_write failed: {:?}", e); | ||
| 163 | defmt::panic!("Test failed: blocking_write"); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | fn test_blocking_read(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 169 | let mut read_buf = [0u8; 8]; | ||
| 170 | |||
| 171 | match i2c.blocking_read(addr, &mut read_buf) { | ||
| 172 | Ok(_) => info!("✓ blocking_read succeeded: {:02x}", read_buf), | ||
| 173 | Err(e) => { | ||
| 174 | error!("✗ blocking_read failed: {:?}", e); | ||
| 175 | defmt::panic!("Test failed: blocking_read"); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | fn test_blocking_write_read(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 181 | let write_data = [0x50, 0x51]; | ||
| 182 | let mut read_buf = [0u8; 6]; | ||
| 183 | |||
| 184 | match i2c.blocking_write_read(addr, &write_data, &mut read_buf) { | ||
| 185 | Ok(_) => { | ||
| 186 | info!("✓ blocking_write_read succeeded"); | ||
| 187 | info!(" Written: {:02x}", write_data); | ||
| 188 | info!(" Read: {:02x}", read_buf); | ||
| 189 | } | ||
| 190 | Err(e) => { | ||
| 191 | error!("✗ blocking_write_read failed: {:?}", e); | ||
| 192 | defmt::panic!("Test failed: blocking_write_read"); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | fn test_blocking_write_vectored(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 198 | let buf1 = [0x60, 0x61, 0x62]; | ||
| 199 | let buf2 = [0x70, 0x71]; | ||
| 200 | let buf3 = [0x80, 0x81, 0x82, 0x83]; | ||
| 201 | let bufs = [&buf1[..], &buf2[..], &buf3[..]]; | ||
| 202 | |||
| 203 | match i2c.blocking_write_vectored(addr, &bufs) { | ||
| 204 | Ok(_) => info!("✓ blocking_write_vectored succeeded (9 bytes total)"), | ||
| 205 | Err(e) => { | ||
| 206 | error!("✗ blocking_write_vectored failed: {:?}", e); | ||
| 207 | defmt::panic!("Test failed: blocking_write_vectored"); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | fn test_blocking_large_buffer(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 213 | // Test with 300 bytes to verify RELOAD mechanism works (needs chunking at 255 bytes) | ||
| 214 | let mut write_buf = [0u8; 300]; | ||
| 215 | for (i, byte) in write_buf.iter_mut().enumerate() { | ||
| 216 | *byte = (i & 0xFF) as u8; | ||
| 217 | } | ||
| 218 | |||
| 219 | match i2c.blocking_write(addr, &write_buf) { | ||
| 220 | Ok(_) => info!("✓ Large buffer write succeeded (300 bytes, tests RELOAD)"), | ||
| 221 | Err(e) => { | ||
| 222 | error!("✗ Large buffer write failed: {:?}", e); | ||
| 223 | defmt::panic!("Test failed: large buffer write"); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | // Test large read | ||
| 228 | let mut read_buf = [0u8; 300]; | ||
| 229 | match i2c.blocking_read(addr, &mut read_buf) { | ||
| 230 | Ok(_) => info!("✓ Large buffer read succeeded (300 bytes, tests RELOAD)"), | ||
| 231 | Err(e) => { | ||
| 232 | error!("✗ Large buffer read failed: {:?}", e); | ||
| 233 | defmt::panic!("Test failed: large buffer read"); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | // ==================== BLOCKING TRANSACTION TEST FUNCTIONS ==================== | ||
| 239 | |||
| 240 | fn test_consecutive_writes_blocking(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 241 | // Expected on bus: START, ADDR+W, data1, data2, data3, STOP | ||
| 242 | // NO intermediate RESTART/STOP between writes - they should be merged | ||
| 243 | let data1 = [0x10, 0x11, 0x12]; | ||
| 244 | let data2 = [0x20, 0x21]; | ||
| 245 | let data3 = [0x30, 0x31, 0x32, 0x33]; | ||
| 246 | |||
| 247 | let mut ops = [ | ||
| 248 | Operation::Write(&data1), | ||
| 249 | Operation::Write(&data2), | ||
| 250 | Operation::Write(&data3), | ||
| 251 | ]; | ||
| 252 | |||
| 253 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 254 | Ok(_) => info!("✓ Consecutive writes succeeded (merged 9 bytes)"), | ||
| 255 | Err(e) => { | ||
| 256 | error!("✗ Consecutive writes failed: {:?}", e); | ||
| 257 | defmt::panic!("Test failed: consecutive writes"); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | fn test_consecutive_reads_blocking(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 263 | // Expected on bus: START, ADDR+R, data1, data2, data3, NACK, STOP | ||
| 264 | // NO intermediate RESTART/STOP between reads - they should be merged | ||
| 265 | let mut buf1 = [0u8; 4]; | ||
| 266 | let mut buf2 = [0u8; 3]; | ||
| 267 | let mut buf3 = [0u8; 2]; | ||
| 268 | |||
| 269 | let mut ops = [ | ||
| 270 | Operation::Read(&mut buf1), | ||
| 271 | Operation::Read(&mut buf2), | ||
| 272 | Operation::Read(&mut buf3), | ||
| 273 | ]; | ||
| 274 | |||
| 275 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 276 | Ok(_) => { | ||
| 277 | info!("✓ Consecutive reads succeeded (merged 9 bytes)"); | ||
| 278 | info!(" buf1: {:02x}", buf1); | ||
| 279 | info!(" buf2: {:02x}", buf2); | ||
| 280 | info!(" buf3: {:02x}", buf3); | ||
| 281 | } | ||
| 282 | Err(e) => { | ||
| 283 | error!("✗ Consecutive reads failed: {:?}", e); | ||
| 284 | defmt::panic!("Test failed: consecutive reads"); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | fn test_write_then_read_blocking(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 290 | // Expected: START, ADDR+W, data, RESTART, ADDR+R, data, NACK, STOP | ||
| 291 | let write_data = [0xAA, 0xBB]; | ||
| 292 | let mut read_buf = [0u8; 4]; | ||
| 293 | |||
| 294 | let mut ops = [Operation::Write(&write_data), Operation::Read(&mut read_buf)]; | ||
| 295 | |||
| 296 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 297 | Ok(_) => { | ||
| 298 | info!("✓ Write-then-read succeeded with RESTART"); | ||
| 299 | info!(" Written: {:02x}", write_data); | ||
| 300 | info!(" Read: {:02x}", read_buf); | ||
| 301 | } | ||
| 302 | Err(e) => { | ||
| 303 | error!("✗ Write-then-read failed: {:?}", e); | ||
| 304 | defmt::panic!("Test failed: write-then-read"); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | fn test_read_then_write_blocking(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 310 | // Expected: START, ADDR+R, data, NACK, RESTART, ADDR+W, data, STOP | ||
| 311 | let mut read_buf = [0u8; 3]; | ||
| 312 | let write_data = [0xCC, 0xDD, 0xEE]; | ||
| 313 | |||
| 314 | let mut ops = [Operation::Read(&mut read_buf), Operation::Write(&write_data)]; | ||
| 315 | |||
| 316 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 317 | Ok(_) => { | ||
| 318 | info!("✓ Read-then-write succeeded with RESTART"); | ||
| 319 | info!(" Read: {:02x}", read_buf); | ||
| 320 | info!(" Written: {:02x}", write_data); | ||
| 321 | } | ||
| 322 | Err(e) => { | ||
| 323 | error!("✗ Read-then-write failed: {:?}", e); | ||
| 324 | defmt::panic!("Test failed: read-then-write"); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | fn test_mixed_sequence_blocking(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 330 | // Complex: W, W, R, R, W, R | ||
| 331 | // Groups: [W,W] RESTART [R,R] RESTART [W] RESTART [R] | ||
| 332 | let w1 = [0x01, 0x02]; | ||
| 333 | let w2 = [0x03, 0x04]; | ||
| 334 | let mut r1 = [0u8; 2]; | ||
| 335 | let mut r2 = [0u8; 2]; | ||
| 336 | let w3 = [0x05]; | ||
| 337 | let mut r3 = [0u8; 1]; | ||
| 338 | |||
| 339 | let mut ops = [ | ||
| 340 | Operation::Write(&w1), | ||
| 341 | Operation::Write(&w2), | ||
| 342 | Operation::Read(&mut r1), | ||
| 343 | Operation::Read(&mut r2), | ||
| 344 | Operation::Write(&w3), | ||
| 345 | Operation::Read(&mut r3), | ||
| 346 | ]; | ||
| 347 | |||
| 348 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 349 | Ok(_) => { | ||
| 350 | info!("✓ Mixed sequence succeeded"); | ||
| 351 | info!(" Groups: [W4] RESTART [R4] RESTART [W1] RESTART [R1]"); | ||
| 352 | } | ||
| 353 | Err(e) => { | ||
| 354 | error!("✗ Mixed sequence failed: {:?}", e); | ||
| 355 | defmt::panic!("Test failed: mixed sequence"); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | fn test_single_operations_blocking(i2c: &mut I2c<'_, Blocking, Master>, addr: u8) { | ||
| 361 | // Test single write | ||
| 362 | let write_data = [0xFF]; | ||
| 363 | let mut ops = [Operation::Write(&write_data)]; | ||
| 364 | |||
| 365 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 366 | Ok(_) => info!("✓ Single write succeeded"), | ||
| 367 | Err(e) => { | ||
| 368 | error!("✗ Single write failed: {:?}", e); | ||
| 369 | defmt::panic!("Test failed: single write"); | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | // Test single read | ||
| 374 | let mut read_buf = [0u8; 1]; | ||
| 375 | let mut ops = [Operation::Read(&mut read_buf)]; | ||
| 376 | |||
| 377 | match i2c.blocking_transaction(addr, &mut ops) { | ||
| 378 | Ok(_) => info!("✓ Single read succeeded, data: 0x{:02x}", read_buf[0]), | ||
| 379 | Err(e) => { | ||
| 380 | error!("✗ Single read failed: {:?}", e); | ||
| 381 | defmt::panic!("Test failed: single read"); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | // ==================== ASYNC DIRECT API TEST FUNCTIONS ==================== | ||
| 387 | |||
| 388 | async fn test_async_write(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 389 | let write_data = [0x42, 0x43, 0x44, 0x45]; | ||
| 390 | |||
| 391 | match i2c.write(addr, &write_data).await { | ||
| 392 | Ok(_) => info!("✓ async write succeeded: {:02x}", write_data), | ||
| 393 | Err(e) => { | ||
| 394 | error!("✗ async write failed: {:?}", e); | ||
| 395 | defmt::panic!("Test failed: async write"); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | async fn test_async_read(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 401 | let mut read_buf = [0u8; 8]; | ||
| 402 | |||
| 403 | match i2c.read(addr, &mut read_buf).await { | ||
| 404 | Ok(_) => info!("✓ async read succeeded: {:02x}", read_buf), | ||
| 405 | Err(e) => { | ||
| 406 | error!("✗ async read failed: {:?}", e); | ||
| 407 | defmt::panic!("Test failed: async read"); | ||
| 408 | } | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | async fn test_async_write_read(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 413 | let write_data = [0x50, 0x51]; | ||
| 414 | let mut read_buf = [0u8; 6]; | ||
| 415 | |||
| 416 | match i2c.write_read(addr, &write_data, &mut read_buf).await { | ||
| 417 | Ok(_) => { | ||
| 418 | info!("✓ async write_read succeeded"); | ||
| 419 | info!(" Written: {:02x}", write_data); | ||
| 420 | info!(" Read: {:02x}", read_buf); | ||
| 421 | } | ||
| 422 | Err(e) => { | ||
| 423 | error!("✗ async write_read failed: {:?}", e); | ||
| 424 | defmt::panic!("Test failed: async write_read"); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | async fn test_async_write_vectored(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 430 | let buf1 = [0x60, 0x61, 0x62]; | ||
| 431 | let buf2 = [0x70, 0x71]; | ||
| 432 | let buf3 = [0x80, 0x81, 0x82, 0x83]; | ||
| 433 | let bufs = [&buf1[..], &buf2[..], &buf3[..]]; | ||
| 434 | |||
| 435 | match i2c.write_vectored(addr.into(), &bufs).await { | ||
| 436 | Ok(_) => info!("✓ async write_vectored succeeded (9 bytes total)"), | ||
| 437 | Err(e) => { | ||
| 438 | error!("✗ async write_vectored failed: {:?}", e); | ||
| 439 | defmt::panic!("Test failed: async write_vectored"); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | async fn test_async_large_buffer(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 445 | // Test with 300 bytes to verify RELOAD mechanism works with DMA (needs chunking at 255 bytes) | ||
| 446 | let mut write_buf = [0u8; 300]; | ||
| 447 | for (i, byte) in write_buf.iter_mut().enumerate() { | ||
| 448 | *byte = (i & 0xFF) as u8; | ||
| 449 | } | ||
| 450 | |||
| 451 | match i2c.write(addr, &write_buf).await { | ||
| 452 | Ok(_) => info!("✓ Large buffer async write succeeded (300 bytes, tests RELOAD with DMA)"), | ||
| 453 | Err(e) => { | ||
| 454 | error!("✗ Large buffer async write failed: {:?}", e); | ||
| 455 | defmt::panic!("Test failed: large buffer async write"); | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | // Test large read | ||
| 460 | let mut read_buf = [0u8; 300]; | ||
| 461 | match i2c.read(addr, &mut read_buf).await { | ||
| 462 | Ok(_) => info!("✓ Large buffer async read succeeded (300 bytes, tests RELOAD with DMA)"), | ||
| 463 | Err(e) => { | ||
| 464 | error!("✗ Large buffer async read failed: {:?}", e); | ||
| 465 | defmt::panic!("Test failed: large buffer async read"); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | // ==================== ASYNC TRANSACTION TEST FUNCTIONS ==================== | ||
| 471 | |||
| 472 | async fn test_consecutive_writes_async(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 473 | let data1 = [0x10, 0x11, 0x12]; | ||
| 474 | let data2 = [0x20, 0x21]; | ||
| 475 | let data3 = [0x30, 0x31, 0x32, 0x33]; | ||
| 476 | |||
| 477 | let mut ops = [ | ||
| 478 | Operation::Write(&data1), | ||
| 479 | Operation::Write(&data2), | ||
| 480 | Operation::Write(&data3), | ||
| 481 | ]; | ||
| 482 | |||
| 483 | match i2c.transaction(addr, &mut ops).await { | ||
| 484 | Ok(_) => info!("✓ Consecutive writes succeeded (merged 9 bytes)"), | ||
| 485 | Err(e) => { | ||
| 486 | error!("✗ Consecutive writes failed: {:?}", e); | ||
| 487 | defmt::panic!("Test failed: consecutive writes"); | ||
| 488 | } | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | async fn test_consecutive_reads_async(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 493 | let mut buf1 = [0u8; 4]; | ||
| 494 | let mut buf2 = [0u8; 3]; | ||
| 495 | let mut buf3 = [0u8; 2]; | ||
| 496 | |||
| 497 | let mut ops = [ | ||
| 498 | Operation::Read(&mut buf1), | ||
| 499 | Operation::Read(&mut buf2), | ||
| 500 | Operation::Read(&mut buf3), | ||
| 501 | ]; | ||
| 502 | |||
| 503 | match i2c.transaction(addr, &mut ops).await { | ||
| 504 | Ok(_) => { | ||
| 505 | info!("✓ Consecutive reads succeeded (merged 9 bytes)"); | ||
| 506 | info!(" buf1: {:02x}", buf1); | ||
| 507 | info!(" buf2: {:02x}", buf2); | ||
| 508 | info!(" buf3: {:02x}", buf3); | ||
| 509 | } | ||
| 510 | Err(e) => { | ||
| 511 | error!("✗ Consecutive reads failed: {:?}", e); | ||
| 512 | defmt::panic!("Test failed: consecutive reads"); | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | async fn test_write_then_read_async(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 518 | let write_data = [0xAA, 0xBB]; | ||
| 519 | let mut read_buf = [0u8; 4]; | ||
| 520 | |||
| 521 | let mut ops = [Operation::Write(&write_data), Operation::Read(&mut read_buf)]; | ||
| 522 | |||
| 523 | match i2c.transaction(addr, &mut ops).await { | ||
| 524 | Ok(_) => { | ||
| 525 | info!("✓ Write-then-read succeeded with RESTART"); | ||
| 526 | info!(" Written: {:02x}", write_data); | ||
| 527 | info!(" Read: {:02x}", read_buf); | ||
| 528 | } | ||
| 529 | Err(e) => { | ||
| 530 | error!("✗ Write-then-read failed: {:?}", e); | ||
| 531 | defmt::panic!("Test failed: write-then-read"); | ||
| 532 | } | ||
| 533 | } | ||
| 534 | } | ||
| 535 | |||
| 536 | async fn test_read_then_write_async(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 537 | let mut read_buf = [0u8; 3]; | ||
| 538 | let write_data = [0xCC, 0xDD, 0xEE]; | ||
| 539 | |||
| 540 | let mut ops = [Operation::Read(&mut read_buf), Operation::Write(&write_data)]; | ||
| 541 | |||
| 542 | match i2c.transaction(addr, &mut ops).await { | ||
| 543 | Ok(_) => { | ||
| 544 | info!("✓ Read-then-write succeeded with RESTART"); | ||
| 545 | info!(" Read: {:02x}", read_buf); | ||
| 546 | info!(" Written: {:02x}", write_data); | ||
| 547 | } | ||
| 548 | Err(e) => { | ||
| 549 | error!("✗ Read-then-write failed: {:?}", e); | ||
| 550 | defmt::panic!("Test failed: read-then-write"); | ||
| 551 | } | ||
| 552 | } | ||
| 553 | } | ||
| 554 | |||
| 555 | async fn test_mixed_sequence_async(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 556 | let w1 = [0x01, 0x02]; | ||
| 557 | let w2 = [0x03, 0x04]; | ||
| 558 | let mut r1 = [0u8; 2]; | ||
| 559 | let mut r2 = [0u8; 2]; | ||
| 560 | let w3 = [0x05]; | ||
| 561 | let mut r3 = [0u8; 1]; | ||
| 562 | |||
| 563 | let mut ops = [ | ||
| 564 | Operation::Write(&w1), | ||
| 565 | Operation::Write(&w2), | ||
| 566 | Operation::Read(&mut r1), | ||
| 567 | Operation::Read(&mut r2), | ||
| 568 | Operation::Write(&w3), | ||
| 569 | Operation::Read(&mut r3), | ||
| 570 | ]; | ||
| 571 | |||
| 572 | match i2c.transaction(addr, &mut ops).await { | ||
| 573 | Ok(_) => { | ||
| 574 | info!("✓ Mixed sequence succeeded"); | ||
| 575 | info!(" Groups: [W4] RESTART [R4] RESTART [W1] RESTART [R1]"); | ||
| 576 | } | ||
| 577 | Err(e) => { | ||
| 578 | error!("✗ Mixed sequence failed: {:?}", e); | ||
| 579 | defmt::panic!("Test failed: mixed sequence"); | ||
| 580 | } | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | async fn test_single_operations_async(i2c: &mut I2c<'_, Async, Master>, addr: u8) { | ||
| 585 | // Test single write | ||
| 586 | let write_data = [0xFF]; | ||
| 587 | let mut ops = [Operation::Write(&write_data)]; | ||
| 588 | |||
| 589 | match i2c.transaction(addr, &mut ops).await { | ||
| 590 | Ok(_) => info!("✓ Single write succeeded"), | ||
| 591 | Err(e) => { | ||
| 592 | error!("✗ Single write failed: {:?}", e); | ||
| 593 | defmt::panic!("Test failed: single write"); | ||
| 594 | } | ||
| 595 | } | ||
| 596 | |||
| 597 | // Test single read | ||
| 598 | let mut read_buf = [0u8; 1]; | ||
| 599 | let mut ops = [Operation::Read(&mut read_buf)]; | ||
| 600 | |||
| 601 | match i2c.transaction(addr, &mut ops).await { | ||
| 602 | Ok(_) => info!("✓ Single read succeeded, data: 0x{:02x}", read_buf[0]), | ||
| 603 | Err(e) => { | ||
| 604 | error!("✗ Single read failed: {:?}", e); | ||
| 605 | defmt::panic!("Test failed: single read"); | ||
| 606 | } | ||
| 607 | } | ||
| 608 | } | ||
diff --git a/tests/stm32/src/common.rs b/tests/stm32/src/common.rs index de06cb267..096cce947 100644 --- a/tests/stm32/src/common.rs +++ b/tests/stm32/src/common.rs | |||
| @@ -60,8 +60,6 @@ teleprobe_meta::target!(b"nucleo-stm32wl55jc"); | |||
| 60 | teleprobe_meta::target!(b"nucleo-stm32wba52cg"); | 60 | teleprobe_meta::target!(b"nucleo-stm32wba52cg"); |
| 61 | #[cfg(feature = "stm32f091rc")] | 61 | #[cfg(feature = "stm32f091rc")] |
| 62 | teleprobe_meta::target!(b"nucleo-stm32f091rc"); | 62 | teleprobe_meta::target!(b"nucleo-stm32f091rc"); |
| 63 | #[cfg(feature = "stm32f072rb")] | ||
| 64 | teleprobe_meta::target!(b"nucleo-stm32f072rb"); | ||
| 65 | #[cfg(feature = "stm32h503rb")] | 63 | #[cfg(feature = "stm32h503rb")] |
| 66 | teleprobe_meta::target!(b"nucleo-stm32h503rb"); | 64 | teleprobe_meta::target!(b"nucleo-stm32h503rb"); |
| 67 | #[cfg(feature = "stm32h7s3l8")] | 65 | #[cfg(feature = "stm32h7s3l8")] |
| @@ -105,14 +103,6 @@ define_peris!( | |||
| 105 | SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA1_CH3, SPI_RX_DMA = DMA1_CH2, | 103 | SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA1_CH3, SPI_RX_DMA = DMA1_CH2, |
| 106 | @irq UART = {USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;}, | 104 | @irq UART = {USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;}, |
| 107 | ); | 105 | ); |
| 108 | #[cfg(feature = "stm32f072rb")] | ||
| 109 | define_peris!( | ||
| 110 | UART = USART1, UART_TX = PA9, UART_RX = PA10, UART_TX_DMA = DMA1_CH4, UART_RX_DMA = DMA1_CH5, | ||
| 111 | SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA1_CH3, SPI_RX_DMA = DMA1_CH2, | ||
| 112 | I2C = I2C1, I2C_SCL = PB8, I2C_SDA = PB9, I2C_TX_DMA = DMA1_CH6, I2C_RX_DMA = DMA1_CH7, | ||
| 113 | @irq UART = {USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;}, | ||
| 114 | @irq I2C = {I2C1 => embassy_stm32::i2c::EventInterruptHandler<embassy_stm32::peripherals::I2C1>, embassy_stm32::i2c::ErrorInterruptHandler<embassy_stm32::peripherals::I2C1>;}, | ||
| 115 | ); | ||
| 116 | #[cfg(any(feature = "stm32f100rd", feature = "stm32f103c8", feature = "stm32f107vc"))] | 106 | #[cfg(any(feature = "stm32f100rd", feature = "stm32f103c8", feature = "stm32f107vc"))] |
| 117 | define_peris!( | 107 | define_peris!( |
| 118 | UART = USART1, UART_TX = PA9, UART_RX = PA10, UART_TX_DMA = DMA1_CH4, UART_RX_DMA = DMA1_CH5, | 108 | UART = USART1, UART_TX = PA9, UART_RX = PA10, UART_TX_DMA = DMA1_CH4, UART_RX_DMA = DMA1_CH5, |
| @@ -335,21 +325,6 @@ pub fn config() -> Config { | |||
| 335 | config.rcc.ahb_pre = AHBPrescaler::DIV1; | 325 | config.rcc.ahb_pre = AHBPrescaler::DIV1; |
| 336 | config.rcc.apb1_pre = APBPrescaler::DIV1; | 326 | config.rcc.apb1_pre = APBPrescaler::DIV1; |
| 337 | } | 327 | } |
| 338 | #[cfg(feature = "stm32f072rb")] | ||
| 339 | { | ||
| 340 | config.rcc.hse = Some(Hse { | ||
| 341 | freq: Hertz(8_000_000), | ||
| 342 | mode: HseMode::Bypass, | ||
| 343 | }); | ||
| 344 | config.rcc.pll = Some(Pll { | ||
| 345 | src: PllSource::HSE, | ||
| 346 | prediv: PllPreDiv::DIV1, | ||
| 347 | mul: PllMul::MUL6, | ||
| 348 | }); | ||
| 349 | config.rcc.sys = Sysclk::PLL1_P; | ||
| 350 | config.rcc.ahb_pre = AHBPrescaler::DIV1; | ||
| 351 | config.rcc.apb1_pre = APBPrescaler::DIV1; | ||
| 352 | } | ||
| 353 | #[cfg(feature = "stm32f103c8")] | 328 | #[cfg(feature = "stm32f103c8")] |
| 354 | { | 329 | { |
| 355 | config.rcc.hse = Some(Hse { | 330 | config.rcc.hse = Some(Hse { |
