3
3
// 2013-06-05 by Jeff Rowberg <jeff@rowberg.net>
4
4
//
5
5
// Changelog:
6
+ // 2021-09-28 - allow custom Wire object as transaction function argument
7
+ // 2020-01-20 - hardija : complete support for Teensy 3.x
8
+ // 2015-10-30 - simondlevy : support i2c_t3 for Teensy3.1
6
9
// 2013-05-06 - add Francesco Ferrara's Fastwire v0.24 implementation with small modifications
7
10
// 2013-05-05 - fix issue with writing bit values to words (Sasquatch/Farzanegan)
8
11
// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
@@ -87,11 +90,6 @@ THE SOFTWARE.
87
90
88
91
#endif
89
92
90
- #ifndef BUFFER_LENGTH
91
- // band-aid fix for platforms without Wire-defined BUFFER_LENGTH (removed from some official implementations)
92
- #define BUFFER_LENGTH 32
93
- #endif
94
-
95
93
/* * Default constructor.
96
94
*/
97
95
I2Cdev::I2Cdev () {
@@ -105,9 +103,9 @@ I2Cdev::I2Cdev() {
105
103
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
106
104
* @return Status of read operation (true = success)
107
105
*/
108
- int8_t I2Cdev::readBit (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) {
106
+ int8_t I2Cdev::readBit (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout, void *wireObj ) {
109
107
uint8_t b;
110
- uint8_t count = readByte (devAddr, regAddr, &b, timeout);
108
+ uint8_t count = readByte (devAddr, regAddr, &b, timeout, wireObj );
111
109
*data = b & (1 << bitNum);
112
110
return count;
113
111
}
@@ -120,9 +118,9 @@ int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t
120
118
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
121
119
* @return Status of read operation (true = success)
122
120
*/
123
- int8_t I2Cdev::readBitW (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) {
121
+ int8_t I2Cdev::readBitW (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout, void *wireObj ) {
124
122
uint16_t b;
125
- uint8_t count = readWord (devAddr, regAddr, &b, timeout);
123
+ uint8_t count = readWord (devAddr, regAddr, &b, timeout, wireObj );
126
124
*data = b & (1 << bitNum);
127
125
return count;
128
126
}
@@ -136,14 +134,14 @@ int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16
136
134
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
137
135
* @return Status of read operation (true = success)
138
136
*/
139
- int8_t I2Cdev::readBits (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) {
137
+ int8_t I2Cdev::readBits (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout, void *wireObj ) {
140
138
// 01101001 read byte
141
139
// 76543210 bit numbers
142
140
// xxx args: bitStart=4, length=3
143
141
// 010 masked
144
142
// -> 010 shifted
145
143
uint8_t count, b;
146
- if ((count = readByte (devAddr, regAddr, &b, timeout)) != 0 ) {
144
+ if ((count = readByte (devAddr, regAddr, &b, timeout, wireObj )) != 0 ) {
147
145
uint8_t mask = ((1 << length) - 1 ) << (bitStart - length + 1 );
148
146
b &= mask;
149
147
b >>= (bitStart - length + 1 );
@@ -161,15 +159,15 @@ int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint
161
159
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
162
160
* @return Status of read operation (1 = success, 0 = failure, -1 = timeout)
163
161
*/
164
- int8_t I2Cdev::readBitsW (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) {
162
+ int8_t I2Cdev::readBitsW (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout, void *wireObj ) {
165
163
// 1101011001101001 read byte
166
164
// fedcba9876543210 bit numbers
167
165
// xxx args: bitStart=12, length=3
168
166
// 010 masked
169
167
// -> 010 shifted
170
168
uint8_t count;
171
169
uint16_t w;
172
- if ((count = readWord (devAddr, regAddr, &w, timeout)) != 0 ) {
170
+ if ((count = readWord (devAddr, regAddr, &w, timeout, wireObj )) != 0 ) {
173
171
uint16_t mask = ((1 << length) - 1 ) << (bitStart - length + 1 );
174
172
w &= mask;
175
173
w >>= (bitStart - length + 1 );
@@ -185,8 +183,8 @@ int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uin
185
183
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
186
184
* @return Status of read operation (true = success)
187
185
*/
188
- int8_t I2Cdev::readByte (uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) {
189
- return readBytes (devAddr, regAddr, 1 , data, timeout);
186
+ int8_t I2Cdev::readByte (uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout, void *wireObj ) {
187
+ return readBytes (devAddr, regAddr, 1 , data, timeout, wireObj );
190
188
}
191
189
192
190
/* * Read single word from a 16-bit device register.
@@ -196,8 +194,8 @@ int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_
196
194
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
197
195
* @return Status of read operation (true = success)
198
196
*/
199
- int8_t I2Cdev::readWord (uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) {
200
- return readWords (devAddr, regAddr, 1 , data, timeout);
197
+ int8_t I2Cdev::readWord (uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout, void *wireObj ) {
198
+ return readWords (devAddr, regAddr, 1 , data, timeout, wireObj );
201
199
}
202
200
203
201
/* * Read multiple bytes from an 8-bit device register.
@@ -208,7 +206,7 @@ int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16
208
206
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
209
207
* @return Number of bytes read (-1 indicates failure)
210
208
*/
211
- int8_t I2Cdev::readBytes (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) {
209
+ int8_t I2Cdev::readBytes (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout, void *wireObj ) {
212
210
#ifdef I2CDEV_SERIAL_DEBUG
213
211
Serial.print (" I2C (0x" );
214
212
Serial.print (devAddr, HEX);
@@ -219,74 +217,66 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8
219
217
Serial.print (" ..." );
220
218
#endif
221
219
222
- int8_t count = 0 ;
220
+ uint8_t count = 0 ;
223
221
uint32_t t1 = millis ();
224
222
225
223
#if (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
224
+ TwoWire *useWire = &Wire;
225
+ if (wireObj) useWire = (TwoWire *)wireObj;
226
226
227
227
#if (ARDUINO < 100)
228
228
// Arduino v00xx (before v1.0), Wire library
229
229
230
230
// I2C/TWI subsystem uses internal buffer that breaks with large data requests
231
- // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
231
+ // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in
232
232
// smaller chunks instead of all at once
233
- for (uint8_t k = 0 ; k < length; k += min ((int )length, BUFFER_LENGTH)) {
234
- Wire.beginTransmission (devAddr);
235
- Wire.send (regAddr);
236
- Wire.endTransmission ();
237
- Wire.beginTransmission (devAddr);
238
- Wire.requestFrom (devAddr, (uint8_t )min (length - k, BUFFER_LENGTH));
239
-
240
- for (; Wire.available () && (timeout == 0 || millis () - t1 < timeout); count++) {
241
- data[count] = Wire.receive ();
233
+ for (int k = 0 ; k < length; k += min ((int )length, I2CDEVLIB_WIRE_BUFFER_LENGTH)) {
234
+ useWire->beginTransmission (devAddr);
235
+ useWire->send (regAddr);
236
+ useWire->endTransmission ();
237
+ useWire->requestFrom ((uint8_t )devAddr, (uint8_t )min ((int )length - k, I2CDEVLIB_WIRE_BUFFER_LENGTH));
238
+ for (; useWire->available () && (timeout == 0 || millis () - t1 < timeout); count++) {
239
+ data[count] = useWire->receive ();
242
240
#ifdef I2CDEV_SERIAL_DEBUG
243
241
Serial.print (data[count], HEX);
244
242
if (count + 1 < length) Serial.print (" " );
245
243
#endif
246
244
}
247
-
248
- Wire.endTransmission ();
249
245
}
250
246
#elif (ARDUINO == 100)
251
247
// Arduino v1.0.0, Wire library
252
248
// Adds standardized write() and read() stream methods instead of send() and receive()
253
249
254
250
// I2C/TWI subsystem uses internal buffer that breaks with large data requests
255
- // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
251
+ // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in
256
252
// smaller chunks instead of all at once
257
- for (uint8_t k = 0 ; k < length; k += min ((int )length, BUFFER_LENGTH)) {
258
- Wire.beginTransmission (devAddr);
259
- Wire.write (regAddr);
260
- Wire.endTransmission ();
261
- Wire.beginTransmission (devAddr);
262
- Wire.requestFrom (devAddr, (uint8_t )min (length - k, BUFFER_LENGTH));
263
-
264
- for (; Wire.available () && (timeout == 0 || millis () - t1 < timeout); count++) {
265
- data[count] = Wire.read ();
253
+ for (int k = 0 ; k < length; k += min ((int )length, I2CDEVLIB_WIRE_BUFFER_LENGTH)) {
254
+ useWire->beginTransmission (devAddr);
255
+ useWire->write (regAddr);
256
+ useWire->endTransmission ();
257
+ useWire->requestFrom ((uint8_t )devAddr, (uint8_t )min ((int )length - k, I2CDEVLIB_WIRE_BUFFER_LENGTH));
258
+ for (; useWire->available () && (timeout == 0 || millis () - t1 < timeout); count++) {
259
+ data[count] = useWire->read ();
266
260
#ifdef I2CDEV_SERIAL_DEBUG
267
261
Serial.print (data[count], HEX);
268
262
if (count + 1 < length) Serial.print (" " );
269
263
#endif
270
264
}
271
-
272
- Wire.endTransmission ();
273
265
}
274
266
#elif (ARDUINO > 100)
275
267
// Arduino v1.0.1+, Wire library
276
268
// Adds official support for repeated start condition, yay!
277
269
278
270
// I2C/TWI subsystem uses internal buffer that breaks with large data requests
279
- // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
271
+ // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in
280
272
// smaller chunks instead of all at once
281
- for (uint8_t k = 0 ; k < length; k += min ((int )length, BUFFER_LENGTH)) {
282
- Wire.beginTransmission (devAddr);
283
- Wire.write (regAddr);
284
- Wire.endTransmission ();
285
- Wire.beginTransmission (devAddr);
286
- Wire.requestFrom (devAddr, (uint8_t )min (length - k, BUFFER_LENGTH));
287
-
288
- for (; Wire.available () && (timeout == 0 || millis () - t1 < timeout); count++) {
289
- data[count] = Wire.read ();
273
+ for (int k = 0 ; k < length; k += min ((int )length, I2CDEVLIB_WIRE_BUFFER_LENGTH)) {
274
+ useWire->beginTransmission (devAddr);
275
+ useWire->write (regAddr);
276
+ useWire->endTransmission ();
277
+ useWire->requestFrom ((uint8_t )devAddr, (uint8_t )min ((int )length - k, I2CDEVLIB_WIRE_BUFFER_LENGTH));
278
+ for (; useWire->available () && (timeout == 0 || millis () - t1 < timeout); count++) {
279
+ data[count] = useWire->read ();
290
280
#ifdef I2CDEV_SERIAL_DEBUG
291
281
Serial.print (data[count], HEX);
292
282
if (count + 1 < length) Serial.print (" " );
@@ -328,7 +318,7 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8
328
318
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
329
319
* @return Number of words read (-1 indicates failure)
330
320
*/
331
- int8_t I2Cdev::readWords (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) {
321
+ int8_t I2Cdev::readWords (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout, void *wireObj ) {
332
322
#ifdef I2CDEV_SERIAL_DEBUG
333
323
Serial.print (" I2C (0x" );
334
324
Serial.print (devAddr, HEX);
@@ -339,32 +329,33 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
339
329
Serial.print (" ..." );
340
330
#endif
341
331
342
- int8_t count = 0 ;
332
+ uint8_t count = 0 ;
343
333
uint32_t t1 = millis ();
344
334
345
335
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE
336
+ TwoWire *useWire = &Wire;
337
+ if (wireObj) useWire = (TwoWire *)wireObj;
346
338
347
339
#if (ARDUINO < 100)
348
340
// Arduino v00xx (before v1.0), Wire library
349
341
350
342
// I2C/TWI subsystem uses internal buffer that breaks with large data requests
351
- // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
343
+ // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in
352
344
// smaller chunks instead of all at once
353
- for (uint8_t k = 0 ; k < length * 2 ; k += min (length * 2 , BUFFER_LENGTH)) {
354
- Wire.beginTransmission (devAddr);
355
- Wire.send (regAddr);
356
- Wire.endTransmission ();
357
- Wire.beginTransmission (devAddr);
358
- Wire.requestFrom (devAddr, (uint8_t )(length * 2 )); // length=words, this wants bytes
345
+ for (uint8_t k = 0 ; k < length * 2 ; k += min (length * 2 , I2CDEVLIB_WIRE_BUFFER_LENGTH)) {
346
+ useWire->beginTransmission (devAddr);
347
+ useWire->send (regAddr);
348
+ useWire->endTransmission ();
349
+ useWire->requestFrom (devAddr, (uint8_t )(length * 2 )); // length=words, this wants bytes
359
350
360
351
bool msb = true ; // starts with MSB, then LSB
361
- for (; Wire. available () && count < length && (timeout == 0 || millis () - t1 < timeout);) {
352
+ for (; useWire-> available () && count < length && (timeout == 0 || millis () - t1 < timeout);) {
362
353
if (msb) {
363
354
// first byte is bits 15-8 (MSb=15)
364
- data[count] = Wire. receive () << 8 ;
355
+ data[count] = useWire-> receive () << 8 ;
365
356
} else {
366
357
// second byte is bits 7-0 (LSb=0)
367
- data[count] |= Wire. receive ();
358
+ data[count] |= useWire-> receive ();
368
359
#ifdef I2CDEV_SERIAL_DEBUG
369
360
Serial.print (data[count], HEX);
370
361
if (count + 1 < length) Serial.print (" " );
@@ -373,31 +364,28 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
373
364
}
374
365
msb = !msb;
375
366
}
376
-
377
- Wire.endTransmission ();
378
367
}
379
368
#elif (ARDUINO == 100)
380
369
// Arduino v1.0.0, Wire library
381
370
// Adds standardized write() and read() stream methods instead of send() and receive()
382
371
383
372
// I2C/TWI subsystem uses internal buffer that breaks with large data requests
384
- // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
373
+ // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in
385
374
// smaller chunks instead of all at once
386
- for (uint8_t k = 0 ; k < length * 2 ; k += min (length * 2 , BUFFER_LENGTH)) {
387
- Wire.beginTransmission (devAddr);
388
- Wire.write (regAddr);
389
- Wire.endTransmission ();
390
- Wire.beginTransmission (devAddr);
391
- Wire.requestFrom (devAddr, (uint8_t )(length * 2 )); // length=words, this wants bytes
375
+ for (uint8_t k = 0 ; k < length * 2 ; k += min (length * 2 , I2CDEVLIB_WIRE_BUFFER_LENGTH)) {
376
+ useWire->beginTransmission (devAddr);
377
+ useWire->write (regAddr);
378
+ useWire->endTransmission ();
379
+ useWire->requestFrom (devAddr, (uint8_t )(length * 2 )); // length=words, this wants bytes
392
380
393
381
bool msb = true ; // starts with MSB, then LSB
394
- for (; Wire. available () && count < length && (timeout == 0 || millis () - t1 < timeout);) {
382
+ for (; useWire-> available () && count < length && (timeout == 0 || millis () - t1 < timeout);) {
395
383
if (msb) {
396
384
// first byte is bits 15-8 (MSb=15)
397
- data[count] = Wire. read () << 8 ;
385
+ data[count] = useWire-> read () << 8 ;
398
386
} else {
399
387
// second byte is bits 7-0 (LSb=0)
400
- data[count] |= Wire. read ();
388
+ data[count] |= useWire-> read ();
401
389
#ifdef I2CDEV_SERIAL_DEBUG
402
390
Serial.print (data[count], HEX);
403
391
if (count + 1 < length) Serial.print (" " );
@@ -406,31 +394,28 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
406
394
}
407
395
msb = !msb;
408
396
}
409
-
410
- Wire.endTransmission ();
411
397
}
412
398
#elif (ARDUINO > 100)
413
399
// Arduino v1.0.1+, Wire library
414
400
// Adds official support for repeated start condition, yay!
415
401
416
402
// I2C/TWI subsystem uses internal buffer that breaks with large data requests
417
- // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
403
+ // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in
418
404
// smaller chunks instead of all at once
419
- for (uint8_t k = 0 ; k < length * 2 ; k += min (length * 2 , BUFFER_LENGTH)) {
420
- Wire.beginTransmission (devAddr);
421
- Wire.write (regAddr);
422
- Wire.endTransmission ();
423
- Wire.beginTransmission (devAddr);
424
- Wire.requestFrom (devAddr, (uint8_t )(length * 2 )); // length=words, this wants bytes
405
+ for (uint8_t k = 0 ; k < length * 2 ; k += min (length * 2 , I2CDEVLIB_WIRE_BUFFER_LENGTH)) {
406
+ useWire->beginTransmission (devAddr);
407
+ useWire->write (regAddr);
408
+ useWire->endTransmission ();
409
+ useWire->requestFrom (devAddr, (uint8_t )(length * 2 )); // length=words, this wants bytes
425
410
426
411
bool msb = true ; // starts with MSB, then LSB
427
- for (; Wire. available () && count < length && (timeout == 0 || millis () - t1 < timeout);) {
412
+ for (; useWire-> available () && count < length && (timeout == 0 || millis () - t1 < timeout);) {
428
413
if (msb) {
429
414
// first byte is bits 15-8 (MSb=15)
430
- data[count] = Wire. read () << 8 ;
415
+ data[count] = useWire-> read () << 8 ;
431
416
} else {
432
417
// second byte is bits 7-0 (LSb=0)
433
- data[count] |= Wire. read ();
418
+ data[count] |= useWire-> read ();
434
419
#ifdef I2CDEV_SERIAL_DEBUG
435
420
Serial.print (data[count], HEX);
436
421
if (count + 1 < length) Serial.print (" " );
@@ -439,8 +424,6 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
439
424
}
440
425
msb = !msb;
441
426
}
442
-
443
- Wire.endTransmission ();
444
427
}
445
428
#endif
446
429
@@ -479,11 +462,11 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
479
462
* @param value New bit value to write
480
463
* @return Status of operation (true = success)
481
464
*/
482
- bool I2Cdev::writeBit (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) {
465
+ bool I2Cdev::writeBit (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data, void *wireObj ) {
483
466
uint8_t b;
484
- readByte (devAddr, regAddr, &b);
467
+ readByte (devAddr, regAddr, &b, I2Cdev::readTimeout, wireObj );
485
468
b = (data != 0 ) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
486
- return writeByte (devAddr, regAddr, b);
469
+ return writeByte (devAddr, regAddr, b, wireObj );
487
470
}
488
471
489
472
/* * write a single bit in a 16-bit device register.
@@ -493,11 +476,11 @@ bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t
493
476
* @param value New bit value to write
494
477
* @return Status of operation (true = success)
495
478
*/
496
- bool I2Cdev::writeBitW (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) {
479
+ bool I2Cdev::writeBitW (uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data, void *wireObj ) {
497
480
uint16_t w;
498
- readWord (devAddr, regAddr, &w);
481
+ readWord (devAddr, regAddr, &w, I2Cdev::readTimeout, wireObj );
499
482
w = (data != 0 ) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum));
500
- return writeWord (devAddr, regAddr, w);
483
+ return writeWord (devAddr, regAddr, w, wireObj );
501
484
}
502
485
503
486
/* * Write multiple bits in an 8-bit device register.
@@ -508,7 +491,7 @@ bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_
508
491
* @param data Right-aligned value to write
509
492
* @return Status of operation (true = success)
510
493
*/
511
- bool I2Cdev::writeBits (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) {
494
+ bool I2Cdev::writeBits (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data, void *wireObj ) {
512
495
// 010 value to write
513
496
// 76543210 bit numbers
514
497
// xxx args: bitStart=4, length=3
@@ -517,13 +500,13 @@ bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8
517
500
// 10100011 original & ~mask
518
501
// 10101011 masked | value
519
502
uint8_t b;
520
- if (readByte (devAddr, regAddr, &b) != 0 ) {
503
+ if (readByte (devAddr, regAddr, &b, I2Cdev::readTimeout, wireObj ) != 0 ) {
521
504
uint8_t mask = ((1 << length) - 1 ) << (bitStart - length + 1 );
522
505
data <<= (bitStart - length + 1 ); // shift data into correct position
523
506
data &= mask; // zero all non-important bits in data
524
507
b &= ~(mask); // zero all important bits in existing byte
525
508
b |= data; // combine data with existing byte
526
- return writeByte (devAddr, regAddr, b);
509
+ return writeByte (devAddr, regAddr, b, wireObj );
527
510
} else {
528
511
return false ;
529
512
}
@@ -537,7 +520,7 @@ bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8
537
520
* @param data Right-aligned value to write
538
521
* @return Status of operation (true = success)
539
522
*/
540
- bool I2Cdev::writeBitsW (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) {
523
+ bool I2Cdev::writeBitsW (uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data, void *wireObj ) {
541
524
// 010 value to write
542
525
// fedcba9876543210 bit numbers
543
526
// xxx args: bitStart=12, length=3
@@ -546,13 +529,13 @@ bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint
546
529
// 1010001110010110 original & ~mask
547
530
// 1010101110010110 masked | value
548
531
uint16_t w;
549
- if (readWord (devAddr, regAddr, &w) != 0 ) {
532
+ if (readWord (devAddr, regAddr, &w, I2Cdev::readTimeout, wireObj ) != 0 ) {
550
533
uint16_t mask = ((1 << length) - 1 ) << (bitStart - length + 1 );
551
534
data <<= (bitStart - length + 1 ); // shift data into correct position
552
535
data &= mask; // zero all non-important bits in data
553
536
w &= ~(mask); // zero all important bits in existing word
554
537
w |= data; // combine data with existing word
555
- return writeWord (devAddr, regAddr, w);
538
+ return writeWord (devAddr, regAddr, w, wireObj );
556
539
} else {
557
540
return false ;
558
541
}
@@ -564,8 +547,8 @@ bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint
564
547
* @param data New byte value to write
565
548
* @return Status of operation (true = success)
566
549
*/
567
- bool I2Cdev::writeByte (uint8_t devAddr, uint8_t regAddr, uint8_t data) {
568
- return writeBytes (devAddr, regAddr, 1 , &data);
550
+ bool I2Cdev::writeByte (uint8_t devAddr, uint8_t regAddr, uint8_t data, void *wireObj ) {
551
+ return writeBytes (devAddr, regAddr, 1 , &data, wireObj );
569
552
}
570
553
571
554
/* * Write single word to a 16-bit device register.
@@ -574,8 +557,8 @@ bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) {
574
557
* @param data New word value to write
575
558
* @return Status of operation (true = success)
576
559
*/
577
- bool I2Cdev::writeWord (uint8_t devAddr, uint8_t regAddr, uint16_t data) {
578
- return writeWords (devAddr, regAddr, 1 , &data);
560
+ bool I2Cdev::writeWord (uint8_t devAddr, uint8_t regAddr, uint16_t data, void *wireObj ) {
561
+ return writeWords (devAddr, regAddr, 1 , &data, wireObj );
579
562
}
580
563
581
564
/* * Write multiple bytes to an 8-bit device register.
@@ -585,7 +568,7 @@ bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) {
585
568
* @param data Buffer to copy new data from
586
569
* @return Status of operation (true = success)
587
570
*/
588
- bool I2Cdev::writeBytes (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t * data) {
571
+ bool I2Cdev::writeBytes (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t * data, void *wireObj ) {
589
572
#ifdef I2CDEV_SERIAL_DEBUG
590
573
Serial.print (" I2C (0x" );
591
574
Serial.print (devAddr, HEX);
@@ -596,14 +579,20 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_
596
579
Serial.print (" ..." );
597
580
#endif
598
581
uint8_t status = 0 ;
582
+
583
+ #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE
584
+ TwoWire *useWire = &Wire;
585
+ if (wireObj) useWire = (TwoWire *)wireObj;
586
+ #endif
587
+
599
588
#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
600
- Wire. beginTransmission (devAddr);
601
- Wire. send ((uint8_t ) regAddr); // send address
589
+ useWire-> beginTransmission (devAddr);
590
+ useWire-> send ((uint8_t ) regAddr); // send address
602
591
#elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \
603
592
|| (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100 ) \
604
593
|| I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
605
- Wire. beginTransmission (devAddr);
606
- Wire. write ((uint8_t ) regAddr); // send address
594
+ useWire-> beginTransmission (devAddr);
595
+ useWire-> write ((uint8_t ) regAddr); // send address
607
596
#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
608
597
Fastwire::beginTransmission (devAddr);
609
598
Fastwire::write (regAddr);
@@ -614,21 +603,21 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_
614
603
if (i + 1 < length) Serial.print (" " );
615
604
#endif
616
605
#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
617
- Wire. send ((uint8_t ) data[i]);
606
+ useWire-> send ((uint8_t ) data[i]);
618
607
#elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \
619
608
|| (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100 ) \
620
609
|| I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
621
- Wire. write ((uint8_t ) data[i]);
610
+ useWire-> write ((uint8_t ) data[i]);
622
611
#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
623
612
Fastwire::write ((uint8_t ) data[i]);
624
613
#endif
625
614
}
626
615
#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
627
- Wire. endTransmission ();
616
+ useWire-> endTransmission ();
628
617
#elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \
629
618
|| (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100 ) \
630
619
|| I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
631
- status = Wire. endTransmission ();
620
+ status = useWire-> endTransmission ();
632
621
#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
633
622
Fastwire::stop ();
634
623
// status = Fastwire::endTransmission();
@@ -646,7 +635,7 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_
646
635
* @param data Buffer to copy new data from
647
636
* @return Status of operation (true = success)
648
637
*/
649
- bool I2Cdev::writeWords (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t * data) {
638
+ bool I2Cdev::writeWords (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t * data, void *wireObj ) {
650
639
#ifdef I2CDEV_SERIAL_DEBUG
651
640
Serial.print (" I2C (0x" );
652
641
Serial.print (devAddr, HEX);
@@ -657,14 +646,20 @@ bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16
657
646
Serial.print (" ..." );
658
647
#endif
659
648
uint8_t status = 0 ;
649
+
650
+ #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE
651
+ TwoWire *useWire = &Wire;
652
+ if (wireObj) useWire = (TwoWire *)wireObj;
653
+ #endif
654
+
660
655
#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
661
- Wire. beginTransmission (devAddr);
662
- Wire. send (regAddr); // send address
656
+ useWire-> beginTransmission (devAddr);
657
+ useWire-> send (regAddr); // send address
663
658
#elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \
664
659
|| (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100 ) \
665
660
|| I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
666
- Wire. beginTransmission (devAddr);
667
- Wire. write (regAddr); // send address
661
+ useWire-> beginTransmission (devAddr);
662
+ useWire-> write (regAddr); // send address
668
663
#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
669
664
Fastwire::beginTransmission (devAddr);
670
665
Fastwire::write (regAddr);
@@ -675,25 +670,25 @@ bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16
675
670
if (i + 1 < length) Serial.print (" " );
676
671
#endif
677
672
#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
678
- Wire. send ((uint8_t )(data[i] >> 8 )); // send MSB
679
- Wire. send ((uint8_t )data[i]); // send LSB
673
+ useWire-> send ((uint8_t )(data[i] >> 8 )); // send MSB
674
+ useWire-> send ((uint8_t )data[i]); // send LSB
680
675
#elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \
681
676
|| (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100 ) \
682
677
|| I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
683
- Wire. write ((uint8_t )(data[i] >> 8 )); // send MSB
684
- Wire. write ((uint8_t )data[i]); // send LSB
678
+ useWire-> write ((uint8_t )(data[i] >> 8 )); // send MSB
679
+ useWire-> write ((uint8_t )data[i]); // send LSB
685
680
#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
686
681
Fastwire::write ((uint8_t )(data[i] >> 8 )); // send MSB
687
682
status = Fastwire::write ((uint8_t )data[i]); // send LSB
688
683
if (status != 0 ) break ;
689
684
#endif
690
685
}
691
686
#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
692
- Wire. endTransmission ();
687
+ useWire-> endTransmission ();
693
688
#elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \
694
689
|| (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100 ) \
695
690
|| I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE)
696
- status = Wire. endTransmission ();
691
+ status = useWire-> endTransmission ();
697
692
#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
698
693
Fastwire::stop ();
699
694
// status = Fastwire::endTransmission();
@@ -753,7 +748,7 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
753
748
#endif
754
749
755
750
TWSR = 0 ; // no prescaler => prescaler = 1
756
- TWBR = (( 16000L / khz) - 16 ) / 2 ; // change the I2C clock rate
751
+ TWBR = F_CPU / 2000 / khz - 8 ; // change the I2C clock rate
757
752
TWCR = 1 << TWEN; // enable twi module, no interrupt
758
753
}
759
754
@@ -993,7 +988,7 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
993
988
TWBR = ((CPU_FREQ / TWI_FREQ) - 16 ) / 2 ; // bitrate register
994
989
// enable twi module, acks, and twi interrupt
995
990
996
- TWCR = _BV ( TWEN) | _BV ( TWIE) | _BV ( TWEA);
991
+ TWCR = ( 1 << TWEN) | ( 1 << TWIE) | ( 1 << TWEA);
997
992
998
993
/* TWEN - TWI Enable Bit
999
994
TWIE - TWI Interrupt Enable
@@ -1062,7 +1057,7 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
1062
1057
}
1063
1058
1064
1059
void twii_SetStart () {
1065
- TWCR = _BV ( TWEN) | _BV ( TWIE) | _BV ( TWEA) | _BV ( TWINT) | _BV ( TWSTA);
1060
+ TWCR = ( 1 << TWEN) | ( 1 << TWIE) | ( 1 << TWEA) | ( 1 << TWINT) | ( 1 << TWSTA);
1066
1061
}
1067
1062
1068
1063
void twi_write01 () {
@@ -1145,19 +1140,19 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
1145
1140
void twi_reply (uint8_t ack) {
1146
1141
// transmit master read ready signal, with or without ack
1147
1142
if (ack){
1148
- TWCR = _BV ( TWEN) | _BV ( TWIE) | _BV ( TWINT) | _BV ( TWEA);
1143
+ TWCR = ( 1 << TWEN) | ( 1 << TWIE) | ( 1 << TWINT) | ( 1 << TWEA);
1149
1144
} else {
1150
- TWCR = _BV ( TWEN) | _BV ( TWIE) | _BV ( TWINT);
1145
+ TWCR = ( 1 << TWEN) | ( 1 << TWIE) | ( 1 << TWINT);
1151
1146
}
1152
1147
}
1153
1148
1154
1149
void twi_stop (void ) {
1155
1150
// send stop condition
1156
- TWCR = _BV ( TWEN) | _BV ( TWIE) | _BV ( TWEA) | _BV ( TWINT) | _BV ( TWSTO);
1151
+ TWCR = ( 1 << TWEN) | ( 1 << TWIE) | ( 1 << TWEA) | ( 1 << TWINT) | ( 1 << TWSTO);
1157
1152
1158
1153
// wait for stop condition to be exectued on bus
1159
1154
// TWINT is not set after a stop condition!
1160
- while (TWCR & _BV ( TWSTO)) {
1155
+ while (TWCR & ( 1 << TWSTO)) {
1161
1156
continue ;
1162
1157
}
1163
1158
@@ -1167,7 +1162,7 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
1167
1162
1168
1163
void twi_releaseBus (void ) {
1169
1164
// release bus
1170
- TWCR = _BV ( TWEN) | _BV ( TWIE) | _BV ( TWEA) | _BV ( TWINT);
1165
+ TWCR = ( 1 << TWEN) | ( 1 << TWIE) | ( 1 << TWEA) | ( 1 << TWINT);
1171
1166
1172
1167
// update twi state
1173
1168
twi_state = TWI_READY;
0 commit comments