dac_external.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "platform_config.h"
  18. #include "adac.h"
  19. static const char TAG[] = "DAC external";
  20. static void speaker(bool active);
  21. static void headset(bool active);
  22. static bool volume(unsigned left, unsigned right) { return false; }
  23. static void power(adac_power_e mode);
  24. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config);
  25. static bool i2c_json_execute(char *set);
  26. const struct adac_s dac_external = { "i2s", init, adac_deinit, power, speaker, headset, volume };
  27. static cJSON *i2c_json;
  28. static int i2c_addr;
  29. static struct {
  30. char *model;
  31. bool mclk;
  32. char *controlset;
  33. } codecs[] = {
  34. { "es8388", true,
  35. "{\"init\":[ \
  36. {\"reg\":8,\"val\":0}, {\"reg\":2,\"val\":243}, {\"reg\":43,\"val\":128}, {\"reg\":0,\"val\":5}, \
  37. {\"reg\":1,\"val\":64}, {\"reg\":4,\"val\":60},"
  38. #if BYTES_PER_FRAME == 8
  39. "{\"reg\":23,\"val\":32},"
  40. #else
  41. "{\"reg\":23,\"val\":24},"
  42. #endif
  43. "{\"reg\":24,\"val\":2}, \
  44. {\"reg\":26,\"val\":0}, {\"reg\":27,\"val\":0}, {\"reg\":25,\"val\":50}, {\"reg\":38,\"val\":0}, \
  45. {\"reg\":39,\"val\":184}, {\"reg\":42,\"val\":184}, {\"reg\":46,\"val\":30}, {\"reg\":47,\"val\":30}, \
  46. {\"reg\":48,\"val\":30}, {\"reg\":49,\"val\":30}, {\"reg\":2,\"val\":170}]}" },
  47. { NULL, false, NULL }
  48. };
  49. /****************************************************************************************
  50. * init
  51. */
  52. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config) {
  53. char *p;
  54. i2c_addr = adac_init(config, i2c_port_num);
  55. if (!i2c_addr) return true;
  56. ESP_LOGI(TAG, "DAC on I2C @%d", i2c_addr);
  57. p = config_alloc_get_str("dac_controlset", CONFIG_DAC_CONTROLSET, NULL);
  58. if ((!p || !*p) && (p = strcasestr(config, "model")) != NULL) {
  59. char model[32] = "";
  60. int i;
  61. sscanf(p, "%*[^=]=%31[^,]", model);
  62. for (i = 0; *model && ((p = codecs[i].controlset) != NULL) && strcasecmp(codecs[i].model, model); i++);
  63. if (p && codecs[i].mclk) {
  64. ESP_LOGI(TAG, "Configuring MCLK on GPIO0");
  65. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  66. REG_WRITE(PIN_CTRL, 0xFFFFFFF0);
  67. }
  68. }
  69. i2c_json = cJSON_Parse(p);
  70. if (!i2c_json) {
  71. ESP_LOGW(TAG, "no i2c controlset found");
  72. return true;
  73. }
  74. if (!i2c_json_execute("init")) {
  75. ESP_LOGE(TAG, "could not intialize DAC");
  76. return false;
  77. }
  78. return true;
  79. }
  80. /****************************************************************************************
  81. * power
  82. */
  83. static void power(adac_power_e mode) {
  84. if (mode == ADAC_STANDBY || mode == ADAC_OFF) i2c_json_execute("poweroff");
  85. else i2c_json_execute("poweron");
  86. }
  87. /****************************************************************************************
  88. * speaker
  89. */
  90. static void speaker(bool active) {
  91. if (active) i2c_json_execute("speakeron");
  92. else i2c_json_execute("speakeroff");
  93. }
  94. /****************************************************************************************
  95. * headset
  96. */
  97. static void headset(bool active) {
  98. if (active) i2c_json_execute("headseton");
  99. else i2c_json_execute("headsetoff");
  100. }
  101. /****************************************************************************************
  102. *
  103. */
  104. bool i2c_json_execute(char *set) {
  105. cJSON *json_set = cJSON_GetObjectItemCaseSensitive(i2c_json, set);
  106. cJSON *item;
  107. if (!json_set) return true;
  108. cJSON_ArrayForEach(item, json_set) {
  109. cJSON *reg = cJSON_GetObjectItemCaseSensitive(item, "reg");
  110. cJSON *val = cJSON_GetObjectItemCaseSensitive(item, "val");
  111. if (cJSON_IsArray(val)) {
  112. cJSON *value;
  113. uint8_t *data = malloc(cJSON_GetArraySize(val));
  114. int count = 0;
  115. if (!data) continue;
  116. cJSON_ArrayForEach(value, val) {
  117. data[count++] = value->valueint;
  118. }
  119. adac_write(i2c_addr, reg->valueint, data, count);
  120. free(data);
  121. } else {
  122. cJSON *mode = cJSON_GetObjectItemCaseSensitive(item, "mode");
  123. if (!reg || !val) continue;
  124. if (!mode) {
  125. adac_write_byte(i2c_addr, reg->valueint, val->valueint);
  126. } else if (!strcasecmp(mode->valuestring, "or")) {
  127. uint8_t data = adac_read_byte(i2c_addr,reg->valueint);
  128. data |= (uint8_t) val->valueint;
  129. adac_write_byte(i2c_addr, reg->valueint, data);
  130. } else if (!strcasecmp(mode->valuestring, "and")) {
  131. uint8_t data = adac_read_byte(i2c_addr, reg->valueint);
  132. data &= (uint8_t) val->valueint;
  133. adac_write_byte(i2c_addr, reg->valueint, data);
  134. }
  135. }
  136. }
  137. return true;
  138. }