-
Notifications
You must be signed in to change notification settings - Fork 11
/
I2C.cpp
executable file
·709 lines (651 loc) · 16.8 KB
/
I2C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
I2C.cpp - I2C library
Copyright (c) 2011-2012 Wayne Truchsess. All right reserved.
Rev 5.0 - January 24th, 2012
- Removed the use of interrupts completely from the library
so TWI state changes are now polled.
- Added calls to lockup() function in most functions
to combat arbitration problems
- Fixed scan() procedure which left timeouts enabled
and set to 80msec after exiting procedure
- Changed scan() address range back to 0 - 0x7F
- Removed all Wire legacy functions from library
- A big thanks to Richard Baldwin for all the testing
and feedback with debugging bus lockups!
Rev 4.0 - January 14th, 2012
- Updated to make compatible with 8MHz clock frequency
Rev 3.0 - January 9th, 2012
- Modified library to be compatible with Arduino 1.0
- Changed argument type from boolean to uint8_t in pullUp(),
setSpeed() and receiveByte() functions for 1.0 compatability
- Modified return values for timeout feature to report
back where in the transmission the timeout occured.
- added function scan() to perform a bus scan to find devices
attached to the I2C bus. Similar to work done by Todbot
and Nick Gammon
Rev 2.0 - September 19th, 2011
- Added support for timeout function to prevent
and recover from bus lockup (thanks to PaulS
and CrossRoads on the Arduino forum)
- Changed return type for stop() from void to
uint8_t to handle timeOut function
Rev 1.0 - August 8th, 2011
This is a modified version of the Arduino Wire/TWI
library. Functions were rewritten to provide more functionality
and also the use of Repeated Start. Some I2C devices will not
function correctly without the use of a Repeated Start. The
initial version of this library only supports the Master.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if(ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <inttypes.h>
#include "I2C.h"
uint8_t I2C::bytesAvailable = 0;
uint8_t I2C::bufferIndex = 0;
uint8_t I2C::totalBytes = 0;
uint16_t I2C::timeOutDelay = 0;
I2C::I2C()
{
}
////////////// Public Methods ////////////////////////////////////////
void I2C::begin()
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// activate internal pull-ups for twi
// as per note from atmega8 manual pg167
sbi(PORTC, 4);
sbi(PORTC, 5);
#else
// activate internal pull-ups for twi
// as per note from atmega128 manual pg204
sbi(PORTD, 0);
sbi(PORTD, 1);
#endif
// initialize twi prescaler and bit rate
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = ((F_CPU / 100000) - 16) / 2;
// enable twi module and acks
TWCR = _BV(TWEN) | _BV(TWEA);
}
void I2C::end()
{
TWCR = 0;
}
void I2C::timeOut(uint16_t _timeOut)
{
timeOutDelay = _timeOut;
}
void I2C::setSpeed(uint8_t _fast)
{
if(!_fast)
{
TWBR = ((F_CPU / 100000) - 16) / 2;
}
else
{
TWBR = ((F_CPU / 400000) - 16) / 2;
}
}
void I2C::pullup(uint8_t activate)
{
if(activate)
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// activate internal pull-ups for twi
// as per note from atmega8 manual pg167
sbi(PORTC, 4);
sbi(PORTC, 5);
#else
// activate internal pull-ups for twi
// as per note from atmega128 manual pg204
sbi(PORTD, 0);
sbi(PORTD, 1);
#endif
}
else
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// deactivate internal pull-ups for twi
// as per note from atmega8 manual pg167
cbi(PORTC, 4);
cbi(PORTC, 5);
#else
// deactivate internal pull-ups for twi
// as per note from atmega128 manual pg204
cbi(PORTD, 0);
cbi(PORTD, 1);
#endif
}
}
void I2C::scan()
{
uint16_t tempTime = timeOutDelay;
timeOut(80);
uint8_t totalDevicesFound = 0;
Serial.println("Scanning for devices...please wait");
Serial.println();
for(uint8_t s = 0; s <= 0x7F; s++)
{
returnStatus = 0;
returnStatus = start();
if(!returnStatus)
{
returnStatus = sendAddress(SLA_W(s));
}
if(returnStatus)
{
if(returnStatus == 1)
{
Serial.println("There is a problem with the bus, could not complete scan");
timeOutDelay = tempTime;
return;
}
}
else
{
Serial.print("Found device at address - ");
Serial.print(" 0x");
Serial.println(s,HEX);
totalDevicesFound++;
}
stop();
}
if(!totalDevicesFound){Serial.println("No devices found");}
timeOutDelay = tempTime;
}
uint8_t I2C::available()
{
return(bytesAvailable);
}
uint8_t I2C::receive()
{
bufferIndex = totalBytes - bytesAvailable;
if(!bytesAvailable)
{
bufferIndex = 0;
return(0);
}
bytesAvailable--;
return(data[bufferIndex]);
}
/*return values for new functions that use the timeOut feature
will now return at what point in the transmission the timeout
occurred. Looking at a full communication sequence between a
master and slave (transmit data and then readback data) there
a total of 7 points in the sequence where a timeout can occur.
These are listed below and correspond to the returned value:
1 - Waiting for successful completion of a Start bit
2 - Waiting for ACK/NACK while addressing slave in transmit mode (MT)
3 - Waiting for ACK/NACK while sending data to the slave
4 - Waiting for successful completion of a Repeated Start
5 - Waiting for ACK/NACK while addressing slave in receiver mode (MR)
6 - Waiting for ACK/NACK while receiving data from the slave
7 - Waiting for successful completion of the Stop bit
All possible return values:
0 Function executed with no errors
1 - 7 Timeout occurred, see above list
8 - 0xFF See datasheet for exact meaning */
/////////////////////////////////////////////////////
uint8_t I2C::write(uint8_t address, uint8_t registerAddress)
{
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::write(int address, int registerAddress)
{
return(write((uint8_t) address, (uint8_t) registerAddress));
}
uint8_t I2C::write(uint8_t address, uint8_t registerAddress, uint8_t data)
{
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = sendByte(data);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::write(int address, int registerAddress, int data)
{
return(write((uint8_t) address, (uint8_t) registerAddress, (uint8_t) data));
}
uint8_t I2C::write(uint8_t address, uint8_t registerAddress, char *data)
{
uint8_t bufferLength = strlen(data);
returnStatus = 0;
returnStatus = write(address, registerAddress, (uint8_t*)data, bufferLength);
return(returnStatus);
}
uint8_t I2C::write(uint8_t address, uint8_t registerAddress, uint8_t *data, uint8_t numberBytes)
{
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
for (uint8_t i = 0; i < numberBytes; i++)
{
returnStatus = sendByte(data[i]);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(int address, int numberBytes)
{
return(read((uint8_t) address, (uint8_t) numberBytes));
}
uint8_t I2C::read(uint8_t address, uint8_t numberBytes)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
data[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(int address, int registerAddress, int numberBytes)
{
return(read((uint8_t) address, (uint8_t) registerAddress, (uint8_t) numberBytes));
}
uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = start();
if(returnStatus)
{
if(returnStatus == 1){return(4);}
return(returnStatus);
}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
data[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
dataBuffer[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes, uint8_t *dataBuffer)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = start();
if(returnStatus)
{
if(returnStatus == 1){return(4);}
return(returnStatus);
}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
dataBuffer[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
/////////////// Private Methods ////////////////////////////////////////
uint8_t I2C::start()
{
unsigned long startingTime = millis();
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if ((TWI_STATUS == START) || (TWI_STATUS == REPEATED_START))
{
return(0);
}
if (TWI_STATUS == LOST_ARBTRTN)
{
uint8_t bufferedStatus = TWI_STATUS;
lockUp();
return(bufferedStatus);
}
return(TWI_STATUS);
}
uint8_t I2C::sendAddress(uint8_t i2cAddress)
{
TWDR = i2cAddress;
unsigned long startingTime = millis();
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if ((TWI_STATUS == MT_SLA_ACK) || (TWI_STATUS == MR_SLA_ACK))
{
return(0);
}
uint8_t bufferedStatus = TWI_STATUS;
if ((TWI_STATUS == MT_SLA_NACK) || (TWI_STATUS == MR_SLA_NACK))
{
stop();
return(bufferedStatus);
}
else
{
lockUp();
return(bufferedStatus);
}
}
uint8_t I2C::sendByte(uint8_t i2cData)
{
TWDR = i2cData;
unsigned long startingTime = millis();
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if (TWI_STATUS == MT_DATA_ACK)
{
return(0);
}
uint8_t bufferedStatus = TWI_STATUS;
if (TWI_STATUS == MT_DATA_NACK)
{
stop();
return(bufferedStatus);
}
else
{
lockUp();
return(bufferedStatus);
}
}
uint8_t I2C::receiveByte(uint8_t ack)
{
unsigned long startingTime = millis();
if(ack)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
}
else
{
TWCR = (1<<TWINT) | (1<<TWEN);
}
while (!(TWCR & (1<<TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if (TWI_STATUS == LOST_ARBTRTN)
{
uint8_t bufferedStatus = TWI_STATUS;
lockUp();
return(bufferedStatus);
}
return(TWI_STATUS);
}
uint8_t I2C::stop()
{
unsigned long startingTime = millis();
TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO);
while ((TWCR & (1<<TWSTO)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
return(0);
}
void I2C::lockUp()
{
TWCR = 0; //releases SDA and SCL lines to high impedance
TWCR = _BV(TWEN) | _BV(TWEA); //reinitialize TWI
}
I2C I2c = I2C();