accessors.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 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 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. * Get IO expander config structure from config string
  453. */
  454. const gpio_exp_config_t* config_gpio_exp_get(int index) {
  455. char *nvs_item, *item, *p;
  456. static gpio_exp_config_t config;
  457. // re-initialize config every time
  458. memset(&config, 0, sizeof(config));
  459. config.intr = -1; config.count = 16; config.base = GPIO_NUM_MAX; config.phy.port = i2c_system_port; config.phy.host = spi_system_host;
  460. nvs_item = config_alloc_get(NVS_TYPE_STR, "gpio_exp_config");
  461. if (!nvs_item || !*nvs_item) return NULL;
  462. // search index items
  463. for (item = strtok(nvs_item, ";"); index && item; index--) {
  464. if ((item = strtok(NULL, ";")) == NULL) {
  465. free(nvs_item);
  466. return NULL;
  467. }
  468. }
  469. if ((p = strcasestr(item, "addr")) != NULL) config.phy.addr = atoi(strchr(p, '=') + 1);
  470. if ((p = strcasestr(item, "cs_pin")) != NULL) config.phy.cs_pin = atoi(strchr(p, '=') + 1);
  471. if ((p = strcasestr(item, "speed")) != NULL) config.phy.speed = atoi(strchr(p, '=') + 1);
  472. if ((p = strcasestr(item, "intr")) != NULL) config.intr = atoi(strchr(p, '=') + 1);
  473. if ((p = strcasestr(item, "base")) != NULL) config.base = atoi(strchr(p, '=') + 1);
  474. if ((p = strcasestr(item, "count")) != NULL) config.count = atoi(strchr(p, '=') + 1);
  475. if ((p = strcasestr(item, "model")) != NULL) sscanf(p, "%*[^=]=%31[^,]", config.model);
  476. if ((p = strcasestr(item, "port")) != NULL) {
  477. char port[8] = "";
  478. sscanf(p, "%*[^=]=%7[^,]", port);
  479. if (strcasestr(port, "dac")) config.phy.port = 0;
  480. }
  481. free(nvs_item);
  482. return &config;
  483. }
  484. /****************************************************************************************
  485. *
  486. */
  487. const gpio_with_level_t * get_gpio_struct_member(const char * nvs_item, const char * name){
  488. static gpio_with_level_t gpio_member={
  489. .gpio=-1,
  490. .level=0
  491. };
  492. if(!nvs_item) return &gpio_member;
  493. const char * p=nvs_item;
  494. char type[20]={0};
  495. int match=0;
  496. do {
  497. 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)){
  498. return &gpio_member;
  499. }
  500. p = strchr(p, ',');
  501. } while (p++);
  502. gpio_member.gpio=-1;
  503. gpio_member.level=0;
  504. return &gpio_member;
  505. }
  506. #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
  507. #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));}
  508. /****************************************************************************************
  509. *
  510. */
  511. const set_GPIO_struct_t * get_gpio_struct(){
  512. static set_GPIO_struct_t gpio_struct;
  513. char * nvs_item=config_alloc_get(NVS_TYPE_STR, "set_GPIO");
  514. #ifdef CONFIG_LED_GREEN_GPIO_LEVEL
  515. gpio_struct.green.level = CONFIG_LED_GREEN_GPIO_LEVEL;
  516. #endif
  517. #ifdef CONFIG_LED_GREEN_GPIO
  518. gpio_struct.green.gpio = CONFIG_LED_GREEN_GPIO;
  519. #endif
  520. #ifdef CONFIG_LED_RED_GPIO_LEVEL
  521. gpio_struct.green.level = CONFIG_LED_RED_GPIO_LEVEL;
  522. #endif
  523. #ifdef CONFIG_LED_RED_GPIO
  524. gpio_struct.red.gpio = CONFIG_LED_RED_GPIO;
  525. #endif
  526. if(nvs_item){
  527. HANDLE_GPIO_STRUCT_MEMBER(amp,false);
  528. #ifndef CONFIG_LED_LOCKED
  529. HANDLE_GPIO_STRUCT_MEMBER(green,false);
  530. HANDLE_GPIO_STRUCT_MEMBER(red,false);
  531. #endif
  532. HANDLE_GPIO_STRUCT_MEMBER(jack,false);
  533. HANDLE_GPIO_STRUCT_MEMBER(spkfault,false);
  534. HANDLE_GPIO_STRUCT_MEMBER(vcc,false);
  535. HANDLE_GPIO_STRUCT_MEMBER(gnd,false);
  536. HANDLE_GPIO_STRUCT_MEMBER(ir,false);
  537. free(nvs_item);
  538. }
  539. #ifdef CONFIG_LED_LOCKED
  540. gpio_struct.red.fixed=true;
  541. gpio_struct.green.fixed=true;
  542. #endif
  543. #ifdef CONFIG_JACK_LOCKED
  544. gpio_struct.jack.gpio=CONFIG_JACK_GPIO;
  545. gpio_struct.jack.fixed=true;
  546. gpio_struct.jack.level=CONFIG_JACK_GPIO_LEVEL;
  547. #endif
  548. #ifdef CONFIG_SPKFAULT_LOCKED
  549. gpio_struct.spkfault.gpio=CONFIG_SPKFAULT_GPIO;
  550. gpio_struct.spkfault.fixed=true;
  551. gpio_struct.spkfault.level=CONFIG_SPKFAULT_GPIO_LEVEL;
  552. #endif
  553. return &gpio_struct;
  554. }
  555. /****************************************************************************************
  556. *
  557. */
  558. const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
  559. char *nvs_item, *p;
  560. static spi_bus_config_t spi = {
  561. .mosi_io_num = -1,
  562. .sclk_io_num = -1,
  563. .miso_io_num = -1,
  564. .quadwp_io_num = -1,
  565. .quadhd_io_num = -1
  566. };
  567. nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
  568. if (nvs_item) {
  569. if ((p = strcasestr(nvs_item, "data")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
  570. if ((p = strcasestr(nvs_item, "mosi")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
  571. if ((p = strcasestr(nvs_item, "miso")) != NULL) spi.miso_io_num = atoi(strchr(p, '=') + 1);
  572. if ((p = strcasestr(nvs_item, "clk")) != NULL) spi.sclk_io_num = atoi(strchr(p, '=') + 1);
  573. if ((p = strcasestr(nvs_item, "dc")) != NULL) spi_system_dc_gpio = atoi(strchr(p, '=') + 1);
  574. // only VSPI (1) can be used as Flash and PSRAM run at 80MHz
  575. // if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1);
  576. free(nvs_item);
  577. }
  578. if(spi_host) *spi_host = spi_system_host;
  579. return &spi;
  580. }
  581. /****************************************************************************************
  582. *
  583. */
  584. void parse_set_GPIO(void (*cb)(int gpio, char *value)) {
  585. char *nvs_item, *p, type[16];
  586. int gpio;
  587. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) == NULL) return;
  588. p = nvs_item;
  589. do {
  590. if (sscanf(p, "%d=%15[^,]", &gpio, type) > 0) cb(gpio, type);
  591. p = strchr(p, ',');
  592. } while (p++);
  593. free(nvs_item);
  594. }
  595. /****************************************************************************************
  596. *
  597. */
  598. const rotary_struct_t * config_rotary_get() {
  599. static rotary_struct_t rotary={ .A = -1, .B = -1, .SW = -1, .longpress = false, .knobonly=false,.timer=0,.volume_lock=false};
  600. char *config = config_alloc_get_default(NVS_TYPE_STR, "rotary_config", NULL, 0);
  601. if (config && *config) {
  602. char *p;
  603. // parse config
  604. if ((p = strcasestr(config, "A")) != NULL) rotary.A = atoi(strchr(p, '=') + 1);
  605. if ((p = strcasestr(config, "B")) != NULL) rotary.B = atoi(strchr(p, '=') + 1);
  606. if ((p = strcasestr(config, "SW")) != NULL) rotary.SW = atoi(strchr(p, '=') + 1);
  607. if ((p = strcasestr(config, "knobonly")) != NULL) {
  608. p = strchr(p, '=');
  609. rotary.knobonly = true;
  610. rotary.timer = p ? atoi(p + 1) : 350;
  611. rotary.longpress = false;
  612. } else {
  613. rotary.knobonly = false;
  614. rotary.timer = 0;
  615. if ((p = strcasestr(config, "volume")) != NULL) rotary.volume_lock = true;
  616. if ((p = strcasestr(config, "longpress")) != NULL) rotary.longpress = true;
  617. }
  618. free(config);
  619. }
  620. return &rotary;
  621. }
  622. /****************************************************************************************
  623. *
  624. */
  625. cJSON * get_gpio_entry(const char * name, const char * prefix, int gpio, bool fixed){
  626. cJSON * entry = cJSON_CreateObject();
  627. cJSON_AddNumberToObject(entry,"gpio",gpio);
  628. cJSON_AddStringToObject(entry,"name",name);
  629. cJSON_AddStringToObject(entry,"group",prefix);
  630. cJSON_AddBoolToObject(entry,"fixed",fixed);
  631. return entry;
  632. }
  633. /****************************************************************************************
  634. *
  635. */
  636. cJSON * add_gpio_for_value(cJSON * list,const char * name,int gpio, const char * prefix, bool fixed){
  637. cJSON * llist = list?list:cJSON_CreateArray();
  638. if(GPIO_IS_VALID_GPIO(gpio) && gpio>0){
  639. cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpio,fixed));
  640. }
  641. return llist;
  642. }
  643. /****************************************************************************************
  644. *
  645. */
  646. cJSON * add_gpio_for_name(cJSON * list,const char * nvs_entry,const char * name, const char * prefix, bool fixed){
  647. cJSON * llist = list?list:cJSON_CreateArray();
  648. char *p;
  649. int gpioNum=0;
  650. if ((p = strcasestr(nvs_entry, name)) != NULL) {
  651. gpioNum = atoi(strchr(p, '=') + 1);
  652. if(gpioNum>=0){
  653. cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpioNum,fixed));
  654. }
  655. }
  656. return llist;
  657. }
  658. /****************************************************************************************
  659. *
  660. */
  661. cJSON * get_GPIO_nvs_list(cJSON * list) {
  662. cJSON * ilist = list?list:cJSON_CreateArray();
  663. const set_GPIO_struct_t * gpios = get_gpio_struct();
  664. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,vcc,"other");
  665. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,gnd,"other");
  666. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,amp,"other");
  667. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,ir,"other");
  668. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,jack,"other");
  669. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,green,"other");
  670. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,red,"other");
  671. ADD_GPIO_STRUCT_MEMBER_TO_ARRAY(ilist,gpios,spkfault,"other");
  672. return ilist;
  673. }
  674. /****************************************************************************************
  675. *
  676. */
  677. cJSON * get_DAC_GPIO(cJSON * list){
  678. cJSON * llist = list;
  679. if(!llist){
  680. llist = cJSON_CreateArray();
  681. }
  682. const i2s_platform_config_t * i2s_config= config_dac_get();
  683. if(i2s_config->pin.bck_io_num>=0){
  684. cJSON_AddItemToArray(llist,get_gpio_entry("bck","dac",i2s_config->pin.bck_io_num,is_dac_config_locked()));
  685. cJSON_AddItemToArray(llist,get_gpio_entry("ws","dac",i2s_config->pin.ws_io_num,is_dac_config_locked()));
  686. cJSON_AddItemToArray(llist,get_gpio_entry("do","dac",i2s_config->pin.data_out_num,is_dac_config_locked()));
  687. if(i2s_config->sda>=0){
  688. cJSON_AddItemToArray(llist,get_gpio_entry("sda","dac",i2s_config->sda,is_dac_config_locked()));
  689. cJSON_AddItemToArray(llist,get_gpio_entry("scl","dac",i2s_config->scl,is_dac_config_locked()));
  690. }
  691. if(i2s_config->mute_gpio>=0){
  692. cJSON_AddItemToArray(llist,get_gpio_entry("mute","dac",i2s_config->mute_gpio,is_dac_config_locked()));
  693. }
  694. }
  695. return llist;
  696. }
  697. /****************************************************************************************
  698. *
  699. */
  700. cJSON * get_Display_GPIO(cJSON * list){
  701. cJSON * llist = list;
  702. if(!llist){
  703. llist = cJSON_CreateArray();
  704. }
  705. const display_config_t * config= config_display_get();
  706. if(config->back >=0){
  707. cJSON_AddItemToArray(llist,get_gpio_entry("backlight","display",config->back,false));
  708. }
  709. if(config->CS_pin >=0){
  710. cJSON_AddItemToArray(llist,get_gpio_entry("CS","display",config->CS_pin,false));
  711. }
  712. if(config->RST_pin >=0){
  713. cJSON_AddItemToArray(llist,get_gpio_entry("reset","display",config->RST_pin,false));
  714. }
  715. return llist;
  716. }
  717. /****************************************************************************************
  718. *
  719. */
  720. cJSON * get_I2C_GPIO(cJSON * list){
  721. cJSON * llist = list;
  722. if(!llist){
  723. llist = cJSON_CreateArray();
  724. }
  725. int port=0;
  726. const i2c_config_t * i2c_config = config_i2c_get(&port);
  727. if(i2c_config->scl_io_num>=0){
  728. cJSON_AddItemToArray(llist,get_gpio_entry("scl","i2c",i2c_config->scl_io_num,false));
  729. cJSON_AddItemToArray(llist,get_gpio_entry("sda","i2c",i2c_config->sda_io_num,false));
  730. }
  731. return llist;
  732. }
  733. /****************************************************************************************
  734. *
  735. */
  736. cJSON * get_SPI_GPIO(cJSON * list){
  737. cJSON * llist = list;
  738. if(!llist){
  739. llist = cJSON_CreateArray();
  740. }
  741. spi_host_device_t spi_host;
  742. const spi_bus_config_t * spi_config = config_spi_get(&spi_host);
  743. if(spi_config->miso_io_num>=0){
  744. cJSON_AddItemToArray(llist,get_gpio_entry("data","spi",spi_config->miso_io_num,false));
  745. cJSON_AddItemToArray(llist,get_gpio_entry("data","clk",spi_config->sclk_io_num,false));
  746. }
  747. if(spi_system_dc_gpio>0){
  748. cJSON_AddItemToArray(llist,get_gpio_entry("data","dc",spi_system_dc_gpio,false));
  749. }
  750. return llist;
  751. }
  752. /****************************************************************************************
  753. *
  754. */
  755. cJSON * get_SPDIF_GPIO(cJSON * list, bool fixed){
  756. cJSON * llist = list?list:cJSON_CreateArray();
  757. char * spdif_config = config_spdif_get_string();
  758. if(spdif_config){
  759. llist = add_gpio_for_name(llist,spdif_config,"bck", "spdif", fixed);
  760. llist = add_gpio_for_name(llist,spdif_config,"ws", "spdif", fixed);
  761. llist = add_gpio_for_name(llist,spdif_config,"do", "spdif", fixed);
  762. free(spdif_config);
  763. }
  764. return llist;
  765. }
  766. /****************************************************************************************
  767. *
  768. */
  769. cJSON * get_Rotary_GPIO(cJSON * list){
  770. cJSON * llist = list?list:cJSON_CreateArray();
  771. const rotary_struct_t *rotary= config_rotary_get();
  772. add_gpio_for_value(llist,"A",rotary->A, "rotary", false);
  773. add_gpio_for_value(llist,"B",rotary->B, "rotary", false);
  774. add_gpio_for_value(llist,"SW",rotary->SW, "rotary", false);
  775. return llist;
  776. }
  777. /****************************************************************************************
  778. *
  779. */
  780. esp_err_t get_gpio_structure(cJSON * gpio_entry, gpio_entry_t ** gpio){
  781. esp_err_t err = ESP_OK;
  782. *gpio = malloc(sizeof(gpio_entry_t));
  783. cJSON * val = cJSON_GetObjectItem(gpio_entry,"gpio");
  784. if(val){
  785. (*gpio)->gpio= (int)val->valuedouble;
  786. } else {
  787. ESP_LOGE(TAG,"gpio pin not found");
  788. err=ESP_FAIL;
  789. }
  790. val = cJSON_GetObjectItem(gpio_entry,"name");
  791. if(val){
  792. (*gpio)->name= strdup(cJSON_GetStringValue(val));
  793. } else {
  794. ESP_LOGE(TAG,"gpio name value not found");
  795. err=ESP_FAIL;
  796. }
  797. val = cJSON_GetObjectItem(gpio_entry,"group");
  798. if(val){
  799. (*gpio)->group= strdup(cJSON_GetStringValue(val));
  800. } else {
  801. ESP_LOGE(TAG,"gpio group value not found");
  802. err=ESP_FAIL;
  803. }
  804. val = cJSON_GetObjectItem(gpio_entry,"fixed");
  805. if(val){
  806. (*gpio)->fixed= cJSON_IsTrue(val);
  807. } else {
  808. ESP_LOGE(TAG,"gpio fixed indicator not found");
  809. err=ESP_FAIL;
  810. }
  811. return err;
  812. }
  813. /****************************************************************************************
  814. *
  815. */
  816. esp_err_t free_gpio_entry( gpio_entry_t ** gpio) {
  817. if(* gpio){
  818. free((* gpio)->name);
  819. free((* gpio)->group);
  820. free(* gpio);
  821. * gpio=NULL;
  822. return ESP_OK;
  823. }
  824. return ESP_FAIL;
  825. }
  826. /****************************************************************************************
  827. *
  828. */
  829. gpio_entry_t * get_gpio_by_no(int gpionum, bool refresh){
  830. cJSON * gpio_header=NULL;
  831. gpio_entry_t * gpio=NULL;
  832. get_gpio_list(refresh);
  833. cJSON_ArrayForEach(gpio_header,gpio_list)
  834. {
  835. if(get_gpio_structure(gpio_header, &gpio)==ESP_OK && gpio->gpio==gpionum){
  836. ESP_LOGD(TAG,"Found GPIO: %s=%d %s", gpio->name,gpio->gpio,gpio->fixed?"(FIXED)":"(VARIABLE)");
  837. }
  838. }
  839. return gpio;
  840. }
  841. /****************************************************************************************
  842. *
  843. */
  844. gpio_entry_t * get_gpio_by_name(char * name,char * group, bool refresh){
  845. cJSON * gpio_header=NULL;
  846. gpio_entry_t * gpio=NULL;
  847. get_gpio_list(refresh);
  848. cJSON_ArrayForEach(gpio_header,gpio_list)
  849. {
  850. if(get_gpio_structure(gpio_header, &gpio)==ESP_OK && strcasecmp(gpio->name,name)&& strcasecmp(gpio->group,group)){
  851. ESP_LOGD(TAG,"Found GPIO: %s=%d %s", gpio->name,gpio->gpio,gpio->fixed?"(FIXED)":"(VARIABLE)");
  852. }
  853. }
  854. return gpio;
  855. }
  856. #ifndef PICO_PSRAM_CLK_IO
  857. #define PICO_PSRAM_CLK_IO 6
  858. #endif
  859. #ifndef PSRAM_SPIQ_SD0_IO
  860. #define PSRAM_SPIQ_SD0_IO 7
  861. #define PSRAM_SPID_SD1_IO 8
  862. #define PSRAM_SPIWP_SD3_IO 10
  863. #define PSRAM_SPIHD_SD2_IO 9
  864. #define FLASH_HSPI_CLK_IO 14
  865. #define FLASH_HSPI_CS_IO 15
  866. #define PSRAM_HSPI_SPIQ_SD0_IO 12
  867. #define PSRAM_HSPI_SPID_SD1_IO 13
  868. #define PSRAM_HSPI_SPIWP_SD3_IO 2
  869. #define PSRAM_HSPI_SPIHD_SD2_IO 4
  870. #endif
  871. cJSON * get_psram_gpio_list(cJSON * list){
  872. const char * psram_dev = "psram";
  873. const char * flash_dev = "flash";
  874. const char * clk = "clk";
  875. const char * cs = "cs";
  876. const char * spiq_sd0_io="spiq_sd0_io";
  877. const char * spid_sd1_io = "spid_sd1_io";
  878. const char * spiwp_sd3_io = "spiwp_sd3_io";
  879. const char * spihd_sd2_io = "spihd_sd2_io";
  880. cJSON * llist=list;
  881. uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG);
  882. uint32_t pkg_ver = chip_ver & 0x7;
  883. if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5) {
  884. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  885. if (cfg.tieh != RTC_VDDSDIO_TIEH_1_8V) {
  886. return llist;
  887. }
  888. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,CONFIG_D2WD_PSRAM_CLK_IO,true));
  889. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,CONFIG_D2WD_PSRAM_CS_IO,true));
  890. } else if ((pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) || (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4)) {
  891. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  892. if (cfg.tieh != RTC_VDDSDIO_TIEH_3_3V) {
  893. return llist;
  894. }
  895. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,PICO_PSRAM_CLK_IO,true));
  896. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,CONFIG_PICO_PSRAM_CS_IO,true));
  897. } else if ((pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ6) || (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ5)){
  898. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,CONFIG_D0WD_PSRAM_CLK_IO,true));
  899. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,CONFIG_D0WD_PSRAM_CS_IO,true));
  900. } else {
  901. ESP_LOGW(TAG, "Cant' determine GPIOs for PSRAM chip id: %d", pkg_ver);
  902. cJSON_AddItemToArray(list,get_gpio_entry(clk,psram_dev,-1,true));
  903. cJSON_AddItemToArray(list,get_gpio_entry(cs,psram_dev,-1,true));
  904. }
  905. const uint32_t spiconfig = ets_efuse_get_spiconfig();
  906. if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) {
  907. cJSON_AddItemToArray(list,get_gpio_entry(spiq_sd0_io,psram_dev,PSRAM_SPIQ_SD0_IO,true));
  908. cJSON_AddItemToArray(list,get_gpio_entry(spid_sd1_io,psram_dev,PSRAM_SPID_SD1_IO,true));
  909. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,PSRAM_SPIWP_SD3_IO,true));
  910. cJSON_AddItemToArray(list,get_gpio_entry(spihd_sd2_io,psram_dev,PSRAM_SPIHD_SD2_IO,true));
  911. } else if (spiconfig == EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  912. cJSON_AddItemToArray(list,get_gpio_entry(spiq_sd0_io,psram_dev,PSRAM_HSPI_SPIQ_SD0_IO,true));
  913. cJSON_AddItemToArray(list,get_gpio_entry(spid_sd1_io,psram_dev,PSRAM_HSPI_SPID_SD1_IO,true));
  914. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,PSRAM_HSPI_SPIWP_SD3_IO,true));
  915. cJSON_AddItemToArray(list,get_gpio_entry(spihd_sd2_io,psram_dev,PSRAM_HSPI_SPIHD_SD2_IO,true));
  916. } else {
  917. cJSON_AddItemToArray(list,get_gpio_entry(spiq_sd0_io,psram_dev,EFUSE_SPICONFIG_RET_SPIQ(spiconfig),true));
  918. cJSON_AddItemToArray(list,get_gpio_entry(spid_sd1_io,psram_dev,EFUSE_SPICONFIG_RET_SPID(spiconfig),true));
  919. cJSON_AddItemToArray(list,get_gpio_entry(spihd_sd2_io,psram_dev,EFUSE_SPICONFIG_RET_SPIHD(spiconfig),true));
  920. // If flash mode is set to QIO or QOUT, the WP pin is equal the value configured in bootloader.
  921. // If flash mode is set to DIO or DOUT, the WP pin should config it via menuconfig.
  922. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_FLASHMODE_QOUT
  923. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,CONFIG_BOOTLOADER_SPI_WP_PIN,true));
  924. #else
  925. cJSON_AddItemToArray(list,get_gpio_entry(spiwp_sd3_io,psram_dev,CONFIG_SPIRAM_SPIWP_SD3_PIN,true));
  926. #endif
  927. }
  928. if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) {
  929. cJSON_AddItemToArray(list,get_gpio_entry(clk,flash_dev,SPI_IOMUX_PIN_NUM_CLK,true));
  930. cJSON_AddItemToArray(list,get_gpio_entry(cs,flash_dev,SPI_IOMUX_PIN_NUM_CS,true));
  931. } else if (spiconfig == EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  932. cJSON_AddItemToArray(list,get_gpio_entry(clk,flash_dev,FLASH_HSPI_CLK_IO,true));
  933. cJSON_AddItemToArray(list,get_gpio_entry(cs,flash_dev,FLASH_HSPI_CS_IO,true));
  934. } else {
  935. cJSON_AddItemToArray(list,get_gpio_entry(clk,flash_dev,EFUSE_SPICONFIG_RET_SPICLK(spiconfig),true));
  936. cJSON_AddItemToArray(list,get_gpio_entry(cs,flash_dev,EFUSE_SPICONFIG_RET_SPICS0(spiconfig),true));
  937. }
  938. return llist;
  939. }
  940. /****************************************************************************************
  941. *
  942. */
  943. cJSON * get_gpio_list(bool refresh) {
  944. gpio_num_t gpio_num;
  945. if(gpio_list && !refresh){
  946. return gpio_list;
  947. }
  948. if(gpio_list){
  949. cJSON_Delete(gpio_list);
  950. }
  951. gpio_list= cJSON_CreateArray();
  952. #ifndef CONFIG_BAT_LOCKED
  953. char *bat_config = config_alloc_get_default(NVS_TYPE_STR, "bat_config", NULL, 0);
  954. if (bat_config) {
  955. char *p;
  956. int channel;
  957. if ((p = strcasestr(bat_config, "channel") ) != NULL) {
  958. channel = atoi(strchr(p, '=') + 1);
  959. if(channel != -1){
  960. if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
  961. cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,false));
  962. }
  963. }
  964. }
  965. free(bat_config);
  966. }
  967. #else
  968. if(adc1_pad_get_io_num(CONFIG_BAT_CHANNEL,&gpio_num )==ESP_OK){
  969. cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,true));
  970. }
  971. #endif
  972. gpio_list=get_GPIO_nvs_list(gpio_list);
  973. gpio_list=get_SPDIF_GPIO(gpio_list,is_spdif_config_locked());
  974. gpio_list=get_Rotary_GPIO(gpio_list);
  975. gpio_list=get_Display_GPIO(gpio_list);
  976. gpio_list=get_SPI_GPIO(gpio_list);
  977. gpio_list=get_I2C_GPIO(gpio_list);
  978. gpio_list=get_DAC_GPIO(gpio_list);
  979. gpio_list=get_psram_gpio_list(gpio_list);
  980. return gpio_list;
  981. }