From 8237e04fff7c4dd8a8a338d1e46091f161fb1b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Jun 2025 14:07:24 +0200 Subject: [PATCH] Replace length check with debug assert (#3599) --- esp-hal/src/i2c/master/mod.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/esp-hal/src/i2c/master/mod.rs b/esp-hal/src/i2c/master/mod.rs index 17e7dc61c..78b014c28 100644 --- a/esp-hal/src/i2c/master/mod.rs +++ b/esp-hal/src/i2c/master/mod.rs @@ -2125,11 +2125,6 @@ impl Driver<'_> { /// Reads from RX FIFO into the given buffer. fn read_all_from_fifo(&self, buffer: &mut [u8]) -> Result<(), Error> { - // we don't support single I2C reads larger than the FIFO - if buffer.len() > I2C_FIFO_SIZE { - return Err(Error::FifoExceeded); - } - if self.regs().sr().read().rxfifo_cnt().bits() < buffer.len() as u8 { return Err(Error::ExecutionIncomplete); } @@ -2139,6 +2134,10 @@ impl Driver<'_> { *byte = read_fifo(self.regs()); } + // The RX FIFO should be empty now. If it is not, it means we queued up reading + // more data than we read, which is an error. + debug_assert!(self.regs().sr().read().rxfifo_cnt().bits() == 0); + Ok(()) } @@ -2382,6 +2381,11 @@ impl Driver<'_> { stop: bool, deadline: Deadline, ) -> Result, Error> { + // We don't support single I2C reads larger than the FIFO. This should be + // enforced by `VariableChunkIterMut` used in `Driver::read` and + // `Driver::read_async`. + debug_assert!(buffer.len() <= I2C_FIFO_SIZE); + let cmd_iterator = &mut self.regs().comd_iter(); self.setup_read(address, buffer, start, will_continue, cmd_iterator)?;