Skip to content

Fix for Due Wire library #1994

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Fix for Due Wire library
Fix reading and use of TWI status register.
Also, update endTransmission to be compatible with original & give more
useful return.
  • Loading branch information
bluesign2k committed Apr 6, 2014
commit d8d6d62853a5308c21b95dad4bdd64e358e857cc
56 changes: 39 additions & 17 deletions hardware/arduino/sam/libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,46 @@ static inline bool TWI_FailedAcknowledge(Twi *pTwi) {
}

static inline bool TWI_WaitTransferComplete(Twi *_twi, uint32_t _timeout) {
while (!TWI_TransferComplete(_twi)) {
if (TWI_FailedAcknowledge(_twi))
uint32_t _status_reg = 0;
while ((_status_reg & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {
_status_reg = TWI_GetStatus(_twi);

if (_status_reg & TWI_SR_NACK)
return false;

if (--_timeout == 0)
return false;
}
return true;
}

static inline bool TWI_WaitByteSent(Twi *_twi, uint32_t _timeout) {
while (!TWI_ByteSent(_twi)) {
if (TWI_FailedAcknowledge(_twi))
uint32_t _status_reg = 0;
while ((_status_reg & TWI_SR_TXRDY) != TWI_SR_TXRDY) {
_status_reg = TWI_GetStatus(_twi);

if (_status_reg & TWI_SR_NACK)
return false;

if (--_timeout == 0)
return false;
}

return true;
}

static inline bool TWI_WaitByteReceived(Twi *_twi, uint32_t _timeout) {
while (!TWI_ByteReceived(_twi)) {
if (TWI_FailedAcknowledge(_twi))
uint32_t _status_reg = 0;
while ((_status_reg & TWI_SR_RXRDY) != TWI_SR_RXRDY) {
_status_reg = TWI_GetStatus(_twi);

if (_status_reg & TWI_SR_NACK)
return false;

if (--_timeout == 0)
return false;
}

return true;
}

Expand Down Expand Up @@ -175,22 +189,30 @@ void TwoWire::beginTransmission(int address) {
// devices will behave oddly if they do not see a STOP.
//
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
uint8_t error = 0;
// transmit buffer (blocking)
TWI_StartWrite(twi, txAddress, 0, 0, txBuffer[0]);
TWI_WaitByteSent(twi, XMIT_TIMEOUT);
int sent = 1;
while (sent < txBufferLength) {
TWI_WriteByte(twi, txBuffer[sent++]);
TWI_WaitByteSent(twi, XMIT_TIMEOUT);
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
error = 2; // error, got NACK on address transmit

if (error == 0) {
uint16_t sent = 1;
while (sent < txBufferLength) {
TWI_WriteByte(twi, txBuffer[sent++]);
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
error = 3; // error, got NACK during data transmmit
}
}

if (error == 0) {
TWI_Stop(twi);
if (!TWI_WaitTransferComplete(twi, XMIT_TIMEOUT))
error = 4; // error, finishing up
}
TWI_Stop( twi);
TWI_WaitTransferComplete(twi, XMIT_TIMEOUT);

// empty buffer
txBufferLength = 0;

txBufferLength = 0; // empty buffer
status = MASTER_IDLE;
return sent;
return error;
}

// This provides backwards compatibility with the original
Expand Down