ES8388AudioSink.cpp 4.6 KB

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