Skip to content

Commit bc51bc5

Browse files
committed
[TEST][DANGER]Add timeout to startTransmissionWIRE
1 parent 4146aa8 commit bc51bc5

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

cores/arduino/SERCOM.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
#include "Arduino.h"
1920
#include "SERCOM.h"
2021
#include "variant.h"
2122

@@ -499,8 +500,13 @@ bool SERCOM::startTransmissionWIRE(uint8_t address, SercomWireReadWriteFlag flag
499500
// 7-bits address + 1-bits R/W
500501
address = (address << 0x1ul) | flag;
501502

503+
long timeout = 50000;
502504
// Wait idle or owner bus mode
503-
while ( !isBusIdleWIRE() && !isBusOwnerWIRE() );
505+
while ( !isBusIdleWIRE() && !isBusOwnerWIRE() ) {
506+
if (timeout-- < 0) {
507+
return false;
508+
}
509+
}
504510

505511
// Send start and address
506512
sercom->I2CM.ADDR.bit.ADDR = address;
@@ -512,6 +518,12 @@ bool SERCOM::startTransmissionWIRE(uint8_t address, SercomWireReadWriteFlag flag
512518
{
513519
// Wait transmission complete
514520
}
521+
// Check for loss of arbitration (multiple masters starting communication at the same time)
522+
if(!isBusOwnerWIRE())
523+
{
524+
// Restart communication
525+
startTransmissionWIRE(address >> 1, flag);
526+
}
515527
}
516528
else // Read mode
517529
{

0 commit comments

Comments
 (0)