accessors.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "config.h"
  13. #include "accessors.h"
  14. #include "globdefs.h"
  15. static const char *TAG = "services";
  16. #define min(a,b) (((a) < (b)) ? (a) : (b))
  17. /****************************************************************************************
  18. *
  19. */
  20. esp_err_t config_i2c_set(const i2c_config_t * config, int port){
  21. int buffer_size=255;
  22. char * config_buffer=calloc(buffer_size,1);
  23. if(config_buffer) {
  24. 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);
  25. ESP_LOGI(TAG,"Updating i2c configuration to %s",config_buffer);
  26. config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer);
  27. free(config_buffer);
  28. }
  29. return ESP_OK;
  30. }
  31. /****************************************************************************************
  32. *
  33. */
  34. const i2c_config_t * config_i2c_get(int * i2c_port) {
  35. char *nvs_item, *p;
  36. static i2c_config_t i2c = {
  37. .mode = I2C_MODE_MASTER,
  38. .sda_io_num = -1,
  39. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  40. .scl_io_num = -1,
  41. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  42. .master.clk_speed = 0,
  43. };
  44. i2c.master.clk_speed = i2c_system_speed;
  45. nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  46. if (nvs_item) {
  47. if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c.scl_io_num = atoi(strchr(p, '=') + 1);
  48. if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c.sda_io_num = atoi(strchr(p, '=') + 1);
  49. if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c.master.clk_speed = atoi(strchr(p, '=') + 1);
  50. if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
  51. free(nvs_item);
  52. }
  53. if(i2c_port) *i2c_port=i2c_system_port;
  54. return &i2c;
  55. }
  56. /****************************************************************************************
  57. *
  58. */
  59. const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
  60. char *nvs_item, *p;
  61. static spi_bus_config_t spi = {
  62. .mosi_io_num = -1,
  63. .sclk_io_num = -1,
  64. .miso_io_num = -1,
  65. .quadwp_io_num = -1,
  66. .quadhd_io_num = -1
  67. };
  68. nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
  69. if (nvs_item) {
  70. if ((p = strcasestr(nvs_item, "data")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
  71. if ((p = strcasestr(nvs_item, "clk")) != NULL) spi.sclk_io_num = atoi(strchr(p, '=') + 1);
  72. if ((p = strcasestr(nvs_item, "dc")) != NULL) spi_system_dc_gpio = atoi(strchr(p, '=') + 1);
  73. if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1);
  74. free(nvs_item);
  75. }
  76. if(spi_host) *spi_host = spi_system_host;
  77. return &spi;
  78. }
  79. /****************************************************************************************
  80. *
  81. */
  82. void parse_set_GPIO(void (*cb)(int gpio, char *value)) {
  83. char *nvs_item, *p, type[16];
  84. int gpio;
  85. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) == NULL) return;
  86. p = nvs_item;
  87. do {
  88. if (sscanf(p, "%d=%15[^,]", &gpio, type) > 0) cb(gpio, type);
  89. p = strchr(p, ',');
  90. } while (p++);
  91. free(nvs_item);
  92. }