I2Cdev.h 12 KB

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