ES8388AudioSink.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "ES8388AudioSink.h"
  2. struct es8388_cmd_s {
  3. uint8_t reg;
  4. uint8_t value;
  5. };
  6. ES8388AudioSink::ES8388AudioSink() {
  7. // configure i2c
  8. i2c_config = {
  9. .mode = I2C_MODE_MASTER,
  10. .sda_io_num = 33,
  11. .scl_io_num = 32,
  12. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  13. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  14. };
  15. i2c_config.master.clk_speed = 100000;
  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 = 27,
  30. .ws_io_num = 25,
  31. .data_out_num = 26,
  32. .data_in_num = -1 //Not used
  33. };
  34. int err;
  35. err = i2s_driver_install((i2s_port_t)0, &i2s_config, 0, NULL);
  36. if (err != ESP_OK) {
  37. ESP_LOGE("OI", "i2s driver installation error: %d", err);
  38. }
  39. err = i2s_set_pin((i2s_port_t)0, &pin_config);
  40. if (err != ESP_OK) {
  41. ESP_LOGE("OI", "i2s set pin error: %d", err);
  42. }
  43. err = i2c_param_config(I2C_NUM_0, &i2c_config);
  44. if (err != ESP_OK) {
  45. ESP_LOGE("OI", "i2c param config error: %d", err);
  46. }
  47. err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
  48. if (err != ESP_OK) {
  49. ESP_LOGE("OI", "i2c driver installation error: %d", err);
  50. }
  51. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  52. err = i2c_master_start(i2c_cmd);
  53. if (err != ESP_OK) {
  54. ESP_LOGE("OI", "i2c master start error: %d", err);
  55. }
  56. /* mute DAC during setup, power up all systems, slave mode */
  57. writeReg(ES8388_DACCONTROL3, 0x04);
  58. writeReg(ES8388_CONTROL2, 0x50);
  59. writeReg(ES8388_CHIPPOWER, 0x00);
  60. writeReg(ES8388_MASTERMODE, 0x00);
  61. /* power up DAC and enable LOUT1+2 / ROUT1+2, ADC sample rate = DAC sample rate */
  62. writeReg(ES8388_DACPOWER, 0x3e);
  63. writeReg(ES8388_CONTROL1, 0x12);
  64. /* DAC I2S setup: 16 bit word length, I2S format; MCLK / Fs = 256*/
  65. writeReg(ES8388_DACCONTROL1, 0x18);
  66. writeReg(ES8388_DACCONTROL2, 0x02);
  67. /* DAC to output route mixer configuration: ADC MIX TO OUTPUT */
  68. writeReg(ES8388_DACCONTROL16, 0x1B);
  69. writeReg(ES8388_DACCONTROL17, 0x90);
  70. writeReg(ES8388_DACCONTROL20, 0x90);
  71. /* DAC and ADC use same LRCK, enable MCLK input; output resistance setup */
  72. writeReg(ES8388_DACCONTROL21, 0x80);
  73. writeReg(ES8388_DACCONTROL23, 0x00);
  74. /* DAC volume control: 0dB (maximum, unattented) */
  75. writeReg(ES8388_DACCONTROL5, 0x00);
  76. writeReg(ES8388_DACCONTROL4, 0x00);
  77. /* power down ADC while configuring; volume: +9dB for both channels */
  78. writeReg(ES8388_ADCPOWER, 0xff);
  79. writeReg(ES8388_ADCCONTROL1, 0x88); // +24db
  80. /* select LINPUT2 / RINPUT2 as ADC input; stereo; 16 bit word length, format right-justified, MCLK / Fs = 256 */
  81. writeReg(ES8388_ADCCONTROL2, 0xf0); // 50
  82. writeReg(ES8388_ADCCONTROL3, 0x80); // 00
  83. writeReg(ES8388_ADCCONTROL4, 0x0e);
  84. writeReg(ES8388_ADCCONTROL5, 0x02);
  85. /* set ADC volume */
  86. writeReg(ES8388_ADCCONTROL8, 0x20);
  87. writeReg(ES8388_ADCCONTROL9, 0x20);
  88. /* set LOUT1 / ROUT1 volume: 0dB (unattenuated) */
  89. writeReg(ES8388_DACCONTROL24, 0x1e);
  90. writeReg(ES8388_DACCONTROL25, 0x1e);
  91. /* set LOUT2 / ROUT2 volume: 0dB (unattenuated) */
  92. writeReg(ES8388_DACCONTROL26, 0x1e);
  93. writeReg(ES8388_DACCONTROL27, 0x1e);
  94. /* power up and enable DAC; power up ADC (no MIC bias) */
  95. writeReg(ES8388_DACPOWER, 0x3c);
  96. writeReg(ES8388_DACCONTROL3, 0x00);
  97. writeReg(ES8388_ADCPOWER, 0x00);
  98. startI2sFeed();
  99. }
  100. void ES8388AudioSink::writeReg(uint8_t reg_add, uint8_t data) {
  101. int res = 0;
  102. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  103. res |= i2c_master_start(cmd);
  104. res |= i2c_master_write_byte(cmd, ES8388_ADDR, ACK_CHECK_EN);
  105. res |= i2c_master_write_byte(cmd, reg_add, ACK_CHECK_EN);
  106. res |= i2c_master_write_byte(cmd, data, ACK_CHECK_EN);
  107. res |= i2c_master_stop(cmd);
  108. res |= i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS);
  109. i2c_cmd_link_delete(cmd);
  110. if (res != ESP_OK) {
  111. ESP_LOGE("RR", "Unable to write to ES8388: %d", res);
  112. } else {
  113. ESP_LOGE("RR", "register successfull written.");
  114. }
  115. }
  116. ES8388AudioSink::~ES8388AudioSink() {}