00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _I2CDEV_H_
00043 #define _I2CDEV_H_
00044
00045
00046
00047
00048 #define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
00049
00050
00051
00052
00053 #define I2CDEV_ARDUINO_WIRE 1 // Wire object from Arduino
00054 #define I2CDEV_BUILTIN_NBWIRE 2 // Tweaked Wire object from Gene Knight's NBWire project
00055
00056 #define I2CDEV_BUILTIN_FASTWIRE 3 // FastWire object from Francesco Ferrara's project
00057
00058
00059
00060
00061
00062
00063 #ifdef ARDUINO
00064 #if ARDUINO < 100
00065 #include "WProgram.h"
00066 #else
00067 #include "Arduino.h"
00068 #endif
00069 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
00070 #include <Wire.h>
00071 #endif
00072 #else
00073 #include "ArduinoWrapper.h"
00074 #endif
00075
00076
00077 #define I2CDEV_DEFAULT_READ_TIMEOUT 1000
00078
00079 class I2Cdev {
00080 public:
00081 I2Cdev();
00082
00083 static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
00084 static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
00085 static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
00086 static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
00087 static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
00088 static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
00089 static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
00090 static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
00091
00092 static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data);
00093 static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data);
00094 static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data);
00095 static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data);
00096 static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data);
00097 static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data);
00098 static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data);
00099 static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data);
00100
00101 static uint16_t readTimeout;
00102 };
00103
00104 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
00105
00107
00108
00110
00111
00112 #define TW_START 0x08
00113 #define TW_REP_START 0x10
00114
00115
00116 #define TW_MT_SLA_ACK 0x18
00117 #define TW_MT_SLA_NACK 0x20
00118 #define TW_MT_DATA_ACK 0x28
00119 #define TW_MT_DATA_NACK 0x30
00120 #define TW_MT_ARB_LOST 0x38
00121
00122
00123 #define TW_MR_ARB_LOST 0x38
00124 #define TW_MR_SLA_ACK 0x40
00125 #define TW_MR_SLA_NACK 0x48
00126 #define TW_MR_DATA_ACK 0x50
00127 #define TW_MR_DATA_NACK 0x58
00128
00129 #define TW_OK 0
00130 #define TW_ERROR 1
00131
00132 class Fastwire {
00133 private:
00134 static boolean waitInt();
00135
00136 public:
00137 static void setup(int khz, boolean pullup);
00138 static byte write(byte device, byte address, byte value);
00139 static byte readBuf(byte device, byte address, byte *data, byte num);
00140 };
00141 #endif
00142
00143 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE
00144
00145
00146
00147
00148 #define NBWIRE_BUFFER_LENGTH 32
00149
00150 class TwoWire {
00151 private:
00152 static uint8_t rxBuffer[];
00153 static uint8_t rxBufferIndex;
00154 static uint8_t rxBufferLength;
00155
00156 static uint8_t txAddress;
00157 static uint8_t txBuffer[];
00158 static uint8_t txBufferIndex;
00159 static uint8_t txBufferLength;
00160
00161
00162 static void (*user_onRequest)(void);
00163 static void (*user_onReceive)(int);
00164 static void onRequestService(void);
00165 static void onReceiveService(uint8_t*, int);
00166
00167 public:
00168 TwoWire();
00169 void begin();
00170 void begin(uint8_t);
00171 void begin(int);
00172 void beginTransmission(uint8_t);
00173
00174 uint8_t endTransmission(uint16_t timeout=0);
00175 void nbendTransmission(void (*function)(int)) ;
00176 uint8_t requestFrom(uint8_t, int, uint16_t timeout=0);
00177
00178 void nbrequestFrom(uint8_t, int, void (*function)(int));
00179 void send(uint8_t);
00180 void send(uint8_t*, uint8_t);
00181
00182 void send(char*);
00183 uint8_t available(void);
00184 uint8_t receive(void);
00185 void onReceive(void (*)(int));
00186 void onRequest(void (*)(void));
00187 };
00188
00189 #define TWI_READY 0
00190 #define TWI_MRX 1
00191 #define TWI_MTX 2
00192 #define TWI_SRX 3
00193 #define TWI_STX 4
00194
00195 #define TW_WRITE 0
00196 #define TW_READ 1
00197
00198 #define TW_MT_SLA_NACK 0x20
00199 #define TW_MT_DATA_NACK 0x30
00200
00201 #define CPU_FREQ 16000000L
00202 #define TWI_FREQ 100000L
00203 #define TWI_BUFFER_LENGTH 32
00204
00205
00206
00207 #define TW_STATUS_MASK (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3))
00208 #define TW_STATUS (TWSR & TW_STATUS_MASK)
00209 #define TW_START 0x08
00210 #define TW_REP_START 0x10
00211 #define TW_MT_SLA_ACK 0x18
00212 #define TW_MT_SLA_NACK 0x20
00213 #define TW_MT_DATA_ACK 0x28
00214 #define TW_MT_DATA_NACK 0x30
00215 #define TW_MT_ARB_LOST 0x38
00216 #define TW_MR_ARB_LOST 0x38
00217 #define TW_MR_SLA_ACK 0x40
00218 #define TW_MR_SLA_NACK 0x48
00219 #define TW_MR_DATA_ACK 0x50
00220 #define TW_MR_DATA_NACK 0x58
00221 #define TW_ST_SLA_ACK 0xA8
00222 #define TW_ST_ARB_LOST_SLA_ACK 0xB0
00223 #define TW_ST_DATA_ACK 0xB8
00224 #define TW_ST_DATA_NACK 0xC0
00225 #define TW_ST_LAST_DATA 0xC8
00226 #define TW_SR_SLA_ACK 0x60
00227 #define TW_SR_ARB_LOST_SLA_ACK 0x68
00228 #define TW_SR_GCALL_ACK 0x70
00229 #define TW_SR_ARB_LOST_GCALL_ACK 0x78
00230 #define TW_SR_DATA_ACK 0x80
00231 #define TW_SR_DATA_NACK 0x88
00232 #define TW_SR_GCALL_DATA_ACK 0x90
00233 #define TW_SR_GCALL_DATA_NACK 0x98
00234 #define TW_SR_STOP 0xA0
00235 #define TW_NO_INFO 0xF8
00236 #define TW_BUS_ERROR 0x00
00237
00238
00239
00240
00241 #ifndef sbi // set bit
00242 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
00243 #endif // sbi
00244
00245 #ifndef cbi // clear bit
00246 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
00247 #endif // cbi
00248
00249 extern TwoWire Wire;
00250
00251 #endif // I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE
00252
00253 #endif