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.

409 lines
12 KiB

Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
7 years ago
  1. /*
  2. I2C MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "i2c.h"
  6. #if I2C_SUPPORT
  7. unsigned int _i2c_locked[16] = {0};
  8. #if I2C_USE_BRZO
  9. unsigned long _i2c_scl_frequency = 0;
  10. #endif
  11. // -----------------------------------------------------------------------------
  12. // Private
  13. // -----------------------------------------------------------------------------
  14. int _i2cGetSDA() {
  15. return getSetting("i2cSDA", I2C_SDA_PIN);
  16. }
  17. int _i2cGetSCL() {
  18. return getSetting("i2cSCL", I2C_SCL_PIN);
  19. }
  20. int _i2cClearbus(int sda, int scl) {
  21. #if defined(TWCR) && defined(TWEN)
  22. // Disable the Atmel 2-Wire interface so we can control the SDA and SCL pins directly
  23. TWCR &= ~(_BV(TWEN));
  24. #endif
  25. // Make SDA (data) and SCL (clock) pins inputs with pullup
  26. pinMode(sda, INPUT_PULLUP);
  27. pinMode(scl, INPUT_PULLUP);
  28. nice_delay(2500);
  29. // Wait 2.5 secs. This is strictly only necessary on the first power
  30. // up of the DS3231 module to allow it to initialize properly,
  31. // but is also assists in reliable programming of FioV3 boards as it gives the
  32. // IDE a chance to start uploaded the program
  33. // before existing sketch confuses the IDE by sending Serial data.
  34. // If it is held low the device cannot become the I2C master
  35. // I2C bus error. Could not clear SCL clock line held low
  36. boolean scl_low = (digitalRead(scl) == LOW);
  37. if (scl_low) return 1;
  38. boolean sda_low = (digitalRead(sda) == LOW);
  39. int clockCount = 20; // > 2x9 clock
  40. // While SDA is low for at most 20 cycles
  41. while (sda_low && (clockCount > 0)) {
  42. clockCount--;
  43. // Note: I2C bus is open collector so do NOT drive SCL or SDA high
  44. pinMode(scl, INPUT); // release SCL pullup so that when made output it will be LOW
  45. pinMode(scl, OUTPUT); // then clock SCL Low
  46. delayMicroseconds(10); // for >5uS
  47. pinMode(scl, INPUT); // release SCL LOW
  48. pinMode(scl, INPUT_PULLUP); // turn on pullup resistors again
  49. // do not force high as slave may be holding it low for clock stretching
  50. delayMicroseconds(10); // The >5uS is so that even the slowest I2C devices are handled
  51. // loop waiting for SCL to become high only wait 2sec
  52. scl_low = (digitalRead(scl) == LOW);
  53. int counter = 20;
  54. while (scl_low && (counter > 0)) {
  55. counter--;
  56. nice_delay(100);
  57. scl_low = (digitalRead(scl) == LOW);
  58. }
  59. // If still low after 2 sec error
  60. // I2C bus error. Could not clear. SCL clock line held low by slave clock stretch for >2sec
  61. if (scl_low) return 2;
  62. sda_low = (digitalRead(sda) == LOW); // and check SDA input again and loop
  63. }
  64. // If still low
  65. // I2C bus error. Could not clear. SDA data line held low
  66. if (sda_low) return 3;
  67. // Pull SDA line low for "start" or "repeated start"
  68. pinMode(sda, INPUT); // remove pullup
  69. pinMode(sda, OUTPUT); // and then make it LOW i.e. send an I2C Start or Repeated start control
  70. // When there is only one I2C master a "start" or "repeat start" has the same function as a "stop" and clears the bus
  71. // A Repeat Start is a Start occurring after a Start with no intervening Stop.
  72. delayMicroseconds(10); // wait >5uS
  73. pinMode(sda, INPUT); // remove output low
  74. pinMode(sda, INPUT_PULLUP); // and make SDA high i.e. send I2C STOP control.
  75. delayMicroseconds(10); // wait >5uS
  76. pinMode(sda, INPUT); // and reset pins as tri-state inputs which is the default state on reset
  77. pinMode(scl, INPUT);
  78. // Everything OK
  79. return 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. DEBUG_MSG_P(
  229. PSTR("[I2C] Clear bus (response: %d)\n"),
  230. _i2cClearbus(_i2cGetSDA(), _i2cGetSCL())
  231. );
  232. }
  233. bool i2cCheck(unsigned char address) {
  234. #if I2C_USE_BRZO
  235. brzo_i2c_start_transaction(address, _i2c_scl_frequency);
  236. brzo_i2c_ACK_polling(1000);
  237. return brzo_i2c_end_transaction();
  238. #else
  239. Wire.beginTransmission(address);
  240. return Wire.endTransmission();
  241. #endif
  242. }
  243. bool i2cGetLock(unsigned char address) {
  244. unsigned char index = address / 8;
  245. unsigned char mask = 1 << (address % 8);
  246. if (_i2c_locked[index] & mask) return false;
  247. _i2c_locked[index] = _i2c_locked[index] | mask;
  248. DEBUG_MSG_P(PSTR("[I2C] Address 0x%02X locked\n"), address);
  249. return true;
  250. }
  251. bool i2cReleaseLock(unsigned char address) {
  252. unsigned char index = address / 8;
  253. unsigned char mask = 1 << (address % 8);
  254. if (_i2c_locked[index] & mask) {
  255. _i2c_locked[index] = _i2c_locked[index] & ~mask;
  256. return true;
  257. }
  258. return false;
  259. }
  260. unsigned char i2cFind(size_t size, unsigned char * addresses, unsigned char &start) {
  261. for (unsigned char i=start; i<size; i++) {
  262. if (i2cCheck(addresses[i]) == 0) {
  263. start = i;
  264. return addresses[i];
  265. }
  266. }
  267. return 0;
  268. }
  269. unsigned char i2cFind(size_t size, unsigned char * addresses) {
  270. unsigned char start = 0;
  271. return i2cFind(size, addresses, start);
  272. }
  273. unsigned char i2cFindAndLock(size_t size, unsigned char * addresses) {
  274. unsigned char start = 0;
  275. unsigned char address = 0;
  276. while ((address = i2cFind(size, addresses, start))) {
  277. if (i2cGetLock(address)) break;
  278. start++;
  279. }
  280. return address;
  281. }
  282. void i2cScan() {
  283. unsigned char nDevices = 0;
  284. for (unsigned char address = 1; address < 127; address++) {
  285. unsigned char error = i2cCheck(address);
  286. if (error == 0) {
  287. DEBUG_MSG_P(PSTR("[I2C] Device found at address 0x%02X\n"), address);
  288. nDevices++;
  289. }
  290. }
  291. if (nDevices == 0) DEBUG_MSG_P(PSTR("[I2C] No devices found\n"));
  292. }
  293. #if TERMINAL_SUPPORT
  294. void _i2cInitCommands() {
  295. terminalRegisterCommand(F("I2C.SCAN"), [](const terminal::CommandContext&) {
  296. i2cScan();
  297. terminalOK();
  298. });
  299. terminalRegisterCommand(F("I2C.CLEAR"), [](const terminal::CommandContext&) {
  300. i2cClearBus();
  301. terminalOK();
  302. });
  303. }
  304. #endif // TERMINAL_SUPPORT
  305. void i2cSetup() {
  306. const auto sda = _i2cGetSDA();
  307. const auto scl = _i2cGetSCL();
  308. #if I2C_USE_BRZO
  309. auto cst = getSetting("i2cCST", I2C_CLOCK_STRETCH_TIME);
  310. _i2c_scl_frequency = getSetting("i2cFreq", I2C_SCL_FREQUENCY);
  311. brzo_i2c_setup(sda, scl, cst);
  312. #else
  313. Wire.begin(sda, scl);
  314. #endif
  315. DEBUG_MSG_P(PSTR("[I2C] Using GPIO%02d for SDA and GPIO%02d for SCL\n"), sda, scl);
  316. #if TERMINAL_SUPPORT
  317. _i2cInitCommands();
  318. #endif
  319. #if I2C_CLEAR_BUS
  320. i2cClearBus();
  321. #endif
  322. #if I2C_PERFORM_SCAN
  323. i2cScan();
  324. #endif
  325. }
  326. #endif