Skip to content

Commit e079baa

Browse files
bluesign2kcmaglie
authored andcommitted
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.
1 parent 4d5d0f6 commit e079baa

File tree

1 file changed

+39
-17
lines changed
  • hardware/arduino/sam/libraries/Wire

1 file changed

+39
-17
lines changed

hardware/arduino/sam/libraries/Wire/Wire.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,46 @@ static inline bool TWI_FailedAcknowledge(Twi *pTwi) {
2929
}
3030

3131
static inline bool TWI_WaitTransferComplete(Twi *_twi, uint32_t _timeout) {
32-
while (!TWI_TransferComplete(_twi)) {
33-
if (TWI_FailedAcknowledge(_twi))
32+
uint32_t _status_reg = 0;
33+
while ((_status_reg & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {
34+
_status_reg = TWI_GetStatus(_twi);
35+
36+
if (_status_reg & TWI_SR_NACK)
3437
return false;
38+
3539
if (--_timeout == 0)
3640
return false;
3741
}
3842
return true;
3943
}
4044

4145
static inline bool TWI_WaitByteSent(Twi *_twi, uint32_t _timeout) {
42-
while (!TWI_ByteSent(_twi)) {
43-
if (TWI_FailedAcknowledge(_twi))
46+
uint32_t _status_reg = 0;
47+
while ((_status_reg & TWI_SR_TXRDY) != TWI_SR_TXRDY) {
48+
_status_reg = TWI_GetStatus(_twi);
49+
50+
if (_status_reg & TWI_SR_NACK)
4451
return false;
52+
4553
if (--_timeout == 0)
4654
return false;
4755
}
56+
4857
return true;
4958
}
5059

5160
static inline bool TWI_WaitByteReceived(Twi *_twi, uint32_t _timeout) {
52-
while (!TWI_ByteReceived(_twi)) {
53-
if (TWI_FailedAcknowledge(_twi))
61+
uint32_t _status_reg = 0;
62+
while ((_status_reg & TWI_SR_RXRDY) != TWI_SR_RXRDY) {
63+
_status_reg = TWI_GetStatus(_twi);
64+
65+
if (_status_reg & TWI_SR_NACK)
5466
return false;
67+
5568
if (--_timeout == 0)
5669
return false;
5770
}
71+
5872
return true;
5973
}
6074

@@ -175,22 +189,30 @@ void TwoWire::beginTransmission(int address) {
175189
// devices will behave oddly if they do not see a STOP.
176190
//
177191
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
192+
uint8_t error = 0;
178193
// transmit buffer (blocking)
179194
TWI_StartWrite(twi, txAddress, 0, 0, txBuffer[0]);
180-
TWI_WaitByteSent(twi, XMIT_TIMEOUT);
181-
int sent = 1;
182-
while (sent < txBufferLength) {
183-
TWI_WriteByte(twi, txBuffer[sent++]);
184-
TWI_WaitByteSent(twi, XMIT_TIMEOUT);
195+
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
196+
error = 2; // error, got NACK on address transmit
197+
198+
if (error == 0) {
199+
uint16_t sent = 1;
200+
while (sent < txBufferLength) {
201+
TWI_WriteByte(twi, txBuffer[sent++]);
202+
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
203+
error = 3; // error, got NACK during data transmmit
204+
}
205+
}
206+
207+
if (error == 0) {
208+
TWI_Stop(twi);
209+
if (!TWI_WaitTransferComplete(twi, XMIT_TIMEOUT))
210+
error = 4; // error, finishing up
185211
}
186-
TWI_Stop( twi);
187-
TWI_WaitTransferComplete(twi, XMIT_TIMEOUT);
188-
189-
// empty buffer
190-
txBufferLength = 0;
191212

213+
txBufferLength = 0; // empty buffer
192214
status = MASTER_IDLE;
193-
return sent;
215+
return error;
194216
}
195217

196218
// This provides backwards compatibility with the original

0 commit comments

Comments
 (0)