services.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/ledc.h"
  11. #include "driver/i2c.h"
  12. #include "platform_config.h"
  13. #include "battery.h"
  14. #include "led.h"
  15. #include "monitor.h"
  16. #include "globdefs.h"
  17. #include "accessors.h"
  18. #include "messaging.h"
  19. extern void battery_svc_init(void);
  20. extern void monitor_svc_init(void);
  21. extern void led_svc_init(void);
  22. int i2c_system_port = I2C_SYSTEM_PORT;
  23. int i2c_system_speed = 400000;
  24. int spi_system_host = SPI_SYSTEM_HOST;
  25. int spi_system_dc_gpio = -1;
  26. pwm_system_t pwm_system = {
  27. .timer = LEDC_TIMER_0,
  28. .base_channel = LEDC_CHANNEL_0,
  29. .max = (1 << LEDC_TIMER_13_BIT),
  30. };
  31. static const char *TAG = "services";
  32. /****************************************************************************************
  33. *
  34. */
  35. void set_power_gpio(int gpio, char *value) {
  36. bool parsed = true;
  37. if (!strcasecmp(value, "vcc") ) {
  38. gpio_pad_select_gpio(gpio);
  39. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  40. gpio_set_level(gpio, 1);
  41. } else if (!strcasecmp(value, "gnd")) {
  42. gpio_pad_select_gpio(gpio);
  43. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  44. gpio_set_level(gpio, 0);
  45. } else parsed = false ;
  46. if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value);
  47. }
  48. /****************************************************************************************
  49. *
  50. */
  51. void services_init(void) {
  52. messaging_service_init();
  53. gpio_install_isr_service(0);
  54. #ifdef CONFIG_I2C_LOCKED
  55. if (i2c_system_port == 0) {
  56. i2c_system_port = 1;
  57. ESP_LOGE(TAG, "Port 0 is reserved for internal DAC use");
  58. }
  59. #endif
  60. // set potential power GPIO
  61. parse_set_GPIO(set_power_gpio);
  62. // shared I2C bus
  63. const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port);
  64. 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);
  65. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  66. i2c_param_config(i2c_system_port, i2c_config);
  67. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  68. } else {
  69. i2c_system_port = -1;
  70. ESP_LOGW(TAG, "no I2C configured");
  71. }
  72. const spi_bus_config_t * spi_config = config_spi_get((spi_host_device_t*) &spi_system_host);
  73. ESP_LOGI(TAG,"Configuring SPI data:%d clk:%d host:%u dc:%d", spi_config->mosi_io_num, spi_config->sclk_io_num, spi_system_host, spi_system_dc_gpio);
  74. if (spi_config->mosi_io_num != -1 && spi_config->sclk_io_num != -1) {
  75. spi_bus_initialize( spi_system_host, spi_config, 1 );
  76. if (spi_system_dc_gpio != -1) {
  77. gpio_set_direction( spi_system_dc_gpio, GPIO_MODE_OUTPUT );
  78. gpio_set_level( spi_system_dc_gpio, 0 );
  79. } else {
  80. ESP_LOGW(TAG, "No DC GPIO set, SPI display will not work");
  81. }
  82. } else {
  83. spi_system_host = -1;
  84. ESP_LOGW(TAG, "no SPI configured");
  85. }
  86. // system-wide PWM timer configuration
  87. ledc_timer_config_t pwm_timer = {
  88. .duty_resolution = LEDC_TIMER_13_BIT,
  89. .freq_hz = 5000,
  90. .speed_mode = LEDC_HIGH_SPEED_MODE,
  91. .timer_num = pwm_system.timer,
  92. };
  93. ledc_timer_config(&pwm_timer);
  94. led_svc_init();
  95. battery_svc_init();
  96. monitor_svc_init();
  97. }