dac_external.c 5.3 KB

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