]> git.piffa.net Git - arduino/blob - sheets/gyro/Test program/Arduino/MPU6050/I2Cdev.h
first commit
[arduino] / sheets / gyro / Test program / Arduino / MPU6050 / I2Cdev.h
1 // I2Cdev library collection - Main I2C device class header file\r
2 // Abstracts bit and byte I2C R/W functions into a convenient class\r
3 // 11/1/2011 by Jeff Rowberg <jeff@rowberg.net>\r
4 //\r
5 // Changelog:\r
6 //     2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)\r
7 //     2011-10-03 - added automatic Arduino version detection for ease of use\r
8 //     2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications\r
9 //     2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)\r
10 //     2011-08-03 - added optional timeout parameter to read* methods to easily change from default\r
11 //     2011-08-02 - added support for 16-bit registers\r
12 //                - fixed incorrect Doxygen comments on some methods\r
13 //                - added timeout value for read operations (thanks mem @ Arduino forums)\r
14 //     2011-07-30 - changed read/write function structures to return success or byte counts\r
15 //                - made all methods static for multi-device memory savings\r
16 //     2011-07-28 - initial release\r
17 \r
18 /* ============================================\r
19 I2Cdev device library code is placed under the MIT license\r
20 Copyright (c) 2011 Jeff Rowberg\r
21 \r
22 Permission is hereby granted, free of charge, to any person obtaining a copy\r
23 of this software and associated documentation files (the "Software"), to deal\r
24 in the Software without restriction, including without limitation the rights\r
25 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
26 copies of the Software, and to permit persons to whom the Software is\r
27 furnished to do so, subject to the following conditions:\r
28 \r
29 The above copyright notice and this permission notice shall be included in\r
30 all copies or substantial portions of the Software.\r
31 \r
32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
33 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
34 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
35 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
36 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
37 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
38 THE SOFTWARE.\r
39 ===============================================\r
40 */\r
41 \r
42 #ifndef _I2CDEV_H_\r
43 #define _I2CDEV_H_\r
44 \r
45 // -----------------------------------------------------------------------------\r
46 // I2C interface implementation setting\r
47 // -----------------------------------------------------------------------------\r
48 #define I2CDEV_IMPLEMENTATION       I2CDEV_ARDUINO_WIRE\r
49 \r
50 // -----------------------------------------------------------------------------\r
51 // I2C interface implementation options\r
52 // -----------------------------------------------------------------------------\r
53 #define I2CDEV_ARDUINO_WIRE         1 // Wire object from Arduino\r
54 #define I2CDEV_BUILTIN_NBWIRE       2 // Tweaked Wire object from Gene Knight's NBWire project\r
55                                       // ^^^ NBWire implementation is still buggy w/some interrupts!\r
56 #define I2CDEV_BUILTIN_FASTWIRE     3 // FastWire object from Francesco Ferrara's project\r
57 \r
58 // -----------------------------------------------------------------------------\r
59 // Arduino-style "Serial.print" debug constant (uncomment to enable)\r
60 // -----------------------------------------------------------------------------\r
61 //#define I2CDEV_SERIAL_DEBUG\r
62 \r
63 #ifdef ARDUINO\r
64     #if ARDUINO < 100\r
65         #include "WProgram.h"\r
66     #else\r
67         #include "Arduino.h"\r
68     #endif\r
69     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE\r
70         #include <Wire.h>\r
71     #endif\r
72 #else\r
73     #include "ArduinoWrapper.h"\r
74 #endif\r
75 \r
76 // 1000ms default read timeout (modify with "I2Cdev::readTimeout = [ms];")\r
77 #define I2CDEV_DEFAULT_READ_TIMEOUT     1000\r
78 \r
79 class I2Cdev {\r
80     public:\r
81         I2Cdev();\r
82         \r
83         static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
84         static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
85         static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
86         static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
87         static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
88         static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
89         static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
90         static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);\r
91 \r
92         static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data);\r
93         static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data);\r
94         static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data);\r
95         static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data);\r
96         static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data);\r
97         static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data);\r
98         static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data);\r
99         static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data);\r
100 \r
101         static uint16_t readTimeout;\r
102 };\r
103 \r
104 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE\r
105     // I2C library\r
106     //////////////////////\r
107     // Copyright(C) 2011\r
108     // Francesco Ferrara\r
109     //////////////////////\r
110     \r
111     /* Master */\r
112     #define TW_START                0x08\r
113     #define TW_REP_START            0x10\r
114 \r
115     /* Master Transmitter */\r
116     #define TW_MT_SLA_ACK           0x18\r
117     #define TW_MT_SLA_NACK          0x20\r
118     #define TW_MT_DATA_ACK          0x28\r
119     #define TW_MT_DATA_NACK         0x30\r
120     #define TW_MT_ARB_LOST          0x38\r
121 \r
122     /* Master Receiver */\r
123     #define TW_MR_ARB_LOST          0x38\r
124     #define TW_MR_SLA_ACK           0x40\r
125     #define TW_MR_SLA_NACK          0x48\r
126     #define TW_MR_DATA_ACK          0x50\r
127     #define TW_MR_DATA_NACK         0x58\r
128 \r
129     #define TW_OK                   0\r
130     #define TW_ERROR                1\r
131 \r
132     class Fastwire {\r
133         private:\r
134           static boolean waitInt();\r
135 \r
136         public:\r
137           static void setup(int khz, boolean pullup);\r
138           static byte write(byte device, byte address, byte value);\r
139           static byte readBuf(byte device, byte address, byte *data, byte num);\r
140     };\r
141 #endif\r
142 \r
143 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE\r
144     // NBWire implementation based heavily on code by Gene Knight <Gene@Telobot.com>\r
145     // Originally posted on the Arduino forum at http://arduino.cc/forum/index.php/topic,70705.0.html\r
146     // Originally offered to the i2cdevlib project at http://arduino.cc/forum/index.php/topic,68210.30.html\r
147 \r
148     #define NBWIRE_BUFFER_LENGTH 32\r
149     \r
150     class TwoWire {\r
151         private:\r
152             static uint8_t rxBuffer[];\r
153             static uint8_t rxBufferIndex;\r
154             static uint8_t rxBufferLength;\r
155         \r
156             static uint8_t txAddress;\r
157             static uint8_t txBuffer[];\r
158             static uint8_t txBufferIndex;\r
159             static uint8_t txBufferLength;\r
160         \r
161             // static uint8_t transmitting;\r
162             static void (*user_onRequest)(void);\r
163             static void (*user_onReceive)(int);\r
164             static void onRequestService(void);\r
165             static void onReceiveService(uint8_t*, int);\r
166     \r
167         public:\r
168             TwoWire();\r
169             void begin();\r
170             void begin(uint8_t);\r
171             void begin(int);\r
172             void beginTransmission(uint8_t);\r
173             //void beginTransmission(int);\r
174             uint8_t endTransmission(uint16_t timeout=0);\r
175             void nbendTransmission(void (*function)(int)) ;\r
176             uint8_t requestFrom(uint8_t, int, uint16_t timeout=0);\r
177             //uint8_t requestFrom(int, int);\r
178             void nbrequestFrom(uint8_t, int, void (*function)(int));\r
179             void send(uint8_t);\r
180             void send(uint8_t*, uint8_t);\r
181             //void send(int);\r
182             void send(char*);\r
183             uint8_t available(void);\r
184             uint8_t receive(void);\r
185             void onReceive(void (*)(int));\r
186             void onRequest(void (*)(void));\r
187     };\r
188     \r
189     #define TWI_READY   0\r
190     #define TWI_MRX     1\r
191     #define TWI_MTX     2\r
192     #define TWI_SRX     3\r
193     #define TWI_STX     4\r
194     \r
195     #define TW_WRITE    0\r
196     #define TW_READ     1\r
197     \r
198     #define TW_MT_SLA_NACK      0x20\r
199     #define TW_MT_DATA_NACK     0x30\r
200     \r
201     #define CPU_FREQ            16000000L\r
202     #define TWI_FREQ            100000L\r
203     #define TWI_BUFFER_LENGTH   32\r
204     \r
205     /* TWI Status is in TWSR, in the top 5 bits: TWS7 - TWS3 */\r
206     \r
207     #define TW_STATUS_MASK              (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3))\r
208     #define TW_STATUS                   (TWSR & TW_STATUS_MASK)\r
209     #define TW_START                    0x08\r
210     #define TW_REP_START                0x10\r
211     #define TW_MT_SLA_ACK               0x18\r
212     #define TW_MT_SLA_NACK              0x20\r
213     #define TW_MT_DATA_ACK              0x28\r
214     #define TW_MT_DATA_NACK             0x30\r
215     #define TW_MT_ARB_LOST              0x38\r
216     #define TW_MR_ARB_LOST              0x38\r
217     #define TW_MR_SLA_ACK               0x40\r
218     #define TW_MR_SLA_NACK              0x48\r
219     #define TW_MR_DATA_ACK              0x50\r
220     #define TW_MR_DATA_NACK             0x58\r
221     #define TW_ST_SLA_ACK               0xA8\r
222     #define TW_ST_ARB_LOST_SLA_ACK      0xB0\r
223     #define TW_ST_DATA_ACK              0xB8\r
224     #define TW_ST_DATA_NACK             0xC0\r
225     #define TW_ST_LAST_DATA             0xC8\r
226     #define TW_SR_SLA_ACK               0x60\r
227     #define TW_SR_ARB_LOST_SLA_ACK      0x68\r
228     #define TW_SR_GCALL_ACK             0x70\r
229     #define TW_SR_ARB_LOST_GCALL_ACK    0x78\r
230     #define TW_SR_DATA_ACK              0x80\r
231     #define TW_SR_DATA_NACK             0x88\r
232     #define TW_SR_GCALL_DATA_ACK        0x90\r
233     #define TW_SR_GCALL_DATA_NACK       0x98\r
234     #define TW_SR_STOP                  0xA0\r
235     #define TW_NO_INFO                  0xF8\r
236     #define TW_BUS_ERROR                0x00\r
237     \r
238     //#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))\r
239     //#define _SFR_BYTE(sfr) _MMIO_BYTE(_SFR_ADDR(sfr))\r
240     \r
241     #ifndef sbi // set bit\r
242         #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))\r
243     #endif // sbi\r
244     \r
245     #ifndef cbi // clear bit\r
246         #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))\r
247     #endif // cbi\r
248     \r
249     extern TwoWire Wire;\r
250 \r
251 #endif // I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE\r
252 \r
253 #endif /* _I2CDEV_H_ */