10 static const char *
const TAG =
"es8311";
13 #define ES8311_ERROR_FAILED(func) \ 15 this->mark_failed(); \ 19 #define ES8311_ERROR_CHECK(func) \ 25 ESP_LOGCONFIG(TAG,
"Setting up ES8311...");
28 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG00_RESET, 0x1F));
29 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG00_RESET, 0x00));
39 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG0D_SYSTEM, 0x01));
41 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG0E_SYSTEM, 0x02));
43 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG12_SYSTEM, 0x00));
45 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG13_SYSTEM, 0x10));
47 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG1C_ADC, 0x6A));
49 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG37_DAC, 0x08));
51 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG00_RESET, 0x80));
55 ESP_LOGCONFIG(TAG,
"ES8311 Audio Codec:");
56 ESP_LOGCONFIG(TAG,
" Use MCLK: %s", YESNO(this->
use_mclk_));
57 ESP_LOGCONFIG(TAG,
" Use Microphone: %s", YESNO(this->
use_mic_));
58 ESP_LOGCONFIG(TAG,
" DAC Bits per Sample: %" PRIu8, this->
resolution_out_);
62 ESP_LOGCONFIG(TAG,
" Failed to initialize!");
68 volume =
clamp(volume, 0.0f, 1.0f);
69 uint8_t reg32 = remap<uint8_t, float>(
volume, 0.0f, 1.0f, 0, 255);
70 return this->
write_byte(ES8311_REG32_DAC, reg32);
75 this->
read_byte(ES8311_REG32_DAC, ®32);
76 return remap<float, uint8_t>(reg32, 0, 255, 0.0f, 1.0f);
97 for (
const auto &coefficient : ES8311_COEFFICIENTS) {
98 if (coefficient.mclk == mclk && coefficient.rate == rate)
106 uint8_t reg01 = 0x3F;
116 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG01_CLK_MANAGER, reg01));
120 if (coefficient ==
nullptr) {
121 ESP_LOGE(TAG,
"Unable to configure sample rate %" PRIu32
"Hz with %" PRIu32
"Hz MCLK", this->
sample_frequency_,
128 ES8311_ERROR_CHECK(this->
read_byte(ES8311_REG02_CLK_MANAGER, ®02));
130 reg02 |= (coefficient->pre_div - 1) << 5;
131 reg02 |= coefficient->pre_mult << 3;
132 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG02_CLK_MANAGER, reg02));
135 const uint8_t reg03 = (coefficient->fs_mode << 6) | coefficient->adc_osr;
136 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG03_CLK_MANAGER, reg03));
139 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG04_CLK_MANAGER, coefficient->dac_osr));
142 const uint8_t reg05 = ((coefficient->adc_div - 1) << 4) | (coefficient->dac_div - 1);
143 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG05_CLK_MANAGER, reg05));
147 ES8311_ERROR_CHECK(this->
read_byte(ES8311_REG06_CLK_MANAGER, ®06));
154 if (coefficient->bclk_div < 19) {
155 reg06 |= (coefficient->bclk_div - 1) << 0;
157 reg06 |= (coefficient->bclk_div) << 0;
159 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG06_CLK_MANAGER, reg06));
163 ES8311_ERROR_CHECK(this->
read_byte(ES8311_REG07_CLK_MANAGER, ®07));
165 reg07 |= coefficient->lrck_h << 0;
166 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG07_CLK_MANAGER, reg07));
169 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG08_CLK_MANAGER, coefficient->lrck_l));
178 ES8311_ERROR_CHECK(this->
read_byte(ES8311_REG00_RESET, ®00));
180 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG00_RESET, reg00));
184 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG09_SDPIN, reg09));
188 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG0A_SDPOUT, reg0a));
195 uint8_t reg14 = 0x1A;
199 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG14_SYSTEM, reg14));
202 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG17_ADC, 0xC8));
213 if (!this->
read_byte(ES8311_REG31_DAC, ®31)) {
218 reg31 |= BIT(6) | BIT(5);
220 reg31 &= ~(BIT(6) | BIT(5));
223 return this->
write_byte(ES8311_REG31_DAC, reg31);
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
bool set_volume(float volume) override
Writes the volume out to the DAC.
bool configure_format_()
Configures the ES8311 registers for the chosen bits per sample.
float volume() override
Gets the current volume out from the DAC.
ES8311Resolution resolution_in_
static uint8_t calculate_resolution_value(ES8311Resolution resolution)
Computes the register value for the configured resolution (bits per sample)
bool set_mute_state_(bool mute_state)
Mutes or unmute the DAC audio out.
uint32_t sample_frequency_
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
bool configure_clock_()
Configures the ES8311 registers for the chosen sample rate.
ES8311Resolution resolution_out_
static const ES8311Coefficient * get_coefficient(uint32_t mclk, uint32_t rate)
Retrieves the appropriate registers values for the configured mclk and rate.
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Implementation of SPI Controller mode.
bool configure_mic_()
Configures the ES8311 microphone registers.
void dump_config() override