From 539aac4d90d307d24739ce3833dfd45c5b6372fd Mon Sep 17 00:00:00 2001 From: liebman Date: Fri, 30 Aug 2024 13:53:15 -0700 Subject: [PATCH] don't short circuit a 0 length write operation if stop=true --- esp-hal/src/i2c.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/esp-hal/src/i2c.rs b/esp-hal/src/i2c.rs index 4c515247043..43213a35ad8 100644 --- a/esp-hal/src/i2c.rs +++ b/esp-hal/src/i2c.rs @@ -769,9 +769,9 @@ mod asynch { where I: Iterator, { - // Short circuit for zero length writes without start as that would be an + // Short circuit for zero length writes without start or end as that would be an // invalid operation write lengths in the TRM (at least for ESP32-S3) are 1-255 - if bytes.is_empty() && !start { + if bytes.is_empty() && !start && !stop { return Ok(()); } @@ -1516,10 +1516,6 @@ pub trait Instance: crate::private::Sealed { where I: Iterator, { - if bytes.is_empty() && !start { - return Err(Error::InvalidZeroLength); - } - // if start is true we can only send 254 additional bytes with the address as // the first let max_len = if start { 254usize } else { 255usize }; @@ -1528,16 +1524,19 @@ pub trait Instance: crate::private::Sealed { return Err(Error::ExceedingFifo); } - let extra_len = if start { 1 } else { 0 }; - // WRITE command - add_cmd( - cmd_iterator, - Command::Write { - ack_exp: Ack::Ack, - ack_check_en: true, - length: extra_len + bytes.len() as u8, - }, - )?; + let write_len = if start { bytes.len() + 1 } else { bytes.len() }; + // don't issue write if there is no data to write + if write_len > 0 { + // WRITE command + add_cmd( + cmd_iterator, + Command::Write { + ack_exp: Ack::Ack, + ack_check_en: true, + length: write_len as u8, + }, + )?; + } self.update_config(); @@ -1986,9 +1985,9 @@ pub trait Instance: crate::private::Sealed { where I: Iterator, { - // Short circuit for zero length writes without start as that would be an + // Short circuit for zero length writes without start or end as that would be an // invalid operation write lengths in the TRM (at least for ESP32-S3) are 1-255 - if bytes.is_empty() && !start { + if bytes.is_empty() && !start && !stop { return Ok(()); }