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.

364 lines
11 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. // I2C API
  79. // ---------------------------------------------------------------------
  80. #if I2C_USE_BRZO
  81. void i2c_write_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  82. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  83. brzo_i2c_write_uint8(buffer, len, false);
  84. brzo_i2c_end_transaction();
  85. }
  86. void i2c_write_uint8(uint8_t address, uint8_t value) {
  87. uint8_t buffer[1] = {value};
  88. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  89. brzo_i2c_write_uint8(buffer, 1, false);
  90. brzo_i2c_end_transaction();
  91. }
  92. uint8_t i2c_read_uint8(uint8_t address) {
  93. uint8_t buffer[1] = {reg};
  94. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  95. brzo_i2c_read(buffer, 1, false);
  96. brzo_i2c_end_transaction();
  97. return buffer[0];
  98. };
  99. uint8_t i2c_read_uint8(uint8_t address, uint8_t reg) {
  100. uint8_t buffer[1] = {reg};
  101. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  102. brzo_i2c_write_uint8(buffer, 1, false);
  103. brzo_i2c_read(buffer, 1, false);
  104. brzo_i2c_end_transaction();
  105. return buffer[0];
  106. };
  107. uint16_t i2c_read_uint16(uint8_t address) {
  108. uint8_t buffer[2] = {reg, 0};
  109. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  110. brzo_i2c_read(buffer, 2, false);
  111. brzo_i2c_end_transaction();
  112. return (buffer[0] * 256) | buffer[1];
  113. };
  114. uint16_t i2c_read_uint16(uint8_t address, uint8_t reg) {
  115. uint8_t buffer[2] = {reg, 0};
  116. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  117. brzo_i2c_write_uint8(buffer, 1, false);
  118. brzo_i2c_read(buffer, 2, false);
  119. brzo_i2c_end_transaction();
  120. return (buffer[0] * 256) | buffer[1];
  121. };
  122. void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  123. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  124. brzo_i2c_read(buffer, len, false);
  125. brzo_i2c_end_transaction();
  126. }
  127. #else // not I2C_USE_BRZO
  128. void i2c_write_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  129. Wire.beginTransmission((uint8_t) address);
  130. Wire.write(buffer, len);
  131. Wire.endTransmission();
  132. }
  133. void i2c_write_uint8(uint8_t address, uint8_t value) {
  134. Wire.beginTransmission((uint8_t) address);
  135. Wire.write((uint8_t) value);
  136. Wire.endTransmission();
  137. }
  138. uint8_t i2c_read_uint8(uint8_t address) {
  139. uint8_t value;
  140. Wire.beginTransmission((uint8_t) address);
  141. Wire.requestFrom((uint8_t) address, (uint8_t) 1);
  142. value = Wire.read();
  143. Wire.endTransmission();
  144. return value;
  145. };
  146. uint8_t i2c_read_uint8(uint8_t address, uint8_t reg) {
  147. uint8_t value;
  148. Wire.beginTransmission((uint8_t) address);
  149. Wire.write((uint8_t) reg);
  150. Wire.endTransmission();
  151. Wire.requestFrom((uint8_t) address, (uint8_t) 1);
  152. value = Wire.read();
  153. Wire.endTransmission();
  154. return value;
  155. };
  156. uint16_t i2c_read_uint16(uint8_t address) {
  157. uint16_t value;
  158. Wire.beginTransmission((uint8_t) address);
  159. Wire.requestFrom((uint8_t) address, (uint8_t) 2);
  160. value = (Wire.read() * 256) | Wire.read();
  161. Wire.endTransmission();
  162. return value;
  163. };
  164. uint16_t i2c_read_uint16(uint8_t address, uint8_t reg) {
  165. uint16_t value;
  166. Wire.beginTransmission((uint8_t) address);
  167. Wire.write((uint8_t) reg);
  168. Wire.endTransmission();
  169. Wire.requestFrom((uint8_t) address, (uint8_t) 2);
  170. value = (Wire.read() * 256) | Wire.read();
  171. Wire.endTransmission();
  172. return value;
  173. };
  174. void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  175. Wire.beginTransmission((uint8_t) address);
  176. Wire.requestFrom(address, (uint8_t) len);
  177. for (int i=0; i<len; i++) buffer[i] = Wire.read();
  178. Wire.endTransmission();
  179. }
  180. #endif // I2C_USE_BRZO
  181. void i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value) {
  182. uint8_t buffer[2] = {reg, value};
  183. i2c_write_buffer(address, buffer, 2);
  184. }
  185. void i2c_write_uint16(uint8_t address, uint8_t reg, uint16_t value) {
  186. uint8_t buffer[3];
  187. buffer[0] = reg;
  188. buffer[1] = (value >> 8) & 0xFF;
  189. buffer[2] = (value >> 0) & 0xFF;
  190. i2c_write_buffer(address, buffer, 3);
  191. }
  192. void i2c_write_uint16(uint8_t address, uint16_t value) {
  193. uint8_t buffer[2];
  194. buffer[0] = (value >> 8) & 0xFF;
  195. buffer[1] = (value >> 0) & 0xFF;
  196. i2c_write_buffer(address, buffer, 2);
  197. }
  198. uint16_t i2c_read_uint16_le(uint8_t address, uint8_t reg) {
  199. uint16_t temp = i2c_read_uint16(address, reg);
  200. return (temp / 256) | (temp * 256);
  201. };
  202. int16_t i2c_read_int16(uint8_t address, uint8_t reg) {
  203. return (int16_t) i2c_read_uint16(address, reg);
  204. };
  205. int16_t i2c_read_int16_le(uint8_t address, uint8_t reg) {
  206. return (int16_t) i2c_read_uint16_le(address, reg);
  207. };
  208. // -----------------------------------------------------------------------------
  209. // Utils
  210. // -----------------------------------------------------------------------------
  211. void i2cClearBus() {
  212. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  213. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  214. DEBUG_MSG_P(PSTR("[I2C] Clear bus (response: %d)\n"), _i2cClearbus(sda, scl));
  215. }
  216. bool i2cCheck(unsigned char address) {
  217. #if I2C_USE_BRZO
  218. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  219. brzo_i2c_ACK_polling(1000);
  220. return brzo_i2c_end_transaction();
  221. #else
  222. Wire.beginTransmission(address);
  223. return Wire.endTransmission();
  224. #endif
  225. }
  226. bool i2cGetLock(unsigned char address) {
  227. unsigned char index = address / 8;
  228. unsigned char mask = 1 << (address % 8);
  229. if (_i2c_locked[index] & mask) return false;
  230. _i2c_locked[index] = _i2c_locked[index] | mask;
  231. DEBUG_MSG_P(PSTR("[I2C] Address 0x%02X locked\n"), address);
  232. return true;
  233. }
  234. bool i2cReleaseLock(unsigned char address) {
  235. unsigned char index = address / 8;
  236. unsigned char mask = 1 << (address % 8);
  237. if (_i2c_locked[index] & mask) {
  238. _i2c_locked[index] = _i2c_locked[index] & ~mask;
  239. return true;
  240. }
  241. return false;
  242. }
  243. unsigned char i2cFind(size_t size, unsigned char * addresses, unsigned char &start) {
  244. for (unsigned char i=start; i<size; i++) {
  245. if (i2cCheck(addresses[i]) == 0) {
  246. start = i;
  247. return addresses[i];
  248. }
  249. }
  250. return 0;
  251. }
  252. unsigned char i2cFind(size_t size, unsigned char * addresses) {
  253. unsigned char start = 0;
  254. return i2cFind(size, addresses, start);
  255. }
  256. unsigned char i2cFindAndLock(size_t size, unsigned char * addresses) {
  257. unsigned char start = 0;
  258. unsigned char address = 0;
  259. while (address = i2cFind(size, addresses, start)) {
  260. if (i2cGetLock(address)) break;
  261. start++;
  262. }
  263. return address;
  264. }
  265. void i2cScan() {
  266. unsigned char nDevices = 0;
  267. for (unsigned char address = 1; address < 127; address++) {
  268. unsigned char error = i2cCheck(address);
  269. if (error == 0) {
  270. DEBUG_MSG_P(PSTR("[I2C] Device found at address 0x%02X\n"), address);
  271. nDevices++;
  272. }
  273. }
  274. if (nDevices == 0) DEBUG_MSG_P(PSTR("[I2C] No devices found\n"));
  275. }
  276. void i2cSetup() {
  277. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  278. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  279. #if I2C_USE_BRZO
  280. unsigned long cst = getSetting("i2cCST", I2C_CLOCK_STRETCH_TIME).toInt();
  281. _i2c_scl_frequency = getSetting("i2cFreq", I2C_SCL_FREQUENCY).toInt();
  282. brzo_i2c_setup(sda, scl, cst);
  283. #else
  284. Wire.begin(sda, scl);
  285. #endif
  286. DEBUG_MSG_P(PSTR("[I2C] Using GPIO%u for SDA and GPIO%u for SCL\n"), sda, scl);
  287. #if I2C_CLEAR_BUS
  288. i2cClearBus();
  289. #endif
  290. #if I2C_PERFORM_SCAN
  291. i2cScan();
  292. #endif
  293. }
  294. #endif