2
0

cmd_i2ctools.c 44 KB

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