Skip to content

Commit 49b821b

Browse files
Merge #203
203: Adding NACK detection and fault condition clearing r=therealprof a=ryan-summers This PR fixes #202 by adding the following: 1. During all busy-wait I2C loops, error flags are continually checked. 2. All fault flags have been updated to manually clear the fault indication. cc: @therealprof Co-authored-by: Ryan Summers <[email protected]>
2 parents 76012d6 + e1fdb58 commit 49b821b

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- [breaking-change] Add a 2 ms delay after changing SDIO card power setting.
1717
- [breaking-change] Changed sdio::{read, write}_block buf argument to &[u8; 512].
1818
- Voltage regulator overdrive is enabled where supported and required for selected HCLK.
19+
- I2C driver updated to detect and clear all error condition flags.
1920

2021
### Added
2122

src/i2c.rs

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ impl PinScl<FMPI2C> for PF15<AlternateOD<AF4>> {}
550550
pub enum Error {
551551
OVERRUN,
552552
NACK,
553+
TIMEOUT,
554+
BUS,
555+
CRC,
556+
ARBITRATION,
553557
}
554558

555559
#[cfg(any(
@@ -767,6 +771,44 @@ where
767771
self.i2c.cr1.modify(|_, w| w.pe().set_bit());
768772
}
769773

774+
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.
777+
let sr1 = self.i2c.sr1.read();
778+
779+
if sr1.timeout().bit_is_set() {
780+
self.i2c.sr1.modify(|_, w| w.timeout().clear_bit());
781+
return Err(Error::TIMEOUT);
782+
}
783+
784+
if sr1.pecerr().bit_is_set() {
785+
self.i2c.sr1.modify(|_, w| w.pecerr().clear_bit());
786+
return Err(Error::CRC);
787+
}
788+
789+
if sr1.ovr().bit_is_set() {
790+
self.i2c.sr1.modify(|_, w| w.ovr().clear_bit());
791+
return Err(Error::OVERRUN);
792+
}
793+
794+
if sr1.af().bit_is_set() {
795+
self.i2c.sr1.modify(|_, w| w.af().clear_bit());
796+
return Err(Error::NACK);
797+
}
798+
799+
if sr1.arlo().bit_is_set() {
800+
self.i2c.sr1.modify(|_, w| w.arlo().clear_bit());
801+
return Err(Error::ARBITRATION);
802+
}
803+
804+
if sr1.berr().bit_is_set() {
805+
self.i2c.sr1.modify(|_, w| w.berr().clear_bit());
806+
return Err(Error::BUS);
807+
}
808+
809+
Ok(())
810+
}
811+
770812
pub fn release(self) -> (I2C, PINS) {
771813
(self.i2c, self.pins)
772814
}
@@ -789,10 +831,15 @@ where
789831
self.i2c.cr1.modify(|_, w| w.start().set_bit());
790832

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

794839
// Also wait until signalled we're master and everything is waiting for us
795840
while {
841+
self.check_and_clear_error_flags()?;
842+
796843
let sr2 = self.i2c.sr2.read();
797844
sr2.msl().bit_is_clear() && sr2.busy().bit_is_clear()
798845
} {}
@@ -803,7 +850,13 @@ where
803850
.write(|w| unsafe { w.bits(u32::from(addr) << 1) });
804851

805852
// Wait until address was sent
806-
while self.i2c.sr1.read().addr().bit_is_clear() {}
853+
while {
854+
// Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set.
855+
self.check_and_clear_error_flags()?;
856+
857+
// Wait for the address to be acknowledged
858+
self.i2c.sr1.read().addr().bit_is_clear()
859+
} {}
807860

808861
// Clear condition by reading SR2
809862
self.i2c.sr2.read();
@@ -819,28 +872,35 @@ where
819872

820873
fn send_byte(&self, byte: u8) -> Result<(), Error> {
821874
// Wait until we're ready for sending
822-
while self.i2c.sr1.read().tx_e().bit_is_clear() {}
875+
while {
876+
// 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()
880+
} {}
823881

824882
// Push out a byte of data
825883
self.i2c.dr.write(|w| unsafe { w.bits(u32::from(byte)) });
826884

827885
// Wait until byte is transferred
828886
while {
829-
let sr1 = self.i2c.sr1.read();
830-
831-
// If we received a NACK, then this is an error
832-
if sr1.af().bit_is_set() {
833-
return Err(Error::NACK);
834-
}
887+
// Check for any potential error conditions.
888+
self.check_and_clear_error_flags()?;
835889

836-
sr1.btf().bit_is_clear()
890+
self.i2c.sr1.read().btf().bit_is_clear()
837891
} {}
838892

839893
Ok(())
840894
}
841895

842896
fn recv_byte(&self) -> Result<u8, Error> {
843-
while self.i2c.sr1.read().rx_ne().bit_is_clear() {}
897+
while {
898+
// Check for any potential error conditions.
899+
self.check_and_clear_error_flags()?;
900+
901+
self.i2c.sr1.read().rx_ne().bit_is_clear()
902+
} {}
903+
844904
let value = self.i2c.dr.read().bits() as u8;
845905
Ok(value)
846906
}

0 commit comments

Comments
 (0)