2
0

cmd_i2ctools.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  8. #include <stdio.h>
  9. #include "cmd_i2ctools.h"
  10. #include "argtable3/argtable3.h"
  11. #include "driver/i2c.h"
  12. #include "esp_console.h"
  13. #include "esp_log.h"
  14. #include "string.h"
  15. #include "stdio.h"
  16. #include "config.h"
  17. #include "accessors.h"
  18. #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
  19. #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
  20. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  21. #define READ_BIT I2C_MASTER_READ /*!< I2C master read */
  22. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  23. #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
  24. #define ACK_VAL 0x0 /*!< I2C ack value */
  25. #define NACK_VAL 0x1 /*!< I2C nack value */
  26. static const char *TAG = "cmd_i2ctools";
  27. static gpio_num_t i2c_gpio_sda = 19;
  28. static gpio_num_t i2c_gpio_scl = 18;
  29. static uint32_t i2c_frequency = 100000;
  30. #ifdef CONFIG_I2C_LOCKED
  31. static i2c_port_t i2c_port = I2C_NUM_1;
  32. #else
  33. static i2c_port_t i2c_port = I2C_NUM_0;
  34. #endif
  35. static struct {
  36. struct arg_int *chip_address;
  37. struct arg_int *register_address;
  38. struct arg_int *data_length;
  39. struct arg_end *end;
  40. } i2cget_args;
  41. static struct {
  42. struct arg_int *chip_address;
  43. struct arg_int *register_address;
  44. struct arg_int *data;
  45. struct arg_end *end;
  46. } i2cset_args;
  47. static struct {
  48. struct arg_int *chip_address;
  49. struct arg_int *size;
  50. struct arg_end *end;
  51. } i2cdump_args;
  52. static struct {
  53. struct arg_lit *load;
  54. struct arg_int *port;
  55. struct arg_int *freq;
  56. struct arg_int *sda;
  57. struct arg_int *scl;
  58. struct arg_end *end;
  59. } i2cconfig_args;
  60. static struct {
  61. struct arg_int *port;
  62. struct arg_end *end;
  63. } i2cstop_args;
  64. static struct {
  65. struct arg_int *port;
  66. struct arg_end *end;
  67. } i2ccheck_args;
  68. static struct {
  69. struct arg_lit *clear;
  70. struct arg_lit *hflip;
  71. struct arg_lit *vflip;
  72. struct arg_lit *rotate;
  73. struct arg_int *address;
  74. struct arg_int *width;
  75. struct arg_int *height;
  76. struct arg_str *name;
  77. struct arg_str *driver;
  78. struct arg_end *end;
  79. } i2cdisp_args;
  80. bool is_i2c_started(i2c_port_t port){
  81. esp_err_t ret = ESP_OK;
  82. ESP_LOGD(TAG,"Determining if i2c is started on port %u", port);
  83. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  84. ret = i2c_master_start(cmd);
  85. if(ret == ESP_OK){
  86. ret = i2c_master_write_byte(cmd,WRITE_BIT, ACK_CHECK_EN);
  87. }
  88. if(ret == ESP_OK){
  89. ret = i2c_master_stop(cmd);
  90. }
  91. if(ret == ESP_OK){
  92. ret = i2c_master_cmd_begin(port, cmd, 50 / portTICK_RATE_MS);
  93. }
  94. i2c_cmd_link_delete(cmd);
  95. ESP_LOGD(TAG,"i2c is %s. %s",ret!=ESP_ERR_INVALID_STATE?"started":"not started", esp_err_to_name(ret));
  96. return (ret!=ESP_ERR_INVALID_STATE);
  97. }
  98. typedef struct {
  99. uint8_t address;
  100. char * description;
  101. } i2c_db_t;
  102. // the list was taken from https://i2cdevices.org/addresses
  103. // on 2020-01-16
  104. static const i2c_db_t i2c_db[] = {
  105. { .address = 0x00, .description = "Unknown"},
  106. { .address = 0x01, .description = "Unknown"},
  107. { .address = 0x02, .description = "Unknown"},
  108. { .address = 0x03, .description = "Unknown"},
  109. { .address = 0x04, .description = "Unknown"},
  110. { .address = 0x05, .description = "Unknown"},
  111. { .address = 0x06, .description = "Unknown"},
  112. { .address = 0x07, .description = "Unknown"},
  113. { .address = 0x0c, .description = "AK8975"},
  114. { .address = 0x0d, .description = "AK8975"},
  115. { .address = 0x0e, .description = "MAG3110 AK8975 IST-8310"},
  116. { .address = 0x0f, .description = "AK8975"},
  117. { .address = 0x10, .description = "VEML7700 VML6075"},
  118. { .address = 0x11, .description = "Si4713 SAA5246 SAA5243P/K SAA5243P/L SAA5243P/E SAA5243P/H"},
  119. { .address = 0x13, .description = "VCNL40x0"},
  120. { .address = 0x18, .description = "MCP9808 LIS3DH LSM303"},
  121. { .address = 0x19, .description = "MCP9808 LIS3DH LSM303"},
  122. { .address = 0x1a, .description = "MCP9808"},
  123. { .address = 0x1b, .description = "MCP9808"},
  124. { .address = 0x1c, .description = "MCP9808 MMA845x FXOS8700"},
  125. { .address = 0x1d, .description = "MCP9808 MMA845x ADXL345 FXOS8700"},
  126. { .address = 0x1e, .description = "MCP9808 FXOS8700 HMC5883 LSM303 LSM303"},
  127. { .address = 0x1f, .description = "MCP9808 FXOS8700"},
  128. { .address = 0x20, .description = "FXAS21002 MCP23008 MCP23017 Chirp!"},
  129. { .address = 0x21, .description = "FXAS21002 MCP23008 MCP23017 SAA4700"},
  130. { .address = 0x22, .description = "MCP23008 MCP23017 PCA1070"},
  131. { .address = 0x23, .description = "MCP23008 MCP23017 SAA4700"},
  132. { .address = 0x24, .description = "MCP23008 MCP23017 PCD3311C PCD3312C"},
  133. { .address = 0x25, .description = "MCP23008 MCP23017 PCD3311C PCD3312C"},
  134. { .address = 0x26, .description = "MCP23008 MCP23017"},
  135. { .address = 0x27, .description = "MCP23008 MCP23017"},
  136. { .address = 0x28, .description = "BNO055 CAP1188"},
  137. { .address = 0x29, .description = "BNO055 CAP1188 TCS34725 TSL2591 VL53L0x VL6180X"},
  138. { .address = 0x2a, .description = "CAP1188"},
  139. { .address = 0x2b, .description = "CAP1188"},
  140. { .address = 0x2c, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"},
  141. { .address = 0x2d, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"},
  142. { .address = 0x2e, .description = "AD5248 AD5251 AD5252"},
  143. { .address = 0x2f, .description = "AD5248 AD5243 AD5251 AD5252"},
  144. { .address = 0x30, .description = "SAA2502"},
  145. { .address = 0x31, .description = "SAA2502"},
  146. { .address = 0x38, .description = "FT6x06 VEML6070 BMA150 SAA1064"},
  147. { .address = 0x39, .description = "TSL2561 APDS-9960 VEML6070 SAA1064"},
  148. { .address = 0x3a, .description = "PCF8577C SAA1064"},
  149. { .address = 0x3b, .description = "SAA1064 PCF8569"},
  150. { .address = 0x3c, .description = "SSD1305 SSD1306 PCF8578 PCF8569 SH1106"},
  151. { .address = 0x3d, .description = "SSD1305 SSD1306 PCF8578 SH1106"},
  152. { .address = 0x40, .description = "HTU21D-F TMP007 PCA9685 NE5751 TDA8421 INA260 TEA6320 TEA6330 TMP006 TEA6300 Si7021 INA219 TDA9860"},
  153. { .address = 0x41, .description = "TMP007 PCA9685 STMPE811 TDA8424 NE5751 TDA8421 INA260 STMPE610 TDA8425 TMP006 INA219 TDA9860 TDA8426"},
  154. { .address = 0x42, .description = "HDC1008 TMP007 TMP006 PCA9685 INA219 TDA8415 TDA8417 INA260"},
  155. { .address = 0x43, .description = "HDC1008 TMP007 TMP006 PCA9685 INA219 INA260"},
  156. { .address = 0x44, .description = "TMP007 TMP006 PCA9685 INA219 STMPE610 SHT31 ISL29125 STMPE811 TDA4688 TDA4672 TDA4780 TDA4670 TDA8442 TDA4687 TDA4671 TDA4680 INA260"},
  157. { .address = 0x45, .description = "TMP007 TMP006 PCA9685 INA219 SHT31 TDA8376 INA260"},
  158. { .address = 0x46, .description = "TMP007 TMP006 PCA9685 INA219 TDA9150 TDA8370 INA260"},
  159. { .address = 0x47, .description = "TMP007 TMP006 PCA9685 INA219 INA260"},
  160. { .address = 0x48, .description = "PCA9685 INA219 PN532 TMP102 INA260 ADS1115"},
  161. { .address = 0x49, .description = "TSL2561 PCA9685 INA219 TMP102 INA260 ADS1115 AS7262"},
  162. { .address = 0x4a, .description = "PCA9685 INA219 TMP102 ADS1115 MAX44009 INA260"},
  163. { .address = 0x4b, .description = "PCA9685 INA219 TMP102 ADS1115 MAX44009 INA260"},
  164. { .address = 0x4c, .description = "PCA9685 INA219 INA260"},
  165. { .address = 0x4d, .description = "PCA9685 INA219 INA260"},
  166. { .address = 0x4e, .description = "PCA9685 INA219 INA260"},
  167. { .address = 0x4f, .description = "PCA9685 INA219 INA260"},
  168. { .address = 0x50, .description = "PCA9685 MB85RC"},
  169. { .address = 0x51, .description = "PCA9685 MB85RC"},
  170. { .address = 0x52, .description = "PCA9685 MB85RC Nunchuck controller APDS-9250"},
  171. { .address = 0x53, .description = "ADXL345 PCA9685 MB85RC"},
  172. { .address = 0x54, .description = "PCA9685 MB85RC"},
  173. { .address = 0x55, .description = "PCA9685 MB85RC"},
  174. { .address = 0x56, .description = "PCA9685 MB85RC"},
  175. { .address = 0x57, .description = "PCA9685 MB85RC MAX3010x"},
  176. { .address = 0x58, .description = "PCA9685 TPA2016 SGP30"},
  177. { .address = 0x59, .description = "PCA9685"},
  178. { .address = 0x5a, .description = "PCA9685 CCS811 MLX90614 DRV2605 MPR121"},
  179. { .address = 0x5b, .description = "PCA9685 CCS811 MPR121"},
  180. { .address = 0x5c, .description = "PCA9685 AM2315 MPR121"},
  181. { .address = 0x5d, .description = "PCA9685 MPR121"},
  182. { .address = 0x5e, .description = "PCA9685"},
  183. { .address = 0x5f, .description = "PCA9685 HTS221"},
  184. { .address = 0x60, .description = "PCA9685 MPL115A2 MPL3115A2 Si5351A Si1145 MCP4725A0 TEA5767 TSA5511 SAB3037 SAB3035 MCP4725A1"},
  185. { .address = 0x61, .description = "PCA9685 Si5351A MCP4725A0 TEA6100 TSA5511 SAB3037 SAB3035 MCP4725A1"},
  186. { .address = 0x62, .description = "PCA9685 MCP4725A1 TSA5511 SAB3037 SAB3035 UMA1014T"},
  187. { .address = 0x63, .description = "Si4713 PCA9685 MCP4725A1 TSA5511 SAB3037 SAB3035 UMA1014T"},
  188. { .address = 0x64, .description = "PCA9685 MCP4725A2 MCP4725A1"},
  189. { .address = 0x65, .description = "PCA9685 MCP4725A2 MCP4725A1"},
  190. { .address = 0x66, .description = "PCA9685 MCP4725A3 IS31FL3731 MCP4725A1"},
  191. { .address = 0x67, .description = "PCA9685 MCP4725A3 MCP4725A1"},
  192. { .address = 0x68, .description = "PCA9685 AMG8833 DS1307 PCF8523 DS3231 MPU-9250 ITG3200 PCF8573 MPU6050"},
  193. { .address = 0x69, .description = "PCA9685 AMG8833 MPU-9250 ITG3200 PCF8573 SPS30 MPU6050"},
  194. { .address = 0x6a, .description = "PCA9685 L3GD20H PCF8573"},
  195. { .address = 0x6b, .description = "PCA9685 L3GD20H PCF8573"},
  196. { .address = 0x6c, .description = "PCA9685"},
  197. { .address = 0x6d, .description = "PCA9685"},
  198. { .address = 0x6e, .description = "PCA9685"},
  199. { .address = 0x6f, .description = "PCA9685"},
  200. { .address = 0x70, .description = "PCA9685 TCA9548 HT16K33"},
  201. { .address = 0x71, .description = "PCA9685 TCA9548 HT16K33"},
  202. { .address = 0x72, .description = "PCA9685 TCA9548 HT16K33"},
  203. { .address = 0x73, .description = "PCA9685 TCA9548 HT16K33"},
  204. { .address = 0x74, .description = "PCA9685 TCA9548 HT16K33"},
  205. { .address = 0x75, .description = "PCA9685 TCA9548 HT16K33"},
  206. { .address = 0x76, .description = "PCA9685 TCA9548 HT16K33 BME280 BMP280 MS5607 MS5611 BME680"},
  207. { .address = 0x77, .description = "PCA9685 TCA9548 HT16K33 IS31FL3731 BME280 BMP280 MS5607 BMP180 BMP085 BMA180 MS5611 BME680"},
  208. { .address = 0x78, .description = "PCA9685"},
  209. { .address = 0x79, .description = "PCA9685"},
  210. { .address = 0x7a, .description = "PCA9685"},
  211. { .address = 0x7b, .description = "PCA9685"},
  212. { .address = 0x7c, .description = "PCA9685"},
  213. { .address = 0x7d, .description = "PCA9685"},
  214. { .address = 0x7e, .description = "PCA9685"},
  215. { .address = 0x7f, .description = "PCA9685"},
  216. { .address = 0, .description = NULL}
  217. };
  218. void i2c_load_configuration(){
  219. ESP_LOGD(TAG,"Loading configuration from nvs");
  220. const i2c_config_t * conf = config_i2c_get((int *)&i2c_port);
  221. i2c_gpio_scl = conf->scl_io_num;
  222. i2c_gpio_sda = conf->sda_io_num;
  223. i2c_frequency = conf->master.clk_speed;
  224. }
  225. const char * i2c_get_description(uint8_t address){
  226. uint8_t i=0;
  227. while(i2c_db[i].description && i2c_db[i].address!=address) i++;
  228. return i2c_db[i].description?i2c_db[i].description:"Unlisted";
  229. }
  230. static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port)
  231. {
  232. if (port >= I2C_NUM_MAX) {
  233. ESP_LOGE(TAG, "Wrong port number: %d", port);
  234. return ESP_FAIL;
  235. }
  236. switch (port) {
  237. case 0:
  238. *i2c_port = I2C_NUM_0;
  239. break;
  240. case 1:
  241. *i2c_port = I2C_NUM_1;
  242. break;
  243. default:
  244. *i2c_port = I2C_NUM_0;
  245. break;
  246. }
  247. return ESP_OK;
  248. }
  249. static esp_err_t i2c_master_driver_install(){
  250. esp_err_t err=ESP_OK;
  251. ESP_LOGD(TAG,"Installing i2c driver on port %u", i2c_port);
  252. if((err=i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0))!=ESP_OK){
  253. ESP_LOGE(TAG,"Driver install failed! %s", esp_err_to_name(err));
  254. }
  255. return err;
  256. }
  257. static esp_err_t i2c_master_driver_initialize()
  258. {
  259. esp_err_t err=ESP_OK;
  260. i2c_config_t conf = {
  261. .mode = I2C_MODE_MASTER,
  262. .sda_io_num = i2c_gpio_sda,
  263. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  264. .scl_io_num = i2c_gpio_scl,
  265. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  266. .master.clk_speed = i2c_frequency
  267. };
  268. ESP_LOGI(TAG,"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", i2c_port, i2c_gpio_sda,i2c_gpio_scl,i2c_frequency);
  269. if((err=i2c_param_config(i2c_port, &conf))!=ESP_OK){
  270. ESP_LOGE(TAG,"i2c driver config load failed. %s", esp_err_to_name(err));
  271. }
  272. return err;
  273. }
  274. esp_err_t i2c_initialize_driver_from_config(){
  275. esp_err_t err = ESP_OK;
  276. ESP_LOGD(TAG,"Initializing driver from configuration.");
  277. i2c_load_configuration();
  278. if(is_i2c_started(i2c_port)){
  279. ESP_LOGW(TAG, "Stopping i2c driver on port %u", i2c_port);
  280. // stop the current driver instance
  281. if((err=i2c_driver_delete(i2c_port))!=ESP_OK){
  282. ESP_LOGE(TAG,"i2c driver delete failed. %s", esp_err_to_name(err));
  283. }
  284. }
  285. if(err==ESP_OK){
  286. err = i2c_master_driver_initialize();
  287. }
  288. if(err == ESP_OK){
  289. err = i2c_master_driver_install();
  290. }
  291. return err;
  292. }
  293. static int do_i2c_stop(int argc, char **argv ){
  294. int nerrors = arg_parse(argc, argv, (void **)&i2cstop_args);
  295. if (nerrors != 0) {
  296. arg_print_errors(stderr, i2cstop_args.end, argv[0]);
  297. return 0;
  298. }
  299. if (i2cstop_args.port->count && i2c_get_port(i2cstop_args.port->ival[0], &i2c_port) != ESP_OK) {
  300. return 1;
  301. }
  302. ESP_LOGW(TAG,"Stopping i2c on port %u.",i2c_port);
  303. i2c_driver_delete(i2c_port);
  304. return 0;
  305. }
  306. static int do_i2c_check(int argc, char **argv ){
  307. i2c_port_t port=0;
  308. int nerrors = arg_parse(argc, argv, (void **)&i2ccheck_args);
  309. if (nerrors != 0) {
  310. arg_print_errors(stderr, i2ccheck_args.end, argv[0]);
  311. return 0;
  312. }
  313. port=i2c_port;
  314. if (i2ccheck_args.port->count && i2c_get_port(i2ccheck_args.port->ival[0], &port) != ESP_OK) {
  315. return 1;
  316. }
  317. bool started=is_i2c_started(port);
  318. ESP_LOGI(TAG,"i2c is %s on port %u.", started?"started":"not started",port );
  319. return 0;
  320. }
  321. static int do_i2c_show_display(int argc, char **argv){
  322. char * config_string = (char * )config_alloc_get(NVS_TYPE_STR, "display_config") ;
  323. if(config_string){
  324. ESP_LOGI(TAG,"Display configuration string is : \n"
  325. "display_config = \"%s\"",config_string);
  326. free(config_string);
  327. }
  328. else {
  329. ESP_LOGW(TAG,"No display configuration found in nvs config display_config");
  330. }
  331. char * nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  332. if (nvs_item) {
  333. ESP_LOGI(TAG,"I2C configuration is: %s", nvs_item);
  334. free(nvs_item);
  335. }
  336. return 0;
  337. }
  338. static int do_i2c_set_display(int argc, char **argv)
  339. {
  340. int width=0, height=0, address=60;
  341. char * name = NULL;
  342. char * driver= NULL;
  343. char config_string[200]={};
  344. int nerrors = arg_parse(argc, argv, (void **)&i2cdisp_args);
  345. if (nerrors != 0) {
  346. arg_print_errors(stderr, i2cdisp_args.end, argv[0]);
  347. return 0;
  348. }
  349. /* Check "--clear" option */
  350. if (i2cdisp_args.clear->count) {
  351. ESP_LOGW(TAG,"Clearing display config");
  352. config_set_value(NVS_TYPE_STR, "display_config", "");
  353. return 0;
  354. }
  355. /* Check "--address" option */
  356. if (i2cdisp_args.address->count) {
  357. address=i2cdisp_args.address->ival[0];
  358. }
  359. /* Check "--width" option */
  360. if (i2cdisp_args.width->count) {
  361. width=i2cdisp_args.width->ival[0];
  362. }
  363. else {
  364. ESP_LOGE(TAG,"Missing parameter: --width");
  365. nerrors ++;
  366. }
  367. /* Check "--height" option */
  368. if (i2cdisp_args.height->count) {
  369. height=i2cdisp_args.height->ival[0];
  370. }
  371. else {
  372. ESP_LOGE(TAG,"Missing parameter: --height");
  373. nerrors ++;
  374. }
  375. /* Check "--name" option */
  376. if (i2cdisp_args.name->count) {
  377. name=strdup(i2cdisp_args.name->sval[0]);
  378. }
  379. /* Check "--driver" option */
  380. if (i2cdisp_args.driver->count) {
  381. driver=strdup(i2cdisp_args.driver->sval[0]);
  382. }
  383. if(!name) name = strdup("I2C");
  384. if(!driver) driver = strdup("SSD1306");
  385. bool rotate = i2cdisp_args.rotate->count>0;
  386. snprintf(config_string, sizeof(config_string),"%s:width=%i,height=%i,address=%i,driver=%s%s%s",
  387. name,width,height,address,driver,rotate || i2cdisp_args.hflip->count?",HFlip":"",rotate || i2cdisp_args.vflip->count?",VFlip":"" );
  388. free(name);
  389. free(driver);
  390. if(nerrors!=0){
  391. return 0;
  392. }
  393. ESP_LOGI(TAG,"Updating display configuration string configuration to :\n"
  394. "display_config = \"%s\"",config_string );
  395. return config_set_value(NVS_TYPE_STR, "display_config", config_string)!=ESP_OK;
  396. }
  397. static int do_i2cconfig_cmd(int argc, char **argv)
  398. {
  399. esp_err_t err=ESP_OK;
  400. int res=0;
  401. int nerrors = arg_parse(argc, argv, (void **)&i2cconfig_args);
  402. if (nerrors != 0) {
  403. arg_print_errors(stderr, i2cconfig_args.end, argv[0]);
  404. return 0;
  405. }
  406. /* Check "--load" option */
  407. if (i2cconfig_args.load->count) {
  408. ESP_LOGW(TAG,"Loading i2c config");
  409. i2c_load_configuration();
  410. }
  411. else {
  412. /* Check "--port" option */
  413. if (i2cconfig_args.port->count) {
  414. if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) {
  415. return 1;
  416. }
  417. }
  418. /* Check "--freq" option */
  419. if (i2cconfig_args.freq->count) {
  420. i2c_frequency = i2cconfig_args.freq->ival[0];
  421. }
  422. if (i2cconfig_args.sda->count){
  423. /* Check "--sda" option */
  424. i2c_gpio_sda = i2cconfig_args.sda->ival[0];
  425. }
  426. else {
  427. ESP_LOGE(TAG,"Missing --sda option.");
  428. res=1;
  429. }
  430. if (i2cconfig_args.scl->count){
  431. /* Check "--sda" option */
  432. i2c_gpio_scl = i2cconfig_args.scl->ival[0];
  433. }
  434. else {
  435. ESP_LOGE(TAG,"Missing --scl option.");
  436. res=1;
  437. }
  438. }
  439. #ifdef CONFIG_SQUEEZEAMP
  440. if (i2c_port == I2C_NUM_0) {
  441. i2c_port = I2C_NUM_1;
  442. ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP. Changing to port 1.");
  443. }
  444. #endif
  445. if(!res){
  446. ESP_LOGI(TAG, "Uninstall i2c driver from port %u if needed",i2c_port);
  447. if(is_i2c_started(i2c_port)){
  448. if((err=i2c_driver_delete(i2c_port))!=ESP_OK){
  449. ESP_LOGE(TAG,"i2c driver delete failed. %s", esp_err_to_name(err));
  450. res = 1;
  451. }
  452. }
  453. }
  454. if(!res){
  455. ESP_LOGI(TAG,"Initializing driver with config scl=%u sda=%u speed=%u port=%u",i2c_gpio_scl,i2c_gpio_sda,i2c_frequency,i2c_port);
  456. if((err=i2c_master_driver_initialize())==ESP_OK){
  457. ESP_LOGI(TAG,"Initalize success.");
  458. // now start the i2c driver
  459. ESP_LOGI(TAG,"Starting the i2c driver.");
  460. if((err=i2c_master_driver_install())!=ESP_OK){
  461. res=1;
  462. }
  463. }
  464. else {
  465. ESP_LOGE(TAG,"I2C initialization failed. %s", esp_err_to_name(err));
  466. res=1;
  467. }
  468. }
  469. if(!res && !i2cconfig_args.load->count){
  470. ESP_LOGI(TAG,"Storing i2c parameters.");
  471. i2c_config_t config={
  472. .mode = I2C_MODE_MASTER,
  473. .sda_io_num = i2c_gpio_sda,
  474. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  475. .scl_io_num = i2c_gpio_scl,
  476. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  477. .master.clk_speed = i2c_frequency
  478. };
  479. config_i2c_set(&config, i2c_port);
  480. }
  481. return res;
  482. }
  483. #define RUN_SHOW_ERROR(c)
  484. static int do_i2cdump_cmd(int argc, char **argv)
  485. {
  486. int nerrors = arg_parse(argc, argv, (void **)&i2cdump_args);
  487. if (nerrors != 0) {
  488. arg_print_errors(stderr, i2cdump_args.end, argv[0]);
  489. return 0;
  490. }
  491. /* Check chip address: "-c" option */
  492. int chip_addr = i2cdump_args.chip_address->ival[0];
  493. /* Check read size: "-s" option */
  494. int size = 1;
  495. if (i2cdump_args.size->count) {
  496. size = i2cdump_args.size->ival[0];
  497. }
  498. if (size != 1 && size != 2 && size != 4) {
  499. ESP_LOGE(TAG, "Wrong read size. Only support 1,2,4");
  500. return 1;
  501. }
  502. esp_err_t ret = i2c_initialize_driver_from_config();
  503. if(ret!=ESP_OK) return 0;
  504. uint8_t data_addr;
  505. uint8_t data[4];
  506. int32_t block[16];
  507. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
  508. " 0123456789abcdef\r\n");
  509. for (int i = 0; i < 128; i += 16) {
  510. printf("%02x: ", i);
  511. for (int j = 0; j < 16; j += size) {
  512. fflush(stdout);
  513. data_addr = i + j;
  514. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  515. i2c_master_start(cmd);
  516. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  517. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  518. i2c_master_start(cmd);
  519. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  520. if (size > 1) {
  521. i2c_master_read(cmd, data, size - 1, ACK_VAL);
  522. }
  523. i2c_master_read_byte(cmd, data + size - 1, NACK_VAL);
  524. i2c_master_stop(cmd);
  525. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
  526. i2c_cmd_link_delete(cmd);
  527. if (ret == ESP_OK) {
  528. for (int k = 0; k < size; k++) {
  529. printf("%02x ", data[k]);
  530. block[j + k] = data[k];
  531. }
  532. } else {
  533. for (int k = 0; k < size; k++) {
  534. printf("XX ");
  535. block[j + k] = -1;
  536. }
  537. }
  538. }
  539. printf(" ");
  540. for (int k = 0; k < 16; k++) {
  541. if (block[k] < 0) {
  542. printf("X");
  543. }
  544. if ((block[k] & 0xff) == 0x00 || (block[k] & 0xff) == 0xff) {
  545. printf(".");
  546. } else if ((block[k] & 0xff) < 32 || (block[k] & 0xff) >= 127) {
  547. printf("?");
  548. } else {
  549. printf("%c", block[k] & 0xff);
  550. }
  551. }
  552. printf("\r\n");
  553. }
  554. // Don't stop the driver; our firmware may be using it for screen, etc
  555. //i2c_driver_delete(i2c_port);
  556. return 0;
  557. }
  558. static int do_i2cset_cmd(int argc, char **argv)
  559. {
  560. int nerrors = arg_parse(argc, argv, (void **)&i2cset_args);
  561. if (nerrors != 0) {
  562. arg_print_errors(stderr, i2cset_args.end, argv[0]);
  563. return 0;
  564. }
  565. /* Check chip address: "-c" option */
  566. int chip_addr = i2cset_args.chip_address->ival[0];
  567. /* Check register address: "-r" option */
  568. int data_addr = 0;
  569. if (i2cset_args.register_address->count) {
  570. data_addr = i2cset_args.register_address->ival[0];
  571. }
  572. /* Check data: "-d" option */
  573. int len = i2cset_args.data->count;
  574. i2c_master_driver_initialize();
  575. if(i2c_master_driver_install()!=ESP_OK){
  576. return 1;
  577. }
  578. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  579. i2c_master_start(cmd);
  580. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  581. if (i2cset_args.register_address->count) {
  582. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  583. }
  584. for (int i = 0; i < len; i++) {
  585. i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN);
  586. }
  587. i2c_master_stop(cmd);
  588. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  589. i2c_cmd_link_delete(cmd);
  590. if (ret == ESP_OK) {
  591. ESP_LOGI(TAG, "Write OK");
  592. } else if (ret == ESP_ERR_TIMEOUT) {
  593. ESP_LOGW(TAG, "Bus is busy");
  594. } else {
  595. ESP_LOGW(TAG, "Write Failed");
  596. }
  597. // Don't stop the driver; our firmware may be using it for screen, etc
  598. //i2c_driver_delete(i2c_port);
  599. return 0;
  600. }
  601. static int do_i2cget_cmd(int argc, char **argv)
  602. {
  603. esp_err_t err=ESP_OK;
  604. int nerrors = arg_parse(argc, argv, (void **)&i2cget_args);
  605. if (nerrors != 0) {
  606. arg_print_errors(stderr, i2cget_args.end, argv[0]);
  607. return 0;
  608. }
  609. /* Check chip address: "-c" option */
  610. int chip_addr = i2cget_args.chip_address->ival[0];
  611. /* Check register address: "-r" option */
  612. int data_addr = -1;
  613. if (i2cget_args.register_address->count) {
  614. data_addr = i2cget_args.register_address->ival[0];
  615. }
  616. /* Check data length: "-l" option */
  617. int len = 1;
  618. if (i2cget_args.data_length->count) {
  619. len = i2cget_args.data_length->ival[0];
  620. }
  621. if((err=i2c_master_driver_initialize())!=ESP_OK){
  622. ESP_LOGE(TAG,"Error initializing i2c driver. %s",esp_err_to_name(err));
  623. return 1;
  624. }
  625. if((err=i2c_master_driver_install())!=ESP_OK){
  626. return 1;
  627. }
  628. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  629. i2c_master_start(cmd);
  630. uint8_t *data = malloc(len);
  631. if (data_addr != -1) {
  632. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  633. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  634. i2c_master_start(cmd);
  635. }
  636. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  637. if (len > 1) {
  638. i2c_master_read(cmd, data, len - 1, ACK_VAL);
  639. }
  640. i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
  641. i2c_master_stop(cmd);
  642. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  643. i2c_cmd_link_delete(cmd);
  644. if (ret == ESP_OK) {
  645. for (int i = 0; i < len; i++) {
  646. printf("0x%02x ", data[i]);
  647. if ((i + 1) % 16 == 0) {
  648. printf("\r\n");
  649. }
  650. }
  651. if (len % 16) {
  652. printf("\r\n");
  653. }
  654. } else if (ret == ESP_ERR_TIMEOUT) {
  655. ESP_LOGW(TAG, "Bus is busy");
  656. } else {
  657. ESP_LOGW(TAG, "Read failed");
  658. }
  659. free(data);
  660. // Don't stop the driver; our firmware may be using it for screen, etc
  661. //i2c_driver_delete(i2c_port);
  662. return 0;
  663. }
  664. static int do_i2cdetect_cmd(int argc, char **argv)
  665. {
  666. uint8_t matches[128]={};
  667. int last_match=0;
  668. esp_err_t ret = i2c_initialize_driver_from_config();
  669. if(ret!=ESP_OK) return 0;
  670. uint8_t address;
  671. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  672. for (int i = 0; i < 128 ; i += 16) {
  673. printf("%02x: ", i);
  674. for (int j = 0; j < 16 ; j++) {
  675. fflush(stdout);
  676. address = i + j;
  677. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  678. i2c_master_start(cmd);
  679. i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN);
  680. i2c_master_stop(cmd);
  681. ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
  682. i2c_cmd_link_delete(cmd);
  683. if (ret == ESP_OK) {
  684. printf("%02x ", address);
  685. matches[++last_match-1] = address;
  686. } else if (ret == ESP_ERR_TIMEOUT) {
  687. printf("UU ");
  688. } else {
  689. printf("-- ");
  690. }
  691. }
  692. printf("\r\n");
  693. }
  694. if(last_match) {
  695. printf("\r\n------------------------------------------------------------------------------------"
  696. "\r\nDetected the following devices (names provided by https://i2cdevices.org/addresses).");
  697. for(int i=0;i<last_match;i++){
  698. //printf("%02x = %s\r\n", matches[i], i2c_get_description(matches[i]));
  699. printf("\r\n%u [%02xh]- %s", matches[i], matches[i], i2c_get_description(matches[i]));
  700. }
  701. printf("\r\n------------------------------------------------------------------------------------\r\n");
  702. }
  703. return 0;
  704. }
  705. static void register_i2c_set_display(){
  706. i2cdisp_args.address = arg_int0("a", "address", "<n>", "Set the device address, default 60");
  707. i2cdisp_args.width = arg_int0("w", "width", "<n>", "Set the display width");
  708. i2cdisp_args.height = arg_int0("h", "height", "<n>", "Set the display height");
  709. i2cdisp_args.name = arg_str0("t", "type", "<I2C|SPI>", "Set the display type. default I2C");
  710. i2cdisp_args.driver = arg_str0("d", "driver", "<string>", "Set the display driver name. Default SSD1306");
  711. i2cdisp_args.clear = arg_litn(NULL, "clear", 0, 1, "clear configuration and return");
  712. i2cdisp_args.hflip = arg_litn(NULL, "hf", 0, 1, "Flip picture horizontally");
  713. i2cdisp_args.vflip = arg_litn(NULL, "vf", 0, 1, "Flip picture vertically");
  714. i2cdisp_args.rotate = arg_litn("r", "rotate", 0, 1, "Rotate the picture 180 deg");
  715. i2cdisp_args.end = arg_end(8);
  716. const esp_console_cmd_t i2c_set_display= {
  717. .command = "setdisplay",
  718. .help="Sets the display options for the board",
  719. .hint = NULL,
  720. .func = &do_i2c_set_display,
  721. .argtable = &i2cdisp_args
  722. };
  723. const esp_console_cmd_t i2c_show_display= {
  724. .command = "getdisplay",
  725. .help="Shows display options and global i2c configuration",
  726. .hint = NULL,
  727. .func = &do_i2c_show_display,
  728. .argtable = NULL
  729. };
  730. ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_set_display));
  731. ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_show_display));
  732. }
  733. static void register_i2cdectect(void)
  734. {
  735. const esp_console_cmd_t i2cdetect_cmd = {
  736. .command = "i2cdetect",
  737. .help = "Scan I2C bus for devices",
  738. .hint = NULL,
  739. .func = &do_i2cdetect_cmd,
  740. .argtable = NULL
  741. };
  742. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdetect_cmd));
  743. }
  744. static void register_i2cget(void)
  745. {
  746. i2cget_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  747. i2cget_args.register_address = arg_int0("r", "register", "<register_addr>", "Specify the address on that chip to read from");
  748. i2cget_args.data_length = arg_int0("l", "length", "<length>", "Specify the length to read from that data address");
  749. i2cget_args.end = arg_end(1);
  750. const esp_console_cmd_t i2cget_cmd = {
  751. .command = "i2cget",
  752. .help = "Read registers visible through the I2C bus",
  753. .hint = NULL,
  754. .func = &do_i2cget_cmd,
  755. .argtable = &i2cget_args
  756. };
  757. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cget_cmd));
  758. }
  759. static void register_i2cset(void)
  760. {
  761. i2cset_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  762. i2cset_args.register_address = arg_int0("r", "register", "<register_addr>", "Specify the address on that chip to read from");
  763. i2cset_args.data = arg_intn(NULL, NULL, "<data>", 0, 256, "Specify the data to write to that data address");
  764. i2cset_args.end = arg_end(2);
  765. const esp_console_cmd_t i2cset_cmd = {
  766. .command = "i2cset",
  767. .help = "Set registers visible through the I2C bus",
  768. .hint = NULL,
  769. .func = &do_i2cset_cmd,
  770. .argtable = &i2cset_args
  771. };
  772. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cset_cmd));
  773. }
  774. static void register_i2cdump(void)
  775. {
  776. i2cdump_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  777. i2cdump_args.size = arg_int0("s", "size", "<size>", "Specify the size of each read");
  778. i2cdump_args.end = arg_end(3);
  779. const esp_console_cmd_t i2cdump_cmd = {
  780. .command = "i2cdump",
  781. .help = "Examine registers visible through the I2C bus",
  782. .hint = NULL,
  783. .func = &do_i2cdump_cmd,
  784. .argtable = &i2cdump_args
  785. };
  786. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdump_cmd));
  787. }
  788. static void register_i2ccheck(){
  789. i2ccheck_args.port = arg_int0("p", "port", "<0|1>", "Set the I2C bus port number");
  790. i2ccheck_args.end = arg_end(2);
  791. const esp_console_cmd_t cmd = {
  792. .command = "i2ccheck",
  793. .help = "Check if the I2C bus is installed",
  794. .hint = NULL,
  795. .func = &do_i2c_check,
  796. .argtable = &i2ccheck_args
  797. };
  798. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  799. }
  800. static void register_i2cstop(){
  801. i2cstop_args.port = arg_int0("p", "port", "<0|1>", "Set the I2C bus port number");
  802. i2cstop_args.end = arg_end(2);
  803. const esp_console_cmd_t i2cconfig_cmd = {
  804. .command = "i2cstop",
  805. .help = "Stop the I2C bus",
  806. .hint = NULL,
  807. .func = &do_i2c_stop,
  808. .argtable = &i2cstop_args
  809. };
  810. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
  811. }
  812. static void register_i2cconfig(void)
  813. {
  814. i2cconfig_args.port = arg_int0("p", "port", "<0|1>", "Set the I2C bus port number");
  815. i2cconfig_args.freq = arg_int0("f", "freq", "<Hz>", "Set the frequency(Hz) of I2C bus. e.g. 100000");
  816. i2cconfig_args.sda = arg_int0("d", "sda", "<gpio>", "Set the gpio for I2C SDA. e.g. 19");
  817. i2cconfig_args.scl = arg_int0("c", "scl", "<gpio>", "Set the gpio for I2C SCL. e.g. 18");
  818. i2cconfig_args.load = arg_litn("l", "load", 0, 1, "load existing configuration and return");
  819. i2cconfig_args.end = arg_end(4);
  820. const esp_console_cmd_t i2cconfig_cmd = {
  821. .command = "i2cconfig",
  822. .help = "Config I2C bus",
  823. .hint = NULL,
  824. .func = &do_i2cconfig_cmd,
  825. .argtable = &i2cconfig_args
  826. };
  827. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
  828. }
  829. void register_i2ctools(void)
  830. {
  831. register_i2cconfig();
  832. register_i2cdectect();
  833. register_i2cget();
  834. register_i2cset();
  835. register_i2cdump();
  836. register_i2c_set_display();
  837. register_i2cstop();
  838. register_i2ccheck();
  839. }