services.c 4.5 KB

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