accessors.c 36 KB

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