1 // Modified for use in the openXsensor code by Mike B.
5 Copyright (c) 2011-2012 Wayne Truchsess. All right reserved.
6 Rev 5.0 - January 24th, 2012
7 - Removed the use of interrupts completely from the library
8 so TWI state changes are now polled.
9 - Added calls to lockup() function in most functions
10 to combat arbitration problems
11 - Fixed scan() procedure which left timeouts enabled
12 and set to 80msec after exiting procedure
13 - Changed scan() address range back to 0 - 0x7F
14 - Removed all Wire legacy functions from library
15 - A big thanks to Richard Baldwin for all the testing
16 and feedback with debugging bus lockups!
17 Rev 4.0 - January 14th, 2012
18 - Updated to make compatible with 8MHz clock frequency
19 Rev 3.0 - January 9th, 2012
20 - Modified library to be compatible with Arduino 1.0
21 - Changed argument type from boolean to uint8_t in pullUp(),
22 setSpeed() and receiveByte() functions for 1.0 compatability
23 - Modified return values for timeout feature to report
24 back where in the transmission the timeout occured.
25 - added function scan() to perform a bus scan to find devices
26 attached to the I2C bus. Similar to work done by Todbot
28 Rev 2.0 - September 19th, 2011
29 - Added support for timeout function to prevent
30 and recover from bus lockup (thanks to PaulS
31 and CrossRoads on the Arduino forum)
32 - Changed return type for stop() from void to
33 uint8_t to handle timeOut function
34 Rev 1.0 - August 8th, 2011
36 This is a modified version of the Arduino Wire/TWI
37 library. Functions were rewritten to provide more functionality
38 and also the use of Repeated Start. Some I2C devices will not
39 function correctly without the use of a Repeated Start. The
40 initial version of this library only supports the Master.
43 This library is free software; you can redistribute it and/or
44 modify it under the terms of the GNU Lesser General Public
45 License as published by the Free Software Foundation; either
46 version 2.1 of the License, or (at your option) any later version.
48 This library is distributed in the hope that it will be useful,
49 but WITHOUT ANY WARRANTY; without even the implied warranty of
50 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
51 Lesser General Public License for more details.
53 You should have received a copy of the GNU Lesser General Public
54 License along with this library; if not, write to the Free Software
55 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
69 uint8_t I2C::bytesAvailable
= 0;
70 uint8_t I2C::bufferIndex
= 0;
71 uint8_t I2C::totalBytes
= 0;
72 uint16_t I2C::timeOutDelay
= 0;
79 ////////////// Public Methods ////////////////////////////////////////
85 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
86 // activate internal pull-ups for twi
87 // as per note from atmega8 manual pg167
88 // with modules like GY-63 there is no need for pull-up because the module have already pull-up resistor
89 // it is safier not to activate internal pull-up when Arduino is running 5 volt and external sensors are running 3.3 volt
93 // activate internal pull-ups for twi
94 // as per note from atmega128 manual pg204
98 // initialize twi prescaler and bit rate
101 TWBR
= ((F_CPU
/ 400000) - 16) / 2; //use 100khz ; for 400khz use TWBR = ((F_CPU / 400000) - 16) / 2;
102 // enable twi module and acks
103 TWCR
= _BV(TWEN
) | _BV(TWEA
);
111 void I2C::timeOut(uint16_t _timeOut
)
113 timeOutDelay
= _timeOut
;
116 void I2C::setSpeed(uint8_t _fast
)
120 TWBR
= ((F_CPU
/ 100000) - 16) / 2;
124 TWBR
= ((F_CPU
/ 400000) - 16) / 2;
128 void I2C::pullup(uint8_t activate
)
132 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
133 // activate internal pull-ups for twi
134 // as per note from atmega8 manual pg167
138 // activate internal pull-ups for twi
139 // as per note from atmega128 manual pg204
146 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
147 // deactivate internal pull-ups for twi
148 // as per note from atmega8 manual pg167
152 // deactivate internal pull-ups for twi
153 // as per note from atmega128 manual pg204
163 uint16_t tempTime
= timeOutDelay
;
165 uint8_t totalDevicesFound
= 0;
167 // Serial.println("Scanning for devices...please wait");
169 for(uint8_t s
= 0; s
<= 0x7F; s
++)
172 returnStatus
= start();
175 returnStatus
= sendAddress(SLA_W(s
));
179 if(returnStatus
== 1)
181 // Serial.println("There is a problem with the bus, could not complete scan");
182 timeOutDelay
= tempTime
;
188 // Serial.print("Found device at address - ");
189 // Serial.print(" 0x");
190 // Serial.println(s,HEX);
198 // if(!totalDevicesFound){Serial.println("No devices found");}
199 timeOutDelay
= tempTime
;
203 uint8_t I2C::available()
205 return(bytesAvailable
);
208 uint8_t I2C::receive()
210 bufferIndex
= totalBytes
- bytesAvailable
;
217 return(data
[bufferIndex
]);
221 /*return values for new functions that use the timeOut feature
222 will now return at what point in the transmission the timeout
223 occurred. Looking at a full communication sequence between a
224 master and slave (transmit data and then readback data) there
225 a total of 7 points in the sequence where a timeout can occur.
226 These are listed below and correspond to the returned value:
227 1 - Waiting for successful completion of a Start bit
228 2 - Waiting for ACK/NACK while addressing slave in transmit mode (MT)
229 3 - Waiting for ACK/NACK while sending data to the slave
230 4 - Waiting for successful completion of a Repeated Start
231 5 - Waiting for ACK/NACK while addressing slave in receiver mode (MR)
232 6 - Waiting for ACK/NACK while receiving data from the slave
233 7 - Waiting for successful completion of the Stop bit
235 All possible return values:
236 0 Function executed with no errors
237 1 - 7 Timeout occurred, see above list
238 8 - 0xFF See datasheet for exact meaning */
241 /////////////////////////////////////////////////////
243 uint8_t I2C::write(uint8_t address
, uint8_t registerAddress
)
246 returnStatus
= start();
248 return(returnStatus
);
250 returnStatus
= sendAddress(SLA_W(address
));
253 if(returnStatus
== 1){return(2);}
254 return(returnStatus
);
256 returnStatus
= sendByte(registerAddress
);
259 if(returnStatus
== 1){return(3);}
260 return(returnStatus
);
262 returnStatus
= stop();
265 if(returnStatus
== 1){return(7);}
266 return(returnStatus
);
268 return(returnStatus
);
271 //uint8_t I2C::write(int address, int registerAddress)
273 // return(write((uint8_t) address, (uint8_t) registerAddress));
276 uint8_t I2C::write(uint8_t address
, uint8_t registerAddress
, uint8_t data
)
279 returnStatus
= start();
281 return(returnStatus
);
283 returnStatus
= sendAddress(SLA_W(address
));
286 if(returnStatus
== 1){
289 return(returnStatus
);
291 returnStatus
= sendByte(registerAddress
);
294 if(returnStatus
== 1){
297 return(returnStatus
);
299 returnStatus
= sendByte(data
);
302 if(returnStatus
== 1){
305 return(returnStatus
);
307 returnStatus
= stop();
310 if(returnStatus
== 1){
313 return(returnStatus
);
315 return(returnStatus
);
318 //uint8_t I2C::write(int address, int registerAddress, int data)
320 // return(write((uint8_t) address, (uint8_t) registerAddress, (uint8_t) data));
323 uint8_t I2C::write(uint8_t address
, uint8_t registerAddress
, char *data
)
325 uint8_t bufferLength
= strlen(data
);
327 returnStatus
= write(address
, registerAddress
, bufferLength
,(uint8_t*)data
);
328 return(returnStatus
);
331 uint8_t I2C::write(uint8_t address
, uint8_t registerAddress
, uint8_t numberBytes
, uint8_t * data
)
334 returnStatus
= start();
336 return(returnStatus
);
338 returnStatus
= sendAddress(SLA_W(address
));
341 if(returnStatus
== 1){
344 return(returnStatus
);
346 returnStatus
= sendByte(registerAddress
);
349 if(returnStatus
== 1){return(3);}
350 return(returnStatus
);
352 for (uint8_t i
= 0; i
< numberBytes
; i
++)
354 returnStatus
= sendByte(data
[i
]);
357 if(returnStatus
== 1){
360 return(returnStatus
);
363 returnStatus
= stop();
366 if(returnStatus
== 1){
369 return(returnStatus
);
371 return(returnStatus
);
375 // Read a device whitout sending first the register to be read; code modified in order to allow that number of byte = 0 (needed for sensor 4525DO)
376 // return a status (0 = OK, others see above)
377 uint8_t I2C::read(uint8_t address
, uint8_t numberBytes
)
381 // if(numberBytes == 0){numberBytes++;}
382 // nack = numberBytes - 1;
384 returnStatus
= start();
386 return(returnStatus
);
388 returnStatus
= sendAddress(SLA_R(address
));
391 if(returnStatus
== 1){
394 return(returnStatus
);
397 nack
= numberBytes
- 1;
398 for(uint8_t i
= 0; i
< numberBytes
; i
++)
402 returnStatus
= receiveByte(0);
403 if(returnStatus
== 1){
406 if(returnStatus
!= MR_DATA_NACK
){
407 return(returnStatus
);
412 returnStatus
= receiveByte(1);
413 if(returnStatus
== 1){
416 if(returnStatus
!= MR_DATA_ACK
){
417 return(returnStatus
);
421 bytesAvailable
= i
+1;
425 returnStatus
= stop();
428 if(returnStatus
== 1){
431 return(returnStatus
);
433 return(returnStatus
);
436 // read a device after sending a command (= register address)
437 // return a status (0 = OK, others see above)
438 uint8_t I2C::read(uint8_t address
, uint8_t registerAddress
, uint8_t numberBytes
)
442 if(numberBytes
== 0){numberBytes
++;}
443 nack
= numberBytes
- 1;
445 returnStatus
= start();
447 return(returnStatus
);
449 returnStatus
= sendAddress(SLA_W(address
));
452 if(returnStatus
== 1){
455 return(returnStatus
);
457 returnStatus
= sendByte(registerAddress
);
460 if(returnStatus
== 1){
463 return(returnStatus
);
465 returnStatus
= start();
468 if(returnStatus
== 1){
471 return(returnStatus
);
473 returnStatus
= sendAddress(SLA_R(address
));
476 if(returnStatus
== 1){return(5);}
477 return(returnStatus
);
479 for(uint8_t i
= 0; i
< numberBytes
; i
++)
483 returnStatus
= receiveByte(0);
484 if(returnStatus
== 1){
487 if(returnStatus
!= MR_DATA_NACK
){
488 return(returnStatus
);
493 returnStatus
= receiveByte(1);
494 if(returnStatus
== 1){
497 if(returnStatus
!= MR_DATA_ACK
){
498 return(returnStatus
);
502 bytesAvailable
= i
+1;
505 returnStatus
= stop();
508 if(returnStatus
== 1){
511 return(returnStatus
);
513 return(returnStatus
);
516 // Read a number of bytes in a specified buffer without sending first a register address
517 // return a status (0 = OK, others see above)
518 uint8_t I2C::read(uint8_t address
, uint8_t numberBytes
, uint8_t *dataBuffer
)
522 // if(numberBytes == 0){numberBytes++;}
523 // nack = numberBytes - 1;
525 returnStatus
= start();
526 if(returnStatus
){return(returnStatus
);}
527 returnStatus
= sendAddress(SLA_R(address
));
530 if(returnStatus
== 1){return(5);}
531 return(returnStatus
);
533 if(numberBytes
> 0) {
534 nack
= numberBytes
- 1;
535 for(uint8_t i
= 0; i
< numberBytes
; i
++)
539 returnStatus
= receiveByte(0);
540 if(returnStatus
== 1){
543 if(returnStatus
!= MR_DATA_NACK
){
544 return(returnStatus
);
549 returnStatus
= receiveByte(1);
550 if(returnStatus
== 1){
553 if(returnStatus
!= MR_DATA_ACK
){
554 return(returnStatus
);
557 dataBuffer
[i
] = TWDR
;
558 bytesAvailable
= i
+1;
562 returnStatus
= stop();
565 if(returnStatus
== 1){
568 return(returnStatus
);
570 return(returnStatus
);
574 // Read a number of bytes in a specified buffer after sending a register address
575 // this version is currently not used in openXsensor so it is in comment
576 uint8_t I2C::read(uint8_t address
, uint8_t registerAddress
, uint8_t numberBytes
, uint8_t *dataBuffer
)
580 if(numberBytes
== 0){numberBytes
++;}
581 nack
= numberBytes
- 1;
583 returnStatus
= start();
585 return(returnStatus
);
587 returnStatus
= sendAddress(SLA_W(address
));
590 if(returnStatus
== 1){
593 return(returnStatus
);
595 returnStatus
= sendByte(registerAddress
);
598 if(returnStatus
== 1){
601 return(returnStatus
);
603 returnStatus
= start();
606 if(returnStatus
== 1){
609 return(returnStatus
);
611 returnStatus
= sendAddress(SLA_R(address
));
614 if(returnStatus
== 1){
617 return(returnStatus
);
619 for(uint8_t i
= 0; i
< numberBytes
; i
++)
623 returnStatus
= receiveByte(0);
624 if(returnStatus
== 1){
627 if(returnStatus
!= MR_DATA_NACK
){
628 return(returnStatus
);
633 returnStatus
= receiveByte(1);
634 if(returnStatus
== 1){
637 if(returnStatus
!= MR_DATA_ACK
){
638 return(returnStatus
);
641 dataBuffer
[i
] = TWDR
;
642 bytesAvailable
= i
+1;
645 returnStatus
= stop();
648 if(returnStatus
== 1){
651 return(returnStatus
);
653 return(returnStatus
);
657 /////////////// Private Methods ////////////////////////////////////////
662 unsigned long startingTime
= millis();
663 TWCR
= (1<<TWINT
)|(1<<TWSTA
)|(1<<TWEN
);
664 while (!(TWCR
& (1<<TWINT
)))
666 if(!timeOutDelay
){continue;}
667 if((millis() - startingTime
) >= timeOutDelay
)
674 if ((TWI_STATUS
== START
) || (TWI_STATUS
== REPEATED_START
))
678 if (TWI_STATUS
== LOST_ARBTRTN
)
680 uint8_t bufferedStatus
= TWI_STATUS
;
682 return(bufferedStatus
);
687 uint8_t I2C::sendAddress(uint8_t i2cAddress
)
690 unsigned long startingTime
= millis();
691 TWCR
= (1<<TWINT
) | (1<<TWEN
);
692 while (!(TWCR
& (1<<TWINT
)))
694 if(!timeOutDelay
){continue;}
695 if((millis() - startingTime
) >= timeOutDelay
)
702 if ((TWI_STATUS
== MT_SLA_ACK
) || (TWI_STATUS
== MR_SLA_ACK
))
706 uint8_t bufferedStatus
= TWI_STATUS
;
707 if ((TWI_STATUS
== MT_SLA_NACK
) || (TWI_STATUS
== MR_SLA_NACK
))
710 return(bufferedStatus
);
715 return(bufferedStatus
);
719 uint8_t I2C::sendByte(uint8_t i2cData
)
722 unsigned long startingTime
= millis();
723 TWCR
= (1<<TWINT
) | (1<<TWEN
);
724 while (!(TWCR
& (1<<TWINT
)))
726 if(!timeOutDelay
){continue;}
727 if((millis() - startingTime
) >= timeOutDelay
)
734 if (TWI_STATUS
== MT_DATA_ACK
)
738 uint8_t bufferedStatus
= TWI_STATUS
;
739 if (TWI_STATUS
== MT_DATA_NACK
)
742 return(bufferedStatus
);
747 return(bufferedStatus
);
751 uint8_t I2C::receiveByte(uint8_t ack
)
753 unsigned long startingTime
= millis();
756 TWCR
= (1<<TWINT
) | (1<<TWEN
) | (1<<TWEA
);
761 TWCR
= (1<<TWINT
) | (1<<TWEN
);
763 while (!(TWCR
& (1<<TWINT
)))
765 if(!timeOutDelay
){continue;}
766 if((millis() - startingTime
) >= timeOutDelay
)
772 if (TWI_STATUS
== LOST_ARBTRTN
)
774 uint8_t bufferedStatus
= TWI_STATUS
;
776 return(bufferedStatus
);
783 unsigned long startingTime
= millis();
784 TWCR
= (1<<TWINT
)|(1<<TWEN
)| (1<<TWSTO
);
785 while ((TWCR
& (1<<TWSTO
)))
787 if(!timeOutDelay
){continue;}
788 if((millis() - startingTime
) >= timeOutDelay
)
800 TWCR
= 0; //releases SDA and SCL lines to high impedance
801 TWCR
= _BV(TWEN
) | _BV(TWEA
); //reinitialize TWI