cmd_i2ctools.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /* cmd_i2ctools.c
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "cmd_i2ctools.h"
  8. #include "accessors.h"
  9. #include "adac.h"
  10. #include "argtable3/argtable3.h"
  11. #include "config.h"
  12. #include "display.h"
  13. #include "driver/gpio.h"
  14. #include "driver/i2c.h"
  15. #include "esp_log.h"
  16. #include "messaging.h"
  17. #include "platform_config.h"
  18. #include "platform_console.h"
  19. #include "stdio.h"
  20. #include "string.h"
  21. #include "tools.h"
  22. #include "trace.h"
  23. #include <stdio.h>
  24. #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
  25. #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
  26. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  27. #define READ_BIT I2C_MASTER_READ /*!< I2C master read */
  28. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  29. #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
  30. #define ACK_VAL 0x0 /*!< I2C ack value */
  31. #define NACK_VAL 0x1 /*!< I2C nack value */
  32. extern int spi_system_host;
  33. extern int spi_system_dc_gpio;
  34. extern int is_output_gpio(struct arg_int* gpio, FILE* f, int* gpio_out, bool mandatory);
  35. static const char* TAG = "cmd_i2ctools";
  36. const char* desc_spiconfig = "SPI Bus Parameters";
  37. const char* desc_i2c = "I2C Bus Parameters";
  38. const char* desc_display = "Display";
  39. #ifdef CONFIG_I2C_LOCKED
  40. static i2c_port_t i2c_port = I2C_NUM_1;
  41. #else
  42. static i2c_port_t i2c_port = I2C_NUM_0;
  43. #endif
  44. static struct {
  45. struct arg_int* chip_address;
  46. struct arg_int* register_address;
  47. struct arg_int* data_length;
  48. struct arg_end* end;
  49. } i2cget_args;
  50. static struct {
  51. struct arg_int* chip_address;
  52. struct arg_int* port;
  53. struct arg_int* register_address;
  54. struct arg_int* data;
  55. struct arg_end* end;
  56. } i2cset_args;
  57. #if CONFIG_WITH_CONFIG_UI
  58. static struct {
  59. struct arg_int* chip_address;
  60. struct arg_int* size;
  61. struct arg_end* end;
  62. } i2cdump_args;
  63. #endif
  64. static struct {
  65. struct arg_int* port;
  66. struct arg_int* freq;
  67. struct arg_int* sda;
  68. struct arg_int* scl;
  69. struct arg_lit* clear;
  70. struct arg_end* end;
  71. } i2cconfig_args;
  72. #if CONFIG_WITH_CONFIG_UI
  73. static struct {
  74. struct arg_int* data;
  75. struct arg_int* miso;
  76. struct arg_int* clk;
  77. struct arg_int* dc;
  78. struct arg_int* host;
  79. struct arg_lit* clear;
  80. struct arg_end* end;
  81. } spiconfig_args;
  82. static struct {
  83. struct arg_str* type;
  84. struct arg_str* driver;
  85. struct arg_int* depth;
  86. struct arg_int* address;
  87. struct arg_int* width;
  88. struct arg_int* height;
  89. struct arg_lit* rotate;
  90. struct arg_lit* hflip;
  91. struct arg_lit* vflip;
  92. struct arg_int* speed;
  93. struct arg_int* cs;
  94. struct arg_int* back;
  95. struct arg_int* reset;
  96. struct arg_lit* clear;
  97. struct arg_lit* invert;
  98. struct arg_int* mode;
  99. struct arg_end* end;
  100. } i2cdisp_args;
  101. #endif
  102. bool is_i2c_started(i2c_port_t port) {
  103. esp_err_t ret = ESP_OK;
  104. ESP_LOGD(TAG, "Determining if i2c is started on port %u", port);
  105. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  106. ret = i2c_master_start(cmd);
  107. if (ret == ESP_OK) {
  108. ret = i2c_master_write_byte(cmd, WRITE_BIT, ACK_CHECK_EN);
  109. }
  110. if (ret == ESP_OK) {
  111. ret = i2c_master_stop(cmd);
  112. }
  113. if (ret == ESP_OK) {
  114. ret = i2c_master_cmd_begin(port, cmd, 50 / portTICK_RATE_MS);
  115. }
  116. i2c_cmd_link_delete(cmd);
  117. ESP_LOGD(TAG, "i2c is %s. %s", ret != ESP_ERR_INVALID_STATE ? "started" : "not started",
  118. esp_err_to_name(ret));
  119. return (ret != ESP_ERR_INVALID_STATE);
  120. }
  121. typedef struct {
  122. uint8_t address;
  123. const char* description;
  124. } i2c_db_t;
  125. #if CONFIG_WITH_CONFIG_UI
  126. // the list was taken from https://i2cdevices.org/addresses
  127. // on 2020-01-16
  128. static const i2c_db_t i2c_db[] = {{.address = 0x00, .description = "Unknown"},
  129. {.address = 0x01, .description = "Unknown"}, {.address = 0x02, .description = "Unknown"},
  130. {.address = 0x03, .description = "Unknown"}, {.address = 0x04, .description = "Unknown"},
  131. {.address = 0x05, .description = "Unknown"}, {.address = 0x06, .description = "Unknown"},
  132. {.address = 0x07, .description = "Unknown"}, {.address = 0x08, .description = "Unknown"},
  133. {.address = 0x0c, .description = "AK8975"}, {.address = 0x0d, .description = "AK8975"},
  134. {.address = 0x0e, .description = "MAG3110 AK8975 IST-8310"},
  135. {.address = 0x0f, .description = "AK8975"},
  136. {.address = 0x10, .description = "VEML7700 VML6075 VEML6075 ES8388"},
  137. {.address = 0x11,
  138. .description = "Si4713 SAA5246 SAA5243P/K SAA5243P/L SAA5243P/E SAA5243P/H ES8388"},
  139. {.address = 0x12, .description = "SEN-17374"},
  140. {.address = 0x13, .description = "VCNL40x0 SEN-17374"},
  141. {.address = 0x18, .description = "MCP9808 LIS3DH LSM303 COM-15093"},
  142. {.address = 0x19, .description = "MCP9808 LIS3DH LSM303 COM-15093"},
  143. {.address = 0x1a, .description = "AC101 MCP9808"}, {.address = 0x1b, .description = "MCP9808"},
  144. {.address = 0x1c, .description = "MCP9808 MMA845x FXOS8700"},
  145. {.address = 0x1d, .description = "MCP9808 MMA845x ADXL345 FXOS8700"},
  146. {.address = 0x1e, .description = "HMC5883 LSM303 MCP9808 LSM303 FXOS8700"},
  147. {.address = 0x1f, .description = "MCP9808 FXOS8700"},
  148. {.address = 0x20, .description = "TCA9554 MCP23008 MA12070P MCP23017 Chirp! FXAS21002"},
  149. {.address = 0x21, .description = "FXAS21002 MCP23008 MCP23017 SAA4700 MA12070P TCA9554"},
  150. {.address = 0x22, .description = "ES8388 MCP23008 MCP23017 PCA1070 MA12070P TCA9554"},
  151. {.address = 0x23, .description = "MCP23008 MCP23017 SAA4700 MA12070P TCA9554"},
  152. {.address = 0x24, .description = "TCA9554 MCP23008 PCD3312C MCP23017 PCD3311C"},
  153. {.address = 0x25, .description = "TCA9554 MCP23008 PCD3312C MCP23017 PCD3311C"},
  154. {.address = 0x26, .description = "MCP23008 MCP23017 TCA9554"},
  155. {.address = 0x27, .description = "MCP23008 MCP23017 HIH6130 TCA9554"},
  156. {.address = 0x28, .description = "BNO055 CAP1188"},
  157. {.address = 0x29, .description = "BNO055 VL53L0x VL6180X CAP1188 TCS34725 TSL2591"},
  158. {.address = 0x2a, .description = "CAP1188"}, {.address = 0x2b, .description = "CAP1188"},
  159. {.address = 0x2c, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"},
  160. {.address = 0x2d, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"},
  161. {.address = 0x2e, .description = "AD5248 AD5251 AD5252 LPS22HB"},
  162. {.address = 0x2f, .description = "AD5248 AD5243 AD5251 AD5252"},
  163. {.address = 0x30, .description = "SAA2502"}, {.address = 0x31, .description = "SAA2502"},
  164. {.address = 0x33, .description = "MLX90640"},
  165. {.address = 0x38, .description = "FT6x06 VEML6070 BMA150 SAA1064 SEN-15892 PCF8574AP"},
  166. {.address = 0x39, .description = "TSL2561 APDS-9960 VEML6070 SAA1064 PCF8574AP"},
  167. {.address = 0x3a, .description = "PCF8577C SAA1064 PCF8574AP"},
  168. {.address = 0x3b, .description = "SAA1064 PCF8569 PCF8574AP"},
  169. {.address = 0x3c, .description = "SSD1305 SSD1306 PCF8578 PCF8569 SH1106 PCF8574AP"},
  170. {.address = 0x3d, .description = "SSD1305 SSD1306 PCF8578 SH1106 PCF8574AP"},
  171. {.address = 0x3e, .description = "PCF8574AP"}, {.address = 0x3f, .description = "PCF8574AP"},
  172. {.address = 0x40,
  173. .description = "Si7021 HTU21D-F TMP007 TMP006 PCA9685 INA219 TEA6330 TEA6300 TDA9860 "
  174. "TEA6320 TDA8421 NE5751 INA260 PCF8574"},
  175. {.address = 0x41,
  176. .description = "TMP007 TDA8421 TDA8424 STMPE610 PCF8574 STMPE811 NE5751 INA260 TDA8425 "
  177. "TMP006 TDA9860 PCA9685 INA219 TDA8426"},
  178. {.address = 0x42,
  179. .description = "TMP007 TDA8417 HDC1008 PCF8574 INA260 TDA8415 TMP006 PCA9685 INA219"},
  180. {.address = 0x43, .description = "TMP007 HDC1008 PCF8574 INA260 TMP006 PCA9685 INA219"},
  181. {.address = 0x44,
  182. .description = "TMP007 TMP006 PCA9685 INA219 STMPE610 SHT31 ISL29125 STMPE811 TDA4688 "
  183. "TDA4672 TDA4780 TDA4670 TDA8442 TDA4687 TDA4671 TDA4680 INA260 PCF8574"},
  184. {.address = 0x45,
  185. .description = "TMP007 TDA7433 PCF8574 TDA8376 INA260 TMP006 PCA9685 INA219 SHT31"},
  186. {.address = 0x46, .description = "TMP007 PCF8574 TDA8370 INA260 TMP006 PCA9685 INA219 TDA9150"},
  187. {.address = 0x47, .description = "TMP007 PCF8574 INA260 TMP006 PCA9685 INA219"},
  188. {.address = 0x48, .description = "PCA9685 INA219 PN532 TMP102 INA260 ADS1115 PCF8574 ADS7828"},
  189. {.address = 0x49,
  190. .description = "TSL2561 PCA9685 INA219 TMP102 INA260 ADS1115 AS7262 PCF8574 ADS7828"},
  191. {.address = 0x4a,
  192. .description = "ADS7828 PCF8574 ADS1115 INA260 PCA9685 MAX44009 INA219 TMP102"},
  193. {.address = 0x4b,
  194. .description = "ADS7828 PCF8574 ADS1115 INA260 PCA9685 MAX44009 INA219 TMP102"},
  195. {.address = 0x4c, .description = "PCA9685 INA219 INA260 PCF8574"},
  196. {.address = 0x4d, .description = "PCA9685 INA219 INA260 PCF8574"},
  197. {.address = 0x4e, .description = "PCA9685 INA219 INA260 PCF8574"},
  198. {.address = 0x4f, .description = "PCA9685 INA219 INA260 PCF8574"},
  199. {.address = 0x50, .description = "PCA9685 MB85RC"},
  200. {.address = 0x51, .description = "PCA9685 MB85RC VCNL4200"},
  201. {.address = 0x52, .description = "PCA9685 MB85RC Nunchuck controller APDS-9250 SI1133"},
  202. {.address = 0x53, .description = "ADXL345 PCA9685 MB85RC"},
  203. {.address = 0x54, .description = "PCA9685 MB85RC"},
  204. {.address = 0x55, .description = "PCA9685 MB85RC MAX30101 SI1133"},
  205. {.address = 0x56, .description = "PCA9685 MB85RC"},
  206. {.address = 0x57, .description = "PCA9685 MB85RC MAX3010x"},
  207. {.address = 0x58, .description = "PCA9685 TPA2016 SGP30"},
  208. {.address = 0x59, .description = "PCA9685"},
  209. {.address = 0x5a, .description = "MPR121 MLX90614 CCS811 PCA9685 DRV2605"},
  210. {.address = 0x5b, .description = "PCA9685 CCS811 MPR121"},
  211. {.address = 0x5c, .description = "PCA9685 AM2315 MPR121"},
  212. {.address = 0x5d, .description = "PCA9685 MPR121"}, {.address = 0x5e, .description = "PCA9685"},
  213. {.address = 0x5f, .description = "PCA9685 HTS221"},
  214. {.address = 0x60,
  215. .description = "SI1132 Si5351A ATECC608A TSA5511 ATECC508A SAB3035 MCP4725A0 SAB3037 "
  216. "PCA9685 MCP4725A1 TEA5767 MPL3115A2 MPL115A2 Si1145"},
  217. {.address = 0x61,
  218. .description = "Si5351A TSA5511 SAB3035 MCP4725A0 SAB3037 TEA6100 PCA9685 MCP4725A1"},
  219. {.address = 0x62,
  220. .description = "SCD40-D-R2 TSA5511 SAB3035 UMA1014T SAB3037 PCA9685 MCP4725A1"},
  221. {.address = 0x63, .description = "Si4713 TSA5511 SAB3035 UMA1014T SAB3037 PCA9685 MCP4725A1"},
  222. {.address = 0x64, .description = "PCA9685 MCP4725A2 MCP4725A1"},
  223. {.address = 0x65, .description = "PCA9685 MCP4725A2 MCP4725A1"},
  224. {.address = 0x66, .description = "PCA9685 MCP4725A3 IS31FL3731 MCP4725A1"},
  225. {.address = 0x67, .description = "PCA9685 MCP4725A3 MCP4725A1"},
  226. {.address = 0x68,
  227. .description =
  228. "MPU-9250 ICM-20948 MPU6050 AMG8833 DS3231 PCA9685 PCF8573 PCF8523 DS1307 ITG3200"},
  229. {.address = 0x69,
  230. .description = "MPU-9250 ICM-20948 MPU6050 AMG8833 PCA9685 PCF8573 ITG3200 SPS30"},
  231. {.address = 0x6a, .description = "PCA9685 L3GD20H PCF8573"},
  232. {.address = 0x6b, .description = "PCA9685 L3GD20H PCF8573"},
  233. {.address = 0x6c, .description = "PCA9685"}, {.address = 0x6d, .description = "PCA9685"},
  234. {.address = 0x6e, .description = "PCA9685"},
  235. {.address = 0x6f, .description = "PCA9685 MCP7940N"},
  236. {.address = 0x70, .description = "PCA9685 TCA9548 HT16K33 SHTC3"},
  237. {.address = 0x71, .description = "PCA9685 TCA9548 HT16K33"},
  238. {.address = 0x72, .description = "PCA9685 TCA9548 HT16K33"},
  239. {.address = 0x73, .description = "PCA9685 TCA9548 HT16K33"},
  240. {.address = 0x74, .description = "PCA9685 TCA9548 HT16K33"},
  241. {.address = 0x75, .description = "PCA9685 TCA9548 HT16K33"},
  242. {.address = 0x76,
  243. .description = "BME688 BME680 MS5611 MS5607 HT16K33 PCA9685 BME280 BMP280 TCA9548"},
  244. {.address = 0x77,
  245. .description = "PCA9685 TCA9548 HT16K33 IS31FL3731 BME280 BMP280 MS5607 BMP180 BMP085 "
  246. "BMA180 MS5611 BME680 BME688"},
  247. {.address = 0x78, .description = "PCA9685"}, {.address = 0x79, .description = "PCA9685"},
  248. {.address = 0x7a, .description = "PCA9685"}, {.address = 0x7b, .description = "PCA9685"},
  249. {.address = 0x7c, .description = "PCA9685"}, {.address = 0x7d, .description = "PCA9685"},
  250. {.address = 0x7e, .description = "PCA9685"}, {.address = 0x7f, .description = "PCA9685"},
  251. {.address = 0, .description = NULL}};
  252. const char* i2c_get_description(uint8_t address) {
  253. uint8_t i = 0;
  254. while (i2c_db[i].description && i2c_db[i].address != address)
  255. i++;
  256. return i2c_db[i].description ? i2c_db[i].description : "Unlisted";
  257. }
  258. #endif
  259. static esp_err_t i2c_get_port(int port, i2c_port_t* i2c_port) {
  260. if (port >= I2C_NUM_MAX) {
  261. log_send_messaging(MESSAGING_ERROR, "Wrong port number: %d", port);
  262. return ESP_FAIL;
  263. }
  264. switch (port) {
  265. case 0:
  266. *i2c_port = I2C_NUM_0;
  267. break;
  268. case 1:
  269. *i2c_port = I2C_NUM_1;
  270. break;
  271. default:
  272. *i2c_port = I2C_NUM_0;
  273. break;
  274. }
  275. return ESP_OK;
  276. }
  277. static esp_err_t i2c_master_driver_install(const char* cmdname) {
  278. esp_err_t err = ESP_OK;
  279. cmd_send_messaging(cmdname, MESSAGING_INFO, "Installing i2c driver on port %u\n", i2c_port);
  280. if ((err = i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE,
  281. I2C_MASTER_TX_BUF_DISABLE, 0)) != ESP_OK) {
  282. cmd_send_messaging(
  283. cmdname, MESSAGING_ERROR, "Driver install failed! %s\n", esp_err_to_name(err));
  284. }
  285. return err;
  286. }
  287. static esp_err_t i2c_master_driver_initialize(const char* cmdname, i2c_config_t* conf) {
  288. esp_err_t err = ESP_OK;
  289. cmd_send_messaging(cmdname, MESSAGING_INFO,
  290. "Initializing i2c driver configuration.\n mode = I2C_MODE_MASTER, \n scl_pullup_en = "
  291. "GPIO_PULLUP_ENABLE, \n i2c port = %u, \n sda_io_num = %u, \n sda_pullup_en = "
  292. "GPIO_PULLUP_ENABLE, \n scl_io_num = %u, \n scl_pullup_en = GPIO_PULLUP_ENABLE, \n "
  293. "master.clk_speed = %u\n",
  294. i2c_port, conf->sda_io_num, conf->scl_io_num, conf->master.clk_speed);
  295. if ((err = i2c_param_config(i2c_port, conf)) != ESP_OK) {
  296. cmd_send_messaging(
  297. cmdname, MESSAGING_ERROR, "i2c driver config load failed. %s\n", esp_err_to_name(err));
  298. }
  299. return err;
  300. }
  301. #if CONFIG_WITH_CONFIG_UI
  302. static int do_i2c_set_display(int argc, char** argv) {
  303. bool bHasi2cConfig = false, bHasSpiConfig = false;
  304. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&i2cdisp_args);
  305. display_config_t config = {.back = -1, .CS_pin = -1, .RST_pin = -1, .depth = 0};
  306. char* nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  307. if (nvs_item && strlen(nvs_item) > 0) {
  308. bHasi2cConfig = true;
  309. }
  310. FREE_AND_NULL(nvs_item);
  311. nvs_item = config_alloc_get(NVS_TYPE_STR, "spi_config");
  312. if (nvs_item && strlen(nvs_item) > 0) {
  313. bHasSpiConfig = true;
  314. }
  315. FREE_AND_NULL(nvs_item);
  316. /* Check "--clear" option */
  317. if (i2cdisp_args.clear->count) {
  318. cmd_send_messaging(argv[0], MESSAGING_WARNING, "Display config cleared");
  319. config_set_value(NVS_TYPE_STR, "display_config", "");
  320. return 0;
  321. }
  322. char* buf = NULL;
  323. size_t buf_size = 0;
  324. FILE* f = open_memstream(&buf, &buf_size);
  325. if (f == NULL) {
  326. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Unable to open memory stream.");
  327. return 1;
  328. }
  329. if (nerrors > 0) {
  330. arg_print_errors(f, i2cdisp_args.end, desc_display);
  331. fclose(f);
  332. return 1;
  333. }
  334. /* Check "--type" option */
  335. if (i2cdisp_args.type->count) {
  336. if (strcasecmp(i2c_name_type, i2cdisp_args.type->sval[0]) == 0) {
  337. config.type = i2c_name_type;
  338. } else {
  339. config.type = spi_name_type;
  340. }
  341. } else {
  342. config.type = i2c_name_type;
  343. }
  344. /* Check "--address" option */
  345. if (strcasecmp(config.type, "I2C") == 0) {
  346. if (i2cdisp_args.address->count > 0) {
  347. config.address = i2cdisp_args.address->ival[0];
  348. } else {
  349. config.address = 60;
  350. }
  351. if (!bHasi2cConfig) {
  352. fprintf(f, "I2C bus has to be configured first. \n");
  353. nerrors++;
  354. }
  355. } else {
  356. // SPI Bus connection
  357. if (!bHasSpiConfig) {
  358. fprintf(f, "SPI bus has to be configured first. \n");
  359. nerrors++;
  360. }
  361. /* Check "--speed" option */
  362. if (i2cdisp_args.speed->count) {
  363. config.speed = i2cdisp_args.speed->ival[0];
  364. } else {
  365. config.speed = 8000000;
  366. }
  367. /* Check "--cs" option */
  368. nerrors += is_output_gpio(i2cdisp_args.cs, f, &config.CS_pin, false);
  369. /* Check "--mode" option */
  370. if (i2cdisp_args.mode->count) {
  371. config.mode = i2cdisp_args.mode->ival[0];
  372. } else {
  373. config.mode = 0;
  374. }
  375. }
  376. nerrors += is_output_gpio(i2cdisp_args.reset, f, &config.RST_pin, false);
  377. /* Check "--width" option */
  378. if (i2cdisp_args.width->count) {
  379. config.width = i2cdisp_args.width->ival[0];
  380. }
  381. /* Check "--height" option */
  382. if (i2cdisp_args.height->count) {
  383. config.height = i2cdisp_args.height->ival[0];
  384. }
  385. /* Check "--driver" option */
  386. if (i2cdisp_args.driver->count) {
  387. config.drivername = display_conf_get_driver_name(i2cdisp_args.driver->sval[0]);
  388. }
  389. if (i2cdisp_args.depth->count > 0 && strcasecmp(config.drivername, "SSD1326") == 0) {
  390. config.depth = i2cdisp_args.depth->ival[0];
  391. }
  392. /* Check "--back" option */
  393. nerrors += is_output_gpio(i2cdisp_args.back, f, &config.back, false);
  394. if (!config.drivername || !display_is_valid_driver(config.drivername)) {
  395. fprintf(f, "Unsupported display driver %s\n", config.drivername);
  396. nerrors++;
  397. }
  398. if (!strcasestr(config.type, "I2C") && !strcasestr(config.type, "SPI")) {
  399. fprintf(f, "Invalid display type %s\n", config.type);
  400. nerrors++;
  401. }
  402. config.rotate = i2cdisp_args.rotate->count > 0;
  403. config.hflip = i2cdisp_args.hflip->count > 0;
  404. config.vflip = i2cdisp_args.vflip->count > 0;
  405. config.invert = i2cdisp_args.invert->count > 0;
  406. if (nerrors == 0) {
  407. fprintf(f, "Saving display configuration\n");
  408. esp_err_t err = config_display_set(&config);
  409. if (err != ESP_OK) {
  410. fprintf(f, "Error: %s\n", esp_err_to_name(err));
  411. nerrors++;
  412. }
  413. }
  414. if (!nerrors) {
  415. fprintf(f, "Done.\n");
  416. }
  417. fflush(f);
  418. cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf);
  419. fclose(f);
  420. FREE_AND_NULL(buf);
  421. return nerrors;
  422. }
  423. static int do_spiconfig_cmd(int argc, char** argv) {
  424. static spi_bus_config_t spi_config = {.mosi_io_num = -1,
  425. .sclk_io_num = -1,
  426. .miso_io_num = -1,
  427. .quadwp_io_num = -1,
  428. .quadhd_io_num = -1};
  429. int dc = -1, host = 0;
  430. esp_err_t err = ESP_OK;
  431. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&spiconfig_args);
  432. /* Check "--clear" option */
  433. if (spiconfig_args.clear->count) {
  434. cmd_send_messaging(argv[0], MESSAGING_WARNING, "SPI config cleared.\n");
  435. config_set_value(NVS_TYPE_STR, "spi_config", "");
  436. return 0;
  437. }
  438. char* buf = NULL;
  439. size_t buf_size = 0;
  440. FILE* f = open_memstream(&buf, &buf_size);
  441. if (f == NULL) {
  442. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n");
  443. return 1;
  444. }
  445. if (nerrors > 0) {
  446. arg_print_errors(f, spiconfig_args.end, desc_spiconfig);
  447. fclose(f);
  448. return 1;
  449. }
  450. /* Check "--clk" option */
  451. nerrors += is_output_gpio(spiconfig_args.clk, f, &spi_config.sclk_io_num, true);
  452. nerrors += is_output_gpio(spiconfig_args.data, f, &spi_config.mosi_io_num, true);
  453. nerrors += is_output_gpio(spiconfig_args.miso, f, &spi_config.miso_io_num, true);
  454. nerrors += is_output_gpio(spiconfig_args.dc, f, &dc, true);
  455. nerrors += is_output_gpio(spiconfig_args.host, f, &host, true);
  456. if (!nerrors) {
  457. fprintf(f, "Configuring SPI data=%d clock=%d host=%u dc: %d\n", spi_config.mosi_io_num,
  458. spi_config.sclk_io_num, host, dc);
  459. err = spi_bus_initialize(host, &spi_config, SPI_DMA_CH_AUTO);
  460. if (err != ESP_OK) {
  461. if (err == ESP_ERR_INVALID_STATE) {
  462. // if user is changing the host number, we need to try freeing both hosts
  463. if ((err = spi_bus_free(host)) != ESP_OK &&
  464. (err = spi_bus_free(host == 1 ? 2 : 1)) != ESP_OK) {
  465. fprintf(f,
  466. "SPI bus init failed. Please clear SPI configuration, restart the device "
  467. "and try again. %s\n",
  468. esp_err_to_name(err));
  469. nerrors++;
  470. } else if ((err = spi_bus_initialize(host, &spi_config, SPI_DMA_CH_AUTO)) !=
  471. ESP_OK) {
  472. fprintf(f, "Failed to initialize SPI Bus. %s\n", esp_err_to_name(err));
  473. nerrors++;
  474. }
  475. } else {
  476. fprintf(f, "SPI bus initialization failed. %s\n", esp_err_to_name(err));
  477. nerrors++;
  478. }
  479. }
  480. }
  481. if (!nerrors) {
  482. fprintf(f, "Storing SPI parameters.\n");
  483. nerrors += (config_spi_set(&spi_config, host, dc) != ESP_OK);
  484. }
  485. if (!nerrors) {
  486. fprintf(f, "Done.\n");
  487. }
  488. fflush(f);
  489. cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf);
  490. fclose(f);
  491. FREE_AND_NULL(buf);
  492. return nerrors;
  493. }
  494. #endif
  495. static int do_i2cconfig_cmd(int argc, char** argv) {
  496. esp_err_t err = ESP_OK;
  497. i2c_config_t conf = {.mode = I2C_MODE_MASTER,
  498. .sda_io_num = 19,
  499. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  500. .scl_io_num = 18,
  501. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  502. .master.clk_speed = 100000};
  503. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&i2cconfig_args);
  504. /* Check "--clear" option */
  505. if (i2cconfig_args.clear->count) {
  506. cmd_send_messaging(argv[0], MESSAGING_WARNING, "i2c config cleared\n");
  507. config_set_value(NVS_TYPE_STR, "i2c_config", "");
  508. return 0;
  509. }
  510. char* buf = NULL;
  511. size_t buf_size = 0;
  512. FILE* f = open_memstream(&buf, &buf_size);
  513. if (f == NULL) {
  514. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n");
  515. return 1;
  516. }
  517. if (nerrors > 0) {
  518. arg_print_errors(f, i2cconfig_args.end, desc_i2c);
  519. fclose(f);
  520. return 1;
  521. }
  522. /* Check "--port" option */
  523. if (i2cconfig_args.port->count) {
  524. if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) {
  525. fprintf(f, "Invalid port %u \n", i2cconfig_args.port->ival[0]);
  526. nerrors++;
  527. }
  528. }
  529. /* Check "--freq" option */
  530. if (i2cconfig_args.freq->count) {
  531. conf.master.clk_speed = i2cconfig_args.freq->ival[0];
  532. }
  533. nerrors += is_output_gpio(i2cconfig_args.sda, f, &conf.sda_io_num, true);
  534. nerrors += is_output_gpio(i2cconfig_args.scl, f, &conf.scl_io_num, true);
  535. #ifdef CONFIG_I2C_LOCKED
  536. if (i2c_port == I2C_NUM_0) {
  537. i2c_port = I2C_NUM_1;
  538. fprintf(f, "can't use i2c port 0 when locked by config. Changing to port 1.\n");
  539. }
  540. #endif
  541. if (!nerrors) {
  542. fprintf(f, "Uninstalling i2c driver from port %u if needed\n", i2c_port);
  543. if (is_i2c_started(i2c_port)) {
  544. if ((err = i2c_driver_delete(i2c_port)) != ESP_OK) {
  545. fprintf(f, "i2c driver delete failed. %s\n", esp_err_to_name(err));
  546. nerrors++;
  547. }
  548. }
  549. }
  550. if (!nerrors) {
  551. if ((err = i2c_master_driver_initialize(argv[0], &conf)) == ESP_OK) {
  552. if ((err = i2c_master_driver_install(argv[0])) != ESP_OK) {
  553. nerrors++;
  554. } else {
  555. fprintf(f, "i2c driver successfully started.\n");
  556. }
  557. } else {
  558. nerrors++;
  559. }
  560. }
  561. if (!nerrors) {
  562. fprintf(f, "Storing i2c parameters.\n");
  563. config_i2c_set(&conf, i2c_port);
  564. }
  565. if (!nerrors) {
  566. fprintf(f, "Done.\n");
  567. }
  568. fflush(f);
  569. cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf);
  570. fclose(f);
  571. FREE_AND_NULL(buf);
  572. return nerrors;
  573. }
  574. #if CONFIG_WITH_CONFIG_UI
  575. static int do_i2cdump_cmd(int argc, char** argv) {
  576. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&i2cdump_args);
  577. if (nerrors != 0) {
  578. return 1;
  579. }
  580. /* Check chip address: "-c" option */
  581. int chip_addr = i2cdump_args.chip_address->ival[0];
  582. /* Check read size: "-s" option */
  583. int size = 1;
  584. if (i2cdump_args.size->count) {
  585. size = i2cdump_args.size->ival[0];
  586. }
  587. i2c_port_t loc_i2c_port = i2c_port;
  588. if (i2cset_args.port->count &&
  589. i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) {
  590. return 1;
  591. }
  592. if (size != 1 && size != 2 && size != 4) {
  593. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Wrong read size. Only support 1,2,4\n");
  594. return 1;
  595. }
  596. char* buf = NULL;
  597. size_t buf_size = 0;
  598. FILE* f = open_memstream(&buf, &buf_size);
  599. if (f == NULL) {
  600. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n");
  601. return 1;
  602. }
  603. uint8_t data_addr;
  604. uint8_t data[4];
  605. int32_t block[16];
  606. fprintf(f, "\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f"
  607. " 0123456789abcdef\r\n");
  608. for (int i = 0; i < 128; i += 16) {
  609. fprintf(f, "%02x: ", i);
  610. for (int j = 0; j < 16; j += size) {
  611. fflush(stdout);
  612. data_addr = i + j;
  613. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  614. i2c_master_start(cmd);
  615. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  616. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  617. i2c_master_start(cmd);
  618. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  619. if (size > 1) {
  620. i2c_master_read(cmd, data, size - 1, ACK_VAL);
  621. }
  622. i2c_master_read_byte(cmd, data + size - 1, NACK_VAL);
  623. i2c_master_stop(cmd);
  624. esp_err_t ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 50 / portTICK_RATE_MS);
  625. i2c_cmd_link_delete(cmd);
  626. if (ret == ESP_OK) {
  627. for (int k = 0; k < size; k++) {
  628. fprintf(f, "%02x ", data[k]);
  629. block[j + k] = data[k];
  630. }
  631. } else {
  632. for (int k = 0; k < size; k++) {
  633. fprintf(f, "XX ");
  634. block[j + k] = -1;
  635. }
  636. }
  637. }
  638. fprintf(f, " ");
  639. for (int k = 0; k < 16; k++) {
  640. if (block[k] < 0) {
  641. fprintf(f, "X");
  642. }
  643. if ((block[k] & 0xff) == 0x00 || (block[k] & 0xff) == 0xff) {
  644. fprintf(f, ".");
  645. } else if ((block[k] & 0xff) < 32 || (block[k] & 0xff) >= 127) {
  646. fprintf(f, "?");
  647. } else {
  648. fprintf(f, "%c", block[k] & 0xff);
  649. }
  650. }
  651. fprintf(f, "\r\n");
  652. }
  653. // Don't stop the driver; our firmware may be using it for screen, etc
  654. // i2c_driver_delete(i2c_port);
  655. fflush(f);
  656. cmd_send_messaging(argv[0], MESSAGING_INFO, "%s", buf);
  657. fclose(f);
  658. FREE_AND_NULL(buf);
  659. return 0;
  660. }
  661. static int do_i2cset_cmd(int argc, char** argv) {
  662. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&i2cset_args);
  663. if (nerrors != 0) {
  664. return 1;
  665. }
  666. /* Check chip address: "-c" option */
  667. int chip_addr = i2cset_args.chip_address->ival[0];
  668. /* Check register address: "-r" option */
  669. int data_addr = 0;
  670. if (i2cset_args.register_address->count) {
  671. data_addr = i2cset_args.register_address->ival[0];
  672. }
  673. i2c_port_t loc_i2c_port = i2c_port;
  674. if (i2cset_args.port->count &&
  675. i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) {
  676. return 1;
  677. }
  678. /* Check data: "-d" option */
  679. int len = i2cset_args.data->count;
  680. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  681. i2c_master_start(cmd);
  682. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  683. if (i2cset_args.register_address->count) {
  684. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  685. }
  686. for (int i = 0; i < len; i++) {
  687. i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN);
  688. }
  689. i2c_master_stop(cmd);
  690. esp_err_t ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 1000 / portTICK_RATE_MS);
  691. i2c_cmd_link_delete(cmd);
  692. if (ret == ESP_OK) {
  693. cmd_send_messaging(argv[0], MESSAGING_INFO, "i2c Write OK\n");
  694. } else if (ret == ESP_ERR_TIMEOUT) {
  695. cmd_send_messaging(argv[0], MESSAGING_WARNING, "i2c Bus is busy\n");
  696. } else {
  697. cmd_send_messaging(argv[0], MESSAGING_ERROR, "i2c Read failed\n");
  698. }
  699. // Don't stop the driver; our firmware may be using it for screen, etc
  700. // i2c_driver_delete(i2c_port);
  701. return 0;
  702. }
  703. #endif
  704. static int do_i2cget_cmd(int argc, char** argv) {
  705. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&i2cget_args);
  706. if (nerrors != 0) {
  707. return 1;
  708. }
  709. /* Check chip address: "-c" option */
  710. int chip_addr = i2cget_args.chip_address->ival[0];
  711. /* Check register address: "-r" option */
  712. int data_addr = -1;
  713. if (i2cget_args.register_address->count) {
  714. data_addr = i2cget_args.register_address->ival[0];
  715. }
  716. /* Check data length: "-l" option */
  717. int len = 1;
  718. if (i2cget_args.data_length->count) {
  719. len = i2cget_args.data_length->ival[0];
  720. }
  721. i2c_port_t loc_i2c_port = i2c_port;
  722. if (i2cset_args.port->count &&
  723. i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) {
  724. return 1;
  725. }
  726. char* buf = NULL;
  727. size_t buf_size = 0;
  728. FILE* f = open_memstream(&buf, &buf_size);
  729. if (f == NULL) {
  730. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n");
  731. return 1;
  732. }
  733. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  734. i2c_master_start(cmd);
  735. uint8_t* data = malloc_init_external(len);
  736. if (data_addr != -1) {
  737. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  738. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  739. i2c_master_start(cmd);
  740. }
  741. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  742. if (len > 1) {
  743. i2c_master_read(cmd, data, len - 1, ACK_VAL);
  744. }
  745. i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
  746. i2c_master_stop(cmd);
  747. esp_err_t ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 1000 / portTICK_RATE_MS);
  748. i2c_cmd_link_delete(cmd);
  749. if (ret == ESP_OK) {
  750. for (int i = 0; i < len; i++) {
  751. fprintf(f, "0x%02x ", data[i]);
  752. if ((i + 1) % 16 == 0) {
  753. fprintf(f, "\r\n");
  754. }
  755. }
  756. if (len % 16) {
  757. fprintf(f, "\r\n");
  758. }
  759. } else if (ret == ESP_ERR_TIMEOUT) {
  760. cmd_send_messaging(argv[0], MESSAGING_WARNING, "i2c Bus is busy\n");
  761. } else {
  762. cmd_send_messaging(argv[0], MESSAGING_ERROR, "i2c Read failed\n");
  763. }
  764. free(data);
  765. // Don't stop the driver; our firmware may be using it for screen, etc
  766. // i2c_driver_delete(i2c_port);
  767. fflush(f);
  768. cmd_send_messaging(argv[0], MESSAGING_INFO, "%s", buf);
  769. fclose(f);
  770. FREE_AND_NULL(buf);
  771. return 0;
  772. }
  773. esp_err_t cmd_i2ctools_scan_bus(FILE* f, int sda, int scl) {
  774. #ifdef CONFIG_WITH_CONFIG_UI
  775. uint8_t matches[128] = {};
  776. #endif
  777. int last_match = 0;
  778. esp_err_t ret = ESP_OK;
  779. if (!GPIO_IS_VALID_GPIO(scl) || !GPIO_IS_VALID_GPIO(sda)) {
  780. fprintf(f, "Invalid GPIO. Cannot scan bus\n");
  781. return 1;
  782. }
  783. // configure i2c
  784. i2c_config_t i2c_config = {
  785. .mode = I2C_MODE_MASTER,
  786. .sda_io_num = -1,
  787. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  788. .scl_io_num = -1,
  789. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  790. .master.clk_speed = 250000,
  791. };
  792. i2c_config.sda_io_num = sda;
  793. i2c_config.scl_io_num = scl;
  794. // we have an I2C configured
  795. i2c_port_t i2c_port = 0;
  796. // make sure that we don't have an i2c driver running
  797. i2c_driver_delete(i2c_port);
  798. ret = i2c_param_config(i2c_port, &i2c_config);
  799. if (ret != ESP_OK) {
  800. fprintf(f, "I2C Param Config failed %s\n", esp_err_to_name(ret));
  801. return ret;
  802. }
  803. ret = i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  804. if (ret != ESP_OK) {
  805. fprintf(f, "I2C driver install failed %s\n", esp_err_to_name(ret));
  806. return ret;
  807. }
  808. #ifndef CONFIG_WITH_CONFIG_UI
  809. fprintf(f, "\n 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  810. #endif
  811. for (int i = 0; i < 128; i++) {
  812. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  813. i2c_master_start(cmd);
  814. i2c_master_write_byte(cmd, (i << 1) | WRITE_BIT, ACK_CHECK_EN);
  815. i2c_master_stop(cmd);
  816. ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
  817. i2c_cmd_link_delete(cmd);
  818. if (ret == ESP_OK) {
  819. #ifndef CONFIG_WITH_CONFIG_UI
  820. fprintf(f, "%02x ", i);
  821. #else
  822. matches[++last_match - 1] = i;
  823. #endif
  824. }
  825. #ifndef CONFIG_WITH_CONFIG_UI
  826. else if (ret == ESP_ERR_TIMEOUT) {
  827. fprintf(f, "UU ");
  828. } else {
  829. fprintf(f, "-- ");
  830. }
  831. #endif
  832. }
  833. i2c_driver_delete(i2c_port);
  834. if (last_match) {
  835. #if CONFIG_WITH_CONFIG_UI
  836. fprintf(f, "i2c device detected (names provided by https://i2cdevices.org/addresses).\n");
  837. for (int i = 0; i < last_match; i++) {
  838. fprintf(f, "%u [%02xh]- %s\n", matches[i], matches[i], i2c_get_description(matches[i]));
  839. }
  840. #endif
  841. } else {
  842. fprintf(f, "No i2c devices found with scl-%d and sda-%d\n", scl, sda);
  843. }
  844. return 0;
  845. }
  846. static int do_i2cdetect_cmd(int argc, char** argv) {
  847. #ifdef CONFIG_WITH_CONFIG_UI
  848. uint8_t matches[128] = {};
  849. int last_match = 0;
  850. #endif
  851. esp_err_t ret = ESP_OK;
  852. i2c_port_t loc_i2c_port = i2c_port;
  853. // if (i2cset_args.port->count && i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) !=
  854. // ESP_OK) { return 1;
  855. // }
  856. int i2c_port;
  857. const i2c_config_t* i2c = config_i2c_get(&i2c_port);
  858. uint8_t address;
  859. char* buf = NULL;
  860. size_t buf_size = 0;
  861. FILE* f = open_memstream(&buf, &buf_size);
  862. if (f == NULL) {
  863. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Unable to open memory stream.");
  864. return 1;
  865. }
  866. if (!GPIO_IS_VALID_OUTPUT_GPIO(i2c->scl_io_num) || !GPIO_IS_VALID_GPIO(i2c->scl_io_num) ||
  867. !GPIO_IS_VALID_OUTPUT_GPIO(i2c->sda_io_num) || !GPIO_IS_VALID_GPIO(i2c->sda_io_num)) {
  868. fprintf(f, "I2C bus configuration invalid.\r\n");
  869. } else {
  870. fprintf(f, "\n 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  871. for (int i = 0; i < 128; i += 16) {
  872. fprintf(f, "%02x: ", i);
  873. for (int j = 0; j < 16; j++) {
  874. address = i + j;
  875. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  876. i2c_master_start(cmd);
  877. i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN);
  878. i2c_master_stop(cmd);
  879. ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 50 / portTICK_RATE_MS);
  880. i2c_cmd_link_delete(cmd);
  881. if (ret == ESP_OK) {
  882. fprintf(f, "%02x ", address);
  883. #ifdef CONFIG_WITH_CONFIG_UI
  884. matches[++last_match - 1] = address;
  885. #endif
  886. } else if (ret == ESP_ERR_TIMEOUT) {
  887. fprintf(f, "UU ");
  888. } else {
  889. fprintf(f, "-- ");
  890. }
  891. }
  892. fprintf(f, "\r\n");
  893. }
  894. #if CONFIG_WITH_CONFIG_UI
  895. if (last_match) {
  896. fprintf(f, "\r\n-----------------------------------------------------------------------"
  897. "-------------"
  898. "\r\nDetected the following devices (names provided by "
  899. "https://i2cdevices.org/addresses).");
  900. for (int i = 0; i < last_match; i++) {
  901. // printf("%02x = %s\r\n", matches[i], i2c_get_description(matches[i]));
  902. fprintf(f, "\r\n%u [%02xh]- %s", matches[i], matches[i],
  903. i2c_get_description(matches[i]));
  904. }
  905. fprintf(f, "\r\n-----------------------------------------------------------------------"
  906. "-------------\r\n");
  907. }
  908. #endif
  909. }
  910. fflush(f);
  911. cmd_send_messaging(argv[0], MESSAGING_INFO, "%s", buf);
  912. fclose(f);
  913. FREE_AND_NULL(buf);
  914. return 0;
  915. }
  916. cJSON* i2c_set_display_cb() {
  917. cJSON* values = cJSON_CreateObject();
  918. const display_config_t* conf = config_display_get();
  919. if (conf) {
  920. if (conf->width > 0) {
  921. cJSON_AddNumberToObject(values, "width", conf->width);
  922. }
  923. if (conf->height > 0) {
  924. cJSON_AddNumberToObject(values, "height", conf->height);
  925. }
  926. if (conf->address > 0) {
  927. cJSON_AddNumberToObject(values, "address", conf->address);
  928. }
  929. if (conf->RST_pin >= 0) {
  930. cJSON_AddNumberToObject(values, "reset", conf->RST_pin);
  931. }
  932. if (conf->drivername && strlen(conf->drivername) > 0) {
  933. cJSON_AddStringToObject(values, "driver", conf->drivername);
  934. }
  935. if (conf->CS_pin >= 0) {
  936. cJSON_AddNumberToObject(values, "cs", conf->CS_pin);
  937. }
  938. if (conf->speed > 0) {
  939. cJSON_AddNumberToObject(values, "speed", conf->speed);
  940. }
  941. if (conf->back >= 0) {
  942. cJSON_AddNumberToObject(values, "back", conf->back);
  943. }
  944. if (conf->depth > 0) {
  945. cJSON_AddNumberToObject(values, "depth", conf->depth);
  946. }
  947. if (conf->type && strlen(conf->type)) {
  948. cJSON_AddStringToObject(values, "type", conf->type);
  949. }
  950. cJSON_AddBoolToObject(values, "rotate", conf->rotate);
  951. cJSON_AddBoolToObject(values, "hf", conf->hflip);
  952. cJSON_AddBoolToObject(values, "vf", conf->vflip);
  953. cJSON_AddBoolToObject(values, "invert", conf->invert);
  954. if (conf->mode >= 0) {
  955. cJSON_AddNumberToObject(values, "mode", conf->mode);
  956. }
  957. }
  958. return values;
  959. }
  960. #if CONFIG_WITH_CONFIG_UI
  961. static void register_i2c_set_display() {
  962. char* supported_drivers = display_get_supported_drivers();
  963. i2cdisp_args.width = arg_int1("w", "width", "<n>", "Width");
  964. i2cdisp_args.height = arg_int1("h", "height", "<n>", "Height");
  965. i2cdisp_args.address = arg_int0("a", "address", "<n>", "I2C address (default 60)");
  966. i2cdisp_args.reset = arg_int0(NULL, "reset", "<n>", "Reset GPIO");
  967. i2cdisp_args.hflip = arg_lit0(NULL, "hf", "Flip horizontally");
  968. i2cdisp_args.vflip = arg_lit0(NULL, "vf", "Flip vertically");
  969. i2cdisp_args.driver =
  970. arg_str0("d", "driver", supported_drivers ? supported_drivers : "<string>", "Driver");
  971. i2cdisp_args.cs = arg_int0("b", "cs", "<n>", "SPI Only. CS GPIO (for SPI displays)");
  972. i2cdisp_args.speed = arg_int0("s", "speed", "<n>",
  973. "SPI Only. Bus Speed (Default 8000000). SPI interface can work up to 26MHz~40MHz");
  974. i2cdisp_args.back = arg_int0("b", "back", "<n>", "Backlight GPIO (if applicable)");
  975. i2cdisp_args.depth = arg_int0("p", "depth", "-1|1|4", "Bit Depth (only for SSD1326 displays)");
  976. i2cdisp_args.type = arg_str0("t", "type", "<I2C|SPI>", "Interface (default I2C)");
  977. i2cdisp_args.rotate = arg_lit0("r", "rotate", "Rotate 180 degrees");
  978. i2cdisp_args.invert = arg_lit0("i", "invert", "Invert colors");
  979. i2cdisp_args.clear = arg_lit0(NULL, "clear", "clear configuration and return");
  980. i2cdisp_args.mode = arg_int0("m", "mode", "<n>", "SPI Only. Transaction Line Mode (Default 0)");
  981. i2cdisp_args.end = arg_end(8);
  982. const esp_console_cmd_t i2c_set_display = {.command = CFG_TYPE_HW("display"),
  983. .help = desc_display,
  984. .hint = NULL,
  985. .func = &do_i2c_set_display,
  986. .argtable = &i2cdisp_args};
  987. cmd_to_json_with_cb(&i2c_set_display, &i2c_set_display_cb);
  988. ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_set_display));
  989. }
  990. #endif
  991. static void register_i2cdectect(void) {
  992. const esp_console_cmd_t i2cdetect_cmd = {.command = "i2cdetect",
  993. .help = "Scan I2C bus for devices",
  994. .hint = NULL,
  995. .func = &do_i2cdetect_cmd,
  996. .argtable = NULL};
  997. cmd_to_json(&i2cdetect_cmd);
  998. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdetect_cmd));
  999. }
  1000. static void register_i2cget(void) {
  1001. i2cget_args.chip_address =
  1002. arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  1003. i2cget_args.register_address = arg_int0(
  1004. "r", "register", "<register_addr>", "Specify the address on that chip to read from");
  1005. i2cget_args.data_length =
  1006. arg_int0("l", "length", "<length>", "Specify the length to read from that data address");
  1007. i2cget_args.end = arg_end(1);
  1008. const esp_console_cmd_t i2cget_cmd = {.command = "i2cget",
  1009. .help = "Read registers visible through the I2C bus",
  1010. .hint = NULL,
  1011. .func = &do_i2cget_cmd,
  1012. .argtable = &i2cget_args};
  1013. cmd_to_json(&i2cget_cmd);
  1014. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cget_cmd));
  1015. }
  1016. #if CONFIG_WITH_CONFIG_UI
  1017. static void register_i2cset(void) {
  1018. i2cset_args.chip_address =
  1019. arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  1020. i2cset_args.register_address = arg_int0(
  1021. "r", "register", "<register_addr>", "Specify the address on that chip to read from");
  1022. i2cset_args.data =
  1023. arg_intn(NULL, NULL, "<data>", 0, 256, "Specify the data to write to that data address");
  1024. i2cset_args.port = arg_intn("p", "port", "<n>", 0, 1, "Specify the i2c port (0|2)");
  1025. i2cset_args.end = arg_end(2);
  1026. const esp_console_cmd_t i2cset_cmd = {.command = "i2cset",
  1027. .help = "Set registers visible through the I2C bus",
  1028. .hint = NULL,
  1029. .func = &do_i2cset_cmd,
  1030. .argtable = &i2cset_args};
  1031. cmd_to_json(&i2cset_cmd);
  1032. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cset_cmd));
  1033. }
  1034. static void register_i2cdump(void) {
  1035. i2cdump_args.chip_address =
  1036. arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  1037. i2cdump_args.size = arg_int0("s", "size", "<size>", "Specify the size of each read");
  1038. i2cdump_args.end = arg_end(3);
  1039. const esp_console_cmd_t i2cdump_cmd = {.command = "i2cdump",
  1040. .help = "Examine registers visible through the I2C bus",
  1041. .hint = NULL,
  1042. .func = &do_i2cdump_cmd,
  1043. .argtable = &i2cdump_args};
  1044. cmd_to_json(&i2cdump_cmd);
  1045. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdump_cmd));
  1046. }
  1047. #endif
  1048. cJSON* i2config_cb() {
  1049. cJSON* values = cJSON_CreateObject();
  1050. int i2c_port;
  1051. const i2c_config_t* i2c = config_i2c_get(&i2c_port);
  1052. if (i2c->scl_io_num > 0) {
  1053. cJSON_AddNumberToObject(values, "scl", i2c->scl_io_num);
  1054. }
  1055. if (i2c->sda_io_num > 0) {
  1056. cJSON_AddNumberToObject(values, "sda", i2c->sda_io_num);
  1057. }
  1058. if (i2c->master.clk_speed > 0) {
  1059. cJSON_AddNumberToObject(values, "speed", i2c->master.clk_speed);
  1060. }
  1061. if (i2c_port >= 0) {
  1062. cJSON_AddNumberToObject(values, "port", i2c_port);
  1063. }
  1064. return values;
  1065. }
  1066. cJSON* spiconfig_cb() {
  1067. cJSON* values = cJSON_CreateObject();
  1068. const spi_bus_config_t* spi_config = config_spi_get(NULL);
  1069. if (spi_config->mosi_io_num > 0) {
  1070. cJSON_AddNumberToObject(values, "data", spi_config->mosi_io_num);
  1071. }
  1072. if (spi_config->sclk_io_num > 0) {
  1073. cJSON_AddNumberToObject(values, "clk", spi_config->sclk_io_num);
  1074. }
  1075. if (spi_system_dc_gpio > 0) {
  1076. cJSON_AddNumberToObject(values, "dc", spi_system_dc_gpio);
  1077. }
  1078. if (spi_system_host > 0) {
  1079. cJSON_AddNumberToObject(values, "host", spi_system_host);
  1080. }
  1081. return values;
  1082. }
  1083. #if CONFIG_WITH_CONFIG_UI
  1084. static void register_spiconfig(void) {
  1085. spiconfig_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  1086. spiconfig_args.clk = arg_int0("k", "clk", "<n>", "Clock GPIO");
  1087. spiconfig_args.data = arg_int0("d", "data", "<n>", "Data OUT GPIO");
  1088. spiconfig_args.miso = arg_int0("d", "miso", "<n>", "Data IN GPIO");
  1089. spiconfig_args.dc = arg_int0("c", "dc", "<n>", "DC GPIO");
  1090. spiconfig_args.host = arg_int0("h", "host", "1|2", "SPI Host Number");
  1091. spiconfig_args.end = arg_end(4);
  1092. const esp_console_cmd_t spiconfig_cmd = {.command = CFG_TYPE_HW("spi"),
  1093. .help = desc_spiconfig,
  1094. .hint = NULL,
  1095. .func = &do_spiconfig_cmd,
  1096. .argtable = &spiconfig_args};
  1097. cmd_to_json_with_cb(&spiconfig_cmd, &spiconfig_cb);
  1098. ESP_ERROR_CHECK(esp_console_cmd_register(&spiconfig_cmd));
  1099. }
  1100. #endif
  1101. static void register_i2cconfig(void) {
  1102. i2cconfig_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  1103. i2cconfig_args.port = arg_int0("p", "port", "0|1", "Port");
  1104. i2cconfig_args.freq = arg_int0("f", "speed", "int", "Frequency (Hz) e.g. 100000");
  1105. i2cconfig_args.sda = arg_int0("d", "sda", "<n>", "SDA GPIO. e.g. 19");
  1106. i2cconfig_args.scl = arg_int0("c", "scl", "<n>", "SCL GPIO. e.g. 18");
  1107. i2cconfig_args.end = arg_end(4);
  1108. const esp_console_cmd_t i2cconfig_cmd = {.command = CFG_TYPE_HW("i2c"),
  1109. .help = desc_i2c,
  1110. .hint = NULL,
  1111. .func = &do_i2cconfig_cmd,
  1112. .argtable = &i2cconfig_args};
  1113. cmd_to_json_with_cb(&i2cconfig_cmd, &i2config_cb);
  1114. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
  1115. }
  1116. void register_i2ctools(void) {
  1117. register_i2cconfig();
  1118. #if CONFIG_WITH_CONFIG_UI
  1119. register_spiconfig();
  1120. #endif
  1121. register_i2cdectect();
  1122. register_i2cget();
  1123. #if CONFIG_WITH_CONFIG_UI
  1124. register_i2cset();
  1125. register_i2cdump();
  1126. register_i2c_set_display();
  1127. #endif
  1128. }