Skip to content

Commit 2d3ad65

Browse files
schonaifacchinm
authored andcommitted
Define names for twi errors
Rebase of arduino/Arduino#2152
1 parent 6c861d8 commit 2d3ad65

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

libraries/Wire/src/utility/twi.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
20+
Modified 2014 by Mickael Germain to define names for twi errors.
2021
*/
2122

2223
#include <math.h>
@@ -217,19 +218,19 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
217218
* length: number of bytes in array
218219
* wait: boolean indicating to wait for write or not
219220
* sendStop: boolean indicating whether or not to send a stop at the end
220-
* Output 0 .. success
221-
* 1 .. length to long for buffer
222-
* 2 .. address send, NACK received
223-
* 3 .. data send, NACK received
224-
* 4 .. other twi error (lost bus arbitration, bus error, ..)
221+
* Output TWI_SUCCESS .. success
222+
* TWI_DATA_TOO_LONG .. length to long for buffer
223+
* TWI_NACK_ON_ADDR .. address send, NACK received
224+
* TWI_NACK_ON_DATA .. data send, NACK received
225+
* TWI_OTHER_ERROR .. other twi error (lost bus arbitration, bus error, ..)
225226
*/
226227
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
227228
{
228229
uint8_t i;
229230

230231
// ensure data will fit into buffer
231232
if(TWI_BUFFER_LENGTH < length){
232-
return 1;
233+
return TWI_DATA_TOO_LONG;
233234
}
234235

235236
// wait until twi is ready, become master transmitter
@@ -280,13 +281,13 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
280281
}
281282

282283
if (twi_error == 0xFF)
283-
return 0; // success
284+
return TWI_SUCCESS; // success
284285
else if (twi_error == TW_MT_SLA_NACK)
285-
return 2; // error: address send, nack received
286+
return TWI_NACK_ON_ADDR; // error: address send, nack received
286287
else if (twi_error == TW_MT_DATA_NACK)
287-
return 3; // error: data send, nack received
288+
return TWI_NACK_ON_DATA; // error: data send, nack received
288289
else
289-
return 4; // other twi error
290+
return TWI_OTHER_ERROR; // other twi error
290291
}
291292

292293
/*
@@ -295,22 +296,22 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
295296
* must be called in slave tx event callback
296297
* Input data: pointer to byte array
297298
* length: number of bytes in array
298-
* Output 1 length too long for buffer
299-
* 2 not slave transmitter
300-
* 0 ok
299+
* Output TWI_DATA_TOO_LONG .. length too long for buffer
300+
* TWI_NOT_SLAVE_TX .. not slave transmitter
301+
* 0 .. ok
301302
*/
302303
uint8_t twi_transmit(const uint8_t* data, uint8_t length)
303304
{
304305
uint8_t i;
305306

306307
// ensure data will fit into buffer
307308
if(TWI_BUFFER_LENGTH < (twi_txBufferLength+length)){
308-
return 1;
309+
return TWI_DATA_TOO_LONG;
309310
}
310311

311312
// ensure we are currently a slave transmitter
312313
if(TWI_STX != twi_state){
313-
return 2;
314+
return TWI_NOT_SLAVE_TX;
314315
}
315316

316317
// set length and copy data into tx buffer

libraries/Wire/src/utility/twi.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@
3737
#define TWI_MTX 2
3838
#define TWI_SRX 3
3939
#define TWI_STX 4
40-
40+
41+
#define TWI_SUCCESS 0
42+
#define TWI_DATA_TOO_LONG 1
43+
#define TWI_NACK_ON_ADDR 2
44+
#define TWI_NOT_SLAVE_TX 2
45+
#define TWI_NACK_ON_DATA 3
46+
#define TWI_OTHER_ERROR 4
47+
4148
void twi_init(void);
4249
void twi_disable(void);
4350
void twi_setAddress(uint8_t);

0 commit comments

Comments
 (0)