Skip to content

Commit

Permalink
add SERCOM patch from arduino SAMD core
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro authored and turmary committed Dec 4, 2020
1 parent 55b798c commit c8507b9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,18 @@ bool SERCOM::startTransmissionWIRE(uint8_t address, SercomWireReadWriteFlag flag
// 7-bits address + 1-bits R/W
address = (address << 0x1ul) | flag;

// Wait idle or owner bus mode
while ( !isBusIdleWIRE() && !isBusOwnerWIRE() );
// If another master owns the bus or the last bus owner has not properly
// sent a stop, return failure early. This will prevent some misbehaved
// devices from deadlocking here at the cost of the caller being responsible
// for retrying the failed transmission. See SercomWireBusState for the
// possible bus states.
if(!isBusOwnerWIRE())
{
if( isBusBusyWIRE() || (isArbLostWIRE() && !isBusIdleWIRE()) || isBusUnknownWIRE() )
{
return false;
}
}

// Send start and address
sercom->I2CM.ADDR.bit.ADDR = address;
Expand Down Expand Up @@ -639,6 +649,21 @@ bool SERCOM::isBusOwnerWIRE( void )
return sercom->I2CM.STATUS.bit.BUSSTATE == WIRE_OWNER_STATE;
}

bool SERCOM::isBusUnknownWIRE( void )
{
return sercom->I2CM.STATUS.bit.BUSSTATE == WIRE_UNKNOWN_STATE;
}

bool SERCOM::isArbLostWIRE( void )
{
return sercom->I2CM.STATUS.bit.ARBLOST == 1;
}

bool SERCOM::isBusBusyWIRE( void )
{
return sercom->I2CM.STATUS.bit.BUSSTATE == WIRE_BUSY_STATE;
}

bool SERCOM::isDataReadyWIRE( void )
{
return sercom->I2CS.INTFLAG.bit.DRDY;
Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/SERCOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class SERCOM
bool isSlaveWIRE( void ) ;
bool isBusIdleWIRE( void ) ;
bool isBusOwnerWIRE( void ) ;
bool isBusUnknownWIRE( void ) ;
bool isArbLostWIRE( void );
bool isBusBusyWIRE( void );
bool isDataReadyWIRE( void ) ;
bool isStopDetectedWIRE( void ) ;
bool isRestartDetectedWIRE( void ) ;
Expand Down

0 comments on commit c8507b9

Please sign in to comment.