diff --git a/esp-hal/src/i2c.rs b/esp-hal/src/i2c.rs index cf39306d03..873c34efee 100644 --- a/esp-hal/src/i2c.rs +++ b/esp-hal/src/i2c.rs @@ -108,6 +108,8 @@ pub enum Error { ExecIncomplete, /// The number of commands issued exceeded the limit. CommandNrExceeded, + /// Zero length read or write operation. + InvalidZeroLength, } #[cfg(any(feature = "embedded-hal", feature = "async"))] @@ -1520,6 +1522,9 @@ pub trait Instance: crate::private::Sealed { where I: Iterator, { + if bytes.is_empty() && !start { + return Err(Error::InvalidZeroLength); + } let max_len = if start { 254usize } else { 255usize }; if bytes.len() > max_len { // we could support more by adding multiple write operations @@ -1568,15 +1573,13 @@ pub trait Instance: crate::private::Sealed { where I: Iterator, { + if buffer.is_empty() { + return Err(Error::InvalidZeroLength); + } let (max_len, initial_len) = if will_continue { (255usize, buffer.len()) } else { - let len = if buffer.is_empty() { - 0 - } else { - buffer.len() - 1 - }; - (254usize, len) + (254usize, buffer.len() - 1) }; if buffer.len() > max_len { // we could support more by adding multiple read operations @@ -1606,9 +1609,7 @@ pub trait Instance: crate::private::Sealed { )?; } - // If we are not continuing the read in the next operation we need to nack the - // last byte. But don't issue the read command if the buffer is empty! - if !will_continue && !buffer.is_empty() { + if !will_continue { // this is the last read so we need to nack the last byte // READ w/o ACK add_cmd(