accessors.c 39 KB

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