cmd_i2ctools.c 41 KB

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