cmd_i2ctools.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 <stdio.h>
  8. #include "cmd_i2ctools.h"
  9. #include "argtable3/argtable3.h"
  10. #include "driver/i2c.h"
  11. #include "esp_console.h"
  12. #include "esp_log.h"
  13. #include "string.h"
  14. #include "stdio.h"
  15. #include "config.h"
  16. #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
  17. #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
  18. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  19. #define READ_BIT I2C_MASTER_READ /*!< I2C master read */
  20. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  21. #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
  22. #define ACK_VAL 0x0 /*!< I2C ack value */
  23. #define NACK_VAL 0x1 /*!< I2C nack value */
  24. static const char *TAG = "cmd_i2ctools";
  25. static gpio_num_t i2c_gpio_sda = 19;
  26. static gpio_num_t i2c_gpio_scl = 18;
  27. static uint32_t i2c_frequency = 100000;
  28. #ifdef CONFIG_SQUEEZEAMP
  29. static i2c_port_t i2c_port = I2C_NUM_1;
  30. #else
  31. static i2c_port_t i2c_port = I2C_NUM_0;
  32. #endif
  33. static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port)
  34. {
  35. if (port >= I2C_NUM_MAX) {
  36. ESP_LOGE(TAG, "Wrong port number: %d", port);
  37. return ESP_FAIL;
  38. }
  39. switch (port) {
  40. case 0:
  41. *i2c_port = I2C_NUM_0;
  42. break;
  43. case 1:
  44. *i2c_port = I2C_NUM_1;
  45. break;
  46. default:
  47. *i2c_port = I2C_NUM_0;
  48. break;
  49. }
  50. return ESP_OK;
  51. }
  52. static esp_err_t i2c_master_driver_install(){
  53. esp_err_t err=ESP_OK;
  54. if((err=i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0))!=ESP_OK){
  55. ESP_LOGW(TAG,"i2c driver was already installed. Deleting it.");
  56. i2c_driver_delete(i2c_port);
  57. if((err=i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0))!=ESP_OK){
  58. ESP_LOGE(TAG,"Driver install failed. %s", esp_err_to_name(err));
  59. }
  60. }
  61. return err;
  62. }
  63. static esp_err_t i2c_master_driver_initialize()
  64. {
  65. esp_err_t err=ESP_OK;
  66. i2c_config_t conf = {
  67. .mode = I2C_MODE_MASTER,
  68. .sda_io_num = i2c_gpio_sda,
  69. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  70. .scl_io_num = i2c_gpio_scl,
  71. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  72. .master.clk_speed = i2c_frequency
  73. };
  74. ESP_LOGI(TAG,"Initializing i2c driver.\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);
  75. if((err=i2c_param_config(i2c_port, &conf))!=ESP_OK){
  76. ESP_LOGE(TAG,"i2c driver initialized failed. %s", esp_err_to_name(err));
  77. }
  78. return err;
  79. }
  80. static struct {
  81. struct arg_lit *load;
  82. struct arg_int *port;
  83. struct arg_int *freq;
  84. struct arg_int *sda;
  85. struct arg_int *scl;
  86. struct arg_end *end;
  87. } i2cconfig_args;
  88. static struct {
  89. struct arg_lit *clear;
  90. struct arg_int *address;
  91. struct arg_int *width;
  92. struct arg_int *height;
  93. struct arg_str *name;
  94. struct arg_str *driver;
  95. struct arg_end *end;
  96. } i2cdisp_args;
  97. static struct {
  98. struct arg_end *end;
  99. } i2cdisp_show_args;
  100. static int do_i2c_show_display(int argc, char **argv){
  101. char * config_string = (char * )config_alloc_get(NVS_TYPE_STR, "display_config") ;
  102. if(config_string){
  103. ESP_LOGI(TAG,"Display configuration string is : \n"
  104. "display_config = \"%s\"",config_string);
  105. free(config_string);
  106. }
  107. else {
  108. ESP_LOGW(TAG,"No display configuration found in nvs config display_config");
  109. }
  110. char * nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  111. if (nvs_item) {
  112. ESP_LOGI(TAG,"I2C configuration is: %s", nvs_item);
  113. free(nvs_item);
  114. }
  115. return 0;
  116. }
  117. static int do_i2c_set_display(int argc, char **argv)
  118. {
  119. int width=0, height=0, address=120;
  120. char * name = strdup("I2S");
  121. char * driver= strdup("SSD136");
  122. char config_string[200]={};
  123. int nerrors = arg_parse(argc, argv, (void **)&i2cdisp_args);
  124. if (nerrors != 0) {
  125. arg_print_errors(stderr, i2cdisp_args.end, argv[0]);
  126. return 0;
  127. }
  128. /* Check "--clear" option */
  129. if (i2cdisp_args.clear->count) {
  130. ESP_LOGW(TAG,"Clearing display config");
  131. config_set_value(NVS_TYPE_STR, "display_config", "");
  132. return 0;
  133. }
  134. /* Check "--address" option */
  135. if (i2cdisp_args.address->count) {
  136. address=i2cdisp_args.address->ival[0];
  137. }
  138. /* Check "--width" option */
  139. if (i2cdisp_args.width->count) {
  140. width=i2cdisp_args.width->ival[0];
  141. }
  142. else {
  143. ESP_LOGE(TAG,"Missing parameter: --width");
  144. nerrors ++;
  145. }
  146. /* Check "--height" option */
  147. if (i2cdisp_args.height->count) {
  148. height=i2cdisp_args.height->ival[0];
  149. }
  150. else {
  151. ESP_LOGE(TAG,"Missing parameter: --height");
  152. nerrors ++;
  153. }
  154. /* Check "--name" option */
  155. if (i2cdisp_args.name->count) {
  156. free(name);
  157. name=strdup(i2cdisp_args.name->sval[0]);
  158. }
  159. /* Check "--driver" option */
  160. if (i2cdisp_args.driver->count) {
  161. free(driver);
  162. driver=strdup(i2cdisp_args.driver->sval[0]);
  163. }
  164. snprintf(config_string, sizeof(config_string),"%s:width=%i,height=%i,address=%i,driver=%s",name,width,height,address,driver );
  165. free(name);
  166. free(driver);
  167. if(nerrors!=0){
  168. return 0;
  169. }
  170. ESP_LOGI(TAG,"Updating display configuration string configuration to :\n"
  171. "display_config = \"%s\"",config_string );
  172. return config_set_value(NVS_TYPE_STR, "display_config", config_string)!=ESP_OK;
  173. }
  174. static void register_i2c_set_display(){
  175. i2cdisp_args.address = arg_int0(NULL, "address", "<n>", "Set the I2C bus port number (decimal format, default 120)");
  176. i2cdisp_args.width = arg_int0("w", "width", "<n>", "Set the display width");
  177. i2cdisp_args.height = arg_int0("h", "height", "<n>", "Set the display height");
  178. i2cdisp_args.name = arg_str0("n", "name", "<string>", "Set the display type. Default is I2S");
  179. i2cdisp_args.driver = arg_str0("d", "driver", "<string>", "Set the display driver name");
  180. i2cdisp_args.clear = arg_litn("c", "clear", 0, 1, "clear configuration and return");
  181. i2cdisp_args.end = arg_end(2);
  182. i2cdisp_show_args.end = arg_end(1);
  183. const esp_console_cmd_t i2c_set_display= {
  184. .command = "setdisplay",
  185. .help="Sets the display options for the board",
  186. .hint = NULL,
  187. .func = &do_i2c_set_display,
  188. .argtable = &i2cdisp_args
  189. };
  190. const esp_console_cmd_t i2c_show_display= {
  191. .command = "show_display",
  192. .help="Shows the board display options and i2c configuration",
  193. .hint = NULL,
  194. .func = &do_i2c_show_display,
  195. .argtable = &i2cdisp_show_args
  196. };
  197. ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_set_display));
  198. ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_show_display));
  199. }
  200. static int do_i2cconfig_cmd(int argc, char **argv)
  201. {
  202. esp_err_t err=ESP_OK;
  203. int res=0;
  204. int nerrors = arg_parse(argc, argv, (void **)&i2cconfig_args);
  205. if (nerrors != 0) {
  206. arg_print_errors(stderr, i2cconfig_args.end, argv[0]);
  207. return 0;
  208. }
  209. /* Check "--load" option */
  210. if (i2cconfig_args.load->count) {
  211. ESP_LOGW(TAG,"Clearing display config");
  212. char * nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  213. if (nvs_item) {
  214. char * p=NULL;
  215. ESP_LOGI(TAG,"Loading I2C configuration : %s", nvs_item);
  216. if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c_gpio_scl = atoi(strchr(p, '=') + 1);
  217. if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c_gpio_sda = atoi(strchr(p, '=') + 1);
  218. if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c_frequency = atoi(strchr(p, '=') + 1);
  219. if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_port = atoi(strchr(p, '=') + 1);
  220. free(nvs_item);
  221. }
  222. }
  223. /* Check "--port" option */
  224. if (i2cconfig_args.port->count) {
  225. if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) {
  226. return 1;
  227. }
  228. }
  229. /* Check "--freq" option */
  230. if (i2cconfig_args.freq->count) {
  231. i2c_frequency = i2cconfig_args.freq->ival[0];
  232. }
  233. if (i2cconfig_args.sda->count){
  234. /* Check "--sda" option */
  235. i2c_gpio_sda = i2cconfig_args.sda->ival[0];
  236. }
  237. else {
  238. ESP_LOGE(TAG,"Missing --sda option.");
  239. res=1;
  240. }
  241. if (i2cconfig_args.scl->count){
  242. /* Check "--sda" option */
  243. i2c_gpio_scl = i2cconfig_args.scl->ival[0];
  244. }
  245. else {
  246. ESP_LOGE(TAG,"Missing --scl option.");
  247. res=1;
  248. }
  249. #ifdef CONFIG_SQUEEZEAMP
  250. if (i2c_port == I2C_NUM_0) {
  251. i2c_port = I2C_NUM_1;
  252. ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP. Changing to port 1.");
  253. }
  254. #endif
  255. if(!res){
  256. int buffer_size=255;
  257. char * config_buffer=malloc(buffer_size);
  258. memset(config_buffer,0x00,buffer_size);
  259. snprintf(config_buffer,buffer_size,"scl=%u sda=%u speed=%u port=%u",i2c_gpio_scl,i2c_gpio_sda,i2c_frequency,i2c_port);
  260. ESP_LOGI(TAG,"Initializing driver with config %s", config_buffer);
  261. if((err=i2c_master_driver_initialize())==ESP_OK){
  262. ESP_LOGI(TAG,"Initalize success. Storing config to nvs");
  263. config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer);
  264. ESP_LOGI(TAG,"Starting the i2s driver.");
  265. // now start the i2c driver
  266. if((err=i2c_master_driver_install())!=ESP_OK){
  267. res=1;
  268. }
  269. }
  270. else {
  271. ESP_LOGE(TAG,"I2C initialization failed. %s", esp_err_to_name(err));
  272. res=1;
  273. }
  274. if(config_buffer) free(config_buffer);
  275. }
  276. return res;
  277. }
  278. static void register_i2cconfig(void)
  279. {
  280. i2cconfig_args.port = arg_int1("p", "port", "<0|1>", "Set the I2C bus port number");
  281. i2cconfig_args.freq = arg_int1("f", "freq", "<Hz>", "Set the frequency(Hz) of I2C bus. e.g. 100000");
  282. i2cconfig_args.sda = arg_int1("d", "sda", "<gpio>", "Set the gpio for I2C SDA. e.g. 19");
  283. i2cconfig_args.scl = arg_int1("c", "scl", "<gpio>", "Set the gpio for I2C SCL. e.g. 18");
  284. i2cconfig_args.load = arg_litn("l", "load", 0, 1, "load existing configuration and return");
  285. i2cconfig_args.end = arg_end(2);
  286. const esp_console_cmd_t i2cconfig_cmd = {
  287. .command = "i2cconfig",
  288. .help = "Config I2C bus",
  289. .hint = NULL,
  290. .func = &do_i2cconfig_cmd,
  291. .argtable = &i2cconfig_args
  292. };
  293. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd));
  294. }
  295. #define RUN_SHOW_ERROR(c)
  296. static int do_i2cdetect_cmd(int argc, char **argv)
  297. {
  298. esp_err_t err=ESP_OK;
  299. if((err=i2c_master_driver_initialize())!=ESP_OK){
  300. ESP_LOGE(TAG,"Error initializing i2c driver. %s",esp_err_to_name(err));
  301. return 1;
  302. }
  303. if((err=i2c_master_driver_install())!=ESP_OK){
  304. return 1;
  305. }
  306. uint8_t address;
  307. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  308. for (int i = 0; i < 128; i += 16) {
  309. printf("%02x: ", i);
  310. for (int j = 0; j < 16; j++) {
  311. fflush(stdout);
  312. address = i + j;
  313. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  314. i2c_master_start(cmd);
  315. i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN);
  316. i2c_master_stop(cmd);
  317. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
  318. i2c_cmd_link_delete(cmd);
  319. if (ret == ESP_OK) {
  320. printf("%02x ", address);
  321. } else if (ret == ESP_ERR_TIMEOUT) {
  322. printf("UU ");
  323. } else {
  324. printf("-- ");
  325. }
  326. }
  327. printf("\r\n");
  328. }
  329. i2c_driver_delete(i2c_port);
  330. return 0;
  331. }
  332. static void register_i2cdectect(void)
  333. {
  334. const esp_console_cmd_t i2cdetect_cmd = {
  335. .command = "i2cdetect",
  336. .help = "Scan I2C bus for devices",
  337. .hint = NULL,
  338. .func = &do_i2cdetect_cmd,
  339. .argtable = NULL
  340. };
  341. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdetect_cmd));
  342. }
  343. static struct {
  344. struct arg_int *chip_address;
  345. struct arg_int *register_address;
  346. struct arg_int *data_length;
  347. struct arg_end *end;
  348. } i2cget_args;
  349. static int do_i2cget_cmd(int argc, char **argv)
  350. {
  351. esp_err_t err=ESP_OK;
  352. int nerrors = arg_parse(argc, argv, (void **)&i2cget_args);
  353. if (nerrors != 0) {
  354. arg_print_errors(stderr, i2cget_args.end, argv[0]);
  355. return 0;
  356. }
  357. /* Check chip address: "-c" option */
  358. int chip_addr = i2cget_args.chip_address->ival[0];
  359. /* Check register address: "-r" option */
  360. int data_addr = -1;
  361. if (i2cget_args.register_address->count) {
  362. data_addr = i2cget_args.register_address->ival[0];
  363. }
  364. /* Check data length: "-l" option */
  365. int len = 1;
  366. if (i2cget_args.data_length->count) {
  367. len = i2cget_args.data_length->ival[0];
  368. }
  369. if((err=i2c_master_driver_initialize())!=ESP_OK){
  370. ESP_LOGE(TAG,"Error initializing i2c driver. %s",esp_err_to_name(err));
  371. return 1;
  372. }
  373. if((err=i2c_master_driver_install())!=ESP_OK){
  374. return 1;
  375. }
  376. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  377. i2c_master_start(cmd);
  378. uint8_t *data = malloc(len);
  379. if (data_addr != -1) {
  380. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  381. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  382. i2c_master_start(cmd);
  383. }
  384. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  385. if (len > 1) {
  386. i2c_master_read(cmd, data, len - 1, ACK_VAL);
  387. }
  388. i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
  389. i2c_master_stop(cmd);
  390. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  391. i2c_cmd_link_delete(cmd);
  392. if (ret == ESP_OK) {
  393. for (int i = 0; i < len; i++) {
  394. printf("0x%02x ", data[i]);
  395. if ((i + 1) % 16 == 0) {
  396. printf("\r\n");
  397. }
  398. }
  399. if (len % 16) {
  400. printf("\r\n");
  401. }
  402. } else if (ret == ESP_ERR_TIMEOUT) {
  403. ESP_LOGW(TAG, "Bus is busy");
  404. } else {
  405. ESP_LOGW(TAG, "Read failed");
  406. }
  407. free(data);
  408. i2c_driver_delete(i2c_port);
  409. return 0;
  410. }
  411. static void register_i2cget(void)
  412. {
  413. i2cget_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  414. i2cget_args.register_address = arg_int0("r", "register", "<register_addr>", "Specify the address on that chip to read from");
  415. i2cget_args.data_length = arg_int0("l", "length", "<length>", "Specify the length to read from that data address");
  416. i2cget_args.end = arg_end(1);
  417. const esp_console_cmd_t i2cget_cmd = {
  418. .command = "i2cget",
  419. .help = "Read registers visible through the I2C bus",
  420. .hint = NULL,
  421. .func = &do_i2cget_cmd,
  422. .argtable = &i2cget_args
  423. };
  424. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cget_cmd));
  425. }
  426. static struct {
  427. struct arg_int *chip_address;
  428. struct arg_int *register_address;
  429. struct arg_int *data;
  430. struct arg_end *end;
  431. } i2cset_args;
  432. static int do_i2cset_cmd(int argc, char **argv)
  433. {
  434. int nerrors = arg_parse(argc, argv, (void **)&i2cset_args);
  435. if (nerrors != 0) {
  436. arg_print_errors(stderr, i2cset_args.end, argv[0]);
  437. return 0;
  438. }
  439. /* Check chip address: "-c" option */
  440. int chip_addr = i2cset_args.chip_address->ival[0];
  441. /* Check register address: "-r" option */
  442. int data_addr = 0;
  443. if (i2cset_args.register_address->count) {
  444. data_addr = i2cset_args.register_address->ival[0];
  445. }
  446. /* Check data: "-d" option */
  447. int len = i2cset_args.data->count;
  448. i2c_master_driver_initialize();
  449. if(i2c_master_driver_install()!=ESP_OK){
  450. return 1;
  451. }
  452. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  453. i2c_master_start(cmd);
  454. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  455. if (i2cset_args.register_address->count) {
  456. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  457. }
  458. for (int i = 0; i < len; i++) {
  459. i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN);
  460. }
  461. i2c_master_stop(cmd);
  462. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  463. i2c_cmd_link_delete(cmd);
  464. if (ret == ESP_OK) {
  465. ESP_LOGI(TAG, "Write OK");
  466. } else if (ret == ESP_ERR_TIMEOUT) {
  467. ESP_LOGW(TAG, "Bus is busy");
  468. } else {
  469. ESP_LOGW(TAG, "Write Failed");
  470. }
  471. i2c_driver_delete(i2c_port);
  472. return 0;
  473. }
  474. static void register_i2cset(void)
  475. {
  476. i2cset_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  477. i2cset_args.register_address = arg_int0("r", "register", "<register_addr>", "Specify the address on that chip to read from");
  478. i2cset_args.data = arg_intn(NULL, NULL, "<data>", 0, 256, "Specify the data to write to that data address");
  479. i2cset_args.end = arg_end(2);
  480. const esp_console_cmd_t i2cset_cmd = {
  481. .command = "i2cset",
  482. .help = "Set registers visible through the I2C bus",
  483. .hint = NULL,
  484. .func = &do_i2cset_cmd,
  485. .argtable = &i2cset_args
  486. };
  487. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cset_cmd));
  488. }
  489. static struct {
  490. struct arg_int *chip_address;
  491. struct arg_int *size;
  492. struct arg_end *end;
  493. } i2cdump_args;
  494. static int do_i2cdump_cmd(int argc, char **argv)
  495. {
  496. int nerrors = arg_parse(argc, argv, (void **)&i2cdump_args);
  497. if (nerrors != 0) {
  498. arg_print_errors(stderr, i2cdump_args.end, argv[0]);
  499. return 0;
  500. }
  501. /* Check chip address: "-c" option */
  502. int chip_addr = i2cdump_args.chip_address->ival[0];
  503. /* Check read size: "-s" option */
  504. int size = 1;
  505. if (i2cdump_args.size->count) {
  506. size = i2cdump_args.size->ival[0];
  507. }
  508. if (size != 1 && size != 2 && size != 4) {
  509. ESP_LOGE(TAG, "Wrong read size. Only support 1,2,4");
  510. return 1;
  511. }
  512. i2c_master_driver_initialize();
  513. if(i2c_master_driver_install()!=ESP_OK){
  514. return 1;
  515. }
  516. uint8_t data_addr;
  517. uint8_t data[4];
  518. int32_t block[16];
  519. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
  520. " 0123456789abcdef\r\n");
  521. for (int i = 0; i < 128; i += 16) {
  522. printf("%02x: ", i);
  523. for (int j = 0; j < 16; j += size) {
  524. fflush(stdout);
  525. data_addr = i + j;
  526. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  527. i2c_master_start(cmd);
  528. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  529. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  530. i2c_master_start(cmd);
  531. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  532. if (size > 1) {
  533. i2c_master_read(cmd, data, size - 1, ACK_VAL);
  534. }
  535. i2c_master_read_byte(cmd, data + size - 1, NACK_VAL);
  536. i2c_master_stop(cmd);
  537. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS);
  538. i2c_cmd_link_delete(cmd);
  539. if (ret == ESP_OK) {
  540. for (int k = 0; k < size; k++) {
  541. printf("%02x ", data[k]);
  542. block[j + k] = data[k];
  543. }
  544. } else {
  545. for (int k = 0; k < size; k++) {
  546. printf("XX ");
  547. block[j + k] = -1;
  548. }
  549. }
  550. }
  551. printf(" ");
  552. for (int k = 0; k < 16; k++) {
  553. if (block[k] < 0) {
  554. printf("X");
  555. }
  556. if ((block[k] & 0xff) == 0x00 || (block[k] & 0xff) == 0xff) {
  557. printf(".");
  558. } else if ((block[k] & 0xff) < 32 || (block[k] & 0xff) >= 127) {
  559. printf("?");
  560. } else {
  561. printf("%c", block[k] & 0xff);
  562. }
  563. }
  564. printf("\r\n");
  565. }
  566. i2c_driver_delete(i2c_port);
  567. return 0;
  568. }
  569. static void register_i2cdump(void)
  570. {
  571. i2cdump_args.chip_address = arg_int1("c", "chip", "<chip_addr>", "Specify the address of the chip on that bus");
  572. i2cdump_args.size = arg_int0("s", "size", "<size>", "Specify the size of each read");
  573. i2cdump_args.end = arg_end(1);
  574. const esp_console_cmd_t i2cdump_cmd = {
  575. .command = "i2cdump",
  576. .help = "Examine registers visible through the I2C bus",
  577. .hint = NULL,
  578. .func = &do_i2cdump_cmd,
  579. .argtable = &i2cdump_args
  580. };
  581. ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdump_cmd));
  582. }
  583. void register_i2ctools(void)
  584. {
  585. register_i2cconfig();
  586. register_i2cdectect();
  587. register_i2cget();
  588. register_i2cset();
  589. register_i2cdump();
  590. register_i2c_set_display();
  591. }