Skip to content

Commit be0d42c

Browse files
committed
Adding NACK detection and fault condition clearing
1 parent 76012d6 commit be0d42c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/i2c.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,24 @@ where
803803
.write(|w| unsafe { w.bits(u32::from(addr) << 1) });
804804

805805
// Wait until address was sent
806-
while self.i2c.sr1.read().addr().bit_is_clear() {}
806+
while {
807+
let sr1 = self.i2c.sr1.read();
808+
809+
// If we received a NACK, then this is an error
810+
if sr1.af().bit_is_set() {
811+
self.i2c.sr1.modify(|_, w| w.af().clear_bit());
812+
return Err(Error::NACK);
813+
}
814+
815+
// Wait for the address to be acknowledged.
816+
sr1.addr().bit_is_clear()
817+
} {}
818+
819+
// Check for address faults (NACK received).
820+
if self.i2c.sr1.read().af().bit_is_set() {
821+
self.i2c.sr1.modify(|_, w| w.af().clear_bit());
822+
return Err(Error::NACK);
823+
}
807824

808825
// Clear condition by reading SR2
809826
self.i2c.sr2.read();
@@ -830,6 +847,7 @@ where
830847

831848
// If we received a NACK, then this is an error
832849
if sr1.af().bit_is_set() {
850+
self.i2c.sr1.modify(|_, w| w.af().clear_bit());
833851
return Err(Error::NACK);
834852
}
835853

0 commit comments

Comments
 (0)