accessors.c 41 KB

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