eeprom: check I2C (n)ack on read/write

This commit is contained in:
occheung 2024-09-06 16:25:14 +08:00
parent b2b3e5c933
commit 9f4503a94c

View File

@ -51,11 +51,17 @@ impl<'a> EEPROM<'a> {
self.select()?;
self.i2c.start()?;
self.i2c.write(self.address << 1)?;
self.i2c.write(addr)?;
if !self.i2c.write(self.address << 1)? {
return Err("eeprom failed to ack control byte (write)");
}
if !self.i2c.write(addr)? {
return Err("eeprom failed to ack read address");
}
self.i2c.restart()?;
self.i2c.write((self.address << 1) | 1)?;
if !self.i2c.write((self.address << 1) | 1)? {
return Err("eeprom failed to ack control byte (read)");
}
let buf_len = buf.len();
for (i, byte) in buf.iter_mut().enumerate() {
*byte = self.i2c.read(i < buf_len - 1)?;
@ -80,10 +86,16 @@ impl<'a> EEPROM<'a> {
for (i, byte) in buf.iter().enumerate() {
if (i == 0) || (pb == 0) {
self.i2c.start()?;
self.i2c.write(self.address << 1)?;
self.i2c.write(addr + (i as u8))?;
if !self.i2c.write(self.address << 1)? {
return Err("eeprom failed to ack control byte (write)");
}
if !self.i2c.write(addr + (i as u8))? {
return Err("eeprom failed to ack write address");
}
}
if !self.i2c.write(*byte)? {
return Err("eeprom failed to ack written value");
}
self.i2c.write(*byte)?;
pb += 1;
if (i == buf_len-1) || (pb == self.page_size) {