accessors.c 39 KB

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