panic symbol fix and formatting
diff --git a/target/ast10x0/harness/pi_test_runner.py b/target/ast10x0/harness/pi_test_runner.py index bd655cf..9ada58f 100644 --- a/target/ast10x0/harness/pi_test_runner.py +++ b/target/ast10x0/harness/pi_test_runner.py
@@ -94,7 +94,7 @@ data = port.read(1024) if data: try: - with (lock or nullcontext()): + with lock or nullcontext(): sys.stdout.buffer.write(data) sys.stdout.buffer.flush() except (BrokenPipeError, OSError): @@ -255,7 +255,10 @@ parser.error(f"paired mode requires: {', '.join(missing)}") slave_firmware_path = Path(args.slave_firmware) if not slave_firmware_path.exists(): - print(f"Error: slave firmware not found: {slave_firmware_path}", file=sys.stderr) + print( + f"Error: slave firmware not found: {slave_firmware_path}", + file=sys.stderr, + ) return 1 return 0 if _run_paired(args, firmware_path, slave_firmware_path) else 1
diff --git a/target/ast10x0/peripherals/i2c/master.rs b/target/ast10x0/peripherals/i2c/master.rs index 238a3eb..9aa4e74 100644 --- a/target/ast10x0/peripherals/i2c/master.rs +++ b/target/ast10x0/peripherals/i2c/master.rs
@@ -211,7 +211,9 @@ while offset < total_len { let chunk_len = core::cmp::min(constants::BUFFER_MODE_SIZE, total_len - offset); - let chunk = &bytes[offset..offset + chunk_len]; + // SAFETY: chunk_len = min(BUFFER_MODE_SIZE, total_len - offset), so + // offset + chunk_len <= total_len == bytes.len(). + let chunk = unsafe { bytes.get_unchecked(offset..offset + chunk_len) }; let is_first = offset == 0; let is_last = offset + chunk_len >= total_len; @@ -336,7 +338,9 @@ } // Copy from hardware buffer AFTER successful transfer - let chunk = &mut buffer[offset..offset + chunk_len]; + // SAFETY: chunk_len = min(BUFFER_MODE_SIZE, total_len - offset), so + // offset + chunk_len <= total_len == buffer.len(). + let chunk = unsafe { buffer.get_unchecked_mut(offset..offset + chunk_len) }; self.copy_from_buffer(chunk)?; #[allow(clippy::cast_possible_truncation)] @@ -442,7 +446,9 @@ while offset < total_len { let chunk_len = core::cmp::min(constants::DMA_MODE_MAX_SIZE, total_len - offset); - let chunk = &bytes[offset..offset + chunk_len]; + // SAFETY: chunk_len = min(DMA_MODE_MAX_SIZE, total_len - offset), so + // offset + chunk_len <= total_len == bytes.len(). + let chunk = unsafe { bytes.get_unchecked(offset..offset + chunk_len) }; let is_first = offset == 0; let is_last = offset + chunk_len >= total_len; @@ -584,7 +590,14 @@ // Copy from DMA buffer into caller's buffer { let dma_buf = self.dma_buf.as_deref().ok_or(I2cError::Invalid)?; - buffer[offset..offset + chunk_len].copy_from_slice(&dma_buf[..chunk_len]); + // SAFETY: chunk_len = min(DMA_MODE_MAX_SIZE, total_len - offset), so + // offset + chunk_len <= total_len == buffer.len(). dma_buf.len() >= chunk_len + // is checked above before the DMA transfer starts. + unsafe { + buffer + .get_unchecked_mut(offset..offset + chunk_len) + .copy_from_slice(dma_buf.get_unchecked(..chunk_len)); + } } #[allow(clippy::cast_possible_truncation)]
diff --git a/target/ast10x0/peripherals/i2c/transfer.rs b/target/ast10x0/peripherals/i2c/transfer.rs index d793c1f..73f5cb0 100644 --- a/target/ast10x0/peripherals/i2c/transfer.rs +++ b/target/ast10x0/peripherals/i2c/transfer.rs
@@ -138,7 +138,8 @@ let mut dword: u32 = 0; for byte_pos in 0..4 { if idx + byte_pos < data.len() { - dword |= u32::from(data[idx + byte_pos]) << (byte_pos * 8); + // SAFETY: idx + byte_pos < data.len() checked above. + dword |= u32::from(unsafe { *data.get_unchecked(idx + byte_pos) }) << (byte_pos * 8); } } @@ -179,7 +180,9 @@ // Extract bytes from DWORD (little-endian) for byte_pos in 0..4 { if idx + byte_pos < data.len() { - data[idx + byte_pos] = ((dword >> (byte_pos * 8)) & 0xFF) as u8; + // SAFETY: idx + byte_pos < data.len() checked above. + *unsafe { data.get_unchecked_mut(idx + byte_pos) } = + ((dword >> (byte_pos * 8)) & 0xFF) as u8; } }
diff --git a/target/ast10x0/tests/peripherals/i2c/i2c_irq/BUILD.bazel b/target/ast10x0/tests/peripherals/i2c/i2c_irq/BUILD.bazel index 21b2dbf..1843875 100644 --- a/target/ast10x0/tests/peripherals/i2c/i2c_irq/BUILD.bazel +++ b/target/ast10x0/tests/peripherals/i2c/i2c_irq/BUILD.bazel
@@ -53,7 +53,10 @@ edition = "2024", tags = ["kernel"], target_compatible_with = TARGET_COMPATIBLE_WITH, - deps = [":codegen", ":linker_script"] + COMMON_DEPS, + deps = [ + ":codegen", + ":linker_script", + ] + COMMON_DEPS, ) system_image( @@ -113,7 +116,10 @@ edition = "2024", tags = ["kernel"], target_compatible_with = TARGET_COMPATIBLE_WITH, - deps = [":slave_codegen", ":slave_linker_script"] + COMMON_DEPS, + deps = [ + ":slave_codegen", + ":slave_linker_script", + ] + COMMON_DEPS, ) system_image(
diff --git a/target/ast10x0/tests/peripherals/i2c/i2c_irq/target.rs b/target/ast10x0/tests/peripherals/i2c/i2c_irq/target.rs index 3dc8758..238fb56 100644 --- a/target/ast10x0/tests/peripherals/i2c/i2c_irq/target.rs +++ b/target/ast10x0/tests/peripherals/i2c/i2c_irq/target.rs
@@ -82,11 +82,12 @@ master .read(SLAVE_ADDR, &mut rx) .map_err(|_| "test 2: master read failed")?; - if rx[0] != 0x55 { - pw_log::error!("test 2: got 0x{:02x}, expected 0x55", rx[0] as u32); + let [rx_byte] = rx; + if rx_byte != 0x55 { + pw_log::error!("test 2: got 0x{:02x}, expected 0x55", rx_byte as u32); return Err("test 2: rx data mismatch"); } - pw_log::info!("Test 2 passed: rx=0x{:02x}", rx[0] as u32); + pw_log::info!("Test 2 passed: rx=0x{:02x}", rx_byte as u32); // ------------------------------------------------------------------ // Test 3: single-byte write → slave Stop event after packet done