Skip to content

Commit

Permalink
return error for 0 length reads and 0 length writes where start=false
Browse files Browse the repository at this point in the history
  • Loading branch information
liebman committed Aug 30, 2024
1 parent 3742ed3 commit c313989
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions esp-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -1520,6 +1522,9 @@ pub trait Instance: crate::private::Sealed {
where
I: Iterator<Item = &'a COMD>,
{
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
Expand Down Expand Up @@ -1568,15 +1573,13 @@ pub trait Instance: crate::private::Sealed {
where
I: Iterator<Item = &'a COMD>,
{
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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c313989

Please sign in to comment.