Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slave Wire: fix TwoWire::onService() to handle repeated start #590

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,61 +219,60 @@ void TwoWire::onRequest(void(*function)(void))

void TwoWire::onService(void)
{
if ( sercom->isSlaveWIRE() )
if (sercom->isSlaveWIRE())
{
if(sercom->isStopDetectedWIRE() ||
(sercom->isAddressMatch() && sercom->isRestartDetectedWIRE() && !sercom->isMasterReadOperationWIRE())) //Stop or Restart detected
if (sercom->isStopDetectedWIRE() || sercom->isAddressMatch())
{
sercom->prepareAckBitWIRE();
sercom->prepareCommandBitsWire(0x03);

//Calling onReceiveCallback, if exists
if(onReceiveCallback)
{
onReceiveCallback(available());
if (sercom->isStopDetectedWIRE() ||
(sercom->isRestartDetectedWIRE() && available()))
{ // Stop or Restart after Rx detected

// Calling onReceiveCallback, if exists
if (onReceiveCallback)
{
onReceiveCallback(available());
}
rxBuffer.clear();
}

rxBuffer.clear();
}
else if(sercom->isAddressMatch()) //Address Match
{
sercom->prepareAckBitWIRE();
sercom->prepareCommandBitsWire(0x03);

if(sercom->isMasterReadOperationWIRE()) //Is a request ?
{
if (sercom->isMasterReadOperationWIRE())
{ // Is a request ?
txBuffer.clear();

transmissionBegun = true;

//Calling onRequestCallback, if exists
if(onRequestCallback)
// Calling onRequestCallback, if exists
if (onRequestCallback)
{
onRequestCallback();
}
}
}
else if(sercom->isDataReadyWIRE())
else if (sercom->isDataReadyWIRE())
{
if (sercom->isMasterReadOperationWIRE())
{
uint8_t c = 0xff;

if( txBuffer.available() ) {
if (txBuffer.available())
{
c = txBuffer.read_char();
}

transmissionBegun = sercom->sendDataSlaveWIRE(c);
} else { //Received data
if (rxBuffer.isFull()) {
sercom->prepareNackBitWIRE();
} else {
//Store data
}
else
{
// Received data
if (rxBuffer.isFull())
{
sercom->prepareNackBitWIRE();
}
else
{
// Store data
rxBuffer.store_char(sercom->readDataWIRE());

sercom->prepareAckBitWIRE();
sercom->prepareAckBitWIRE();
}

sercom->prepareCommandBitsWire(0x03);
}
}
Expand Down