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.

387 lines
12 KiB

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