services.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "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. /****************************************************************************************
  22. *
  23. */
  24. void services_init(void) {
  25. int scl = -1, sda = -1, i2c_speed = 250000;
  26. char *nvs_item, *p;
  27. gpio_install_isr_service(0);
  28. nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  29. if (nvs_item) {
  30. if ((p = strcasestr(nvs_item, "scl")) != NULL) scl = atoi(strchr(p, '=') + 1);
  31. if ((p = strcasestr(nvs_item, "sda")) != NULL) sda = atoi(strchr(p, '=') + 1);
  32. if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c_speed = atoi(strchr(p, '=') + 1);
  33. if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
  34. free(nvs_item);
  35. }
  36. #ifdef CONFIG_SQUEEZEAMP
  37. if (i2c_system_port == 0) {
  38. i2c_system_port = 1;
  39. ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP");
  40. }
  41. #endif
  42. ESP_LOGI(TAG,"Configuring I2C sda:%d scl:%d port:%u speed:%u", sda, scl, i2c_system_port, i2c_speed);
  43. if (sda != -1 && scl != -1) {
  44. i2c_config_t i2c = { 0 };
  45. i2c.mode = I2C_MODE_MASTER;
  46. i2c.sda_io_num = sda;
  47. i2c.sda_pullup_en = GPIO_PULLUP_ENABLE;
  48. i2c.scl_io_num = scl;
  49. i2c.scl_pullup_en = GPIO_PULLUP_ENABLE;
  50. i2c.master.clk_speed = i2c_speed;
  51. i2c_param_config(i2c_system_port, &i2c);
  52. i2c_driver_install(i2c_system_port, i2c.mode, 0, 0, 0 );
  53. } else {
  54. ESP_LOGE(TAG, "can't initialize I2C");
  55. }
  56. ESP_LOGD(TAG,"Configuring LEDs");
  57. led_svc_init();
  58. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  59. led_config(LED_RED, LED_RED_GPIO, 0);
  60. battery_svc_init();
  61. monitor_svc_init();
  62. }