Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

305 рядки
12KB

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