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.

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