@@ -556,29 +556,35 @@ bool SERCOM::startTransmissionWIRE(uint8_t address, SercomWireReadWriteFlag flag
556
556
// Address Transmitted
557
557
if ( flag == WIRE_WRITE_FLAG ) // Write mode
558
558
{
559
- while ( !sercom->I2CM .INTFLAG .bit .MB )
560
- {
559
+ while ( !sercom->I2CM .INTFLAG .bit .MB ) {
561
560
// Wait transmission complete
561
+
562
+ // If certain errors occur, the MB bit may never be set (RFTM: SAMD21 sec:28.10.6; SAMD51 sec:36.10.7).
563
+ // The data transfer errors that can occur (including BUSERR) are all
564
+ // rolled up into INTFLAG.bit.ERROR from STATUS.reg
565
+ if (sercom->I2CM .INTFLAG .bit .ERROR ) {
566
+ return false ;
567
+ }
562
568
}
563
569
}
564
570
else // Read mode
565
571
{
566
- while ( !sercom->I2CM .INTFLAG .bit .SB )
567
- {
568
- // If the slave NACKS the address, the MB bit will be set.
569
- // In that case, send a stop condition and return false.
570
- if (sercom->I2CM .INTFLAG .bit .MB ) {
571
- sercom->I2CM .CTRLB .bit .CMD = 3 ; // Stop condition
572
- return false ;
573
- }
572
+ while ( !sercom->I2CM .INTFLAG .bit .SB ) {
574
573
// Wait transmission complete
574
+
575
+ // If the slave NACKS the address, the MB bit will be set.
576
+ // A variety of errors in the STATUS register can set the ERROR bit in the INTFLAG register
577
+ // In that case, send a stop condition and return false.
578
+ if (sercom->I2CM .INTFLAG .bit .MB || sercom->I2CM .INTFLAG .bit .ERROR ) {
579
+ sercom->I2CM .CTRLB .bit .CMD = 3 ; // Stop condition
580
+ return false ;
581
+ }
575
582
}
576
583
577
584
// Clean the 'Slave on Bus' flag, for further usage.
578
585
// sercom->I2CM.INTFLAG.bit.SB = 0x1ul;
579
586
}
580
587
581
-
582
588
// ACK received (0: ACK, 1: NACK)
583
589
if (sercom->I2CM .STATUS .bit .RXNACK )
584
590
{
@@ -597,10 +603,11 @@ bool SERCOM::sendDataMasterWIRE(uint8_t data)
597
603
598
604
// Wait transmission successful
599
605
while (!sercom->I2CM .INTFLAG .bit .MB ) {
600
-
601
- // If a bus error occurs, the MB bit may never be set.
602
- // Check the bus error bit and bail if it's set.
603
- if (sercom->I2CM .STATUS .bit .BUSERR ) {
606
+ // If a data transfer error occurs, the MB bit may never be set.
607
+ // Check the error bit and bail if it's set.
608
+ // The data transfer errors that can occur (including BUSERR) are all
609
+ // rolled up into INTFLAG.bit.ERROR from STATUS.reg
610
+ if (sercom->I2CM .INTFLAG .bit .ERROR ) {
604
611
return false ;
605
612
}
606
613
}
@@ -701,9 +708,17 @@ uint8_t SERCOM::readDataWIRE( void )
701
708
{
702
709
if (isMasterWIRE ())
703
710
{
704
- while ( sercom->I2CM .INTFLAG .bit .SB == 0 )
705
- {
711
+ while (sercom->I2CM .INTFLAG .bit .SB == 0 ) {
706
712
// Waiting complete receive
713
+ // A variety of errors in the STATUS register can set the ERROR bit in the INTFLAG register
714
+ // In that case, send a stop condition and return false.
715
+ // readDataWIRE should really be able to indicate an error (which would never be used
716
+ // because the readDataWIRE callers (in Wire.cpp) should have checked availableWIRE() first and timed it
717
+ // out if the data never showed up
718
+ if (sercom->I2CM .INTFLAG .bit .MB || sercom->I2CM .INTFLAG .bit .ERROR ) {
719
+ sercom->I2CM .CTRLB .bit .CMD = 3 ; // Stop condition
720
+ return 0xFF ;
721
+ }
707
722
}
708
723
709
724
return sercom->I2CM .DATA .bit .DATA ;
0 commit comments