Skip to content

Commit 3ced735

Browse files
committed
Fixing flag checks
1 parent b554ee6 commit 3ced735

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/i2c.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -772,46 +772,37 @@ where
772772
}
773773

774774
fn check_and_clear_error_flags(&self) -> Result<(), Error> {
775+
// Note that flags should only be cleared once they have been registered. If flags are
776+
// cleared otherwise, there may be an inherent race condition and flags may be missed.
775777
let sr1 = self.i2c.sr1.read();
776778

777-
// Clear all pending error flags. We have already read the SR1, so it's safe to clear them
778-
// before returning the error code.
779-
self.i2c.sr1.modify(|_, w| {
780-
w.timeout()
781-
.clear_bit()
782-
.pecerr()
783-
.clear_bit()
784-
.ovr()
785-
.clear_bit()
786-
.af()
787-
.clear_bit()
788-
.arlo()
789-
.clear_bit()
790-
.berr()
791-
.clear_bit()
792-
});
793-
794779
if sr1.timeout().bit_is_set() {
780+
self.i2c.sr1.modify(|_, w| w.timeout().clear_bit());
795781
return Err(Error::TIMEOUT);
796782
}
797783

798784
if sr1.pecerr().bit_is_set() {
785+
self.i2c.sr1.modify(|_, w| w.pecerr().clear_bit());
799786
return Err(Error::CRC);
800787
}
801788

802789
if sr1.ovr().bit_is_set() {
790+
self.i2c.sr1.modify(|_, w| w.ovr().clear_bit());
803791
return Err(Error::OVERRUN);
804792
}
805793

806794
if sr1.af().bit_is_set() {
795+
self.i2c.sr1.modify(|_, w| w.af().clear_bit());
807796
return Err(Error::NACK);
808797
}
809798

810799
if sr1.arlo().bit_is_set() {
800+
self.i2c.sr1.modify(|_, w| w.arlo().clear_bit());
811801
return Err(Error::ARBITRATION);
812802
}
813803

814804
if sr1.berr().bit_is_set() {
805+
self.i2c.sr1.modify(|_, w| w.berr().clear_bit());
815806
return Err(Error::BUS);
816807
}
817808

0 commit comments

Comments
 (0)