services.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 set_power_gpio(int gpio, char *value) {
  26. bool parsed = true;
  27. if (!strcasecmp(value, "vcc") ) {
  28. gpio_pad_select_gpio(gpio);
  29. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  30. gpio_set_level(gpio, 1);
  31. } else if (!strcasecmp(value, "gnd")) {
  32. gpio_pad_select_gpio(gpio);
  33. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  34. gpio_set_level(gpio, 0);
  35. } else parsed = false ;
  36. if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value);
  37. }
  38. /****************************************************************************************
  39. *
  40. */
  41. void services_init(void) {
  42. char *nvs_item;
  43. gpio_install_isr_service(0);
  44. #ifdef CONFIG_SQUEEZEAMP
  45. if (i2c_system_port == 0) {
  46. i2c_system_port = 1;
  47. ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP");
  48. }
  49. #endif
  50. // set potential power GPIO
  51. parse_set_GPIO(set_power_gpio);
  52. const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port);
  53. 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);
  54. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  55. i2c_param_config(i2c_system_port, i2c_config);
  56. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  57. } else {
  58. ESP_LOGE(TAG, "can't initialize I2C");
  59. }
  60. ESP_LOGD(TAG,"Configuring LEDs");
  61. led_svc_init();
  62. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  63. led_config(LED_RED, LED_RED_GPIO, 0);
  64. battery_svc_init();
  65. monitor_svc_init();
  66. }