accessors.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  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 "esp_log.h"
  9. #include "driver/gpio.h"
  10. #include "driver/i2c.h"
  11. #include "driver/spi_master.h"
  12. #include "platform_config.h"
  13. #include "accessors.h"
  14. #include "globdefs.h"
  15. #include "display.h"
  16. #include "cJSON.h"
  17. #include "driver/gpio.h"
  18. #include "stdbool.h"
  19. #include "driver/adc.h"
  20. #include "esp_attr.h"
  21. #include "soc/spi_periph.h"
  22. #include "esp_err.h"
  23. #include "soc/rtc.h"
  24. #include "sdkconfig.h"
  25. #include "soc/efuse_periph.h"
  26. #include "driver/gpio.h"
  27. #include "driver/spi_common_internal.h"
  28. #include "esp32/rom/efuse.h"
  29. #include "trace.h"
  30. #include "monitor.h"
  31. #include "messaging.h"
  32. static const char *TAG = "services";
  33. const char *i2c_name_type="I2C";
  34. const char *spi_name_type="SPI";
  35. cJSON * gpio_list=NULL;
  36. #define min(a,b) (((a) < (b)) ? (a) : (b))
  37. #ifndef QUOTE
  38. #define QUOTE(name) #name
  39. #endif
  40. #ifndef STR
  41. #define STR(macro) QUOTE(macro)
  42. #endif
  43. bool are_statistics_enabled(){
  44. #if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY) && defined (CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS)
  45. return true;
  46. #endif
  47. return false;
  48. }
  49. /****************************************************************************************
  50. *
  51. */
  52. static char * config_spdif_get_string(){
  53. return config_alloc_get_str("spdif_config", CONFIG_SPDIF_CONFIG, "bck=" STR(CONFIG_SPDIF_BCK_IO)
  54. ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO));
  55. }
  56. /****************************************************************************************
  57. *
  58. */
  59. static char * get_dac_config_string(){
  60. return config_alloc_get_str("dac_config", CONFIG_DAC_CONFIG, "model=i2s,bck=" STR(CONFIG_I2S_BCK_IO)
  61. ",ws=" STR(CONFIG_I2S_WS_IO) ",do=" STR(CONFIG_I2S_DO_IO)
  62. ",sda=" STR(CONFIG_I2C_SDA) ",scl=" STR(CONFIG_I2C_SCL)
  63. ",mute=" STR(CONFIG_MUTE_GPIO));
  64. }
  65. /****************************************************************************************
  66. *
  67. */
  68. bool is_dac_config_locked(){
  69. #if ( defined CONFIG_DAC_CONFIG )
  70. if(strlen(CONFIG_DAC_CONFIG) > 0){
  71. return true;
  72. }
  73. #endif
  74. #if defined(CONFIG_I2S_BCK_IO) && CONFIG_I2S_BCK_IO>0
  75. return true;
  76. #endif
  77. return false;
  78. }
  79. /****************************************************************************************
  80. *
  81. */
  82. bool is_spdif_config_locked(){
  83. #if ( defined CONFIG_SPDIF_CONFIG )
  84. if(strlen(CONFIG_SPDIF_CONFIG) > 0){
  85. return true;
  86. }
  87. #endif
  88. #if defined(CONFIG_SPDIF_BCK_IO) && CONFIG_SPDIF_BCK_IO>0
  89. return true;
  90. #endif
  91. return false;
  92. }
  93. /****************************************************************************************
  94. * Set pin from config string
  95. */
  96. static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) {
  97. char *p;
  98. pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1;
  99. if ((p = strcasestr(config, "bck"))) sscanf(p, "bck%*[^=]=%d", &pin_config->bck_io_num);
  100. if ((p = strcasestr(config, "ws"))) sscanf(p, "ws%*[^=]=%d", &pin_config->ws_io_num);
  101. if ((p = strcasestr(config, "do"))) sscanf(p, "do%*[^=]=%d", &pin_config->data_out_num);
  102. }
  103. /****************************************************************************************
  104. * Get i2s config structure from config string
  105. */
  106. const i2s_platform_config_t * config_i2s_get_from_str(char * dac_config ){
  107. static i2s_platform_config_t i2s_dac_pin = {
  108. .i2c_addr = -1,
  109. .sda= -1,
  110. .scl = -1,
  111. .mute_gpio = -1,
  112. .mute_level = -1
  113. };
  114. set_i2s_pin(dac_config, &i2s_dac_pin.pin);
  115. strcpy(i2s_dac_pin.model, "i2s");
  116. char * p=NULL;
  117. PARSE_PARAM(dac_config, "i2c", '=', i2s_dac_pin.i2c_addr);
  118. PARSE_PARAM(dac_config, "sda", '=', i2s_dac_pin.sda);
  119. PARSE_PARAM(dac_config, "scl", '=', i2s_dac_pin.scl);
  120. PARSE_PARAM_STR(dac_config, "model", '=', i2s_dac_pin.model, 31);
  121. if ((p = strcasestr(dac_config, "mute"))) {
  122. char mute[8] = "";
  123. sscanf(p, "%*[^=]=%7[^,]", mute);
  124. i2s_dac_pin.mute_gpio = atoi(mute);
  125. PARSE_PARAM(p, "mute", ':', i2s_dac_pin.mute_level);
  126. }
  127. return &i2s_dac_pin;
  128. }
  129. /****************************************************************************************
  130. * Get spdif config structure
  131. */
  132. const i2s_platform_config_t * config_spdif_get( ){
  133. char * spdif_config = config_spdif_get_string();
  134. static EXT_RAM_ATTR i2s_platform_config_t i2s_dac_config;
  135. memcpy(&i2s_dac_config, config_i2s_get_from_str(spdif_config), sizeof(i2s_dac_config));
  136. free(spdif_config);
  137. return &i2s_dac_config;
  138. }
  139. /****************************************************************************************
  140. * Get dac config structure
  141. */
  142. const i2s_platform_config_t * config_dac_get(){
  143. char * spdif_config = get_dac_config_string();
  144. static EXT_RAM_ATTR i2s_platform_config_t i2s_dac_config;
  145. memcpy(&i2s_dac_config, config_i2s_get_from_str(spdif_config), sizeof(i2s_dac_config));
  146. free(spdif_config);
  147. return &i2s_dac_config;
  148. }
  149. /****************************************************************************************
  150. *
  151. */
  152. esp_err_t config_i2c_set(const i2c_config_t * config, int port){
  153. int buffer_size=255;
  154. esp_err_t err=ESP_OK;
  155. char * config_buffer=calloc(buffer_size,1);
  156. if(config_buffer) {
  157. snprintf(config_buffer,buffer_size,"scl=%u,sda=%u,speed=%u,port=%u",config->scl_io_num,config->sda_io_num,config->master.clk_speed,port);
  158. log_send_messaging(MESSAGING_INFO,"Updating I2C configuration to %s",config_buffer);
  159. err = config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer);
  160. if(err!=ESP_OK){
  161. log_send_messaging(MESSAGING_ERROR,"Error: %s",esp_err_to_name(err));
  162. }
  163. free(config_buffer);
  164. }
  165. return err;
  166. }
  167. /****************************************************************************************
  168. *
  169. */
  170. esp_err_t config_rotary_set(rotary_struct_t * config){
  171. int buffer_size=512;
  172. esp_err_t err=ESP_OK;
  173. char * config_buffer=calloc(buffer_size,1);
  174. char * config_buffer2=calloc(buffer_size,1);
  175. if(config_buffer && config_buffer2) {
  176. snprintf(config_buffer,buffer_size,"A=%i,B=%i",config->A, config->B);
  177. if(config->SW >=0 ){
  178. snprintf(config_buffer2,buffer_size,"%s,SW=%i",config_buffer,config->SW);
  179. strcpy(config_buffer,config_buffer2);
  180. }
  181. if(config->knobonly){
  182. strncat(config_buffer,",knobonly",buffer_size);
  183. if(config->timer>0){
  184. snprintf(config_buffer2,buffer_size,"%s=%i",config_buffer,config->timer);
  185. strcpy(config_buffer,config_buffer2);
  186. }
  187. }
  188. if(config->volume_lock){
  189. strncat(config_buffer,",volume",buffer_size);
  190. }
  191. if(config->longpress){
  192. strncat(config_buffer,",longpress",buffer_size);
  193. }
  194. log_send_messaging(MESSAGING_INFO,"Updating rotary configuration to %s",config_buffer);
  195. err = config_set_value(NVS_TYPE_STR, "rotary_config", config_buffer);
  196. if(err!=ESP_OK){
  197. log_send_messaging(MESSAGING_ERROR,"Error: %s",esp_err_to_name(err));
  198. }
  199. }
  200. else {
  201. err = ESP_ERR_NO_MEM;
  202. }
  203. FREE_AND_NULL(config_buffer);
  204. FREE_AND_NULL(config_buffer2);
  205. return err;
  206. }
  207. /****************************************************************************************
  208. *
  209. */
  210. esp_err_t config_display_set(const display_config_t * config){
  211. int buffer_size=512;
  212. esp_err_t err=ESP_OK;
  213. char * config_buffer=calloc(buffer_size,1);
  214. char * config_buffer2=calloc(buffer_size,1);
  215. if(config_buffer && config_buffer2) {
  216. snprintf(config_buffer,buffer_size,"%s,width=%i,height=%i",config->type,config->width,config->height);
  217. if(strcasecmp("I2C",config->type)==0){
  218. if(config->address>0 ){
  219. snprintf(config_buffer2,buffer_size,"%s,address=%i",config_buffer,config->address);
  220. strcpy(config_buffer,config_buffer2);
  221. }
  222. }
  223. else {
  224. if(config->CS_pin >=0 ){
  225. snprintf(config_buffer2,buffer_size,"%s,cs=%i",config_buffer,config->CS_pin);
  226. strcpy(config_buffer,config_buffer2);
  227. }
  228. }
  229. if(config->RST_pin >=0 ){
  230. snprintf(config_buffer2,buffer_size,"%s,reset=%i",config_buffer,config->RST_pin);
  231. strcpy(config_buffer,config_buffer2);
  232. }
  233. // I2C,width=<pixels>,height=<pixels>[address=<i2c_address>][,reset=<gpio>][,HFlip][,VFlip][driver=SSD1306|SSD1326[:1|4]|SSD1327|SH1106]
  234. // SPI,width=<pixels>,height=<pixels>,cs=<gpio>[,back=<gpio>][,reset=<gpio>][,speed=<speed>][,HFlip][,VFlip][driver=SSD1306|SSD1322|SSD1326[:1|4]|SSD1327|SH1106|SSD1675|ST7735|ST7789[,rotate]]
  235. if(config->back >=0 ){
  236. snprintf(config_buffer2,buffer_size,"%s,back=%i",config_buffer,config->back);
  237. strcpy(config_buffer,config_buffer2);
  238. }
  239. if(config->speed >0 && strcasecmp("SPI",config->type)==0){
  240. snprintf(config_buffer2,buffer_size,"%s,speed=%i",config_buffer,config->speed);
  241. strcpy(config_buffer,config_buffer2);
  242. }
  243. snprintf(config_buffer2,buffer_size,"%s,driver=%s%s%s%s",config_buffer,config->drivername,config->hflip?",HFlip":"",config->vflip?",VFlip":"",config->rotate?",rotate":"");
  244. strcpy(config_buffer,config_buffer2);
  245. log_send_messaging(MESSAGING_INFO,"Updating display configuration to %s",config_buffer);
  246. err = config_set_value(NVS_TYPE_STR, "display_config", config_buffer);
  247. if(err!=ESP_OK){
  248. log_send_messaging(MESSAGING_ERROR,"Error: %s",esp_err_to_name(err));
  249. }
  250. }
  251. else {
  252. err = ESP_ERR_NO_MEM;
  253. }
  254. FREE_AND_NULL(config_buffer);
  255. FREE_AND_NULL(config_buffer2);
  256. return err;
  257. }
  258. /****************************************************************************************
  259. *
  260. */
  261. esp_err_t config_i2s_set(const i2s_platform_config_t * config, const char * nvs_name){
  262. int buffer_size=255;
  263. esp_err_t err=ESP_OK;
  264. char * config_buffer=calloc(buffer_size,1);
  265. char * config_buffer2=calloc(buffer_size,1);
  266. if(config_buffer && config_buffer2) {
  267. snprintf(config_buffer,buffer_size,"model=%s,bck=%u,ws=%u,do=%u",config->model,config->pin.bck_io_num,config->pin.ws_io_num,config->pin.data_out_num);
  268. if(config->mute_gpio>=0){
  269. snprintf(config_buffer2,buffer_size,"%s,mute=%u:%u",config_buffer,config->mute_gpio,config->mute_level);
  270. strcpy(config_buffer,config_buffer2);
  271. }
  272. if(config->sda>=0){
  273. snprintf(config_buffer2,buffer_size,"%s,sda=%u,scl=%u",config_buffer,config->sda,config->scl);
  274. strcpy(config_buffer,config_buffer2);
  275. }
  276. if(config->i2c_addr>0){
  277. snprintf(config_buffer2,buffer_size,"%s,i2c=%u",config_buffer,config->i2c_addr);
  278. strcpy(config_buffer,config_buffer2);
  279. }
  280. log_send_messaging(MESSAGING_INFO,"Updating dac configuration to %s",config_buffer);
  281. err = config_set_value(NVS_TYPE_STR, nvs_name, config_buffer);
  282. if(err!=ESP_OK){
  283. log_send_messaging(MESSAGING_ERROR,"Error: %s",esp_err_to_name(err));
  284. }
  285. }
  286. else {
  287. err = ESP_ERR_NO_MEM;
  288. }
  289. FREE_AND_NULL(config_buffer);
  290. FREE_AND_NULL(config_buffer2);
  291. return err;
  292. }
  293. /****************************************************************************************
  294. *
  295. */
  296. esp_err_t config_spdif_set(const i2s_platform_config_t * config){
  297. int buffer_size=255;
  298. esp_err_t err=ESP_OK;
  299. char * config_buffer=calloc(buffer_size,1);
  300. if(config_buffer ) {
  301. snprintf(config_buffer,buffer_size,"bck=%u,ws=%u,do=%u",config->pin.bck_io_num,config->pin.ws_io_num,config->pin.data_out_num);
  302. log_send_messaging(MESSAGING_INFO,"Updating SPDIF configuration to %s",config_buffer);
  303. err = config_set_value(NVS_TYPE_STR, "spdif_config", config_buffer);
  304. if(err!=ESP_OK){
  305. log_send_messaging(MESSAGING_ERROR,"Error: %s",esp_err_to_name(err));
  306. }
  307. }
  308. else {
  309. err = ESP_ERR_NO_MEM;
  310. }
  311. FREE_AND_NULL(config_buffer);
  312. return err;
  313. }
  314. /****************************************************************************************
  315. *
  316. */
  317. esp_err_t config_spi_set(const spi_bus_config_t * config, int host, int dc){
  318. int buffer_size=255;
  319. esp_err_t err = ESP_OK;
  320. char * config_buffer=calloc(buffer_size,1);
  321. if(config_buffer) {
  322. snprintf(config_buffer,buffer_size,"data=%u,clk=%u,dc=%u,host=%u,miso=%d",config->mosi_io_num,config->sclk_io_num,dc,host,config->miso_io_num);
  323. log_send_messaging(MESSAGING_INFO,"Updating SPI configuration to %s",config_buffer);
  324. err = config_set_value(NVS_TYPE_STR, "spi_config", config_buffer);
  325. if(err!=ESP_OK){
  326. log_send_messaging(MESSAGING_ERROR,"Error: %s",esp_err_to_name(err));
  327. }
  328. free(config_buffer);
  329. }
  330. return err;
  331. }
  332. /****************************************************************************************
  333. *
  334. */
  335. const display_config_t * config_display_get(){
  336. static display_config_t dstruct = {
  337. .back = -1,
  338. .CS_pin = -1,
  339. .RST_pin = -1,
  340. .depth = -1,
  341. .address = 0,
  342. .drivername = NULL,
  343. .height = 0,
  344. .width = 0,
  345. .vflip = false,
  346. .hflip = false,
  347. .type = NULL,
  348. .speed = 0,
  349. .rotate = false,
  350. .invert = false,
  351. .colorswap = 0,
  352. };
  353. char *config = config_alloc_get(NVS_TYPE_STR, "display_config");
  354. if (!config) {
  355. return NULL;
  356. }
  357. char * p=NULL;
  358. if ((p = strcasestr(config, "driver")) != NULL){
  359. sscanf(p, "%*[^:]:%u", &dstruct.depth);
  360. dstruct.drivername = display_conf_get_driver_name(strchr(p, '=') + 1);
  361. }
  362. PARSE_PARAM(config, "width", '=', dstruct.width);
  363. PARSE_PARAM(config, "height", '=', dstruct.height);
  364. PARSE_PARAM(config, "reset", '=', dstruct.RST_pin);
  365. PARSE_PARAM(config, "address", '=', dstruct.address);
  366. PARSE_PARAM(config, "cs", '=', dstruct.CS_pin);
  367. PARSE_PARAM(config, "speed", '=', dstruct.speed);
  368. PARSE_PARAM(config, "back", '=', dstruct.back);
  369. if (strstr(config, "I2C") ) dstruct.type=i2c_name_type;
  370. if (strstr(config, "SPI") ) dstruct.type=spi_name_type;
  371. dstruct.hflip= strcasestr(config, "HFlip") ? true : false;
  372. dstruct.vflip= strcasestr(config, "VFlip") ? true : false;
  373. dstruct.rotate= strcasestr(config, "rotate") ? true : false;
  374. dstruct.invert= strcasestr(config, "invert") ? true : false;
  375. dstruct.colorswap= strcasestr(config, "cswap") ? 1 : 0;
  376. return &dstruct;
  377. }
  378. /****************************************************************************************
  379. *
  380. */
  381. const i2c_config_t * config_i2c_get(int * i2c_port) {
  382. char *nvs_item;
  383. static i2c_config_t i2c = {
  384. .mode = I2C_MODE_MASTER,
  385. .sda_io_num = -1,
  386. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  387. .scl_io_num = -1,
  388. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  389. .master.clk_speed = 0,
  390. };
  391. i2c.master.clk_speed = i2c_system_speed;
  392. nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  393. if (nvs_item) {
  394. PARSE_PARAM(nvs_item, "scl", '=', i2c.scl_io_num);
  395. PARSE_PARAM(nvs_item, "sda", '=', i2c.sda_io_num);
  396. PARSE_PARAM(nvs_item, "speed", '=', i2c.master.clk_speed);
  397. PARSE_PARAM(nvs_item, "port", '=', i2c_system_port);
  398. free(nvs_item);
  399. }
  400. if(i2c_port) {
  401. #ifdef CONFIG_I2C_LOCKED
  402. *i2c_port= I2C_NUM_1;
  403. #else
  404. *i2c_port=i2c_system_port;
  405. #endif
  406. }
  407. return &i2c;
  408. }
  409. /****************************************************************************************
  410. * Get IO expander config structure from config string
  411. */
  412. const gpio_exp_config_t* config_gpio_exp_get(int index) {
  413. char *nvs_item, *item, *p;
  414. static gpio_exp_config_t config;
  415. // re-initialize config every time
  416. memset(&config, 0, sizeof(config));
  417. config.intr = -1; config.count = 16; config.base = GPIO_NUM_MAX; config.phy.port = i2c_system_port; config.phy.host = spi_system_host;
  418. nvs_item = config_alloc_get(NVS_TYPE_STR, "gpio_exp_config");
  419. if (!nvs_item || !*nvs_item) return NULL;
  420. // search index items
  421. for (item = strtok(nvs_item, ";"); index && item; index--) {
  422. if ((item = strtok(NULL, ";")) == NULL) {
  423. free(nvs_item);
  424. return NULL;
  425. }
  426. }
  427. PARSE_PARAM(item, "addr", '=', config.phy.addr);
  428. PARSE_PARAM(item, "cs", '=', config.phy.cs_pin);
  429. PARSE_PARAM(item, "speed", '=', config.phy.speed);
  430. PARSE_PARAM(item, "intr", '=', config.intr);
  431. PARSE_PARAM(item, "base", '=', config.base);
  432. PARSE_PARAM(item, "count", '=', config.count);
  433. PARSE_PARAM_STR(item, "model", '=', config.model, 31);
  434. if ((p = strcasestr(item, "port")) != NULL) {
  435. char port[8] = "";
  436. sscanf(p, "%*[^=]=%7[^,]", port);
  437. if (strcasestr(port, "dac")) config.phy.port = 0;
  438. }
  439. free(nvs_item);
  440. return &config;
  441. }
  442. /****************************************************************************************
  443. *
  444. */
  445. const gpio_with_level_t * get_gpio_struct_member(const char * nvs_item, const char * name){
  446. static gpio_with_level_t gpio_member={
  447. .gpio=-1,
  448. .level=0
  449. };
  450. if(!nvs_item) return &gpio_member;
  451. const char * p=nvs_item;
  452. char type[20]={0};
  453. int match=0;
  454. do {
  455. if ((match=sscanf(p, "%d=%19[^,:]:%d", &gpio_member.gpio, type,&gpio_member.level)) >0 && (GPIO_IS_VALID_GPIO(gpio_member.gpio) || gpio_member.gpio==GPIO_NUM_NC) && strcasestr(type,name)){
  456. return &gpio_member;
  457. }
  458. p = strchr(p, ',');
  459. } while (p++);
  460. gpio_member.gpio=-1;
  461. gpio_member.level=0;
  462. return &gpio_member;
  463. }
  464. #define HANDLE_GPIO_STRUCT_MEMBER(name,fixval) memcpy(&gpio_struct.name, get_gpio_struct_member(nvs_item, QUOTE(name)), sizeof(gpio_struct.name)); gpio_struct.name.fixed=fixval
  465. #define ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(array,structvar,name,type) if(((set_GPIO_struct_t *)structvar)->name.gpio>=0){cJSON_AddItemToArray(array,get_gpio_entry(QUOTE(name),type,((set_GPIO_struct_t *)structvar)->name.gpio, ((set_GPIO_struct_t *)structvar)->name.fixed));}
  466. /****************************************************************************************
  467. *
  468. */
  469. const set_GPIO_struct_t * get_gpio_struct(){
  470. static set_GPIO_struct_t gpio_struct;
  471. char * nvs_item=config_alloc_get(NVS_TYPE_STR, "set_GPIO");
  472. #ifdef CONFIG_LED_GREEN_GPIO_LEVEL
  473. gpio_struct.green.level = CONFIG_LED_GREEN_GPIO_LEVEL;
  474. #endif
  475. #ifdef CONFIG_LED_GREEN_GPIO
  476. gpio_struct.green.gpio = CONFIG_LED_GREEN_GPIO;
  477. #endif
  478. #ifdef CONFIG_LED_RED_GPIO_LEVEL
  479. gpio_struct.green.level = CONFIG_LED_RED_GPIO_LEVEL;
  480. #endif
  481. #ifdef CONFIG_LED_RED_GPIO
  482. gpio_struct.red.gpio = CONFIG_LED_RED_GPIO;
  483. #endif
  484. if(nvs_item){
  485. HANDLE_GPIO_STRUCT_MEMBER(amp,false);
  486. #ifndef CONFIG_LED_LOCKED
  487. HANDLE_GPIO_STRUCT_MEMBER(green,false);
  488. HANDLE_GPIO_STRUCT_MEMBER(red,false);
  489. #endif
  490. HANDLE_GPIO_STRUCT_MEMBER(jack,false);
  491. HANDLE_GPIO_STRUCT_MEMBER(spkfault,false);
  492. HANDLE_GPIO_STRUCT_MEMBER(vcc,false);
  493. HANDLE_GPIO_STRUCT_MEMBER(gnd,false);
  494. HANDLE_GPIO_STRUCT_MEMBER(ir,false);
  495. free(nvs_item);
  496. }
  497. #ifdef CONFIG_LED_LOCKED
  498. gpio_struct.red.fixed=true;
  499. gpio_struct.green.fixed=true;
  500. #endif
  501. #ifdef CONFIG_JACK_LOCKED
  502. gpio_struct.jack.gpio=CONFIG_JACK_GPIO;
  503. gpio_struct.jack.fixed=true;
  504. gpio_struct.jack.level=CONFIG_JACK_GPIO_LEVEL;
  505. #endif
  506. #ifdef CONFIG_SPKFAULT_LOCKED
  507. gpio_struct.spkfault.gpio=CONFIG_SPKFAULT_GPIO;
  508. gpio_struct.spkfault.fixed=true;
  509. gpio_struct.spkfault.level=CONFIG_SPKFAULT_GPIO_LEVEL;
  510. #endif
  511. return &gpio_struct;
  512. }
  513. /****************************************************************************************
  514. *
  515. */
  516. const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
  517. char *nvs_item;
  518. static spi_bus_config_t spi = {
  519. .mosi_io_num = -1,
  520. .sclk_io_num = -1,
  521. .miso_io_num = -1,
  522. .quadwp_io_num = -1,
  523. .quadhd_io_num = -1
  524. };
  525. nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
  526. if (nvs_item) {
  527. PARSE_PARAM(nvs_item, "data", '=', spi.mosi_io_num);
  528. PARSE_PARAM(nvs_item, "mosi", '=', spi.mosi_io_num);
  529. PARSE_PARAM(nvs_item, "miso", '=', spi.miso_io_num);
  530. PARSE_PARAM(nvs_item, "clk", '=', spi.sclk_io_num);
  531. PARSE_PARAM(nvs_item, "dc", '=', spi_system_dc_gpio);
  532. // only VSPI (1) can be used as Flash and PSRAM run at 80MHz
  533. // if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1);
  534. free(nvs_item);
  535. }
  536. if(spi_host) *spi_host = spi_system_host;
  537. return &spi;
  538. }
  539. /****************************************************************************************
  540. *
  541. */
  542. void parse_set_GPIO(void (*cb)(int gpio, char *value)) {
  543. char *nvs_item, *p, type[16];
  544. int gpio;
  545. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) == NULL) return;
  546. p = nvs_item;
  547. do {
  548. if (sscanf(p, "%d=%15[^,]", &gpio, type) > 0) cb(gpio, type);
  549. p = strchr(p, ',');
  550. } while (p++);
  551. free(nvs_item);
  552. }
  553. /****************************************************************************************
  554. *
  555. */
  556. const rotary_struct_t * config_rotary_get() {
  557. static rotary_struct_t rotary={ .A = -1, .B = -1, .SW = -1, .longpress = false, .knobonly=false,.timer=0,.volume_lock=false};
  558. char *config = config_alloc_get_default(NVS_TYPE_STR, "rotary_config", NULL, 0);
  559. if (config && *config) {
  560. char *p;
  561. // parse config
  562. PARSE_PARAM(config, "A", '=', rotary.A);
  563. PARSE_PARAM(config, "B", '=', rotary.B);
  564. PARSE_PARAM(config, "SW", '=', rotary.SW);
  565. if ((p = strcasestr(config, "knobonly")) != NULL) {
  566. p = strchr(p, '=');
  567. rotary.knobonly = true;
  568. rotary.timer = p ? atoi(p + 1) : 350;
  569. rotary.longpress = false;
  570. } else {
  571. rotary.knobonly = false;
  572. rotary.timer = 0;
  573. if ((p = strcasestr(config, "volume")) != NULL) rotary.volume_lock = true;
  574. if ((p = strcasestr(config, "longpress")) != NULL) rotary.longpress = true;
  575. }
  576. free(config);
  577. }
  578. return &rotary;
  579. }
  580. /****************************************************************************************
  581. *
  582. */
  583. cJSON * get_gpio_entry(const char * name, const char * prefix, int gpio, bool fixed){
  584. cJSON * entry = cJSON_CreateObject();
  585. cJSON_AddNumberToObject(entry,"gpio",gpio);
  586. cJSON_AddStringToObject(entry,"name",name);
  587. cJSON_AddStringToObject(entry,"group",prefix);
  588. cJSON_AddBoolToObject(entry,"fixed",fixed);
  589. return entry;
  590. }
  591. /****************************************************************************************
  592. *
  593. */
  594. cJSON * add_gpio_for_value(cJSON * list,const char * name,int gpio, const char * prefix, bool fixed){
  595. cJSON * llist = list?list:cJSON_CreateArray();
  596. if(GPIO_IS_VALID_GPIO(gpio) && gpio>0){
  597. cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpio,fixed));
  598. }
  599. return llist;
  600. }
  601. /****************************************************************************************
  602. *
  603. */
  604. cJSON * add_gpio_for_name(cJSON * list,const char * nvs_entry,const char * name, const char * prefix, bool fixed){
  605. cJSON * llist = list?list:cJSON_CreateArray();
  606. int gpioNum=0;
  607. PARSE_PARAM(nvs_entry, name, '=', gpioNum);
  608. if(gpioNum>=0){
  609. cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpioNum,fixed));
  610. }
  611. return llist;
  612. }
  613. /****************************************************************************************
  614. *
  615. */
  616. cJSON * get_GPIO_nvs_list(cJSON * list) {
  617. cJSON * ilist = list?list:cJSON_CreateArray();
  618. const set_GPIO_struct_t * gpios = get_gpio_struct();
  619. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,vcc,"other");
  620. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,gnd,"other");
  621. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,amp,"other");
  622. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,ir,"other");
  623. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,jack,"other");
  624. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,green,"other");
  625. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,red,"other");
  626. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,spkfault,"other");
  627. return ilist;
  628. }
  629. /****************************************************************************************
  630. *
  631. */
  632. cJSON * get_DAC_GPIO(cJSON * list){
  633. cJSON * llist = list;
  634. if(!llist){
  635. llist = cJSON_CreateArray();
  636. }
  637. const i2s_platform_config_t * i2s_config= config_dac_get();
  638. if(i2s_config->pin.bck_io_num>=0){
  639. cJSON_AddItemToArray(llist,get_gpio_entry("bck","dac",i2s_config->pin.bck_io_num,is_dac_config_locked()));
  640. cJSON_AddItemToArray(llist,get_gpio_entry("ws","dac",i2s_config->pin.ws_io_num,is_dac_config_locked()));
  641. cJSON_AddItemToArray(llist,get_gpio_entry("do","dac",i2s_config->pin.data_out_num,is_dac_config_locked()));
  642. if(i2s_config->sda>=0){
  643. cJSON_AddItemToArray(llist,get_gpio_entry("sda","dac",i2s_config->sda,is_dac_config_locked()));
  644. cJSON_AddItemToArray(llist,get_gpio_entry("scl","dac",i2s_config->scl,is_dac_config_locked()));
  645. }
  646. if(i2s_config->mute_gpio>=0){
  647. cJSON_AddItemToArray(llist,get_gpio_entry("mute","dac",i2s_config->mute_gpio,is_dac_config_locked()));
  648. }
  649. }
  650. return llist;
  651. }
  652. /****************************************************************************************
  653. *
  654. */
  655. cJSON * get_Display_GPIO(cJSON * list){
  656. cJSON * llist = list;
  657. if(!llist){
  658. llist = cJSON_CreateArray();
  659. }
  660. const display_config_t * config= config_display_get();
  661. if(config->back >=0){
  662. cJSON_AddItemToArray(llist,get_gpio_entry("backlight","display",config->back,false));
  663. }
  664. if(config->CS_pin >=0){
  665. cJSON_AddItemToArray(llist,get_gpio_entry("CS","display",config->CS_pin,false));
  666. }
  667. if(config->RST_pin >=0){
  668. cJSON_AddItemToArray(llist,get_gpio_entry("reset","display",config->RST_pin,false));
  669. }
  670. return llist;
  671. }
  672. /****************************************************************************************
  673. *
  674. */
  675. cJSON * get_I2C_GPIO(cJSON * list){
  676. cJSON * llist = list;
  677. if(!llist){
  678. llist = cJSON_CreateArray();
  679. }
  680. int port=0;
  681. const i2c_config_t * i2c_config = config_i2c_get(&port);
  682. if(i2c_config->scl_io_num>=0){
  683. cJSON_AddItemToArray(llist,get_gpio_entry("scl","i2c",i2c_config->scl_io_num,false));
  684. cJSON_AddItemToArray(llist,get_gpio_entry("sda","i2c",i2c_config->sda_io_num,false));
  685. }
  686. return llist;
  687. }
  688. /****************************************************************************************
  689. *
  690. */
  691. cJSON * get_SPI_GPIO(cJSON * list){
  692. cJSON * llist = list;
  693. if(!llist){
  694. llist = cJSON_CreateArray();
  695. }
  696. spi_host_device_t spi_host;
  697. const spi_bus_config_t * spi_config = config_spi_get(&spi_host);
  698. if(spi_config->miso_io_num>=0){
  699. cJSON_AddItemToArray(llist,get_gpio_entry("data","spi",spi_config->miso_io_num,false));
  700. cJSON_AddItemToArray(llist,get_gpio_entry("data","clk",spi_config->sclk_io_num,false));
  701. }
  702. if(spi_system_dc_gpio>0){
  703. cJSON_AddItemToArray(llist,get_gpio_entry("data","dc",spi_system_dc_gpio,false));
  704. }
  705. return llist;
  706. }
  707. /****************************************************************************************
  708. *
  709. */
  710. cJSON * get_SPDIF_GPIO(cJSON * list, bool fixed){
  711. cJSON * llist = list?list:cJSON_CreateArray();
  712. char * spdif_config = config_spdif_get_string();
  713. if(spdif_config){
  714. llist = add_gpio_for_name(llist,spdif_config,"bck", "spdif", fixed);
  715. llist = add_gpio_for_name(llist,spdif_config,"ws", "spdif", fixed);
  716. llist = add_gpio_for_name(llist,spdif_config,"do", "spdif", fixed);
  717. free(spdif_config);
  718. }
  719. return llist;
  720. }
  721. /****************************************************************************************
  722. *
  723. */
  724. cJSON * get_Rotary_GPIO(cJSON * list){
  725. cJSON * llist = list?list:cJSON_CreateArray();
  726. const rotary_struct_t *rotary= config_rotary_get();
  727. add_gpio_for_value(llist,"A",rotary->A, "rotary", false);
  728. add_gpio_for_value(llist,"B",rotary->B, "rotary", false);
  729. add_gpio_for_value(llist,"SW",rotary->SW, "rotary", false);
  730. return llist;
  731. }
  732. /****************************************************************************************
  733. *
  734. */
  735. esp_err_t get_gpio_structure(cJSON * gpio_entry, gpio_entry_t ** gpio){
  736. esp_err_t err = ESP_OK;
  737. *gpio = malloc(sizeof(gpio_entry_t));
  738. cJSON * val = cJSON_GetObjectItem(gpio_entry,"gpio");
  739. if(val){
  740. (*gpio)->gpio= (int)val->valuedouble;
  741. } else {
  742. ESP_LOGE(TAG,"gpio pin not found");
  743. err=ESP_FAIL;
  744. }
  745. val = cJSON_GetObjectItem(gpio_entry,"name");
  746. if(val){
  747. (*gpio)->name= strdup(cJSON_GetStringValue(val));
  748. } else {
  749. ESP_LOGE(TAG,"gpio name value not found");
  750. err=ESP_FAIL;
  751. }
  752. val = cJSON_GetObjectItem(gpio_entry,"group");
  753. if(val){
  754. (*gpio)->group= strdup(cJSON_GetStringValue(val));
  755. } else {
  756. ESP_LOGE(TAG,"gpio group value not found");
  757. err=ESP_FAIL;
  758. }
  759. val = cJSON_GetObjectItem(gpio_entry,"fixed");
  760. if(val){
  761. (*gpio)->fixed= cJSON_IsTrue(val);
  762. } else {
  763. ESP_LOGE(TAG,"gpio fixed indicator not found");
  764. err=ESP_FAIL;
  765. }
  766. return err;
  767. }
  768. /****************************************************************************************
  769. *
  770. */
  771. esp_err_t free_gpio_entry( gpio_entry_t ** gpio) {
  772. if(* gpio){
  773. free((* gpio)->name);
  774. free((* gpio)->group);
  775. free(* gpio);
  776. * gpio=NULL;
  777. return ESP_OK;
  778. }
  779. return ESP_FAIL;
  780. }
  781. /****************************************************************************************
  782. *
  783. */
  784. gpio_entry_t * get_gpio_by_no(int gpionum, bool refresh){
  785. cJSON * gpio_header=NULL;
  786. gpio_entry_t * gpio=NULL;
  787. get_gpio_list(refresh);
  788. cJSON_ArrayForEach(gpio_header,gpio_list)
  789. {
  790. if(get_gpio_structure(gpio_header, &gpio)==ESP_OK && gpio->gpio==gpionum){
  791. ESP_LOGD(TAG,"Found GPIO: %s=%d %s", gpio->name,gpio->gpio,gpio->fixed?"(FIXED)":"(VARIABLE)");
  792. }
  793. }
  794. return gpio;
  795. }
  796. /****************************************************************************************
  797. *
  798. */
  799. gpio_entry_t * get_gpio_by_name(char * name,char * group, bool refresh){
  800. cJSON * gpio_header=NULL;
  801. gpio_entry_t * gpio=NULL;
  802. get_gpio_list(refresh);
  803. cJSON_ArrayForEach(gpio_header,gpio_list)
  804. {
  805. if(get_gpio_structure(gpio_header, &gpio)==ESP_OK && strcasecmp(gpio->name,name)&& strcasecmp(gpio->group,group)){
  806. ESP_LOGD(TAG,"Found GPIO: %s=%d %s", gpio->name,gpio->gpio,gpio->fixed?"(FIXED)":"(VARIABLE)");
  807. }
  808. }
  809. return gpio;
  810. }
  811. #ifndef PICO_PSRAM_CLK_IO
  812. #define PICO_PSRAM_CLK_IO 6
  813. #endif
  814. #ifndef PSRAM_SPIQ_SD0_IO
  815. #define PSRAM_SPIQ_SD0_IO 7
  816. #define PSRAM_SPID_SD1_IO 8
  817. #define PSRAM_SPIWP_SD3_IO 10
  818. #define PSRAM_SPIHD_SD2_IO 9
  819. #define FLASH_HSPI_CLK_IO 14
  820. #define FLASH_HSPI_CS_IO 15
  821. #define PSRAM_HSPI_SPIQ_SD0_IO 12
  822. #define PSRAM_HSPI_SPID_SD1_IO 13
  823. #define PSRAM_HSPI_SPIWP_SD3_IO 2
  824. #define PSRAM_HSPI_SPIHD_SD2_IO 4
  825. #endif
  826. cJSON * get_psram_gpio_list(cJSON * list){
  827. const char * psram_dev = "psram";
  828. const char * flash_dev = "flash";
  829. const char * clk = "clk";
  830. const char * cs = "cs";
  831. const char * spiq_sd0_io="spiq_sd0_io";
  832. const char * spid_sd1_io = "spid_sd1_io";
  833. const char * spiwp_sd3_io = "spiwp_sd3_io";
  834. const char * spihd_sd2_io = "spihd_sd2_io";
  835. cJSON * llist=list;
  836. uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG);
  837. uint32_t pkg_ver = chip_ver & 0x7;
  838. if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5) {
  839. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  840. if (cfg.tieh != RTC_VDDSDIO_TIEH_1_8V) {
  841. return llist;
  842. }
  843. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,CONFIG_D2WD_PSRAM_CLK_IO,true));
  844. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,CONFIG_D2WD_PSRAM_CS_IO,true));
  845. } else if ((pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) || (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4)) {
  846. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  847. if (cfg.tieh != RTC_VDDSDIO_TIEH_3_3V) {
  848. return llist;
  849. }
  850. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,PICO_PSRAM_CLK_IO,true));
  851. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,CONFIG_PICO_PSRAM_CS_IO,true));
  852. } else if ((pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ6) || (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ5)){
  853. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,CONFIG_D0WD_PSRAM_CLK_IO,true));
  854. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,CONFIG_D0WD_PSRAM_CS_IO,true));
  855. } else {
  856. ESP_LOGW(TAG, "Cant' determine GPIOs for PSRAM chip id: %d", pkg_ver);
  857. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,-1,true));
  858. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,-1,true));
  859. }
  860. const uint32_t spiconfig = ets_efuse_get_spiconfig();
  861. if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) {
  862. cJSON_AddItemToArray(list,get_gpio_entry(spiq_sd0_io,psram_dev,PSRAM_SPIQ_SD0_IO,true));
  863. cJSON_AddItemToArray(list,get_gpio_entry(spid_sd1_io,psram_dev,PSRAM_SPID_SD1_IO,true));
  864. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,PSRAM_SPIWP_SD3_IO,true));
  865. cJSON_AddItemToArray(list,get_gpio_entry(spihd_sd2_io,psram_dev,PSRAM_SPIHD_SD2_IO,true));
  866. } else if (spiconfig == EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  867. cJSON_AddItemToArray(list,get_gpio_entry(spiq_sd0_io,psram_dev,PSRAM_HSPI_SPIQ_SD0_IO,true));
  868. cJSON_AddItemToArray(list,get_gpio_entry(spid_sd1_io,psram_dev,PSRAM_HSPI_SPID_SD1_IO,true));
  869. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,PSRAM_HSPI_SPIWP_SD3_IO,true));
  870. cJSON_AddItemToArray(list,get_gpio_entry(spihd_sd2_io,psram_dev,PSRAM_HSPI_SPIHD_SD2_IO,true));
  871. } else {
  872. cJSON_AddItemToArray(list,get_gpio_entry(spiq_sd0_io,psram_dev,EFUSE_SPICONFIG_RET_SPIQ(spiconfig),true));
  873. cJSON_AddItemToArray(list,get_gpio_entry(spid_sd1_io,psram_dev,EFUSE_SPICONFIG_RET_SPID(spiconfig),true));
  874. cJSON_AddItemToArray(list,get_gpio_entry(spihd_sd2_io,psram_dev,EFUSE_SPICONFIG_RET_SPIHD(spiconfig),true));
  875. // If flash mode is set to QIO or QOUT, the WP pin is equal the value configured in bootloader.
  876. // If flash mode is set to DIO or DOUT, the WP pin should config it via menuconfig.
  877. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_FLASHMODE_QOUT
  878. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,CONFIG_BOOTLOADER_SPI_WP_PIN,true));
  879. #else
  880. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,CONFIG_SPIRAM_SPIWP_SD3_PIN,true));
  881. #endif
  882. }
  883. if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) {
  884. cJSON_AddItemToArray(list,get_gpio_entry(clk,flash_dev,SPI_IOMUX_PIN_NUM_CLK,true));
  885. cJSON_AddItemToArray(list,get_gpio_entry(cs,flash_dev,SPI_IOMUX_PIN_NUM_CS,true));
  886. } else if (spiconfig == EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  887. cJSON_AddItemToArray(list,get_gpio_entry(clk,flash_dev,FLASH_HSPI_CLK_IO,true));
  888. cJSON_AddItemToArray(list,get_gpio_entry(cs,flash_dev,FLASH_HSPI_CS_IO,true));
  889. } else {
  890. cJSON_AddItemToArray(list,get_gpio_entry(clk,flash_dev,EFUSE_SPICONFIG_RET_SPICLK(spiconfig),true));
  891. cJSON_AddItemToArray(list,get_gpio_entry(cs,flash_dev,EFUSE_SPICONFIG_RET_SPICS0(spiconfig),true));
  892. }
  893. return llist;
  894. }
  895. /****************************************************************************************
  896. *
  897. */
  898. cJSON * get_gpio_list(bool refresh) {
  899. gpio_num_t gpio_num;
  900. if(gpio_list && !refresh){
  901. return gpio_list;
  902. }
  903. if(gpio_list){
  904. cJSON_Delete(gpio_list);
  905. }
  906. gpio_list= cJSON_CreateArray();
  907. char *bat_config = config_alloc_get_default(NVS_TYPE_STR, "bat_config", NULL, 0);
  908. if (bat_config) {
  909. int channel = -1;
  910. PARSE_PARAM(bat_config, "channel", '=', channel);
  911. if(channel != -1){
  912. if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
  913. cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,false));
  914. }
  915. }
  916. free(bat_config);
  917. }
  918. gpio_list=get_GPIO_nvs_list(gpio_list);
  919. gpio_list=get_SPDIF_GPIO(gpio_list,is_spdif_config_locked());
  920. gpio_list=get_Rotary_GPIO(gpio_list);
  921. gpio_list=get_Display_GPIO(gpio_list);
  922. gpio_list=get_SPI_GPIO(gpio_list);
  923. gpio_list=get_I2C_GPIO(gpio_list);
  924. gpio_list=get_DAC_GPIO(gpio_list);
  925. gpio_list=get_psram_gpio_list(gpio_list);
  926. return gpio_list;
  927. }