dac_57xx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "squeezelite.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "driver/i2s.h"
  25. #include "driver/i2c.h"
  26. #include "driver/gpio.h"
  27. #include "adac.h"
  28. #define VOLUME_GPIO 14
  29. #define TAS575x 0x98
  30. #define TAS578x 0x90
  31. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
  32. static void deinit(void);
  33. static void speaker(bool active);
  34. static void headset(bool active);
  35. static void volume(unsigned left, unsigned right);
  36. static void power(adac_power_e mode);
  37. struct adac_s dac_tas57xx = { init, deinit, power, speaker, headset, volume };
  38. struct tas57xx_cmd_s {
  39. uint8_t reg;
  40. uint8_t value;
  41. };
  42. static const struct tas57xx_cmd_s tas57xx_init_sequence[] = {
  43. { 0x00, 0x00 }, // select page 0
  44. { 0x02, 0x10 }, // standby
  45. { 0x0d, 0x10 }, // use SCK for PLL
  46. { 0x25, 0x08 }, // ignore SCK halt
  47. { 0x08, 0x10 }, // Mute control enable (from TAS5780)
  48. { 0x54, 0x02 }, // Mute output control (from TAS5780)
  49. { 0x02, 0x00 }, // restart
  50. { 0xff, 0xff } // end of table
  51. };
  52. // matching orders
  53. typedef enum { TAS57_ACTIVE = 0, TAS57_STANDBY, TAS57_DOWN, TAS57_ANALOGUE_OFF, TAS57_ANALOGUE_ON, TAS57_VOLUME } dac_cmd_e;
  54. static const struct tas57xx_cmd_s tas57xx_cmd[] = {
  55. { 0x02, 0x00 }, // TAS57_ACTIVE
  56. { 0x02, 0x10 }, // TAS57_STANDBY
  57. { 0x02, 0x01 }, // TAS57_DOWN
  58. { 0x56, 0x10 }, // TAS57_ANALOGUE_OFF
  59. { 0x56, 0x00 }, // TAS57_ANALOGUE_ON
  60. };
  61. static log_level loglevel = lINFO;
  62. static u8_t tas57_addr;
  63. static int i2c_port;
  64. static void dac_cmd(dac_cmd_e cmd, ...);
  65. static int tas57_detect(void);
  66. /****************************************************************************************
  67. * init
  68. */
  69. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
  70. LOG_INFO("Initializing TAS57xx ");
  71. i2c_port = i2c_port_num;
  72. // init volume & mute
  73. gpio_pad_select_gpio(VOLUME_GPIO);
  74. gpio_set_direction(VOLUME_GPIO, GPIO_MODE_OUTPUT);
  75. gpio_set_level(VOLUME_GPIO, 0);
  76. // configure i2c
  77. i2c_config_t i2c_config = {
  78. .mode = I2C_MODE_MASTER,
  79. .sda_io_num = 27,
  80. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  81. .scl_io_num = 26,
  82. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  83. .master.clk_speed = 100000,
  84. };
  85. i2c_param_config(i2c_port, &i2c_config);
  86. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  87. LOG_INFO("DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
  88. // find which TAS we are using
  89. tas57_addr = tas57_detect();
  90. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  91. for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
  92. i2c_master_start(i2c_cmd);
  93. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  94. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].reg, I2C_MASTER_NACK);
  95. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].value, I2C_MASTER_NACK);
  96. LOG_DEBUG("i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
  97. }
  98. i2c_master_stop(i2c_cmd);
  99. esp_err_t ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_RATE_MS);
  100. i2c_cmd_link_delete(i2c_cmd);
  101. // configure I2S pins & install driver
  102. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = 33, .ws_io_num = 25,
  103. .data_out_num = 32, .data_in_num = -1 //Not used
  104. };
  105. i2s_driver_install(i2s_num, i2s_config, 0, NULL);
  106. i2s_set_pin(i2s_num, &i2s_pin_config);
  107. LOG_INFO("DAC using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  108. if (ret != ESP_OK) {
  109. LOG_ERROR("could not intialize TAS57xx %d", ret);
  110. return false;
  111. } else {
  112. return true;
  113. }
  114. }
  115. /****************************************************************************************
  116. * init
  117. */
  118. static void deinit(void) {
  119. i2c_driver_delete(i2c_port);
  120. }
  121. /****************************************************************************************
  122. * change volume
  123. */
  124. static void volume(unsigned left, unsigned right) {
  125. LOG_INFO("TAS57xx volume (L:%u R:%u)", left, right);
  126. gpio_set_level(VOLUME_GPIO, left || right);
  127. }
  128. /****************************************************************************************
  129. * power
  130. */
  131. static void power(adac_power_e mode) {
  132. switch(mode) {
  133. case ADAC_STANDBY:
  134. dac_cmd(TAS57_STANDBY);
  135. break;
  136. case ADAC_ON:
  137. dac_cmd(TAS57_ACTIVE);
  138. break;
  139. case ADAC_OFF:
  140. dac_cmd(TAS57_DOWN);
  141. break;
  142. default:
  143. LOG_WARN("unknown DAC command");
  144. break;
  145. }
  146. }
  147. /****************************************************************************************
  148. * speaker
  149. */
  150. static void speaker(bool active) {
  151. if (active) dac_cmd(TAS57_ANALOGUE_ON);
  152. else dac_cmd(TAS57_ANALOGUE_OFF);
  153. }
  154. /****************************************************************************************
  155. * headset
  156. */
  157. static void headset(bool active) {
  158. }
  159. /****************************************************************************************
  160. * DAC specific commands
  161. */
  162. void dac_cmd(dac_cmd_e cmd, ...) {
  163. va_list args;
  164. esp_err_t ret = ESP_OK;
  165. va_start(args, cmd);
  166. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  167. switch(cmd) {
  168. case TAS57_VOLUME:
  169. LOG_ERROR("DAC volume not handled yet");
  170. break;
  171. default:
  172. i2c_master_start(i2c_cmd);
  173. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  174. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].reg, I2C_MASTER_NACK);
  175. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].value, I2C_MASTER_NACK);
  176. i2c_master_stop(i2c_cmd);
  177. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  178. }
  179. i2c_cmd_link_delete(i2c_cmd);
  180. if (ret != ESP_OK) {
  181. LOG_ERROR("could not intialize TAS57xx %d", ret);
  182. }
  183. va_end(args);
  184. }
  185. /****************************************************************************************
  186. * TAS57 detection
  187. */
  188. static int tas57_detect(void) {
  189. u8_t data, addr[] = {TAS578x, TAS575x};
  190. int ret;
  191. for (int i = 0; i < sizeof(addr); i++) {
  192. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  193. i2c_master_start(i2c_cmd);
  194. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  195. i2c_master_write_byte(i2c_cmd, 00, I2C_MASTER_NACK);
  196. i2c_master_start(i2c_cmd);
  197. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_READ, I2C_MASTER_NACK);
  198. i2c_master_read_byte(i2c_cmd, &data, I2C_MASTER_NACK);
  199. i2c_master_stop(i2c_cmd);
  200. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  201. i2c_cmd_link_delete(i2c_cmd);
  202. if (ret == ESP_OK) {
  203. LOG_INFO("Detected TAS @0x%x", addr[i]);
  204. return addr[i];
  205. }
  206. }
  207. return 0;
  208. }