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.

209 lines
6.5 KiB

6 years ago
  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. // Utils
  79. // -----------------------------------------------------------------------------
  80. void i2cClearBus() {
  81. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  82. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  83. DEBUG_MSG_P(PSTR("[I2C] Clear bus (response: %d)\n"), _i2cClearbus(sda, scl));
  84. }
  85. bool i2cCheck(unsigned char address) {
  86. #if I2C_USE_BRZO
  87. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  88. brzo_i2c_ACK_polling(1000);
  89. return brzo_i2c_end_transaction();
  90. #else
  91. Wire.beginTransmission(address);
  92. return Wire.endTransmission();
  93. #endif
  94. }
  95. bool i2cGetLock(unsigned char address) {
  96. unsigned char index = address / 8;
  97. unsigned char mask = 1 << (address % 8);
  98. if (_i2c_locked[index] & mask) return false;
  99. _i2c_locked[index] = _i2c_locked[index] | mask;
  100. DEBUG_MSG_P(PSTR("[I2C] Address 0x%02X locked\n"), address);
  101. return true;
  102. }
  103. bool i2cReleaseLock(unsigned char address) {
  104. unsigned char index = address / 8;
  105. unsigned char mask = 1 << (address % 8);
  106. if (_i2c_locked[index] & mask) {
  107. _i2c_locked[index] = _i2c_locked[index] & ~mask;
  108. return true;
  109. }
  110. return false;
  111. }
  112. unsigned char i2cFind(size_t size, unsigned char * addresses, unsigned char &start) {
  113. for (unsigned char i=start; i<size; i++) {
  114. if (i2cCheck(addresses[i]) == 0) {
  115. start = i;
  116. return addresses[i];
  117. }
  118. }
  119. return 0;
  120. }
  121. unsigned char i2cFind(size_t size, unsigned char * addresses) {
  122. unsigned char start = 0;
  123. return i2cFind(size, addresses, start);
  124. }
  125. unsigned char i2cFindAndLock(size_t size, unsigned char * addresses) {
  126. unsigned char start = 0;
  127. unsigned char address = 0;
  128. while (address = i2cFind(size, addresses, start)) {
  129. if (i2cGetLock(address)) break;
  130. start++;
  131. }
  132. return address;
  133. }
  134. void i2cScan() {
  135. unsigned char nDevices = 0;
  136. for (unsigned char address = 1; address < 127; address++) {
  137. unsigned char error = i2cCheck(address);
  138. if (error == 0) {
  139. DEBUG_MSG_P(PSTR("[I2C] Device found at address 0x%02X\n"), address);
  140. nDevices++;
  141. }
  142. }
  143. if (nDevices == 0) DEBUG_MSG_P(PSTR("[I2C] No devices found\n"));
  144. }
  145. void i2cSetup() {
  146. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  147. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  148. #if I2C_USE_BRZO
  149. unsigned long cst = getSetting("i2cCST", I2C_CLOCK_STRETCH_TIME).toInt();
  150. _i2c_scl_frequency = getSetting("i2cFreq", I2C_SCL_FREQUENCY).toInt();
  151. brzo_i2c_setup(sda, scl, cst);
  152. #else
  153. Wire.begin(sda, scl);
  154. #endif
  155. DEBUG_MSG_P(PSTR("[I2C] Using GPIO%u for SDA and GPIO%u for SCL\n"), sda, scl);
  156. #if I2C_CLEAR_BUS
  157. i2cClearBus();
  158. #endif
  159. #if I2C_PERFORM_SCAN
  160. i2cScan();
  161. #endif
  162. }
  163. #endif