dac_57xx.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <string.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "driver/i2s.h"
  15. #include "driver/i2c.h"
  16. #include "driver/gpio.h"
  17. #include "esp_log.h"
  18. #include "adac.h"
  19. #define TAS575x (0x98 >> 1)
  20. #define TAS578x (0x90 >> 1)
  21. static const char TAG[] = "TAS575x/8x";
  22. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config);
  23. static void speaker(bool active);
  24. static void headset(bool active);
  25. static bool volume(unsigned left, unsigned right);
  26. static void power(adac_power_e mode);
  27. const struct adac_s dac_tas57xx = { "TAS57xx", init, adac_deinit, power, speaker, headset, volume };
  28. struct tas57xx_cmd_s {
  29. uint8_t reg;
  30. uint8_t value;
  31. };
  32. static const struct tas57xx_cmd_s tas57xx_init_sequence[] = {
  33. { 0x00, 0x00 }, // select page 0
  34. { 0x02, 0x10 }, // standby
  35. { 0x0d, 0x10 }, // use SCK for PLL
  36. { 0x25, 0x08 }, // ignore SCK halt
  37. { 0x08, 0x10 }, // Mute control enable (from TAS5780)
  38. { 0x54, 0x02 }, // Mute output control (from TAS5780)
  39. #if BYTES_PER_FRAME == 8
  40. { 0x28, 0x03 }, // I2S length 32 bits
  41. #else
  42. { 0x28, 0x00 }, // I2S length 16 bits
  43. #endif
  44. { 0x02, 0x00 }, // restart
  45. { 0xff, 0xff } // end of table
  46. };
  47. // matching orders
  48. typedef enum { TAS57_ACTIVE = 0, TAS57_STANDBY, TAS57_DOWN, TAS57_ANALOGUE_OFF, TAS57_ANALOGUE_ON, TAS57_VOLUME } dac_cmd_e;
  49. static const struct tas57xx_cmd_s tas57xx_cmd[] = {
  50. { 0x02, 0x00 }, // TAS57_ACTIVE
  51. { 0x02, 0x10 }, // TAS57_STANDBY
  52. { 0x02, 0x01 }, // TAS57_DOWN
  53. { 0x56, 0x10 }, // TAS57_ANALOGUE_OFF
  54. { 0x56, 0x00 }, // TAS57_ANALOGUE_ON
  55. };
  56. static uint8_t tas57_addr;
  57. static void dac_cmd(dac_cmd_e cmd, ...);
  58. static int tas57_detect(void);
  59. /****************************************************************************************
  60. * init
  61. */
  62. static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) {
  63. // find which TAS we are using (if any)
  64. tas57_addr = adac_init(config, i2c_port);
  65. if (!tas57_addr) tas57_addr = tas57_detect();
  66. if (!tas57_addr) {
  67. ESP_LOGW(TAG, "No TAS57xx detected");
  68. adac_deinit();
  69. return false;
  70. }
  71. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  72. for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
  73. i2c_master_start(i2c_cmd);
  74. i2c_master_write_byte(i2c_cmd, (tas57_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  75. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].reg, I2C_MASTER_NACK);
  76. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].value, I2C_MASTER_NACK);
  77. ESP_LOGD(TAG, "i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
  78. }
  79. i2c_master_stop(i2c_cmd);
  80. esp_err_t res = i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_RATE_MS);
  81. i2c_cmd_link_delete(i2c_cmd);
  82. if (res != ESP_OK) {
  83. ESP_LOGE(TAG, "could not intialize TAS57xx %d", res);
  84. return false;
  85. }
  86. return true;
  87. }
  88. /****************************************************************************************
  89. * change volume
  90. */
  91. static bool volume(unsigned left, unsigned right) {
  92. return false;
  93. }
  94. /****************************************************************************************
  95. * power
  96. */
  97. static void power(adac_power_e mode) {
  98. switch(mode) {
  99. case ADAC_STANDBY:
  100. dac_cmd(TAS57_STANDBY);
  101. break;
  102. case ADAC_ON:
  103. dac_cmd(TAS57_ACTIVE);
  104. break;
  105. case ADAC_OFF:
  106. dac_cmd(TAS57_DOWN);
  107. break;
  108. default:
  109. ESP_LOGW(TAG, "unknown DAC command");
  110. break;
  111. }
  112. }
  113. /****************************************************************************************
  114. * speaker
  115. */
  116. static void speaker(bool active) {
  117. if (active) dac_cmd(TAS57_ANALOGUE_ON);
  118. else dac_cmd(TAS57_ANALOGUE_OFF);
  119. }
  120. /****************************************************************************************
  121. * headset
  122. */
  123. static void headset(bool active) { }
  124. /****************************************************************************************
  125. * DAC specific commands
  126. */
  127. void dac_cmd(dac_cmd_e cmd, ...) {
  128. va_list args;
  129. esp_err_t ret = ESP_OK;
  130. va_start(args, cmd);
  131. switch(cmd) {
  132. case TAS57_VOLUME:
  133. ESP_LOGE(TAG, "DAC volume not handled yet");
  134. break;
  135. default:
  136. ret = adac_write_byte(tas57_addr, tas57xx_cmd[cmd].reg, tas57xx_cmd[cmd].value);
  137. }
  138. if (ret != ESP_OK) {
  139. ESP_LOGE(TAG, "could not use TAS57xx %d", ret);
  140. }
  141. va_end(args);
  142. }
  143. /****************************************************************************************
  144. * TAS57 detection
  145. */
  146. static int tas57_detect(void) {
  147. uint8_t addr[] = {TAS578x, TAS575x};
  148. for (int i = 0; i < sizeof(addr); i++) {
  149. if (adac_read_byte(addr[i], 0) != 255) {
  150. ESP_LOGI(TAG, "Detected TAS @0x%x", addr[i]);
  151. return addr[i];
  152. }
  153. }
  154. return 0;
  155. }