From 9f4503a94c0bf4f3629682f94a34543878c2c54c Mon Sep 17 00:00:00 2001 From: occheung Date: Fri, 6 Sep 2024 16:25:14 +0800 Subject: [PATCH] eeprom: check I2C (n)ack on read/write --- libboard_zynq/src/i2c/eeprom.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/libboard_zynq/src/i2c/eeprom.rs b/libboard_zynq/src/i2c/eeprom.rs index a90670e..825cf56 100644 --- a/libboard_zynq/src/i2c/eeprom.rs +++ b/libboard_zynq/src/i2c/eeprom.rs @@ -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) {