ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
es8388.cpp
Go to the documentation of this file.
1#include "es8388.h"
2
3#include <cinttypes>
4#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8namespace esphome::es8388 {
9
10ESPHOME_LOG_TAG(TAG, "es8388");
11
12// Mark the component as failed; use only in setup
13#define ES8388_ERROR_FAILED(func) \
14 if (!(func)) { \
15 this->mark_failed(LOG_STR("Failed to initialize")); \
16 return; \
17 }
18
19// Return false; use outside of setup
20#define ES8388_ERROR_CHECK(func) \
21 if (!(func)) { \
22 return false; \
23 }
24
25static constexpr i2c::I2CRegValue ES8388_INIT[] PROGMEM = {
26 // I2S worker mode
27 {ES8388_MASTERMODE, 0x00},
28 /* Chip Control and Power Management */
29 {ES8388_CONTROL2, 0x50},
30 // normal all and power up all
31 {ES8388_CHIPPOWER, 0x00},
32 // vmidsel/500k
33 // EnRef=0,Play&Record Mode,(0x17-both of mic&play)
34 {ES8388_CONTROL1, 0x12},
35 // i2s 16 bits
36 {ES8388_DACCONTROL1, 0x18},
37 // sample freq 256
38 // DACFsMode,SINGLE SPEED; DACFsRatio,256
39 {ES8388_DACCONTROL2, 0x02},
40 // 0x00 audio on LIN1&RIN1, 0x09 LIN2&RIN2
41 {ES8388_DACCONTROL16, 0x00},
42 // only left DAC to left mixer enable 0db
43 {ES8388_DACCONTROL17, 0x90},
44 // only right DAC to right mixer enable 0db
45 {ES8388_DACCONTROL20, 0x90},
46 // set internal ADC and DAC use the same LRCK clock, ADC LRCK as internal LRCK
47 {ES8388_DACCONTROL21, 0x80},
48 // vroi=0 - 1.5k VREF to analog output resistance (default)
49 {ES8388_DACCONTROL23, 0x00},
50 // power down adc and line in
51 {ES8388_ADCPOWER, 0xFF},
52 // set to Mono Right
53 {ES8388_ADCCONTROL3, 0x02},
54 // I2S 16 Bits length and I2S serial audio data format
55 {ES8388_ADCCONTROL4, 0x0d},
56 // ADCFsMode,singel SPEED,RATIO=256
57 {ES8388_ADCCONTROL5, 0x02},
58 // ADC Volume
59 {ES8388_ADCCONTROL8, 0x00},
60 {ES8388_ADCCONTROL9, 0x00},
61 //@nightDav
62 // ALC Config (as recommended by ES8388 user guide for voice recording)
63 // Reg 0x12 = 0xe2 (ALC enable, PGA Max. Gain=23.5dB, Min. Gain=0dB)
64 {ES8388_ADCCONTROL10, 0xe2},
65 // Reg 0x13 = 0xa0 (ALC Target=-1.5dB, ALC Hold time =0 mS)
66 {ES8388_ADCCONTROL11, 0xa0},
67 // Reg 0x14 = 0x12(Decay time =820uS , Attack time = 416 uS)
68 {ES8388_ADCCONTROL12, 0x12},
69 // Reg 0x15 = 0x06(ALC mode)
70 {ES8388_ADCCONTROL13, 0x06},
71 // Reg 0x16 = 0xc3(nose gate = -40.5dB, NGG = 0x01(mute ADC))
72 {ES8388_ADCCONTROL14, 0xc3},
73 // Power on ADC
74 {ES8388_DACCONTROL21, 0x80},
75 // Start state machine
76 {ES8388_CHIPPOWER, 0xF0},
77};
78
79static constexpr i2c::I2CRegValue ES8388_UNMUTE[] PROGMEM = {
80 // unmute ADC with fade in
81 {ES8388_ADCCONTROL7, 0x60},
82 // unmute DAC with fade in
83 {ES8388_DACCONTROL3, 0x20},
84 // Power on ADC, Enable LIN&RIN, Power off MICBIAS, set int1lp to low power mode
85 {ES8388_ADCPOWER, 0x09},
86};
87
88void ES8388::setup() {
89 // mute DAC
90 this->set_mute_state_(true);
91
92 ES8388_ERROR_FAILED(this->write_registers(ES8388_INIT));
93 // ADC PGA gain (reg 0x09): MicAmpL in bits 7:4, MicAmpR in bits 3:0, 3 dB per step
94 ES8388_ERROR_FAILED(this->write_byte(ES8388_ADCCONTROL1, this->mic_gain_));
95 delay(1);
96 ES8388_ERROR_FAILED(this->write_byte(ES8388_CHIPPOWER, 0x00));
97
98 // DAC volume max
99 // Set initial volume
100 // this->set_volume(0.75); // 0.75 = 0xBF = 0dB
101
102 this->set_mute_state_(false);
103
104 ES8388_ERROR_FAILED(this->write_registers(ES8388_UNMUTE));
105
106#ifdef USE_ES8388_SELECT
107 if (this->dac_output_select_ != nullptr) {
108 if (this->dac_output_initial_.has_value()) {
109 const DacOutputLine line = this->dac_output_initial_.value();
110 ES8388_ERROR_FAILED(this->set_dac_output(line));
111 this->dac_output_select_->publish_state(static_cast<size_t>(line));
112 } else {
113 auto dac_power = this->get_dac_power();
114 if (dac_power.has_value()) {
115 if (this->dac_output_select_->has_index(dac_power.value())) {
116 this->dac_output_select_->publish_state(dac_power.value());
117 } else {
118 ESP_LOGW(TAG, "Unknown DAC output power value: %d", dac_power.value());
119 }
120 }
121 }
122 }
123 if (this->adc_input_mic_select_ != nullptr) {
124 if (this->adc_input_mic_initial_.has_value()) {
125 const AdcInputMicLine line = this->adc_input_mic_initial_.value();
126 ES8388_ERROR_FAILED(this->set_adc_input_mic(line));
127 this->adc_input_mic_select_->publish_state(static_cast<size_t>(line));
128 } else {
129 auto mic_input = this->get_mic_input();
130 if (mic_input.has_value()) {
131 if (this->adc_input_mic_select_->has_index(mic_input.value())) {
132 this->adc_input_mic_select_->publish_state(mic_input.value());
133 } else {
134 ESP_LOGW(TAG, "Unknown ADC input mic value: %d", mic_input.value());
135 }
136 }
137 }
138 }
139#endif
140}
141
142void ES8388::dump_config() {
143 ESP_LOGCONFIG(TAG, "ES8388 Audio Codec:");
144 LOG_I2C_DEVICE(this);
145 ESP_LOGCONFIG(TAG, " Mic gain: %u dB", (this->mic_gain_ & 0x0F) * 3);
146#ifdef USE_ES8388_SELECT
147 LOG_SELECT(" ", "DAC Output", this->dac_output_select_);
148 LOG_SELECT(" ", "ADC Input Mic", this->adc_input_mic_select_);
149#endif
150}
151
152bool ES8388::set_volume(float volume) {
153 volume = clamp(volume, 0.0f, 1.0f);
154 uint8_t value = remap<uint8_t, float>(volume, 0.0f, 1.0f, 192, 0);
155 ESP_LOGD(TAG, "Setting ES8388_DACCONTROL4 / ES8388_DACCONTROL5 to 0x%02X (volume: %f)", value, volume);
156 ES8388_ERROR_CHECK(this->write_byte(ES8388_DACCONTROL4, value));
157 ES8388_ERROR_CHECK(this->write_byte(ES8388_DACCONTROL5, value));
158
159 return true;
160}
161
162float ES8388::volume() {
163 uint8_t value;
164 ES8388_ERROR_CHECK(this->read_byte(ES8388_DACCONTROL4, &value));
165 return remap<float, uint8_t>(value, 192, 0, 0.0f, 1.0f);
166}
167
168bool ES8388::set_mute_state_(bool mute_state) {
169 this->is_muted_ = mute_state;
170
171 // Only toggle the DACMute bit; the other bits of this register hold unrelated DAC settings
172 return this->update_register(ES8388_DACCONTROL3, ES8388_DACCONTROL3_DAC_MUTE,
173 mute_state ? ES8388_DACCONTROL3_DAC_MUTE : 0);
174}
175
176// Set dac power output
177bool ES8388::set_dac_output(DacOutputLine line) {
178 uint8_t reg_out1 = 0;
179 uint8_t reg_out2 = 0;
180 uint8_t dac_power = 0;
181
182 // 0x00: -30dB , 0x1E: 0dB
183 switch (line) {
184 case DAC_OUTPUT_LINE1:
185 reg_out1 = 0x1E;
186 dac_power = ES8388_DAC_OUTPUT_LOUT1_ROUT1;
187 break;
188 case DAC_OUTPUT_LINE2:
189 reg_out2 = 0x1E;
190 dac_power = ES8388_DAC_OUTPUT_LOUT2_ROUT2;
191 break;
192 case DAC_OUTPUT_BOTH:
193 reg_out1 = 0x1E;
194 reg_out2 = 0x1E;
195 dac_power = ES8388_DAC_OUTPUT_BOTH;
196 break;
197 default:
198 ESP_LOGE(TAG, "Unknown DAC output line: %d", line);
199 return false;
200 };
201
202 ESP_LOGV(TAG,
203 "DAC output config:\n"
204 " DACPOWER: 0x%02X\n"
205 " DACCONTROL24/25: 0x%02X\n"
206 " DACCONTROL26/27: 0x%02X",
207 dac_power, reg_out1, reg_out2);
208
209 ES8388_ERROR_CHECK(this->write_byte(ES8388_DACCONTROL24, reg_out1)); // LOUT1VOL
210 ES8388_ERROR_CHECK(this->write_byte(ES8388_DACCONTROL25, reg_out1)); // ROUT1VOL
211 ES8388_ERROR_CHECK(this->write_byte(ES8388_DACCONTROL26, reg_out2)); // LOUT2VOL
212 ES8388_ERROR_CHECK(this->write_byte(ES8388_DACCONTROL27, reg_out2)); // ROUT1VOL
213
214 return this->write_byte(ES8388_DACPOWER, dac_power);
215}
216
217optional<DacOutputLine> ES8388::get_dac_power() {
218 uint8_t dac_power;
219 if (!this->read_byte(ES8388_DACPOWER, &dac_power)) {
221 return {};
222 }
223 switch (dac_power) {
224 case ES8388_DAC_OUTPUT_LOUT1_ROUT1:
225 return DAC_OUTPUT_LINE1;
226 case ES8388_DAC_OUTPUT_LOUT2_ROUT2:
227 return DAC_OUTPUT_LINE2;
228 case ES8388_DAC_OUTPUT_BOTH:
229 return DAC_OUTPUT_BOTH;
230 default:
231 return {};
232 }
233}
234
235// Set ADC input MIC
236bool ES8388::set_adc_input_mic(AdcInputMicLine line) {
237 uint8_t mic_input = 0;
238
239 switch (line) {
241 mic_input = ES8388_ADC_INPUT_LINPUT1_RINPUT1;
242 break;
244 mic_input = ES8388_ADC_INPUT_LINPUT2_RINPUT2;
245 break;
247 mic_input = ES8388_ADC_INPUT_DIFFERENCE;
248 break;
249 default:
250 ESP_LOGE(TAG, "Unknown ADC input mic line: %d", line);
251 return false;
252 }
253
254 ESP_LOGV(TAG, "Setting ES8388_ADCCONTROL2 to 0x%02X", mic_input);
255 ES8388_ERROR_CHECK(this->write_byte(ES8388_ADCCONTROL2, mic_input));
256
257 return true;
258}
259
260optional<AdcInputMicLine> ES8388::get_mic_input() {
261 uint8_t mic_input;
262 if (!this->read_byte(ES8388_ADCCONTROL2, &mic_input)) {
264 return {};
265 }
266 switch (mic_input) {
267 case ES8388_ADC_INPUT_LINPUT1_RINPUT1:
268 return ADC_INPUT_MIC_LINE1;
269 case ES8388_ADC_INPUT_LINPUT2_RINPUT2:
270 return ADC_INPUT_MIC_LINE2;
271 case ES8388_ADC_INPUT_DIFFERENCE:
273 default:
274 return {};
275 };
276}
277
278} // namespace esphome::es8388
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_momentary_warning(uint32_t length=5000)
Set warning status flag and clear it after length ms.
optional< DacOutputLine > dac_output_initial_
Definition es8388.h:101
bool set_mute_state_(bool mute_state)
Mutes or unmutes the DAC audio out.
Definition es8388.cpp:168
optional< AdcInputMicLine > adc_input_mic_initial_
Definition es8388.h:102
ES8388MicGain mic_gain_
Definition es8388.h:98
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
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
Definition bme680.cpp:28
ESPHOME_LOG_TAG(TAG, "es8388")
@ ADC_INPUT_MIC_LINE1
Definition es8388.h:23
@ ADC_INPUT_MIC_DIFFERENCE
Definition es8388.h:25
@ ADC_INPUT_MIC_LINE2
Definition es8388.h:24
uint32_t value
Definition hal.cpp:41
void HOT delay(uint32_t ms)
Definition hal.cpp:85
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
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18