accessors.c 38 KB

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