13#define ES8311_ERROR_FAILED(func) \
15 this->mark_failed(LOG_STR("Failed to initialize")); \
19#define ES8311_ERROR_CHECK(func) \
26 {ES8311_REG0D_SYSTEM, 0x01},
28 {ES8311_REG0E_SYSTEM, 0x02},
30 {ES8311_REG12_SYSTEM, 0x00},
32 {ES8311_REG13_SYSTEM, 0x10},
34 {ES8311_REG1C_ADC, 0x6A},
36 {ES8311_REG37_DAC, 0x08},
38 {ES8311_REG00_RESET, 0x80},
43 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG00_RESET, 0x1F));
44 ES8311_ERROR_FAILED(this->
write_byte(ES8311_REG00_RESET, 0x00));
58 "ES8311 Audio Codec:\n"
60 " Use Microphone: %s\n"
61 " DAC Bits per Sample: %" PRIu8
"\n"
62 " Sample Rate: %" PRIu32,
69 return this->
write_byte(ES8311_REG32_DAC, reg32);
74 this->
read_byte(ES8311_REG32_DAC, ®32);
97static uint8_t encode_pre_mult(uint8_t pre_mult) {
111 for (
const auto &coefficient : ES8311_COEFFICIENTS) {
112 if (coefficient.mclk == mclk && coefficient.rate == rate)
120 uint8_t reg01 = 0x3F;
130 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG01_CLK_MANAGER, reg01));
134 if (coefficient ==
nullptr) {
135 ESP_LOGE(TAG,
"Unable to configure sample rate %" PRIu32
"Hz with %" PRIu32
"Hz MCLK", this->
sample_frequency_,
143 ((coefficient->pre_div - 1) << 5) | (encode_pre_mult(coefficient->pre_mult) << 3)));
146 const uint8_t reg03 = (coefficient->fs_mode << 6) | coefficient->adc_osr;
147 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG03_CLK_MANAGER, reg03));
150 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG04_CLK_MANAGER, coefficient->dac_osr));
153 const uint8_t reg05 = ((coefficient->adc_div - 1) << 4) | (coefficient->dac_div - 1);
154 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG05_CLK_MANAGER, reg05));
157 uint8_t reg06 = coefficient->bclk_div < 19 ? coefficient->bclk_div - 1 : coefficient->bclk_div;
160 ES8311_ERROR_CHECK(this->
update_register(ES8311_REG06_CLK_MANAGER, BIT(5) | 0x1F, reg06));
163 ES8311_ERROR_CHECK(this->
update_register(ES8311_REG07_CLK_MANAGER, 0x3F, coefficient->lrck_h));
166 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG08_CLK_MANAGER, coefficient->lrck_l));
174 ES8311_ERROR_CHECK(this->
update_register(ES8311_REG00_RESET, BIT(6), 0));
178 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG09_SDPIN, reg09));
182 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG0A_SDPOUT, reg0a));
189 uint8_t reg14 = 0x1A;
193 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG14_SYSTEM, reg14));
196 ES8311_ERROR_CHECK(this->
write_byte(ES8311_REG17_ADC, 0xC8));
204 const uint8_t mask = BIT(6) | BIT(5);
205 return this->
update_register(ES8311_REG31_DAC, mask, mute_state ? mask : 0);
float volume() override
Gets the current volume out from the DAC.
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.
ES8311Resolution resolution_in_
uint32_t sample_frequency_
static uint8_t calculate_resolution_value(ES8311Resolution resolution)
Computes the register value for the configured resolution (bits per sample)
void dump_config() override
bool configure_format_()
Configures the ES8311 registers for the chosen bits per sample.
bool configure_clock_()
Configures the ES8311 registers for the chosen sample rate.
bool configure_mic_()
Configures the ES8311 microphone registers.
bool set_mute_state_(bool mute_state)
Mutes or unmute the DAC audio out.
bool set_volume(float volume) override
Writes the volume out to the DAC.
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
ESPHOME_LOG_TAG(TAG, "es8311")
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.