TAS5711AudioSink.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "TAS5711AudioSink.h"
  2. struct tas5711_cmd_s {
  3. uint8_t reg;
  4. uint8_t value;
  5. };
  6. static const struct tas5711_cmd_s tas5711_init_sequence[] = {
  7. {0x00, 0x6c}, // 0x6c - 256 x mclk
  8. {0x04, 0x03}, // 0x03 - 16 bit i2s
  9. {0x05, 0x00}, // system control 0x00 is audio playback
  10. {0x06, 0x00}, // disable mute
  11. {0x07, 0x50}, // volume register
  12. {0xff, 0xff}
  13. };
  14. i2c_ack_type_t ACK_CHECK_EN = (i2c_ack_type_t)0x1;
  15. TAS5711AudioSink::TAS5711AudioSink() {
  16. i2s_config_t i2s_config = {
  17. .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX), // Only TX
  18. .sample_rate = 44100,
  19. .bits_per_sample = (i2s_bits_per_sample_t)16,
  20. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  21. .communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_MSB,
  22. .intr_alloc_flags = 0, //Default interrupt priority
  23. .dma_buf_count = 8,
  24. .dma_buf_len = 512,
  25. .use_apll = true,
  26. .tx_desc_auto_clear = true, //Auto clear tx descriptor on underflow
  27. .fixed_mclk = 256 * 44100};
  28. i2s_pin_config_t pin_config = {
  29. .bck_io_num = 5,
  30. .ws_io_num = 25,
  31. .data_out_num = 26,
  32. .data_in_num = -1 //Not used
  33. };
  34. i2s_driver_install((i2s_port_t)0, &i2s_config, 0, NULL);
  35. i2s_set_pin((i2s_port_t)0, &pin_config);
  36. // configure i2c
  37. i2c_config = {
  38. .mode = I2C_MODE_MASTER,
  39. .sda_io_num = 21,
  40. .scl_io_num = 23,
  41. .sda_pullup_en = GPIO_PULLUP_DISABLE,
  42. .scl_pullup_en = GPIO_PULLUP_DISABLE,
  43. };
  44. i2c_config.master.clk_speed = 250000;
  45. i2c_param_config(i2c_port, &i2c_config);
  46. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  47. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  48. uint8_t data, addr = (0x1b);
  49. i2c_master_start(i2c_cmd);
  50. i2c_master_write_byte(i2c_cmd, (addr << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
  51. i2c_master_write_byte(i2c_cmd, 00, ACK_CHECK_EN);
  52. i2c_master_start(i2c_cmd);
  53. i2c_master_write_byte(i2c_cmd, (addr << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
  54. i2c_master_read_byte(i2c_cmd, &data, ACK_CHECK_EN);
  55. i2c_master_stop(i2c_cmd);
  56. int ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_PERIOD_MS);
  57. i2c_cmd_link_delete(i2c_cmd);
  58. if (ret == ESP_OK) {
  59. ESP_LOGI("RR", "Detected TAS");
  60. } else {
  61. ESP_LOGI("RR", "Unable to detect dac");
  62. }
  63. writeReg(0x1b, 0x00);
  64. vTaskDelay(100 / portTICK_PERIOD_MS);
  65. for (int i = 0; tas5711_init_sequence[i].reg != 0xff; i++) {
  66. writeReg(tas5711_init_sequence[i].reg, tas5711_init_sequence[i].value);
  67. vTaskDelay(1 / portTICK_PERIOD_MS);
  68. }
  69. startI2sFeed();
  70. }
  71. void TAS5711AudioSink::writeReg(uint8_t reg, uint8_t value) {
  72. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  73. i2c_master_start(i2c_cmd);
  74. i2c_master_write_byte(i2c_cmd, (0x1b << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
  75. i2c_master_write_byte(i2c_cmd, reg, ACK_CHECK_EN);
  76. i2c_master_write_byte(i2c_cmd, value, ACK_CHECK_EN);
  77. i2c_master_stop(i2c_cmd);
  78. esp_err_t res =
  79. i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_PERIOD_MS);
  80. if (res != ESP_OK) {
  81. ESP_LOGE("RR", "Unable to write to TAS5711");
  82. }
  83. i2c_cmd_link_delete(i2c_cmd);
  84. }
  85. TAS5711AudioSink::~TAS5711AudioSink() {}