Skip to content

Commit 23c6ec9

Browse files
Merge #206
206: Optimize I2C status flags reading r=thalesfragoso a=therealprof Signed-off-by: Daniel Egger <[email protected]> Co-authored-by: Daniel Egger <[email protected]>
2 parents ceee3f3 + 08982c8 commit 23c6ec9

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/i2c.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ where
771771
self.i2c.cr1.modify(|_, w| w.pe().set_bit());
772772
}
773773

774-
fn check_and_clear_error_flags(&self) -> Result<(), Error> {
774+
fn check_and_clear_error_flags(&self) -> Result<i2c1::sr1::R, Error> {
775775
// Note that flags should only be cleared once they have been registered. If flags are
776776
// cleared otherwise, there may be an inherent race condition and flags may be missed.
777777
let sr1 = self.i2c.sr1.read();
@@ -806,7 +806,7 @@ where
806806
return Err(Error::BUS);
807807
}
808808

809-
Ok(())
809+
Ok(sr1)
810810
}
811811

812812
pub fn release(self) -> (I2C, PINS) {
@@ -831,10 +831,7 @@ where
831831
self.i2c.cr1.modify(|_, w| w.start().set_bit());
832832

833833
// Wait until START condition was generated
834-
while {
835-
self.check_and_clear_error_flags()?;
836-
self.i2c.sr1.read().sb().bit_is_clear()
837-
} {}
834+
while self.check_and_clear_error_flags()?.sb().bit_is_clear() {}
838835

839836
// Also wait until signalled we're master and everything is waiting for us
840837
while {
@@ -852,10 +849,10 @@ where
852849
// Wait until address was sent
853850
while {
854851
// Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set.
855-
self.check_and_clear_error_flags()?;
852+
let sr1 = self.check_and_clear_error_flags()?;
856853

857854
// Wait for the address to be acknowledged
858-
self.i2c.sr1.read().addr().bit_is_clear()
855+
sr1.addr().bit_is_clear()
859856
} {}
860857

861858
// Clear condition by reading SR2
@@ -874,9 +871,7 @@ where
874871
// Wait until we're ready for sending
875872
while {
876873
// Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set.
877-
self.check_and_clear_error_flags()?;
878-
879-
self.i2c.sr1.read().tx_e().bit_is_clear()
874+
self.check_and_clear_error_flags()?.tx_e().bit_is_clear()
880875
} {}
881876

882877
// Push out a byte of data
@@ -885,9 +880,7 @@ where
885880
// Wait until byte is transferred
886881
while {
887882
// Check for any potential error conditions.
888-
self.check_and_clear_error_flags()?;
889-
890-
self.i2c.sr1.read().btf().bit_is_clear()
883+
self.check_and_clear_error_flags()?.btf().bit_is_clear()
891884
} {}
892885

893886
Ok(())

0 commit comments

Comments
 (0)