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