Add missing check for empty buffer in asynchronous read_write()

This commit is contained in:
Sebastian Goll 2024-03-27 10:39:33 +01:00
parent 13636556d9
commit b52e9a60eb
2 changed files with 10 additions and 0 deletions
embassy-stm32/src/i2c

View file

@ -415,6 +415,10 @@ fn operation_frames<'a, 'b: 'a>(
// Check empty read buffer before starting transaction. Otherwise, we would risk halting with an
// error in the middle of the transaction.
//
// In principle, we could allow empty read frames within consecutive read operations, as long as
// at least one byte remains in the final (merged) read operation, but that makes the logic more
// complicated and error-prone.
if operations.iter().any(|op| match op {
Operation::Read(read) => read.is_empty(),
Operation::Write(_) => false,

View file

@ -669,6 +669,12 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
RXDMA: crate::i2c::RxDma<T>,
TXDMA: crate::i2c::TxDma<T>,
{
// Check empty read buffer before starting transaction. Otherwise, we would not generate the
// stop condition below.
if read.is_empty() {
return Err(Error::Overrun);
}
self.write_frame(address, write, FrameOptions::FirstFrame).await?;
self.read_frame(address, read, FrameOptions::FirstAndLastFrame).await
}