@@ -550,6 +550,10 @@ impl PinScl<FMPI2C> for PF15<AlternateOD<AF4>> {}
550
550
pub enum Error {
551
551
OVERRUN ,
552
552
NACK ,
553
+ TIMEOUT ,
554
+ BUS ,
555
+ CRC ,
556
+ ARBITRATION ,
553
557
}
554
558
555
559
#[ cfg( any(
@@ -767,6 +771,44 @@ where
767
771
self . i2c . cr1 . modify ( |_, w| w. pe ( ) . set_bit ( ) ) ;
768
772
}
769
773
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
+
770
812
pub fn release ( self ) -> ( I2C , PINS ) {
771
813
( self . i2c , self . pins )
772
814
}
@@ -789,10 +831,15 @@ where
789
831
self . i2c . cr1 . modify ( |_, w| w. start ( ) . set_bit ( ) ) ;
790
832
791
833
// 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
+ } { }
793
838
794
839
// Also wait until signalled we're master and everything is waiting for us
795
840
while {
841
+ self . check_and_clear_error_flags ( ) ?;
842
+
796
843
let sr2 = self . i2c . sr2 . read ( ) ;
797
844
sr2. msl ( ) . bit_is_clear ( ) && sr2. busy ( ) . bit_is_clear ( )
798
845
} { }
@@ -803,7 +850,13 @@ where
803
850
. write ( |w| unsafe { w. bits ( u32:: from ( addr) << 1 ) } ) ;
804
851
805
852
// 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
+ } { }
807
860
808
861
// Clear condition by reading SR2
809
862
self . i2c . sr2 . read ( ) ;
@@ -819,28 +872,35 @@ where
819
872
820
873
fn send_byte ( & self , byte : u8 ) -> Result < ( ) , Error > {
821
874
// 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
+ } { }
823
881
824
882
// Push out a byte of data
825
883
self . i2c . dr . write ( |w| unsafe { w. bits ( u32:: from ( byte) ) } ) ;
826
884
827
885
// Wait until byte is transferred
828
886
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 ( ) ?;
835
889
836
- sr1. btf ( ) . bit_is_clear ( )
890
+ self . i2c . sr1 . read ( ) . btf ( ) . bit_is_clear ( )
837
891
} { }
838
892
839
893
Ok ( ( ) )
840
894
}
841
895
842
896
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
+
844
904
let value = self . i2c . dr . read ( ) . bits ( ) as u8 ;
845
905
Ok ( value)
846
906
}
0 commit comments