ES8311AudioSink.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "ES8311AudioSink.h"
  2. extern "C" {
  3. #include "es8311.h"
  4. }
  5. ES8311AudioSink::ES8311AudioSink() {
  6. this->softwareVolumeControl = false;
  7. esp_err_t ret_val = ESP_OK;
  8. Es8311Config cfg = {
  9. .esMode = ES_MODE_SLAVE,
  10. .i2c_port_num = I2C_NUM_0,
  11. .i2c_cfg =
  12. {
  13. .mode = I2C_MODE_MASTER,
  14. .sda_io_num = 1,
  15. .scl_io_num = 2,
  16. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  17. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  18. },
  19. .dacOutput = (DacOutput)(DAC_OUTPUT_LOUT1 | DAC_OUTPUT_LOUT2 |
  20. DAC_OUTPUT_ROUT1 | DAC_OUTPUT_ROUT2),
  21. .adcInput = ADC_INPUT_LINPUT1_RINPUT1,
  22. };
  23. cfg.i2c_cfg.master.clk_speed = 100000;
  24. Es8311Init(&cfg);
  25. Es8311SetBitsPerSample(ES_MODULE_DAC, BIT_LENGTH_16BITS);
  26. Es8311ConfigFmt(ES_MODULE_DAC, ES_I2S_NORMAL);
  27. Es8311SetVoiceVolume(60);
  28. Es8311Start(ES_MODULE_DAC);
  29. ES8311WriteReg(ES8311_CLK_MANAGER_REG01, 0xbf);
  30. ES8311WriteReg(ES8311_CLK_MANAGER_REG02, 0x18);
  31. // .codec_mode = AUDIO_HAL_CODEC_MODE_DECODE,
  32. // .i2s_iface = {
  33. // .mode = AUDIO_HAL_MODE_SLAVE,
  34. // .fmt = AUDIO_HAL_I2S_NORMAL,
  35. // .samples = AUDIO_HAL_44K_SAMPLES,
  36. // .bits = AUDIO_HAL_BIT_LENGTH_16BITS,
  37. // },
  38. // };
  39. // ret_val |= es8311_codec_init(&cfg);
  40. // ret_val |= es8311_set_bits_per_sample(cfg.i2s_iface.bits);
  41. // ret_val |= es8311_config_fmt((es_i2s_fmt_t) cfg.i2s_iface.fmt);
  42. // ret_val |= es8311_codec_set_voice_volume(60);
  43. // ret_val |= es8311_codec_ctrl_state(cfg.codec_mode, AUDIO_HAL_CTRL_START);
  44. // ret_val |= es8311_codec_set_clk();
  45. i2s_config_t i2s_config = {
  46. .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX), // Only TX
  47. .sample_rate = 44100,
  48. .bits_per_sample = (i2s_bits_per_sample_t)16,
  49. .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // 2-channels
  50. .communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_I2S,
  51. .intr_alloc_flags = 0, // Default interrupt priority
  52. .dma_buf_count = 8,
  53. .dma_buf_len = 512,
  54. .use_apll = false,
  55. .tx_desc_auto_clear = true, // Auto clear tx descriptor on underflow
  56. };
  57. i2s_pin_config_t pin_config = {
  58. .mck_io_num = 42,
  59. .bck_io_num = 40,
  60. .ws_io_num = 41,
  61. .data_out_num = 39,
  62. .data_in_num = -1,
  63. };
  64. int err;
  65. err = i2s_driver_install((i2s_port_t)0, &i2s_config, 0, NULL);
  66. if (err != ESP_OK) {
  67. ESP_LOGE("OI", "i2s driver installation error: %d", err);
  68. }
  69. err = i2s_set_pin((i2s_port_t)0, &pin_config);
  70. if (err != ESP_OK) {
  71. ESP_LOGE("OI", "i2s set pin error: %d", err);
  72. }
  73. // PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  74. // REG_SET_FIELD(PIN_CTRL, CLK_OUT1, 0);
  75. // ESP_LOGI("OI", "MCLK output on CLK_OUT1");
  76. startI2sFeed();
  77. }
  78. void ES8311AudioSink::volumeChanged(uint16_t volume) {
  79. Es8311SetVoiceVolume(volume);
  80. }
  81. void ES8311AudioSink::writeReg(uint8_t reg_add, uint8_t data) {}
  82. void ES8311AudioSink::setSampleRate(uint32_t sampleRate) {
  83. std::cout << "ES8311AudioSink::setSampleRate(" << sampleRate << ")"
  84. << std::endl;
  85. // i2s set sample rate
  86. es8311_Codec_Startup(0, sampleRate);
  87. }
  88. ES8311AudioSink::~ES8311AudioSink() {}