2
0

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. #include "accessors.h"
  17. extern void battery_svc_init(void);
  18. extern void monitor_svc_init(void);
  19. extern void led_svc_init(void);
  20. int i2c_system_port = I2C_SYSTEM_PORT;
  21. static const char *TAG = "services";
  22. /****************************************************************************************
  23. *
  24. */
  25. void services_init(void) {
  26. char *nvs_item;
  27. gpio_install_isr_service(0);
  28. #ifdef CONFIG_SQUEEZEAMP
  29. if (i2c_system_port == 0) {
  30. i2c_system_port = 1;
  31. ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP");
  32. }
  33. #endif
  34. // set fixed gpio if any
  35. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) != NULL) {
  36. char *p = nvs_item, type[4];
  37. int gpio;
  38. do {
  39. if (sscanf(p, "%d=%3[^,]", &gpio, type) > 0) {
  40. gpio_pad_select_gpio(gpio);
  41. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  42. if (!strcasecmp(type, "vcc")) gpio_set_level(gpio, 1);
  43. else if (!strcasecmp(type, "gnd")) gpio_set_level(gpio, 0);
  44. ESP_LOGI(TAG, "set GPIO %u to %s", gpio, type);
  45. }
  46. p = strchr(p, ',');
  47. } while (p++);
  48. free(nvs_item);
  49. }
  50. const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port);
  51. 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);
  52. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  53. i2c_param_config(i2c_system_port, i2c_config);
  54. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  55. } else {
  56. ESP_LOGE(TAG, "can't initialize I2C");
  57. }
  58. ESP_LOGD(TAG,"Configuring LEDs");
  59. led_svc_init();
  60. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  61. led_config(LED_RED, LED_RED_GPIO, 0);
  62. battery_svc_init();
  63. monitor_svc_init();
  64. }