ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
es8311.cpp
Go to the documentation of this file.
1#include "es8311.h"
2#include "es8311_const.h"
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6#include <cinttypes>
7
8namespace esphome::es8311 {
9
10ESPHOME_LOG_TAG(TAG, "es8311");
11
12// Mark the component as failed; use only in setup
13#define ES8311_ERROR_FAILED(func) \
14 if (!(func)) { \
15 this->mark_failed(LOG_STR("Failed to initialize")); \
16 return; \
17 }
18// Return false; use outside of setup
19#define ES8311_ERROR_CHECK(func) \
20 if (!(func)) { \
21 return false; \
22 }
23
24static constexpr i2c::I2CRegValue ES8311_POWER_UP[] PROGMEM = {
25 // Power up analog circuitry
26 {ES8311_REG0D_SYSTEM, 0x01},
27 // Enable analog PGA, enable ADC modulator
28 {ES8311_REG0E_SYSTEM, 0x02},
29 // Power up DAC
30 {ES8311_REG12_SYSTEM, 0x00},
31 // Enable output to HP drive
32 {ES8311_REG13_SYSTEM, 0x10},
33 // ADC Equalizer bypass, cancel DC offset in digital domain
34 {ES8311_REG1C_ADC, 0x6A},
35 // Bypass DAC equalizer
36 {ES8311_REG37_DAC, 0x08},
37 // Power On
38 {ES8311_REG00_RESET, 0x80},
39};
40
42 // Reset
43 ES8311_ERROR_FAILED(this->write_byte(ES8311_REG00_RESET, 0x1F));
44 ES8311_ERROR_FAILED(this->write_byte(ES8311_REG00_RESET, 0x00));
45
46 ES8311_ERROR_FAILED(this->configure_clock_());
47 ES8311_ERROR_FAILED(this->configure_format_());
48 ES8311_ERROR_FAILED(this->configure_mic_());
49
50 // Set initial volume
51 this->set_volume(0.75); // 0.75 = 0xBF = 0dB
52
53 ES8311_ERROR_FAILED(this->write_registers(ES8311_POWER_UP));
54}
55
57 ESP_LOGCONFIG(TAG,
58 "ES8311 Audio Codec:\n"
59 " Use MCLK: %s\n"
60 " Use Microphone: %s\n"
61 " DAC Bits per Sample: %" PRIu8 "\n"
62 " Sample Rate: %" PRIu32,
63 YESNO(this->use_mclk_), YESNO(this->use_mic_), this->resolution_out_, this->sample_frequency_);
64}
65
66bool ES8311::set_volume(float volume) {
67 volume = clamp(volume, 0.0f, 1.0f);
68 uint8_t reg32 = remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 255);
69 return this->write_byte(ES8311_REG32_DAC, reg32);
70}
71
73 uint8_t reg32;
74 this->read_byte(ES8311_REG32_DAC, &reg32);
75 return remap<float, uint8_t>(reg32, 0, 255, 0.0f, 1.0f);
76}
77
79 switch (resolution) {
81 return (3 << 2);
83 return (2 << 2);
85 return (1 << 2);
87 return (0 << 2);
89 return (4 << 2);
90 default:
91 return 0;
92 }
93}
94
97static uint8_t encode_pre_mult(uint8_t pre_mult) {
98 switch (pre_mult) {
99 case 2:
100 return 1;
101 case 4:
102 return 2;
103 case 8:
104 return 3;
105 default:
106 return 0;
107 }
108}
109
111 for (const auto &coefficient : ES8311_COEFFICIENTS) {
112 if (coefficient.mclk == mclk && coefficient.rate == rate)
113 return &coefficient;
114 }
115 return nullptr;
116}
117
119 // Register 0x01: select clock source for internal MCLK and determine its frequency
120 uint8_t reg01 = 0x3F; // Enable all clocks
121
122 uint32_t mclk_frequency = this->sample_frequency_ * this->mclk_multiple_;
123 if (!this->use_mclk_) {
124 reg01 |= BIT(7); // Use SCLK
125 mclk_frequency = this->sample_frequency_ * (int) this->resolution_out_ * 2;
126 }
127 if (this->mclk_inverted_) {
128 reg01 |= BIT(6); // Invert MCLK pin
129 }
130 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG01_CLK_MANAGER, reg01));
131
132 // Get clock coefficients from coefficient table
133 auto *coefficient = get_coefficient(mclk_frequency, this->sample_frequency_);
134 if (coefficient == nullptr) {
135 ESP_LOGE(TAG, "Unable to configure sample rate %" PRIu32 "Hz with %" PRIu32 "Hz MCLK", this->sample_frequency_,
136 mclk_frequency);
137 return false;
138 }
139
140 // Register 0x02
141 ES8311_ERROR_CHECK(
142 this->update_register(ES8311_REG02_CLK_MANAGER, 0xF8,
143 ((coefficient->pre_div - 1) << 5) | (encode_pre_mult(coefficient->pre_mult) << 3)));
144
145 // Register 0x03
146 const uint8_t reg03 = (coefficient->fs_mode << 6) | coefficient->adc_osr;
147 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG03_CLK_MANAGER, reg03));
148
149 // Register 0x04
150 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG04_CLK_MANAGER, coefficient->dac_osr));
151
152 // Register 0x05
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));
155
156 // Register 0x06
157 uint8_t reg06 = coefficient->bclk_div < 19 ? coefficient->bclk_div - 1 : coefficient->bclk_div;
158 if (this->sclk_inverted_)
159 reg06 |= BIT(5);
160 ES8311_ERROR_CHECK(this->update_register(ES8311_REG06_CLK_MANAGER, BIT(5) | 0x1F, reg06));
161
162 // Register 0x07
163 ES8311_ERROR_CHECK(this->update_register(ES8311_REG07_CLK_MANAGER, 0x3F, coefficient->lrck_h));
164
165 // Register 0x08
166 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG08_CLK_MANAGER, coefficient->lrck_l));
167
168 // Successfully configured the clock
169 return true;
170}
171
173 // Configure I2S mode and format
174 ES8311_ERROR_CHECK(this->update_register(ES8311_REG00_RESET, BIT(6), 0));
175
176 // Configure SDP in resolution
177 uint8_t reg09 = calculate_resolution_value(this->resolution_in_);
178 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG09_SDPIN, reg09));
179
180 // Configure SDP out resolution
181 uint8_t reg0a = calculate_resolution_value(this->resolution_out_);
182 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG0A_SDPOUT, reg0a));
183
184 // Successfully configured the format
185 return true;
186}
187
189 uint8_t reg14 = 0x1A; // Enable analog MIC and max PGA gain
190 if (this->use_mic_) {
191 reg14 |= BIT(6); // Enable PDM digital microphone
192 }
193 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG14_SYSTEM, reg14));
194
195 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG16_ADC, this->mic_gain_)); // ADC gain scale up
196 ES8311_ERROR_CHECK(this->write_byte(ES8311_REG17_ADC, 0xC8)); // Set ADC gain
197
198 // Successfully configured the microphones
199 return true;
200}
201
202bool ES8311::set_mute_state_(bool mute_state) {
203 this->is_muted_ = mute_state;
204 const uint8_t mask = BIT(6) | BIT(5);
205 return this->update_register(ES8311_REG31_DAC, mask, mute_state ? mask : 0);
206}
207
208} // namespace esphome::es8311
float volume() override
Gets the current volume out from the DAC.
Definition es8311.cpp:72
ES8311Resolution resolution_out_
Definition es8311.h:129
static const ES8311Coefficient * get_coefficient(uint32_t mclk, uint32_t rate)
Retrieves the appropriate registers values for the configured mclk and rate.
Definition es8311.cpp:110
ES8311Resolution resolution_in_
Definition es8311.h:128
uint32_t sample_frequency_
Definition es8311.h:127
uint32_t mclk_multiple_
Definition es8311.h:125
static uint8_t calculate_resolution_value(ES8311Resolution resolution)
Computes the register value for the configured resolution (bits per sample)
Definition es8311.cpp:78
void dump_config() override
Definition es8311.cpp:56
bool configure_format_()
Configures the ES8311 registers for the chosen bits per sample.
Definition es8311.cpp:172
bool configure_clock_()
Configures the ES8311 registers for the chosen sample rate.
Definition es8311.cpp:118
void setup() override
Definition es8311.cpp:41
bool configure_mic_()
Configures the ES8311 microphone registers.
Definition es8311.cpp:188
bool set_mute_state_(bool mute_state)
Mutes or unmute the DAC audio out.
Definition es8311.cpp:202
bool set_volume(float volume) override
Writes the volume out to the DAC.
Definition es8311.cpp:66
ES8311MicGain mic_gain_
Definition es8311.h:120
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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.
Definition i2c.cpp:95
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
Definition i2c.cpp:73
Resolution resolution
Definition msa3xx.h:1
@ ES8311_RESOLUTION_32
Definition es8311.h:27
@ ES8311_RESOLUTION_16
Definition es8311.h:23
@ ES8311_RESOLUTION_20
Definition es8311.h:25
@ ES8311_RESOLUTION_24
Definition es8311.h:26
@ ES8311_RESOLUTION_18
Definition es8311.h:24
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).
Definition helpers.h:932
static void uint32_t
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18