Skip to content

Commit 7d8d7c1

Browse files
committed
Fix off-by-one
1 parent 53a07ca commit 7d8d7c1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/i2c.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,9 @@ impl<I2C: Instance, A, R> I2cTarget<I2C, A, R> {
11251125
for (i, data) in bytes.iter().enumerate() {
11261126
match self.write_byte(*data) {
11271127
Ok(()) => {}
1128-
Err(Error::NotAcknowledge) => return Ok(i),
1128+
// If we receive a NACK it will be on the FIFO write subsequent to the last byte
1129+
// actually written to the bus
1130+
Err(Error::NotAcknowledge) => return Ok(i - 1),
11291131
Err(error) => return Err(error),
11301132
}
11311133
}
@@ -1141,7 +1143,12 @@ impl<I2C: Instance, A, R> I2cTarget<I2C, A, R> {
11411143
for (i, data) in bytes.iter().chain(iter::repeat(&0)).enumerate() {
11421144
match self.write_byte(*data) {
11431145
Ok(()) => {}
1144-
Err(Error::NotAcknowledge) => return Ok(core::cmp::min(i, bytes.len())),
1146+
Err(Error::NotAcknowledge) => {
1147+
// If we receive a NACK it will be on the FIFO write subsequent to the last byte
1148+
// actually written to the bus. If we start writing zeroes out, we only want to
1149+
// indicate how many bytes from the buffer we wrote.
1150+
return Ok(core::cmp::min(i - 1, bytes.len()));
1151+
}
11451152
Err(error) => return Err(error),
11461153
}
11471154
}

0 commit comments

Comments
 (0)