@@ -772,46 +772,37 @@ where
772
772
}
773
773
774
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.
775
777
let sr1 = self . i2c . sr1 . read ( ) ;
776
778
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
-
794
779
if sr1. timeout ( ) . bit_is_set ( ) {
780
+ self . i2c . sr1 . modify ( |_, w| w. timeout ( ) . clear_bit ( ) ) ;
795
781
return Err ( Error :: TIMEOUT ) ;
796
782
}
797
783
798
784
if sr1. pecerr ( ) . bit_is_set ( ) {
785
+ self . i2c . sr1 . modify ( |_, w| w. pecerr ( ) . clear_bit ( ) ) ;
799
786
return Err ( Error :: CRC ) ;
800
787
}
801
788
802
789
if sr1. ovr ( ) . bit_is_set ( ) {
790
+ self . i2c . sr1 . modify ( |_, w| w. ovr ( ) . clear_bit ( ) ) ;
803
791
return Err ( Error :: OVERRUN ) ;
804
792
}
805
793
806
794
if sr1. af ( ) . bit_is_set ( ) {
795
+ self . i2c . sr1 . modify ( |_, w| w. af ( ) . clear_bit ( ) ) ;
807
796
return Err ( Error :: NACK ) ;
808
797
}
809
798
810
799
if sr1. arlo ( ) . bit_is_set ( ) {
800
+ self . i2c . sr1 . modify ( |_, w| w. arlo ( ) . clear_bit ( ) ) ;
811
801
return Err ( Error :: ARBITRATION ) ;
812
802
}
813
803
814
804
if sr1. berr ( ) . bit_is_set ( ) {
805
+ self . i2c . sr1 . modify ( |_, w| w. berr ( ) . clear_bit ( ) ) ;
815
806
return Err ( Error :: BUS ) ;
816
807
}
817
808
0 commit comments