services.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "services.h"
  11. #include "config.h"
  12. #include "battery.h"
  13. #include "led.h"
  14. #include "monitor.h"
  15. #include "globdefs.h"
  16. extern void battery_svc_init(void);
  17. extern void monitor_svc_init(void);
  18. extern void led_svc_init(void);
  19. int i2c_system_port = I2C_SYSTEM_PORT;
  20. static const char *TAG = "services";
  21. esp_err_t services_store_i2c_config(const i2c_config_t * config, int i2c_system_port){
  22. int buffer_size=255;
  23. char * config_buffer=malloc(buffer_size);
  24. memset(config_buffer,0x00,buffer_size);
  25. if(config_buffer) {
  26. 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,i2c_system_port);
  27. ESP_LOGI(TAG,"Updating i2c configuration to %s",config_buffer);
  28. config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer);
  29. free(config_buffer);
  30. }
  31. return ESP_OK;
  32. }
  33. const i2c_config_t * services_get_i2c_config(int * i2c_port) {
  34. char *nvs_item, *p;
  35. static i2c_config_t i2c = {
  36. .mode = I2C_MODE_MASTER,
  37. .sda_io_num = -1,
  38. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  39. .scl_io_num = -1,
  40. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  41. .master.clk_speed = 400000
  42. };
  43. nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  44. if (nvs_item) {
  45. if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c.scl_io_num = atoi(strchr(p, '=') + 1);
  46. if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c.sda_io_num = atoi(strchr(p, '=') + 1);
  47. if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c.master.clk_speed = atoi(strchr(p, '=') + 1);
  48. if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
  49. free(nvs_item);
  50. }
  51. if(i2c_port) *i2c_port=i2c_system_port;
  52. return &i2c;
  53. }
  54. /****************************************************************************************
  55. *
  56. */
  57. void services_init(void) {
  58. gpio_install_isr_service(0);
  59. const i2c_config_t * i2c_config = services_get_i2c_config(&i2c_system_port);
  60. #ifdef CONFIG_SQUEEZEAMP
  61. if (i2c_system_port == 0) {
  62. i2c_system_port = 1;
  63. ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP");
  64. }
  65. #endif
  66. ESP_LOGI(TAG,"Configuring I2C sda:%d scl:%d port:%u speed:%u", i2c_config->sda_io_num,i2c_config->scl_io_num, i2c_system_port, i2c_config->master.clk_speed);
  67. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  68. i2c_param_config(i2c_system_port, i2c_config);
  69. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  70. } else {
  71. ESP_LOGE(TAG, "can't initialize I2C");
  72. }
  73. ESP_LOGD(TAG,"Configuring LEDs");
  74. led_svc_init();
  75. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  76. led_config(LED_RED, LED_RED_GPIO, 0);
  77. battery_svc_init();
  78. monitor_svc_init();
  79. }