Update oXs_out_frsky.cpp
[openXsensor.git] / openXsensor / I2C.h
blob8d56a7889ef0b41d6ded3ce34038068c90c6fad5
1 /*
2 I2C.h - I2C library
3 Copyright (c) 2011-2012 Wayne Truchsess. All right reserved.
4 Rev 5.0 - January 24th, 2012
5 - Removed the use of interrupts completely from the library
6 so TWI state changes are now polled.
7 - Added calls to lockup() function in most functions
8 to combat arbitration problems
9 - Fixed scan() procedure which left timeouts enabled
10 and set to 80msec after exiting procedure
11 - Changed scan() address range back to 0 - 0x7F
12 - Removed all Wire legacy functions from library
13 - A big thanks to Richard Baldwin for all the testing
14 and feedback with debugging bus lockups!
15 Rev 4.0 - January 14th, 2012
16 - Updated to make compatible with 8MHz clock frequency
17 Rev 3.0 - January 9th, 2012
18 - Modified library to be compatible with Arduino 1.0
19 - Changed argument type from boolean to uint8_t in pullUp(),
20 setSpeed() and receiveByte() functions for 1.0 compatability
21 - Modified return values for timeout feature to report
22 back where in the transmission the timeout occured.
23 - added function scan() to perform a bus scan to find devices
24 attached to the I2C bus. Similar to work done by Todbot
25 and Nick Gammon
26 Rev 2.0 - September 19th, 2011
27 - Added support for timeout function to prevent
28 and recover from bus lockup (thanks to PaulS
29 and CrossRoads on the Arduino forum)
30 - Changed return type for stop() from void to
31 uint8_t to handle timeOut function
32 Rev 1.0 - August 8th, 2011
34 This is a modified version of the Arduino Wire/TWI
35 library. Functions were rewritten to provide more functionality
36 and also the use of Repeated Start. Some I2C devices will not
37 function correctly without the use of a Repeated Start. The
38 initial version of this library only supports the Master.
41 This library is free software; you can redistribute it and/or
42 modify it under the terms of the GNU Lesser General Public
43 License as published by the Free Software Foundation; either
44 version 2.1 of the License, or (at your option) any later version.
46 This library is distributed in the hope that it will be useful,
47 but WITHOUT ANY WARRANTY; without even the implied warranty of
48 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 Lesser General Public License for more details.
51 You should have received a copy of the GNU Lesser General Public
52 License along with this library; if not, write to the Free Software
53 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
56 #if(ARDUINO >= 100)
57 #include <Arduino.h>
58 #else
59 #include <WProgram.h>
60 #endif
62 #include <inttypes.h>
64 #ifndef I2C_h
65 #define I2C_h
68 #define START 0x08
69 #define REPEATED_START 0x10
70 #define MT_SLA_ACK 0x18
71 #define MT_SLA_NACK 0x20
72 #define MT_DATA_ACK 0x28
73 #define MT_DATA_NACK 0x30
74 #define MR_SLA_ACK 0x40
75 #define MR_SLA_NACK 0x48
76 #define MR_DATA_ACK 0x50
77 #define MR_DATA_NACK 0x58
78 #define LOST_ARBTRTN 0x38
79 #define TWI_STATUS (TWSR & 0xF8)
80 #define SLA_W(address) (address << 1)
81 #define SLA_R(address) ((address << 1) + 0x01)
82 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
83 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
85 #define MAX_BUFFER_SIZE 32
87 class I2C {
88 public:
89 I2C();
90 void begin();
91 void end();
92 void timeOut(uint16_t);
93 void setSpeed(uint8_t);
94 void pullup(uint8_t);
95 void scan();
96 uint8_t available();
97 uint8_t receive();
98 uint8_t write(uint8_t, uint8_t);
99 // uint8_t write(int, int);
100 uint8_t write(uint8_t, uint8_t, uint8_t);
101 // uint8_t write(int, int, int);
102 uint8_t write(uint8_t, uint8_t, char*);
103 uint8_t write(uint8_t, uint8_t, uint8_t , uint8_t* );
104 uint8_t read(uint8_t, uint8_t);
105 // uint8_t read(int, int);
106 uint8_t read(uint8_t, uint8_t, uint8_t);
107 // uint8_t read(int, int, int);
108 uint8_t read(uint8_t, uint8_t, uint8_t*);
109 uint8_t read(uint8_t, uint8_t, uint8_t, uint8_t* );
111 uint8_t scanAdr ;
113 private:
114 uint8_t start();
115 uint8_t sendAddress(uint8_t);
116 uint8_t sendByte(uint8_t);
117 uint8_t receiveByte(uint8_t);
118 uint8_t stop();
119 void lockUp();
120 uint8_t returnStatus;
121 uint8_t nack;
122 uint8_t data[MAX_BUFFER_SIZE];
123 static uint8_t bytesAvailable;
124 static uint8_t bufferIndex;
125 static uint8_t totalBytes;
126 static uint16_t timeOutDelay;
130 extern I2C I2c;
132 #endif