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