accessors.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #define LOG_LOCAL_LEVEL ESP_LOG_INFO
  8. #include <stdio.h>
  9. #include "esp_log.h"
  10. #include "driver/gpio.h"
  11. #include "driver/i2c.h"
  12. #include "driver/spi_master.h"
  13. #include "Config.h"
  14. #include "accessors.h"
  15. #include "globdefs.h"
  16. #include "display.h"
  17. #include "cJSON.h"
  18. #include "driver/gpio.h"
  19. #include "stdbool.h"
  20. #include "driver/adc.h"
  21. #include "esp_attr.h"
  22. #include "soc/spi_periph.h"
  23. #include "esp_err.h"
  24. #include "soc/rtc.h"
  25. #include "sdkconfig.h"
  26. #include "soc/efuse_periph.h"
  27. #include "driver/gpio.h"
  28. #include "driver/spi_common_internal.h"
  29. #if CONFIG_IDF_TARGET_ESP32
  30. #include "esp32/rom/efuse.h"
  31. #endif
  32. #include "tools.h"
  33. #include "monitor.h"
  34. #include "messaging.h"
  35. #include "network_ethernet.h"
  36. static const char *TAG = "services";
  37. const char *i2c_name_type="I2C";
  38. const char *spi_name_type="SPI";
  39. cJSON * gpio_list=NULL;
  40. #define min(a,b) (((a) < (b)) ? (a) : (b))
  41. #ifndef QUOTE
  42. #define QUOTE(name) #name
  43. #endif
  44. #ifndef STR
  45. #define STR(macro) QUOTE(macro)
  46. #endif
  47. extern cJSON * get_gpio_list(bool refresh);
  48. bool are_statistics_enabled(){
  49. #if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY) && defined (CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS)
  50. return true;
  51. #endif
  52. return false;
  53. }
  54. /****************************************************************************************
  55. *
  56. */
  57. // static char * config_spdif_get_string(){
  58. // // return config_alloc_get_str("spdif_config", CONFIG_SPDIF_CONFIG, "bck=" STR(CONFIG_SPDIF_BCK_IO)
  59. // // ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO));
  60. // config_alloc_get_str("spdif_config", CONFIG_SPDIF_CONFIG, "bck=" STR(CONFIG_SPDIF_BCK_IO)
  61. // ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO));
  62. // }
  63. /****************************************************************************************
  64. *
  65. */
  66. // static char * get_dac_config_string(){
  67. // return config_alloc_get_str("dac_config", CONFIG_DAC_CONFIG, "model=i2s,bck=" STR(CONFIG_I2S_BCK_IO)
  68. // ",ws=" STR(CONFIG_I2S_WS_IO) ",do=" STR(CONFIG_I2S_DO_IO)
  69. // ",sda=" STR(CONFIG_I2C_SDA) ",scl=" STR(CONFIG_I2C_SCL)
  70. // ",mute=" STR(CONFIG_MUTE_GPIO));
  71. // }
  72. /****************************************************************************************
  73. *
  74. */
  75. bool is_dac_config_locked(){
  76. #if ( defined CONFIG_DAC_CONFIG )
  77. if(strlen(CONFIG_DAC_CONFIG) > 0){
  78. return true;
  79. }
  80. #endif
  81. #if defined(CONFIG_I2S_BCK_IO) && CONFIG_I2S_BCK_IO>0
  82. return true;
  83. #endif
  84. return false;
  85. }
  86. const sys_i2c_bus* get_i2c_bus(i2c_port_t port){
  87. if(platform->dev.has_i2c && port == platform->dev.i2c.port-sys_i2c_port_PORT0 && platform->dev.i2c.scl>=0){
  88. return &platform->dev.i2c;
  89. }
  90. if(platform->dev.has_dac && platform->dev.dac.has_i2c && platform->dev.dac.i2c.port == port && platform->dev.dac.i2c.scl>=0){
  91. return &platform->dev.dac.i2c;
  92. }
  93. return NULL;
  94. }
  95. /****************************************************************************************
  96. *
  97. */
  98. const i2c_config_t * config_i2c_get(sys_i2c_bus * bus) {
  99. static i2c_config_t i2c = {
  100. .mode = I2C_MODE_MASTER,
  101. .sda_io_num = -1,
  102. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  103. .scl_io_num = -1,
  104. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  105. .master.clk_speed = 0,
  106. };
  107. if(bus && bus->scl >=0 && bus->sda >=0 ){
  108. if(bus->scl>=0){
  109. i2c.scl_io_num = bus->scl;
  110. }
  111. else {
  112. ESP_LOGE(TAG,"%s missing for i2c","SCL");
  113. }
  114. if(bus->sda>=0){
  115. i2c.sda_io_num= bus->sda;
  116. }
  117. else {
  118. ESP_LOGE(TAG,"%s missing for i2c","SDA");
  119. }
  120. if(bus->speed>0){
  121. i2c.master.clk_speed = bus->speed;
  122. }
  123. }
  124. return &i2c;
  125. }
  126. void config_set_gpio(int * pin, sys_gpio_config * gpio,bool has_value, const char * name, bool mandatory){
  127. if(has_value){
  128. ESP_LOGD(TAG, "Setting pin %d as %s", gpio->pin, name);
  129. if(pin) *pin= gpio->pin;
  130. }
  131. else if(mandatory) {
  132. ESP_LOGE(TAG,"Pin %s has no value",name);
  133. }
  134. else {
  135. ESP_LOGD(TAG,"Pin %s has no value",name);
  136. }
  137. }
  138. /****************************************************************************************
  139. *
  140. */
  141. const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
  142. // don't memset all to 0xff as it's more than just GPIO
  143. static spi_bus_config_t spi = {
  144. .mosi_io_num = -1,
  145. .sclk_io_num = -1,
  146. .miso_io_num = -1,
  147. .quadwp_io_num = -1,
  148. .quadhd_io_num = -1
  149. };
  150. if(platform->has_dev && platform->dev.has_spi){
  151. ESP_LOGD(TAG,"SPI Configuration found");
  152. spi.mosi_io_num = platform->dev.spi.mosi;
  153. spi.miso_io_num = platform->dev.spi.miso;
  154. spi.sclk_io_num = platform->dev.spi.clk;
  155. spi_system_dc_gpio = platform->dev.spi.dc;
  156. // only VSPI (1) can be used as Flash and PSRAM run at 80MHz
  157. if(platform->dev.spi.host!=sys_dev_common_hosts_NONE){
  158. spi_system_host = platform->dev.spi.host-sys_dev_common_hosts_Host0;
  159. }
  160. }
  161. else {
  162. ESP_LOGI(TAG,"SPI not configured");
  163. }
  164. if(spi_host) *spi_host = spi_system_host;
  165. return &spi;
  166. }