accessors.c 41 KB

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