dac_external.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include <freertos/FreeRTOS.h>
  12. #include <freertos/task.h>
  13. #include <driver/i2s.h>
  14. #include "driver/i2c.h"
  15. #include "esp_log.h"
  16. #include "cJSON.h"
  17. #include "config.h"
  18. #include "adac.h"
  19. static const char TAG[] = "DAC external";
  20. static void deinit(void) { }
  21. static void speaker(bool active) { }
  22. static void headset(bool active) { }
  23. static bool volume(unsigned left, unsigned right) { return false; }
  24. static void power(adac_power_e mode);
  25. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config);
  26. static bool i2c_json_execute(char *set);
  27. static esp_err_t i2c_write_reg(uint8_t reg, uint8_t val);
  28. static uint8_t i2c_read_reg(uint8_t reg);
  29. const struct adac_s dac_external = { "i2s", init, deinit, power, speaker, headset, volume };
  30. static int i2c_port, i2c_addr;
  31. static cJSON *i2c_json;
  32. /****************************************************************************************
  33. * init
  34. */
  35. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config) {
  36. char *p;
  37. i2c_port = i2c_port_num;
  38. // configure i2c
  39. i2c_config_t i2c_config = {
  40. .mode = I2C_MODE_MASTER,
  41. .sda_io_num = -1,
  42. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  43. .scl_io_num = -1,
  44. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  45. .master.clk_speed = 250000,
  46. };
  47. if ((p = strcasestr(config, "i2c")) != NULL) i2c_addr = atoi(strchr(p, '=') + 1);
  48. if ((p = strcasestr(config, "sda")) != NULL) i2c_config.sda_io_num = atoi(strchr(p, '=') + 1);
  49. if ((p = strcasestr(config, "scl")) != NULL) i2c_config.scl_io_num = atoi(strchr(p, '=') + 1);
  50. p = config_alloc_get_str("dac_controlset", CONFIG_DAC_CONTROLSET, NULL);
  51. i2c_json = cJSON_Parse(p);
  52. if (!i2c_addr || !i2c_json || i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) {
  53. if (p) free(p);
  54. ESP_LOGW(TAG, "No i2c controlset found");
  55. return true;
  56. }
  57. ESP_LOGI(TAG, "DAC uses I2C @%d with sda:%d, scl:%d", i2c_addr, i2c_config.sda_io_num, i2c_config.scl_io_num);
  58. // we have an I2C configured
  59. i2c_param_config(i2c_port, &i2c_config);
  60. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  61. if (!i2c_json_execute("init")) {
  62. ESP_LOGE(TAG, "could not intialize DAC");
  63. return false;
  64. }
  65. return true;
  66. }
  67. /****************************************************************************************
  68. * power
  69. */
  70. static void power(adac_power_e mode) {
  71. if (mode == ADAC_STANDBY || mode == ADAC_OFF) i2c_json_execute("poweroff");
  72. else i2c_json_execute("poweron");
  73. }
  74. /****************************************************************************************
  75. *
  76. */
  77. bool i2c_json_execute(char *set) {
  78. cJSON *json_set = cJSON_GetObjectItemCaseSensitive(i2c_json, set);
  79. cJSON *item;
  80. if (!json_set) return true;
  81. cJSON_ArrayForEach(item, json_set)
  82. {
  83. cJSON *reg = cJSON_GetObjectItemCaseSensitive(item, "reg");
  84. cJSON *val = cJSON_GetObjectItemCaseSensitive(item, "val");
  85. cJSON *mode = cJSON_GetObjectItemCaseSensitive(item, "mode");
  86. if (!reg || !val) continue;
  87. if (!mode) {
  88. i2c_write_reg(reg->valueint, val->valueint);
  89. } else if (!strcasecmp(mode->valuestring, "or")) {
  90. uint8_t data = i2c_read_reg(reg->valueint);
  91. data |= (uint8_t) val->valueint;
  92. i2c_write_reg(reg->valueint, data);
  93. } else if (!strcasecmp(mode->valuestring, "and")) {
  94. uint8_t data = i2c_read_reg(reg->valueint);
  95. data &= (uint8_t) val->valueint;
  96. i2c_write_reg(reg->valueint, data);
  97. }
  98. }
  99. return true;
  100. }
  101. /****************************************************************************************
  102. *
  103. */
  104. static esp_err_t i2c_write_reg(uint8_t reg, uint8_t val) {
  105. esp_err_t ret;
  106. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  107. i2c_master_start(cmd);
  108. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  109. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  110. i2c_master_write_byte(cmd, val, I2C_MASTER_NACK);
  111. i2c_master_stop(cmd);
  112. ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  113. i2c_cmd_link_delete(cmd);
  114. if (ret != ESP_OK) {
  115. ESP_LOGW(TAG, "I2C write failed");
  116. }
  117. return ret;
  118. }
  119. /****************************************************************************************
  120. *
  121. */
  122. static uint8_t i2c_read_reg(uint8_t reg) {
  123. esp_err_t ret;
  124. uint8_t data = 0;
  125. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  126. i2c_master_start(cmd);
  127. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  128. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  129. i2c_master_start(cmd);
  130. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  131. i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
  132. i2c_master_stop(cmd);
  133. ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  134. i2c_cmd_link_delete(cmd);
  135. if (ret != ESP_OK) {
  136. ESP_LOGW(TAG, "I2C read failed");
  137. }
  138. return data;
  139. }