Skip to content

Commit 9334ca2

Browse files
committed
Fix Wire API compliance
TWI_MasterWriteRead return now differs if a read or a write was triggered Also, endTrasmissin() with zero bytes length is used to scan the bus; fix this case handling toRead==0 && toWrite==0
1 parent b2fa63a commit 9334ca2

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

cores/arduino/wiring.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,10 @@ void init()
528528

529529
//#endif
530530

531+
/****************************** TWI *******************************************/
532+
533+
// PORTMUX.TWISPIROUTEA = (PORTMUX_TWI0_ALT1_gc | PORTMUX_SPI0_ALT2_gc);
534+
531535

532536
/****************************** USART *****************************************/
533537

libraries/Wire/src/utility/twi.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,11 @@ uint8_t TWI_MasterWrite(uint8_t slave_address,
190190
uint8_t bytes_to_write,
191191
uint8_t send_stop)
192192
{
193-
TWI_MasterWriteRead(slave_address,
193+
return TWI_MasterWriteRead(slave_address,
194194
write_data,
195195
bytes_to_write,
196196
0,
197197
send_stop);
198-
199-
return master_trans_status;
200198
}
201199

202200

@@ -249,10 +247,10 @@ uint8_t TWI_MasterWriteRead(uint8_t slave_address,
249247

250248
/* Parameter sanity check. */
251249
if (bytes_to_write > TWI_BUFFER_SIZE) {
252-
return false;
250+
return 1;
253251
}
254252
if (bytes_to_read > TWI_BUFFER_SIZE) {
255-
return false;
253+
return 0;
256254
}
257255

258256
/*Initiate transaction if bus is ready. */
@@ -290,13 +288,28 @@ uint8_t TWI_MasterWriteRead(uint8_t slave_address,
290288
uint8_t readAddress = ADD_READ_BIT(master_slaveAddress);
291289
TWI0.MADDR = readAddress;
292290
}
293-
291+
292+
else if (master_bytesToWrite == 0 && master_bytesToRead == 0) {
293+
twi_mode = TWI_MODE_MASTER_TRANSMIT;
294+
uint8_t writeAddress = ADD_WRITE_BIT(master_slaveAddress);
295+
TWI0.MADDR = writeAddress;
296+
}
297+
294298
/* Arduino requires blocking function */
295-
while((master_bytesRead < master_bytesToRead) || (master_bytesWritten < master_bytesToWrite));
296-
297-
return master_bytesRead;
299+
while(master_result == TWIM_RESULT_UNKNOWN) {}
300+
301+
uint8_t ret = 0;
302+
if (master_bytesToRead > 0) {
303+
// return bytes really read
304+
ret = master_bytesRead;
305+
} else {
306+
// return 0 if success, >0 otherwise
307+
ret = (master_result == TWIM_RESULT_OK ? 0 : 1);
308+
}
309+
310+
return ret;
298311
} else {
299-
return 0;
312+
return 1;
300313
}
301314
}
302315

0 commit comments

Comments
 (0)