I2Cdev.h 12 KB

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