ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
es7210.cpp
Go to the documentation of this file.
1#include "es7210.h"
2#include "es7210_const.h"
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5#include <cinttypes>
6
7namespace esphome::es7210 {
8
9ESPHOME_LOG_TAG(TAG, "es7210");
10
11static const size_t MCLK_DIV_FRE = 256;
12
13// Mark the component as failed; use only in setup
14#define ES7210_ERROR_FAILED(func) \
15 if (!(func)) { \
16 this->mark_failed(LOG_STR("Failed to initialize")); \
17 return; \
18 }
19
20// Return false; use outside of setup
21#define ES7210_ERROR_CHECK(func) \
22 if (!(func)) { \
23 return false; \
24 }
25
27 ESP_LOGCONFIG(TAG,
28 "ES7210 audio ADC:\n"
29 " Bits Per Sample: %" PRIu8 "\n"
30 " Sample Rate: %" PRIu32 "\n"
31 " Channel Gains: %.1f dB, %.1f dB, %.1f dB, %.1f dB",
32 this->bits_per_sample_, this->sample_rate_, this->channel_gains_[0], this->channel_gains_[1],
33 this->channel_gains_[2], this->channel_gains_[3]);
34}
35
36static constexpr i2c::I2CRegValue ES7210_RESET[] PROGMEM = {
37 // Software reset
38 {ES7210_RESET_REG00, 0xff},
39 {ES7210_RESET_REG00, 0x32},
40 {ES7210_CLOCK_OFF_REG01, 0x3f},
41 // Set initialization time when device powers up
42 {ES7210_TIME_CONTROL0_REG09, 0x30},
43 {ES7210_TIME_CONTROL1_REG0A, 0x30},
44 // Configure HFP for all ADC channels
45 {ES7210_ADC12_HPF2_REG23, 0x2a},
46 {ES7210_ADC12_HPF1_REG22, 0x0a},
47 {ES7210_ADC34_HPF2_REG20, 0x0a},
48 {ES7210_ADC34_HPF1_REG21, 0x2a},
49};
50
51static constexpr i2c::I2CRegValue ES7210_ANALOG[] PROGMEM = {
52 // Configure analog power
53 {ES7210_ANALOG_REG40, 0xC3},
54 // Set mic bias
55 {ES7210_MIC12_BIAS_REG41, 0x70},
56 {ES7210_MIC34_BIAS_REG42, 0x70},
57};
58
59static constexpr i2c::I2CRegValue ES7210_POWER_ON[] PROGMEM = {
60 // Power on mics 1 through 4
61 {ES7210_MIC1_POWER_REG47, 0x08},
62 {ES7210_MIC2_POWER_REG48, 0x08},
63 {ES7210_MIC3_POWER_REG49, 0x08},
64 {ES7210_MIC4_POWER_REG4A, 0x08},
65 // Power down DLL
66 {ES7210_POWER_DOWN_REG06, 0x04},
67 // Power on MIC1-4 bias & ADC1-4 & PGA1-4 Power
68 {ES7210_MIC12_POWER_REG4B, 0x0F},
69 {ES7210_MIC34_POWER_REG4C, 0x0F},
70 // Enable device
71 {ES7210_RESET_REG00, 0x71},
72 {ES7210_RESET_REG00, 0x41},
73};
74
76 ES7210_ERROR_FAILED(this->write_registers(ES7210_RESET));
77
78 // Secondary I2S mode settings
79 ES7210_ERROR_FAILED(this->update_register(ES7210_MODE_CONFIG_REG08, 0x01, 0x00));
80
81 ES7210_ERROR_FAILED(this->write_registers(ES7210_ANALOG));
82
83 // Configure I2S settings, sample rate, and microphone gains
84 ES7210_ERROR_FAILED(this->configure_i2s_format_());
85 ES7210_ERROR_FAILED(this->configure_sample_rate_());
86 ES7210_ERROR_FAILED(this->configure_mic_gain_());
87
88 ES7210_ERROR_FAILED(this->write_registers(ES7210_POWER_ON));
89
90 this->setup_complete_ = true;
91}
92
93bool ES7210::set_mic_gain(float mic_gain) {
94 this->channel_gains_.fill(clamp<float>(mic_gain, ES7210_MIC_GAIN_MIN, ES7210_MIC_GAIN_MAX));
95 if (this->setup_complete_) {
96 return this->configure_mic_gain_();
97 }
98 return true;
99}
100
101bool ES7210::set_channel_gain(uint8_t channel, float gain) {
102 if (channel >= this->channel_gains_.size())
103 return false;
104 this->channel_gains_[channel] = clamp<float>(gain, ES7210_MIC_GAIN_MIN, ES7210_MIC_GAIN_MAX);
105 if (this->setup_complete_) {
106 return this->configure_mic_gain_();
107 }
108 return true;
109}
110
112 uint32_t mclk_fre = this->sample_rate_ * MCLK_DIV_FRE;
113 int coeff = -1;
114
115 for (size_t i = 0; i < (sizeof(ES7210_COEFFICIENTS) / sizeof(ES7210_COEFFICIENTS[0])); ++i) {
116 if (ES7210_COEFFICIENTS[i].lrclk == this->sample_rate_ && ES7210_COEFFICIENTS[i].mclk == mclk_fre)
117 coeff = static_cast<int>(i);
118 }
119
120 if (coeff >= 0) {
121 // Set adc_div & doubler & dll
122 uint8_t regv;
123 ES7210_ERROR_CHECK(this->read_byte(ES7210_MAINCLK_REG02, &regv));
124 regv = regv & 0x00;
125 regv |= ES7210_COEFFICIENTS[coeff].adc_div;
126 regv |= ES7210_COEFFICIENTS[coeff].doubler << 6;
127 regv |= ES7210_COEFFICIENTS[coeff].dll << 7;
128
129 ES7210_ERROR_CHECK(this->write_byte(ES7210_MAINCLK_REG02, regv));
130
131 // Set osr
132 regv = ES7210_COEFFICIENTS[coeff].osr;
133 ES7210_ERROR_CHECK(this->write_byte(ES7210_OSR_REG07, regv));
134 // Set lrck
135 regv = ES7210_COEFFICIENTS[coeff].lrck_h;
136 ES7210_ERROR_CHECK(this->write_byte(ES7210_LRCK_DIVH_REG04, regv));
137 regv = ES7210_COEFFICIENTS[coeff].lrck_l;
138 ES7210_ERROR_CHECK(this->write_byte(ES7210_LRCK_DIVL_REG05, regv));
139 } else {
140 // Invalid sample frequency
141 ESP_LOGE(TAG, "Invalid sample rate");
142 return false;
143 }
144
145 return true;
146}
147
149 for (uint8_t i = 0; i < 4; ++i) {
150 ES7210_ERROR_CHECK(this->update_register(ES7210_MIC1_GAIN_REG43 + i, 0x10, 0x00));
151 }
152 ES7210_ERROR_CHECK(this->write_byte(ES7210_MIC12_POWER_REG4B, 0xff));
153 ES7210_ERROR_CHECK(this->write_byte(ES7210_MIC34_POWER_REG4C, 0xff));
154
155 // Configure mic 1
156 ES7210_ERROR_CHECK(this->update_register(ES7210_CLOCK_OFF_REG01, 0x0b, 0x00));
157 ES7210_ERROR_CHECK(this->write_byte(ES7210_MIC12_POWER_REG4B, 0x00));
158 ES7210_ERROR_CHECK(this->update_register(ES7210_MIC1_GAIN_REG43, 0x10, 0x10));
159 ES7210_ERROR_CHECK(
160 this->update_register(ES7210_MIC1_GAIN_REG43, 0x0f, this->es7210_gain_reg_value_(this->channel_gains_[0])));
161
162 // Configure mic 2
163 ES7210_ERROR_CHECK(this->update_register(ES7210_CLOCK_OFF_REG01, 0x0b, 0x00));
164 ES7210_ERROR_CHECK(this->write_byte(ES7210_MIC12_POWER_REG4B, 0x00));
165 ES7210_ERROR_CHECK(this->update_register(ES7210_MIC2_GAIN_REG44, 0x10, 0x10));
166 ES7210_ERROR_CHECK(
167 this->update_register(ES7210_MIC2_GAIN_REG44, 0x0f, this->es7210_gain_reg_value_(this->channel_gains_[1])));
168
169 // Configure mic 3
170 // MIC3 uses the ADC3/4 and MIC3/4 clock domains (bits 2 and 4), not the MIC1/2 domains.
171 ES7210_ERROR_CHECK(this->update_register(ES7210_CLOCK_OFF_REG01, 0x15, 0x00));
172 ES7210_ERROR_CHECK(this->write_byte(ES7210_MIC34_POWER_REG4C, 0x00));
173 ES7210_ERROR_CHECK(this->update_register(ES7210_MIC3_GAIN_REG45, 0x10, 0x10));
174 ES7210_ERROR_CHECK(
175 this->update_register(ES7210_MIC3_GAIN_REG45, 0x0f, this->es7210_gain_reg_value_(this->channel_gains_[2])));
176
177 // Configure mic 4
178 ES7210_ERROR_CHECK(this->update_register(ES7210_CLOCK_OFF_REG01, 0x15, 0x00));
179 ES7210_ERROR_CHECK(this->write_byte(ES7210_MIC34_POWER_REG4C, 0x00));
180 ES7210_ERROR_CHECK(this->update_register(ES7210_MIC4_GAIN_REG46, 0x10, 0x10));
181 ES7210_ERROR_CHECK(
182 this->update_register(ES7210_MIC4_GAIN_REG46, 0x0f, this->es7210_gain_reg_value_(this->channel_gains_[3])));
183
184 return true;
185}
186
187uint8_t ES7210::es7210_gain_reg_value_(float mic_gain) {
188 // reg: 12 - 34.5dB, 13 - 36dB, 14 - 37.5dB
189 mic_gain += 0.5f;
190 if (mic_gain <= 33.0f) {
191 return (uint8_t) (mic_gain / 3);
192 }
193 if (mic_gain < 36.0f) {
194 return 12;
195 }
196 if (mic_gain < 37.0f) {
197 return 13;
198 }
199 return 14;
200}
201
203 // Configure bits per sample
204 uint8_t reg_val = 0;
205 switch (this->bits_per_sample_) {
207 reg_val = 0x60;
208 break;
210 reg_val = 0x40;
211 break;
213 reg_val = 0x20;
214 break;
216 reg_val = 0x00;
217 break;
219 reg_val = 0x80;
220 break;
221 default:
222 return false;
223 }
224 ES7210_ERROR_CHECK(this->write_byte(ES7210_SDP_INTERFACE1_REG11, reg_val));
225
226 if (this->enable_tdm_) {
227 ES7210_ERROR_CHECK(this->write_byte(ES7210_SDP_INTERFACE2_REG12, 0x02));
228 } else {
229 // Microphones 1 and 2 output on SDOUT1, microphones 3 and 4 output on SDOUT2
230 ES7210_ERROR_CHECK(this->write_byte(ES7210_SDP_INTERFACE2_REG12, 0x00));
231 }
232
233 return true;
234}
235
236} // namespace esphome::es7210
uint8_t es7210_gain_reg_value_(float mic_gain)
Convert floating point mic gain value to register value.
Definition es7210.cpp:187
float mic_gain() override
Returns the MIC1 gain; other channels may have different gains.
Definition es7210.h:40
void setup() override
Definition es7210.cpp:75
void dump_config() override
Definition es7210.cpp:26
uint32_t sample_rate_
Definition es7210.h:56
bool set_mic_gain(float mic_gain) override
Sets all four channel gains, replacing any configured per-channel gains.
Definition es7210.cpp:93
std::array< float, 4 > channel_gains_
Definition es7210.h:54
bool set_channel_gain(uint8_t channel, float gain)
Sets a zero-based channel gain; returns false for an invalid channel or an I2C failure.
Definition es7210.cpp:101
ES7210BitsPerSample bits_per_sample_
Definition es7210.h:55
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
AlsGain501 gain
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
Definition bme680.cpp:28
ESPHOME_LOG_TAG(TAG, "es7210")
@ ES7210_BITS_PER_SAMPLE_20
Definition es7210.h:16
@ ES7210_BITS_PER_SAMPLE_18
Definition es7210.h:15
@ ES7210_BITS_PER_SAMPLE_16
Definition es7210.h:14
@ ES7210_BITS_PER_SAMPLE_32
Definition es7210.h:18
@ ES7210_BITS_PER_SAMPLE_24
Definition es7210.h:17
static void uint32_t
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18