Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.7 KiB

  1. /*
  2. I2C MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if I2C_SUPPORT
  6. unsigned int _i2c_locked[16] = {0};
  7. #if I2C_USE_BRZO
  8. #include "brzo_i2c.h"
  9. unsigned long _i2c_scl_frequency = 0;
  10. #else
  11. #include "Wire.h"
  12. #endif
  13. // -----------------------------------------------------------------------------
  14. // Private
  15. // -----------------------------------------------------------------------------
  16. int _i2cClearbus(int sda, int scl) {
  17. #if defined(TWCR) && defined(TWEN)
  18. // Disable the Atmel 2-Wire interface so we can control the SDA and SCL pins directly
  19. TWCR &= ~(_BV(TWEN));
  20. #endif
  21. // Make SDA (data) and SCL (clock) pins inputs with pullup
  22. pinMode(sda, INPUT_PULLUP);
  23. pinMode(scl, INPUT_PULLUP);
  24. delay(2500);
  25. // Wait 2.5 secs. This is strictly only necessary on the first power
  26. // up of the DS3231 module to allow it to initialize properly,
  27. // but is also assists in reliable programming of FioV3 boards as it gives the
  28. // IDE a chance to start uploaded the program
  29. // before existing sketch confuses the IDE by sending Serial data.
  30. // If it is held low the device cannot become the I2C master
  31. // I2C bus error. Could not clear SCL clock line held low
  32. boolean scl_low = (digitalRead(scl) == LOW);
  33. if (scl_low) return 1;
  34. boolean sda_low = (digitalRead(sda) == LOW);
  35. int clockCount = 20; // > 2x9 clock
  36. // While SDA is low for at most 20 cycles
  37. while (sda_low && (clockCount > 0)) {
  38. clockCount--;
  39. // Note: I2C bus is open collector so do NOT drive SCL or SDA high
  40. pinMode(scl, INPUT); // release SCL pullup so that when made output it will be LOW
  41. pinMode(scl, OUTPUT); // then clock SCL Low
  42. delayMicroseconds(10); // for >5uS
  43. pinMode(scl, INPUT); // release SCL LOW
  44. pinMode(scl, INPUT_PULLUP); // turn on pullup resistors again
  45. // do not force high as slave may be holding it low for clock stretching
  46. delayMicroseconds(10); // The >5uS is so that even the slowest I2C devices are handled
  47. // loop waiting for SCL to become high only wait 2sec
  48. scl_low = (digitalRead(scl) == LOW);
  49. int counter = 20;
  50. while (scl_low && (counter > 0)) {
  51. counter--;
  52. delay(100);
  53. scl_low = (digitalRead(scl) == LOW);
  54. }
  55. // If still low after 2 sec error
  56. // I2C bus error. Could not clear. SCL clock line held low by slave clock stretch for >2sec
  57. if (scl_low) return 2;
  58. sda_low = (digitalRead(sda) == LOW); // and check SDA input again and loop
  59. }
  60. // If still low
  61. // I2C bus error. Could not clear. SDA data line held low
  62. if (sda_low) return 3;
  63. // Pull SDA line low for "start" or "repeated start"
  64. pinMode(sda, INPUT); // remove pullup
  65. pinMode(sda, OUTPUT); // and then make it LOW i.e. send an I2C Start or Repeated start control
  66. // When there is only one I2C master a "start" or "repeat start" has the same function as a "stop" and clears the bus
  67. // A Repeat Start is a Start occurring after a Start with no intervening Stop.
  68. delayMicroseconds(10); // wait >5uS
  69. pinMode(sda, INPUT); // remove output low
  70. pinMode(sda, INPUT_PULLUP); // and make SDA high i.e. send I2C STOP control.
  71. delayMicroseconds(10); // wait >5uS
  72. pinMode(sda, INPUT); // and reset pins as tri-state inputs which is the default state on reset
  73. pinMode(scl, INPUT);
  74. // Everything OK
  75. return 0;
  76. }
  77. // ---------------------------------------------------------------------
  78. // I2C API
  79. // ---------------------------------------------------------------------
  80. void i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value) {
  81. Wire.beginTransmission((uint8_t) address);
  82. Wire.write((uint8_t) reg);
  83. Wire.write((uint8_t) value);
  84. Wire.endTransmission();
  85. }
  86. uint8_t i2c_read_uint8(uint8_t address, uint8_t reg) {
  87. uint8_t value;
  88. Wire.beginTransmission((uint8_t) address);
  89. Wire.write((uint8_t) reg);
  90. Wire.endTransmission();
  91. Wire.requestFrom((uint8_t)address, (uint8_t) 1);
  92. value = Wire.read();
  93. Wire.endTransmission();
  94. return value;
  95. };
  96. uint16_t i2c_read_uint16(uint8_t address, uint8_t reg) {
  97. uint16_t value;
  98. Wire.beginTransmission((uint8_t) address);
  99. Wire.write((uint8_t) reg);
  100. Wire.endTransmission();
  101. Wire.requestFrom((uint8_t) address, (uint8_t) 2);
  102. value = (Wire.read() << 8) | Wire.read();
  103. Wire.endTransmission();
  104. return value;
  105. };
  106. uint16_t i2c_read_uint16_le(uint8_t address, uint8_t reg) {
  107. uint16_t temp = i2c_read_uint16(address, reg);
  108. return (temp >> 8) | (temp << 8);
  109. };
  110. int16_t i2c_read_int16(uint8_t address, uint8_t reg) {
  111. return (int16_t) i2c_read_uint16(address, reg);
  112. };
  113. int16_t i2c_read_int16_le(uint8_t address, uint8_t reg) {
  114. return (int16_t) i2c_read_uint16_le(address, reg);
  115. };
  116. // -----------------------------------------------------------------------------
  117. // Utils
  118. // -----------------------------------------------------------------------------
  119. void i2cClearBus() {
  120. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  121. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  122. DEBUG_MSG_P(PSTR("[I2C] Clear bus (response: %d)\n"), _i2cClearbus(sda, scl));
  123. }
  124. bool i2cCheck(unsigned char address) {
  125. #if I2C_USE_BRZO
  126. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  127. brzo_i2c_ACK_polling(1000);
  128. return brzo_i2c_end_transaction();
  129. #else
  130. Wire.beginTransmission(address);
  131. return Wire.endTransmission();
  132. #endif
  133. }
  134. bool i2cGetLock(unsigned char address) {
  135. unsigned char index = address / 8;
  136. unsigned char mask = 1 << (address % 8);
  137. if (_i2c_locked[index] & mask) return false;
  138. _i2c_locked[index] = _i2c_locked[index] | mask;
  139. DEBUG_MSG_P(PSTR("[I2C] Address 0x%02X locked\n"), address);
  140. return true;
  141. }
  142. bool i2cReleaseLock(unsigned char address) {
  143. unsigned char index = address / 8;
  144. unsigned char mask = 1 << (address % 8);
  145. if (_i2c_locked[index] & mask) {
  146. _i2c_locked[index] = _i2c_locked[index] & ~mask;
  147. return true;
  148. }
  149. return false;
  150. }
  151. unsigned char i2cFind(size_t size, unsigned char * addresses, unsigned char &start) {
  152. for (unsigned char i=start; i<size; i++) {
  153. if (i2cCheck(addresses[i]) == 0) {
  154. start = i;
  155. return addresses[i];
  156. }
  157. }
  158. return 0;
  159. }
  160. unsigned char i2cFind(size_t size, unsigned char * addresses) {
  161. unsigned char start = 0;
  162. return i2cFind(size, addresses, start);
  163. }
  164. unsigned char i2cFindAndLock(size_t size, unsigned char * addresses) {
  165. unsigned char start = 0;
  166. unsigned char address = 0;
  167. while (address = i2cFind(size, addresses, start)) {
  168. if (i2cGetLock(address)) break;
  169. start++;
  170. }
  171. return address;
  172. }
  173. void i2cScan() {
  174. unsigned char nDevices = 0;
  175. for (unsigned char address = 1; address < 127; address++) {
  176. unsigned char error = i2cCheck(address);
  177. if (error == 0) {
  178. DEBUG_MSG_P(PSTR("[I2C] Device found at address 0x%02X\n"), address);
  179. nDevices++;
  180. }
  181. }
  182. if (nDevices == 0) DEBUG_MSG_P(PSTR("[I2C] No devices found\n"));
  183. }
  184. void i2cSetup() {
  185. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  186. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  187. #if I2C_USE_BRZO
  188. unsigned long cst = getSetting("i2cCST", I2C_CLOCK_STRETCH_TIME).toInt();
  189. _i2c_scl_frequency = getSetting("i2cFreq", I2C_SCL_FREQUENCY).toInt();
  190. brzo_i2c_setup(sda, scl, cst);
  191. #else
  192. Wire.begin(sda, scl);
  193. #endif
  194. DEBUG_MSG_P(PSTR("[I2C] Using GPIO%u for SDA and GPIO%u for SCL\n"), sda, scl);
  195. }
  196. #endif