Adafruit_I2CDevice.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <Adafruit_I2CDevice.h>
  2. #include <Arduino.h>
  3. //#define DEBUG_SERIAL Serial
  4. /*!
  5. * @brief Create an I2C device at a given address
  6. * @param addr The 7-bit I2C address for the device
  7. * @param theWire The I2C bus to use, defaults to &Wire
  8. */
  9. Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
  10. _addr = addr;
  11. _wire = theWire;
  12. _begun = false;
  13. #ifdef ARDUINO_ARCH_SAMD
  14. _maxBufferSize = 250; // as defined in Wire.h's RingBuffer
  15. #else
  16. _maxBufferSize = 32;
  17. #endif
  18. }
  19. /*!
  20. * @brief Initializes and does basic address detection
  21. * @param addr_detect Whether we should attempt to detect the I2C address
  22. * with a scan. 99% of sensors/devices don't mind but once in a while, they spaz
  23. * on a scan!
  24. * @return True if I2C initialized and a device with the addr found
  25. */
  26. bool Adafruit_I2CDevice::begin(bool addr_detect) {
  27. _wire->begin();
  28. _begun = true;
  29. if (addr_detect) {
  30. return detected();
  31. }
  32. return true;
  33. }
  34. /*!
  35. * @brief Scans I2C for the address - note will give a false-positive
  36. * if there's no pullups on I2C
  37. * @return True if I2C initialized and a device with the addr found
  38. */
  39. bool Adafruit_I2CDevice::detected(void) {
  40. // Init I2C if not done yet
  41. if (!_begun && !begin()) {
  42. return false;
  43. }
  44. // A basic scanner, see if it ACK's
  45. _wire->beginTransmission(_addr);
  46. if (_wire->endTransmission() == 0) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. /*!
  52. * @brief Write a buffer or two to the I2C device. Cannot be more than
  53. * maxBufferSize() bytes.
  54. * @param buffer Pointer to buffer of data to write
  55. * @param len Number of bytes from buffer to write
  56. * @param prefix_buffer Pointer to optional array of data to write before
  57. * buffer. Cannot be more than maxBufferSize() bytes.
  58. * @param prefix_len Number of bytes from prefix buffer to write
  59. * @param stop Whether to send an I2C STOP signal on write
  60. * @return True if write was successful, otherwise false.
  61. */
  62. bool Adafruit_I2CDevice::write(uint8_t *buffer, size_t len, bool stop,
  63. uint8_t *prefix_buffer, size_t prefix_len) {
  64. if ((len + prefix_len) > maxBufferSize()) {
  65. // currently not guaranteed to work if more than 32 bytes!
  66. // we will need to find out if some platforms have larger
  67. // I2C buffer sizes :/
  68. #ifdef DEBUG_SERIAL
  69. DEBUG_SERIAL.println(F("\tI2CDevice could not write such a large buffer"));
  70. #endif
  71. return false;
  72. }
  73. _wire->beginTransmission(_addr);
  74. // Write the prefix data (usually an address)
  75. if ((prefix_len != 0) && (prefix_buffer != NULL)) {
  76. if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
  77. #ifdef DEBUG_SERIAL
  78. DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
  79. #endif
  80. return false;
  81. }
  82. }
  83. // Write the data itself
  84. if (_wire->write(buffer, len) != len) {
  85. #ifdef DEBUG_SERIAL
  86. DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
  87. #endif
  88. return false;
  89. }
  90. #ifdef DEBUG_SERIAL
  91. DEBUG_SERIAL.print(F("\tI2CWRITE @ 0x"));
  92. DEBUG_SERIAL.print(_addr, HEX);
  93. DEBUG_SERIAL.print(F(" :: "));
  94. if ((prefix_len != 0) && (prefix_buffer != NULL)) {
  95. for (uint16_t i = 0; i < prefix_len; i++) {
  96. DEBUG_SERIAL.print(F("0x"));
  97. DEBUG_SERIAL.print(prefix_buffer[i], HEX);
  98. DEBUG_SERIAL.print(F(", "));
  99. }
  100. }
  101. for (uint16_t i = 0; i < len; i++) {
  102. DEBUG_SERIAL.print(F("0x"));
  103. DEBUG_SERIAL.print(buffer[i], HEX);
  104. DEBUG_SERIAL.print(F(", "));
  105. if (i % 32 == 31) {
  106. DEBUG_SERIAL.println();
  107. }
  108. }
  109. DEBUG_SERIAL.println();
  110. #endif
  111. #ifdef DEBUG_SERIAL
  112. // DEBUG_SERIAL.print("Stop: "); DEBUG_SERIAL.println(stop);
  113. #endif
  114. if (_wire->endTransmission(stop) == 0) {
  115. #ifdef DEBUG_SERIAL
  116. // DEBUG_SERIAL.println("Sent!");
  117. #endif
  118. return true;
  119. } else {
  120. #ifdef DEBUG_SERIAL
  121. DEBUG_SERIAL.println("Failed to send!");
  122. #endif
  123. return false;
  124. }
  125. }
  126. /*!
  127. * @brief Read from I2C into a buffer from the I2C device.
  128. * Cannot be more than maxBufferSize() bytes.
  129. * @param buffer Pointer to buffer of data to read into
  130. * @param len Number of bytes from buffer to read.
  131. * @param stop Whether to send an I2C STOP signal on read
  132. * @return True if read was successful, otherwise false.
  133. */
  134. bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
  135. if (len > maxBufferSize()) {
  136. // currently not guaranteed to work if more than 32 bytes!
  137. // we will need to find out if some platforms have larger
  138. // I2C buffer sizes :/
  139. #ifdef DEBUG_SERIAL
  140. DEBUG_SERIAL.println(F("\tI2CDevice could not read such a large buffer"));
  141. #endif
  142. return false;
  143. }
  144. size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);
  145. if (recv != len) {
  146. // Not enough data available to fulfill our obligation!
  147. #ifdef DEBUG_SERIAL
  148. DEBUG_SERIAL.print(F("\tI2CDevice did not receive enough data: "));
  149. DEBUG_SERIAL.println(recv);
  150. #endif
  151. return false;
  152. }
  153. for (uint16_t i = 0; i < len; i++) {
  154. buffer[i] = _wire->read();
  155. }
  156. #ifdef DEBUG_SERIAL
  157. DEBUG_SERIAL.print(F("\tI2CREAD @ 0x"));
  158. DEBUG_SERIAL.print(_addr, HEX);
  159. DEBUG_SERIAL.print(F(" :: "));
  160. for (uint16_t i = 0; i < len; i++) {
  161. DEBUG_SERIAL.print(F("0x"));
  162. DEBUG_SERIAL.print(buffer[i], HEX);
  163. DEBUG_SERIAL.print(F(", "));
  164. if (len % 32 == 31) {
  165. DEBUG_SERIAL.println();
  166. }
  167. }
  168. DEBUG_SERIAL.println();
  169. #endif
  170. return true;
  171. }
  172. /*!
  173. * @brief Write some data, then read some data from I2C into another buffer.
  174. * Cannot be more than maxBufferSize() bytes. The buffers can point to
  175. * same/overlapping locations.
  176. * @param write_buffer Pointer to buffer of data to write from
  177. * @param write_len Number of bytes from buffer to write.
  178. * @param read_buffer Pointer to buffer of data to read into.
  179. * @param read_len Number of bytes from buffer to read.
  180. * @param stop Whether to send an I2C STOP signal between the write and read
  181. * @return True if write & read was successful, otherwise false.
  182. */
  183. bool Adafruit_I2CDevice::write_then_read(uint8_t *write_buffer,
  184. size_t write_len, uint8_t *read_buffer,
  185. size_t read_len, bool stop) {
  186. if (!write(write_buffer, write_len, stop)) {
  187. return false;
  188. }
  189. return read(read_buffer, read_len);
  190. }
  191. /*!
  192. * @brief Returns the 7-bit address of this device
  193. * @return The 7-bit address of this device
  194. */
  195. uint8_t Adafruit_I2CDevice::address(void) { return _addr; }
  196. /*!
  197. * @brief Change the I2C clock speed to desired (relies on
  198. * underlying Wire support!
  199. * @param desiredclk The desired I2C SCL frequency
  200. * @return True if this platform supports changing I2C speed.
  201. * Not necessarily that the speed was achieved!
  202. */
  203. bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) {
  204. #if (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER)
  205. _wire->setClock(desiredclk);
  206. return true;
  207. #else
  208. return false;
  209. #endif
  210. }