services.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "gpio_exp.h"
  14. #include "battery.h"
  15. #include "led.h"
  16. #include "monitor.h"
  17. #include "globdefs.h"
  18. #include "accessors.h"
  19. #include "messaging.h"
  20. extern void battery_svc_init(void);
  21. extern void monitor_svc_init(void);
  22. extern void led_svc_init(void);
  23. int i2c_system_port = I2C_SYSTEM_PORT;
  24. int i2c_system_speed = 400000;
  25. int spi_system_host = SPI_SYSTEM_HOST;
  26. int spi_system_dc_gpio = -1;
  27. pwm_system_t pwm_system = {
  28. .timer = LEDC_TIMER_0,
  29. .base_channel = LEDC_CHANNEL_0,
  30. .max = (1 << LEDC_TIMER_13_BIT),
  31. };
  32. static const char *TAG = "services";
  33. /****************************************************************************************
  34. *
  35. */
  36. void set_chip_power_gpio(int gpio, char *value) {
  37. bool parsed = true;
  38. // we only parse on-chip GPIOs
  39. if (gpio >= GPIO_NUM_MAX) return;
  40. if (!strcasecmp(value, "vcc") ) {
  41. gpio_pad_select_gpio(gpio);
  42. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  43. gpio_set_level(gpio, 1);
  44. } else if (!strcasecmp(value, "gnd")) {
  45. gpio_pad_select_gpio(gpio);
  46. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  47. gpio_set_level(gpio, 0);
  48. } else parsed = false;
  49. if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value);
  50. }
  51. void set_exp_power_gpio(int gpio, char *value) {
  52. bool parsed = true;
  53. // we only parse on-chip GPIOs
  54. if (gpio < GPIO_NUM_MAX) return;
  55. if (!strcasecmp(value, "vcc") ) {
  56. gpio_exp_set_direction(gpio, GPIO_MODE_OUTPUT, NULL);
  57. gpio_exp_set_level(gpio, 1, true, NULL);
  58. } else if (!strcasecmp(value, "gnd")) {
  59. gpio_exp_set_direction(gpio, GPIO_MODE_OUTPUT, NULL);
  60. gpio_exp_set_level(gpio, 0, true, NULL);
  61. } else parsed = false;
  62. if (parsed) ESP_LOGI(TAG, "set expanded GPIO %u to %s", gpio, value);
  63. }
  64. /****************************************************************************************
  65. *
  66. */
  67. void services_init(void) {
  68. messaging_service_init();
  69. gpio_install_isr_service(0);
  70. #ifdef CONFIG_I2C_LOCKED
  71. if (i2c_system_port == 0) {
  72. i2c_system_port = 1;
  73. ESP_LOGE(TAG, "Port 0 is reserved for internal DAC use");
  74. }
  75. #endif
  76. // set potential power GPIO on chip first in case expanders are power using these
  77. parse_set_GPIO(set_chip_power_gpio);
  78. // shared I2C bus
  79. const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port);
  80. 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);
  81. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  82. i2c_param_config(i2c_system_port, i2c_config);
  83. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  84. } else {
  85. i2c_system_port = -1;
  86. ESP_LOGW(TAG, "no I2C configured");
  87. }
  88. const spi_bus_config_t * spi_config = config_spi_get((spi_host_device_t*) &spi_system_host);
  89. ESP_LOGI(TAG,"Configuring SPI mosi:%d miso:%d clk:%d host:%u dc:%d", spi_config->mosi_io_num, spi_config->miso_io_num, spi_config->sclk_io_num, spi_system_host, spi_system_dc_gpio);
  90. if (spi_config->mosi_io_num != -1 && spi_config->sclk_io_num != -1) {
  91. spi_bus_initialize( spi_system_host, spi_config, 1 );
  92. if (spi_system_dc_gpio != -1) {
  93. gpio_set_direction( spi_system_dc_gpio, GPIO_MODE_OUTPUT );
  94. gpio_set_level( spi_system_dc_gpio, 0 );
  95. } else {
  96. ESP_LOGW(TAG, "No DC GPIO set, SPI display will not work");
  97. }
  98. } else {
  99. spi_system_host = -1;
  100. ESP_LOGW(TAG, "no SPI configured");
  101. }
  102. // create GPIO expanders
  103. const gpio_exp_config_t* gpio_exp_config;
  104. for (int count = 0; (gpio_exp_config = config_gpio_exp_get(count)); count++) gpio_exp_create(gpio_exp_config);
  105. // now set potential power GPIO on expander
  106. parse_set_GPIO(set_exp_power_gpio);
  107. // system-wide PWM timer configuration
  108. ledc_timer_config_t pwm_timer = {
  109. .duty_resolution = LEDC_TIMER_13_BIT,
  110. .freq_hz = 5000,
  111. .speed_mode = LEDC_HIGH_SPEED_MODE,
  112. .timer_num = pwm_system.timer,
  113. };
  114. ledc_timer_config(&pwm_timer);
  115. led_svc_init();
  116. battery_svc_init();
  117. monitor_svc_init();
  118. }