fix: Fix for async I2C v2 driver sequential read/write reads. Fixes #3887

For I2C devices that support variable length reads, sending chunks of data as long as the master keeps ACK:ing after each received byte, sequential reads will sometimes get out of sync, causing additional reads to return invalid data.

This was caused by a delay between awaiting the DMA read and sending the software STOP signal, which may result in the slave to have time to send a byte of data in between, causing potential miss-alignment. This would then cause subsequent reads to return invalid data.

Async write-reads now no longer send STOP after the initial write, matching the behaviour of the blocking version.
This commit is contained in:
Fredrik Reinholdsen 2025-02-15 12:05:16 +01:00
parent 0ce6da9706
commit 7f4cce536a

View File

@ -438,6 +438,7 @@ impl<'d> I2c<'d, Async> {
write: &[u8],
first_slice: bool,
last_slice: bool,
send_stop: bool,
timeout: Timeout,
) -> Result<(), Error> {
let total_len = write.len();
@ -505,10 +506,12 @@ impl<'d> I2c<'d, Async> {
.await?;
dma_transfer.await;
if last_slice {
// This should be done already
self.wait_tc(timeout)?;
}
if last_slice & send_stop {
self.master_stop();
}
@ -556,16 +559,16 @@ impl<'d> I2c<'d, Async> {
self.info,
address,
total_len.min(255),
Stop::Software,
Stop::Automatic,
total_len > 255,
restart,
timeout,
)?;
} else if remaining_len == 0 {
return Poll::Ready(Ok(()));
} else if !(isr.tcr() || isr.tc()) {
// poll_fn was woken without an interrupt present
return Poll::Pending;
} else if remaining_len == 0 {
return Poll::Ready(Ok(()));
} else {
let last_piece = remaining_len <= 255;
@ -581,11 +584,6 @@ impl<'d> I2c<'d, Async> {
.await?;
dma_transfer.await;
// This should be done already
self.wait_tc(timeout)?;
self.master_stop();
drop(on_drop);
Ok(())
@ -601,7 +599,7 @@ impl<'d> I2c<'d, Async> {
self.write_internal(address, write, true, timeout)
} else {
timeout
.with(self.write_dma_internal(address, write, true, true, timeout))
.with(self.write_dma_internal(address, write, true, true, true, timeout))
.await
}
}
@ -623,7 +621,7 @@ impl<'d> I2c<'d, Async> {
let next = iter.next();
let is_last = next.is_none();
let fut = self.write_dma_internal(address, c, first, is_last, timeout);
let fut = self.write_dma_internal(address, c, first, is_last, is_last, timeout);
timeout.with(fut).await?;
first = false;
current = next;
@ -650,7 +648,7 @@ impl<'d> I2c<'d, Async> {
if write.is_empty() {
self.write_internal(address, write, false, timeout)?;
} else {
let fut = self.write_dma_internal(address, write, true, true, timeout);
let fut = self.write_dma_internal(address, write, true, true, false, timeout);
timeout.with(fut).await?;
}