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.

392 lines
12 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. Module key prefix: i2c
  5. */
  6. #if I2C_SUPPORT
  7. unsigned int _i2c_locked[16] = {0};
  8. #if I2C_USE_BRZO
  9. #include "brzo_i2c.h"
  10. unsigned long _i2c_scl_frequency = 0;
  11. #else
  12. #include "Wire.h"
  13. #endif
  14. // -----------------------------------------------------------------------------
  15. // Private
  16. // -----------------------------------------------------------------------------
  17. int _i2cClearbus(int sda, int scl) {
  18. #if defined(TWCR) && defined(TWEN)
  19. // Disable the Atmel 2-Wire interface so we can control the SDA and SCL pins directly
  20. TWCR &= ~(_BV(TWEN));
  21. #endif
  22. // Make SDA (data) and SCL (clock) pins inputs with pullup
  23. pinMode(sda, INPUT_PULLUP);
  24. pinMode(scl, INPUT_PULLUP);
  25. nice_delay(2500);
  26. // Wait 2.5 secs. This is strictly only necessary on the first power
  27. // up of the DS3231 module to allow it to initialize properly,
  28. // but is also assists in reliable programming of FioV3 boards as it gives the
  29. // IDE a chance to start uploaded the program
  30. // before existing sketch confuses the IDE by sending Serial data.
  31. // If it is held low the device cannot become the I2C master
  32. // I2C bus error. Could not clear SCL clock line held low
  33. boolean scl_low = (digitalRead(scl) == LOW);
  34. if (scl_low) return 1;
  35. boolean sda_low = (digitalRead(sda) == LOW);
  36. int clockCount = 20; // > 2x9 clock
  37. // While SDA is low for at most 20 cycles
  38. while (sda_low && (clockCount > 0)) {
  39. clockCount--;
  40. // Note: I2C bus is open collector so do NOT drive SCL or SDA high
  41. pinMode(scl, INPUT); // release SCL pullup so that when made output it will be LOW
  42. pinMode(scl, OUTPUT); // then clock SCL Low
  43. delayMicroseconds(10); // for >5uS
  44. pinMode(scl, INPUT); // release SCL LOW
  45. pinMode(scl, INPUT_PULLUP); // turn on pullup resistors again
  46. // do not force high as slave may be holding it low for clock stretching
  47. delayMicroseconds(10); // The >5uS is so that even the slowest I2C devices are handled
  48. // loop waiting for SCL to become high only wait 2sec
  49. scl_low = (digitalRead(scl) == LOW);
  50. int counter = 20;
  51. while (scl_low && (counter > 0)) {
  52. counter--;
  53. nice_delay(100);
  54. scl_low = (digitalRead(scl) == LOW);
  55. }
  56. // If still low after 2 sec error
  57. // I2C bus error. Could not clear. SCL clock line held low by slave clock stretch for >2sec
  58. if (scl_low) return 2;
  59. sda_low = (digitalRead(sda) == LOW); // and check SDA input again and loop
  60. }
  61. // If still low
  62. // I2C bus error. Could not clear. SDA data line held low
  63. if (sda_low) return 3;
  64. // Pull SDA line low for "start" or "repeated start"
  65. pinMode(sda, INPUT); // remove pullup
  66. pinMode(sda, OUTPUT); // and then make it LOW i.e. send an I2C Start or Repeated start control
  67. // When there is only one I2C master a "start" or "repeat start" has the same function as a "stop" and clears the bus
  68. // A Repeat Start is a Start occurring after a Start with no intervening Stop.
  69. delayMicroseconds(10); // wait >5uS
  70. pinMode(sda, INPUT); // remove output low
  71. pinMode(sda, INPUT_PULLUP); // and make SDA high i.e. send I2C STOP control.
  72. delayMicroseconds(10); // wait >5uS
  73. pinMode(sda, INPUT); // and reset pins as tri-state inputs which is the default state on reset
  74. pinMode(scl, INPUT);
  75. // Everything OK
  76. return 0;
  77. }
  78. #if WEB_SUPPORT
  79. bool _i2cWebSocketOnReceive(const char * key, JsonVariant& value) {
  80. return (strncmp(key, "i2c", 3) == 0);
  81. }
  82. #endif // WEB_SUPPORT
  83. // ---------------------------------------------------------------------
  84. // I2C API
  85. // ---------------------------------------------------------------------
  86. #if I2C_USE_BRZO
  87. void i2c_wakeup(uint8_t address) {
  88. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  89. brzo_i2c_end_transaction();
  90. }
  91. uint8_t i2c_write_uint8(uint8_t address, uint8_t value) {
  92. uint8_t buffer[1] = {value};
  93. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  94. brzo_i2c_write_uint8(buffer, 1, false);
  95. return brzo_i2c_end_transaction();
  96. }
  97. uint8_t i2c_write_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  98. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  99. brzo_i2c_write_uint8(buffer, len, false);
  100. return brzo_i2c_end_transaction();
  101. }
  102. uint8_t i2c_read_uint8(uint8_t address) {
  103. uint8_t buffer[1] = {reg};
  104. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  105. brzo_i2c_read(buffer, 1, false);
  106. brzo_i2c_end_transaction();
  107. return buffer[0];
  108. };
  109. uint8_t i2c_read_uint8(uint8_t address, uint8_t reg) {
  110. uint8_t buffer[1] = {reg};
  111. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  112. brzo_i2c_write_uint8(buffer, 1, false);
  113. brzo_i2c_read(buffer, 1, false);
  114. brzo_i2c_end_transaction();
  115. return buffer[0];
  116. };
  117. uint16_t i2c_read_uint16(uint8_t address) {
  118. uint8_t buffer[2] = {reg, 0};
  119. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  120. brzo_i2c_read(buffer, 2, false);
  121. brzo_i2c_end_transaction();
  122. return (buffer[0] * 256) | buffer[1];
  123. };
  124. uint16_t i2c_read_uint16(uint8_t address, uint8_t reg) {
  125. uint8_t buffer[2] = {reg, 0};
  126. brzo_i2c_start_transaction(_address, _i2c_scl_frequency);
  127. brzo_i2c_write_uint8(buffer, 1, false);
  128. brzo_i2c_read(buffer, 2, false);
  129. brzo_i2c_end_transaction();
  130. return (buffer[0] * 256) | buffer[1];
  131. };
  132. void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  133. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  134. brzo_i2c_read(buffer, len, false);
  135. brzo_i2c_end_transaction();
  136. }
  137. #else // not I2C_USE_BRZO
  138. void i2c_wakeup(uint8_t address) {
  139. Wire.beginTransmission((uint8_t) address);
  140. Wire.endTransmission();
  141. }
  142. uint8_t i2c_write_uint8(uint8_t address, uint8_t value) {
  143. Wire.beginTransmission((uint8_t) address);
  144. Wire.write((uint8_t) value);
  145. return Wire.endTransmission();
  146. }
  147. uint8_t i2c_write_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  148. Wire.beginTransmission((uint8_t) address);
  149. Wire.write(buffer, len);
  150. return Wire.endTransmission();
  151. }
  152. uint8_t i2c_read_uint8(uint8_t address) {
  153. uint8_t value;
  154. Wire.beginTransmission((uint8_t) address);
  155. Wire.requestFrom((uint8_t) address, (uint8_t) 1);
  156. value = Wire.read();
  157. Wire.endTransmission();
  158. return value;
  159. };
  160. uint8_t i2c_read_uint8(uint8_t address, uint8_t reg) {
  161. uint8_t value;
  162. Wire.beginTransmission((uint8_t) address);
  163. Wire.write((uint8_t) reg);
  164. Wire.endTransmission();
  165. Wire.requestFrom((uint8_t) address, (uint8_t) 1);
  166. value = Wire.read();
  167. Wire.endTransmission();
  168. return value;
  169. };
  170. uint16_t i2c_read_uint16(uint8_t address) {
  171. uint16_t value;
  172. Wire.beginTransmission((uint8_t) address);
  173. Wire.requestFrom((uint8_t) address, (uint8_t) 2);
  174. value = (Wire.read() * 256) | Wire.read();
  175. Wire.endTransmission();
  176. return value;
  177. };
  178. uint16_t i2c_read_uint16(uint8_t address, uint8_t reg) {
  179. uint16_t value;
  180. Wire.beginTransmission((uint8_t) address);
  181. Wire.write((uint8_t) reg);
  182. Wire.endTransmission();
  183. Wire.requestFrom((uint8_t) address, (uint8_t) 2);
  184. value = (Wire.read() * 256) | Wire.read();
  185. Wire.endTransmission();
  186. return value;
  187. };
  188. void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len) {
  189. Wire.beginTransmission((uint8_t) address);
  190. Wire.requestFrom(address, (uint8_t) len);
  191. for (int i=0; i<len; i++) buffer[i] = Wire.read();
  192. Wire.endTransmission();
  193. }
  194. #endif // I2C_USE_BRZO
  195. uint8_t i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value) {
  196. uint8_t buffer[2] = {reg, value};
  197. return i2c_write_buffer(address, buffer, 2);
  198. }
  199. uint8_t i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value1, uint8_t value2) {
  200. uint8_t buffer[3] = {reg, value1, value2};
  201. return i2c_write_buffer(address, buffer, 3);
  202. }
  203. uint8_t i2c_write_uint16(uint8_t address, uint8_t reg, uint16_t value) {
  204. uint8_t buffer[3];
  205. buffer[0] = reg;
  206. buffer[1] = (value >> 8) & 0xFF;
  207. buffer[2] = (value >> 0) & 0xFF;
  208. return i2c_write_buffer(address, buffer, 3);
  209. }
  210. uint8_t i2c_write_uint16(uint8_t address, uint16_t value) {
  211. uint8_t buffer[2];
  212. buffer[0] = (value >> 8) & 0xFF;
  213. buffer[1] = (value >> 0) & 0xFF;
  214. return i2c_write_buffer(address, buffer, 2);
  215. }
  216. uint16_t i2c_read_uint16_le(uint8_t address, uint8_t reg) {
  217. uint16_t temp = i2c_read_uint16(address, reg);
  218. return (temp / 256) | (temp * 256);
  219. };
  220. int16_t i2c_read_int16(uint8_t address, uint8_t reg) {
  221. return (int16_t) i2c_read_uint16(address, reg);
  222. };
  223. int16_t i2c_read_int16_le(uint8_t address, uint8_t reg) {
  224. return (int16_t) i2c_read_uint16_le(address, reg);
  225. };
  226. // -----------------------------------------------------------------------------
  227. // Utils
  228. // -----------------------------------------------------------------------------
  229. void i2cClearBus() {
  230. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  231. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  232. DEBUG_MSG_P(PSTR("[I2C] Clear bus (response: %d)\n"), _i2cClearbus(sda, scl));
  233. }
  234. bool i2cCheck(unsigned char address) {
  235. #if I2C_USE_BRZO
  236. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  237. brzo_i2c_ACK_polling(1000);
  238. return brzo_i2c_end_transaction();
  239. #else
  240. Wire.beginTransmission(address);
  241. return Wire.endTransmission();
  242. #endif
  243. }
  244. bool i2cGetLock(unsigned char address) {
  245. unsigned char index = address / 8;
  246. unsigned char mask = 1 << (address % 8);
  247. if (_i2c_locked[index] & mask) return false;
  248. _i2c_locked[index] = _i2c_locked[index] | mask;
  249. DEBUG_MSG_P(PSTR("[I2C] Address 0x%02X locked\n"), address);
  250. return true;
  251. }
  252. bool i2cReleaseLock(unsigned char address) {
  253. unsigned char index = address / 8;
  254. unsigned char mask = 1 << (address % 8);
  255. if (_i2c_locked[index] & mask) {
  256. _i2c_locked[index] = _i2c_locked[index] & ~mask;
  257. return true;
  258. }
  259. return false;
  260. }
  261. unsigned char i2cFind(size_t size, unsigned char * addresses, unsigned char &start) {
  262. for (unsigned char i=start; i<size; i++) {
  263. if (i2cCheck(addresses[i]) == 0) {
  264. start = i;
  265. return addresses[i];
  266. }
  267. }
  268. return 0;
  269. }
  270. unsigned char i2cFind(size_t size, unsigned char * addresses) {
  271. unsigned char start = 0;
  272. return i2cFind(size, addresses, start);
  273. }
  274. unsigned char i2cFindAndLock(size_t size, unsigned char * addresses) {
  275. unsigned char start = 0;
  276. unsigned char address = 0;
  277. while (address = i2cFind(size, addresses, start)) {
  278. if (i2cGetLock(address)) break;
  279. start++;
  280. }
  281. return address;
  282. }
  283. void i2cScan() {
  284. unsigned char nDevices = 0;
  285. for (unsigned char address = 1; address < 127; address++) {
  286. unsigned char error = i2cCheck(address);
  287. if (error == 0) {
  288. DEBUG_MSG_P(PSTR("[I2C] Device found at address 0x%02X\n"), address);
  289. nDevices++;
  290. }
  291. }
  292. if (nDevices == 0) DEBUG_MSG_P(PSTR("[I2C] No devices found\n"));
  293. }
  294. void i2cSetup() {
  295. unsigned char sda = getSetting("i2cSDA", I2C_SDA_PIN).toInt();
  296. unsigned char scl = getSetting("i2cSCL", I2C_SCL_PIN).toInt();
  297. #if I2C_USE_BRZO
  298. unsigned long cst = getSetting("i2cCST", I2C_CLOCK_STRETCH_TIME).toInt();
  299. _i2c_scl_frequency = getSetting("i2cFreq", I2C_SCL_FREQUENCY).toInt();
  300. brzo_i2c_setup(sda, scl, cst);
  301. #else
  302. Wire.begin(sda, scl);
  303. #endif
  304. #if WEB_SUPPORT
  305. wsOnReceiveRegister(_i2cWebSocketOnReceive);
  306. #endif
  307. DEBUG_MSG_P(PSTR("[I2C] Using GPIO%u for SDA and GPIO%u for SCL\n"), sda, scl);
  308. #if I2C_CLEAR_BUS
  309. i2cClearBus();
  310. #endif
  311. #if I2C_PERFORM_SCAN
  312. i2cScan();
  313. #endif
  314. }
  315. #endif