arduino-audio-driver
Loading...
Searching...
No Matches
ES8374.h
Go to the documentation of this file.
1/*
2 * ESPRESSIF MIT License
3 *
4 * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
5 *
6 * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in
7 * which case, it is free of charge, to any person obtaining a copy of this
8 * software and associated documentation files (the "Software"), to deal in the
9 * Software without restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do
12 * so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#pragma once
28
30#include "DriverCommon.h"
31#include "Platforms/API_I2C.h"
32#include "stdbool.h"
33#include <string.h>
34
35
36namespace audio_driver {
37
38/* Moved up from the bottom so static helpers that use it can see it */
45
51class ES8374 {
52 public:
53 static constexpr int ES8374_ADDR = 0x10;
54 ES8374() = default;
55
57 void setWire(i2c_bus_handle_t handle) { i2c_handle = handle; }
58
60 void setAddress(int addr) {
61 if (addr > 0) i2c_addr = addr;
62 }
63
65 void readAll() {
66 for (int i = 0; i < 50; i++) {
67 uint8_t reg = 0;
68 readReg(i, &reg);
69 AD_LOGI("%x: %x", i, reg);
70 }
71 }
72
74 error_t setVoiceMute(bool enable) {
75 error_t res = RESULT_OK;
76 uint8_t reg = 0;
77
78 res |= readReg(0x36, &reg);
79 if (res == 0) {
80 reg = reg & 0xdf;
81 res |= writeReg(0x36, reg | (((int)enable) << 5));
82 }
83
84 return res;
85 }
86
89 error_t res = RESULT_OK;
90 uint8_t reg = 0;
91
92 res |= readReg(0x36, &reg);
93 if (res == RESULT_OK) {
94 reg = reg & 0x40;
95 }
96
97 return res == RESULT_OK ? reg : res;
98 }
99
102 error_t res = RESULT_OK;
103 int tmp = 0;
104 res |= configFmt(CODEC_MODE_BOTH, iface->fmt);
105 if (iface->bits == BIT_LENGTH_16BITS) {
106 tmp = BIT_LENGTH_16BITS;
107 } else if (iface->bits == BIT_LENGTH_24BITS) {
108 tmp = BIT_LENGTH_24BITS;
109 } else {
110 tmp = BIT_LENGTH_32BITS;
111 }
113 return res;
114 }
115
118 if (initialized) {
119 AD_LOGW("The es8374 codec has already been initialized!");
120 return RESULT_FAIL;
121 }
122 error_t res = RESULT_OK;
123 es_i2s_clock_t clkdiv;
124
125 clkdiv.lclk_div = LCLK_DIV_256;
126 clkdiv.sclk_div = MCLK_DIV_4;
127
128 // TODO
129 res |= stop(codec_mode);
130 res |= initReg(
131 (codec_mode_t)cfg->i2s.mode,
132 static_cast<i2s_format_t>((BIT_LENGTH_16BITS << 4) | cfg->i2s.fmt),
133 clkdiv);
136 res |= configFmt(codec_mode, (i2s_format_t)cfg->i2s.mode);
137 res |= configI2S(codec_mode, &(cfg->i2s));
138 initialized = true;
139 return res;
140 }
141
144 initialized = false;
145 return writeReg(0x00, 0x7F); // IC Reset and STOP
146 }
147
149 error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active) {
150 error_t res = RESULT_OK;
151 int es_mode_t = 0;
152 switch (mode) {
154 es_mode_t = CODEC_MODE_ENCODE;
155 break;
157 es_mode_t = CODEC_MODE_LINE_IN;
158 break;
160 es_mode_t = CODEC_MODE_DECODE;
161 break;
162 case CODEC_MODE_BOTH:
163 es_mode_t = CODEC_MODE_BOTH;
164 break;
165 default:
166 es_mode_t = CODEC_MODE_DECODE;
167 AD_LOGW("Codec mode not support, default is decode mode");
168 break;
169 }
170 if (!ctrl_state_active) {
171 res = stop((codec_mode_t)es_mode_t);
172 } else {
173 res = start((codec_mode_t)es_mode_t);
174 AD_LOGD("start default is decode mode:%d", es_mode_t);
175 }
176 return res;
177 }
178
181 error_t res = RESULT_OK;
182
183 if (volume < 0) {
184 volume = 192;
185 } else if (volume > 96) {
186 volume = 0;
187 } else {
188 volume = 192 - volume * 2;
189 }
190
191 res = writeReg(0x38, volume);
192
193 return res;
194 }
195
197 error_t getVoiceVolume(int* volume) {
198 error_t res = 0;
199 uint8_t reg = 0;
200
201 res = readReg(0x38, &reg);
202
203 if (res == RESULT_FAIL) {
204 *volume = 0;
205 } else {
206 *volume = (192 - reg) / 2;
207 if (*volume > 96) {
208 *volume = 100;
209 }
210 }
211
212 return res;
213 }
214 error_t writeReg(uint8_t reg_add, uint8_t data) {
215 return i2c_bus_write_bytes(i2c_handle, i2c_addr, &reg_add, sizeof(reg_add),
216 &data, sizeof(data));
217 }
218
219 int readReg(uint8_t reg_add, uint8_t* regv) {
220 uint8_t regdata = 0xFF;
221 uint8_t res = 0;
222
223 if (i2c_bus_read_bytes(i2c_handle, i2c_addr, &reg_add, sizeof(reg_add),
224 &regdata, 1) == 0) {
225 *regv = regdata;
226 return res;
227 } else {
228 AD_LOGI("Read Audio Codec Register Failed!");
229 res = -1;
230 return res;
231 }
232 }
233
236 error_t res = RESULT_OK;
237 uint8_t reg = 0;
238 int bits = (int)bit_per_sample & 0x0f;
239
240 if (mode == CODEC_MODE_ENCODE || mode == CODEC_MODE_BOTH) {
241 res |= readReg(0x10, &reg);
242 if (res == 0) {
243 reg = reg & 0xe3;
244 res |= writeReg(0x10, reg | (bits << 2));
245 }
246 }
247 if (mode == CODEC_MODE_DECODE || mode == CODEC_MODE_BOTH) {
248 res |= readReg(0x11, &reg);
249 if (res == 0) {
250 reg = reg & 0xe3;
251 res |= writeReg(0x11, reg | (bits << 2));
252 }
253 }
254
255 return res;
256 }
257
260 error_t res = RESULT_OK;
261 uint8_t reg = 0;
262 int fmt_tmp, fmt_i2s;
263
264 fmt_tmp = ((fmt & 0xf0) >> 4);
265 fmt_i2s = fmt & 0x0f;
266 if (mode == CODEC_MODE_ENCODE || mode == CODEC_MODE_BOTH) {
267 res |= readReg(0x10, &reg);
268 if (res == 0) {
269 reg = reg & 0xfc;
270 res |= writeReg(0x10, reg | fmt_i2s);
271 res |= setBitsPerSample(mode, (sample_bits_t)fmt_tmp);
272 }
273 }
274 if (mode == CODEC_MODE_DECODE || mode == CODEC_MODE_BOTH) {
275 res |= readReg(0x11, &reg);
276 if (res == 0) {
277 reg = reg & 0xfc;
278 res |= writeReg(0x11, reg | (fmt_i2s));
279 res |= setBitsPerSample(mode, (sample_bits_t)fmt_tmp);
280 }
281 }
282
283 return res;
284 }
285
288 error_t res = RESULT_OK;
289 uint8_t reg = 0;
290
291 if (mode == CODEC_MODE_LINE_IN) {
292 res |= readReg(0x1a, &reg); // set monomixer
293 reg |= 0x60;
294 reg |= 0x20;
295 reg &= 0xf7;
296 res |= writeReg(0x1a, reg);
297 res |= readReg(0x1c, &reg); // set spk mixer
298 reg |= 0x40;
299 res |= writeReg(0x1c, reg);
300 res |= writeReg(0x1D, 0x02); // spk set
301 res |= writeReg(0x1F, 0x00); // spk set
302 res |= writeReg(0x1E, 0xA0); // spk on
303 }
304 if (mode == CODEC_MODE_ENCODE || mode == CODEC_MODE_BOTH ||
305 mode == CODEC_MODE_LINE_IN) {
306 res |= readReg(0x21, &reg); // power up adc and input
307 reg &= 0x3f;
308 res |= writeReg(0x21, reg);
309 res |= readReg(0x10, &reg); // power up adc and input
310 reg &= 0x3f;
311 res |= writeReg(0x10, reg);
312 }
313
314 if (mode == CODEC_MODE_DECODE || mode == CODEC_MODE_BOTH ||
315 mode == CODEC_MODE_LINE_IN) {
316 res |= readReg(0x1a, &reg); // disable lout
317 reg |= 0x08;
318 res |= writeReg(0x1a, reg);
319 reg &= 0xdf;
320 res |= writeReg(0x1a, reg);
321 res |= writeReg(0x1D, 0x12); // mute speaker
322 res |= writeReg(0x1E, 0x20); // disable class d
323 res |= readReg(0x15, &reg); // power up dac
324 reg &= 0xdf;
325 res |= writeReg(0x15, reg);
326 res |= readReg(0x1a, &reg); // disable lout
327 reg |= 0x20;
328 res |= writeReg(0x1a, reg);
329 reg &= 0xf7;
330 res |= writeReg(0x1a, reg);
331 res |= writeReg(0x1D, 0x02); // mute speaker
332 res |= writeReg(0x1E, 0xa0); // disable class d
333
334 res |= setVoiceMute(false);
335 }
336
337 return res;
338 }
339
342 error_t res = RESULT_OK;
343 uint8_t reg = 0;
344
345 if (mode == CODEC_MODE_LINE_IN) {
346 res |= readReg(0x1a, &reg); // disable lout
347 reg |= 0x08;
348 res |= writeReg(0x1a, reg);
349 reg &= 0x9f;
350 res |= writeReg(0x1a, reg);
351 res |= writeReg(0x1D, 0x12); // mute speaker
352 res |= writeReg(0x1E, 0x20); // disable class d
353 res |= readReg(0x1c, &reg); // disable spkmixer
354 reg &= 0xbf;
355 res |= writeReg(0x1c, reg);
356 res |= writeReg(0x1F, 0x00); // spk set
357 }
358 if (mode == CODEC_MODE_DECODE || mode == CODEC_MODE_BOTH) {
359 res |= setVoiceMute(true);
360
361 res |= readReg(0x1a, &reg); // disable lout
362 reg |= 0x08;
363 res |= writeReg(0x1a, reg);
364 reg &= 0xdf;
365 res |= writeReg(0x1a, reg);
366 res |= writeReg(0x1D, 0x12); // mute speaker
367 res |= writeReg(0x1E, 0x20); // disable class d
368 res |= readReg(0x15, &reg); // power up dac
369 reg |= 0x20;
370 res |= writeReg(0x15, reg);
371 }
372 if (mode == CODEC_MODE_ENCODE || mode == CODEC_MODE_BOTH) {
373 res |= readReg(0x10, &reg); // power up adc and input
374 reg |= 0xc0;
375 res |= writeReg(0x10, reg);
376 res |= readReg(0x21, &reg); // power up adc and input
377 reg |= 0xc0;
378 res |= writeReg(0x21, reg);
379 }
380
381 return res;
382 }
383
386 error_t res = RESULT_OK;
387 uint8_t reg = 0;
388
389 res |= readReg(0x0f, &reg); // power up adc and input
390 reg &= 0xe0;
391 int divratio = 0;
392 switch (cfg.sclk_div) {
393 case MCLK_DIV_1:
394 divratio = 1;
395 break;
396 case MCLK_DIV_2: // = 2,
397 divratio = 2;
398 break;
399 case MCLK_DIV_3: // = 3,
400 divratio = 3;
401 break;
402 case MCLK_DIV_4: // = 4,
403 divratio = 4;
404 break;
405 case MCLK_DIV_5: // = 20,
406 divratio = 5;
407 break;
408 case MCLK_DIV_6: // = 5,
409 divratio = 6;
410 break;
411 case MCLK_DIV_7: // = 29,
412 divratio = 7;
413 break;
414 case MCLK_DIV_8: // = 6,
415 divratio = 8;
416 break;
417 case MCLK_DIV_9: // = 7,
418 divratio = 9;
419 break;
420 case MCLK_DIV_10: // = 21,
421 divratio = 10;
422 break;
423 case MCLK_DIV_11: // = 8,
424 divratio = 11;
425 break;
426 case MCLK_DIV_12: // = 9,
427 divratio = 12;
428 break;
429 case MCLK_DIV_13: // = 30,
430 divratio = 13;
431 break;
432 case MCLK_DIV_14: // = 31
433 divratio = 14;
434 break;
435 case MCLK_DIV_15: // = 22,
436 divratio = 15;
437 break;
438 case MCLK_DIV_16: // = 10,
439 divratio = 16;
440 break;
441 case MCLK_DIV_17: // = 23,
442 divratio = 17;
443 break;
444 case MCLK_DIV_18: // = 11,
445 divratio = 18;
446 break;
447 case MCLK_DIV_20: // = 24,
448 divratio = 19;
449 break;
450 case MCLK_DIV_22: // = 12,
451 divratio = 20;
452 break;
453 case MCLK_DIV_24: // = 13,
454 divratio = 21;
455 break;
456 case MCLK_DIV_25: // = 25,
457 divratio = 22;
458 break;
459 case MCLK_DIV_30: // = 26,
460 divratio = 23;
461 break;
462 case MCLK_DIV_32: // = 27,
463 divratio = 24;
464 break;
465 case MCLK_DIV_33: // = 14,
466 divratio = 25;
467 break;
468 case MCLK_DIV_34: // = 28,
469 divratio = 26;
470 break;
471 case MCLK_DIV_36: // = 15,
472 divratio = 27;
473 break;
474 case MCLK_DIV_44: // = 16,
475 divratio = 28;
476 break;
477 case MCLK_DIV_48: // = 17,
478 divratio = 29;
479 break;
480 case MCLK_DIV_66: // = 18,
481 divratio = 30;
482 break;
483 case MCLK_DIV_72: // = 19,
484 divratio = 31;
485 break;
486 default:
487 break;
488 }
489 reg |= divratio;
490 res |= writeReg(0x0f, reg);
491
492 int dacratio_l = 0;
493 int dacratio_h = 0;
494
495 switch (cfg.lclk_div) {
496 case LCLK_DIV_128:
497 dacratio_l = 128 % 256;
498 dacratio_h = 128 / 256;
499 break;
500 case LCLK_DIV_192:
501 dacratio_l = 192 % 256;
502 dacratio_h = 192 / 256;
503 break;
504 case LCLK_DIV_256:
505 dacratio_l = 256 % 256;
506 dacratio_h = 256 / 256;
507 break;
508 case LCLK_DIV_384:
509 dacratio_l = 384 % 256;
510 dacratio_h = 384 / 256;
511 break;
512 case LCLK_DIV_512:
513 dacratio_l = 512 % 256;
514 dacratio_h = 512 / 256;
515 break;
516 case LCLK_DIV_576:
517 dacratio_l = 576 % 256;
518 dacratio_h = 576 / 256;
519 break;
520 case LCLK_DIV_768:
521 dacratio_l = 768 % 256;
522 dacratio_h = 768 / 256;
523 break;
524 case LCLK_DIV_1024:
525 dacratio_l = 1024 % 256;
526 dacratio_h = 1024 / 256;
527 break;
528 case LCLK_DIV_1152:
529 dacratio_l = 1152 % 256;
530 dacratio_h = 1152 / 256;
531 break;
532 case LCLK_DIV_1408:
533 dacratio_l = 1408 % 256;
534 dacratio_h = 1408 / 256;
535 break;
536 case LCLK_DIV_1536:
537 dacratio_l = 1536 % 256;
538 dacratio_h = 1536 / 256;
539 break;
540 case LCLK_DIV_2112:
541 dacratio_l = 2112 % 256;
542 dacratio_h = 2112 / 256;
543 break;
544 case LCLK_DIV_2304:
545 dacratio_l = 2304 % 256;
546 dacratio_h = 2304 / 256;
547 break;
548 case LCLK_DIV_125:
549 dacratio_l = 125 % 256;
550 dacratio_h = 125 / 256;
551 break;
552 case LCLK_DIV_136:
553 dacratio_l = 136 % 256;
554 dacratio_h = 136 / 256;
555 break;
556 case LCLK_DIV_250:
557 dacratio_l = 250 % 256;
558 dacratio_h = 250 / 256;
559 break;
560 case LCLK_DIV_272:
561 dacratio_l = 272 % 256;
562 dacratio_h = 272 / 256;
563 break;
564 case LCLK_DIV_375:
565 dacratio_l = 375 % 256;
566 dacratio_h = 375 / 256;
567 break;
568 case LCLK_DIV_500:
569 dacratio_l = 500 % 256;
570 dacratio_h = 500 / 256;
571 break;
572 case LCLK_DIV_544:
573 dacratio_l = 544 % 256;
574 dacratio_h = 544 / 256;
575 break;
576 case LCLK_DIV_750:
577 dacratio_l = 750 % 256;
578 dacratio_h = 750 / 256;
579 break;
580 case LCLK_DIV_1000:
581 dacratio_l = 1000 % 256;
582 dacratio_h = 1000 / 256;
583 break;
584 case LCLK_DIV_1088:
585 dacratio_l = 1088 % 256;
586 dacratio_h = 1088 / 256;
587 break;
588 case LCLK_DIV_1496:
589 dacratio_l = 1496 % 256;
590 dacratio_h = 1496 / 256;
591 break;
592 case LCLK_DIV_1500:
593 dacratio_l = 1500 % 256;
594 dacratio_h = 1500 / 256;
595 break;
596 default:
597 break;
598 }
599 res |= writeReg(0x06, dacratio_h); // ADCFsMode,singel
600 // SPEED,RATIO=256
601 res |= writeReg(0x07, dacratio_l); // ADCFsMode,singel
602 // SPEED,RATIO=256
603
604 return res;
605 }
606
609 error_t res = RESULT_OK;
610 uint8_t reg = 0;
611
612 reg = 0x1d;
613
614 res = writeReg(reg, 0x02);
615 res |= readReg(0x1c, &reg); // set spk mixer
616 reg |= 0x80;
617 res |= writeReg(0x1c, reg);
618 res |= writeReg(0x1D, 0x02); // spk set
619 res |= writeReg(0x1F, 0x00); // spk set
620 res |= writeReg(0x1E, 0xA0); // spk on
621
622 return res;
623 }
624
627 error_t res = RESULT_OK;
628 uint8_t reg = 0;
629
630 res |= readReg(0x21, &reg);
631 if (res == 0) {
632 reg = (reg & 0xcf) | 0x14;
633 res |= writeReg(0x21, reg);
634 }
635
636 return res;
637 }
638
641 error_t res = RESULT_OK;
642
643 if (gain > MIC_GAIN_MIN && gain < MIC_GAIN_24DB) {
644 int gain_n = 0;
645 gain_n = (int)gain / 3;
646 res = writeReg(0x22, gain_n | (gain_n << 4)); // MIC PGA
647 } else {
648 res = -1;
649 AD_LOGI("invalid microphone gain!");
650 }
651
652 return res;
653 }
654
655 error_t setAdcDacVolume(int mode, int volume, int dot) {
656 error_t res = RESULT_OK;
657
658 if (volume < -96 || volume > 0) {
659 AD_LOGI("Warning: volume < -96! or > 0!");
660 if (volume < -96) {
661 volume = -96;
662 } else {
663 volume = 0;
664 }
665 }
666 dot = (dot >= 5 ? 1 : 0);
667 volume = (-volume << 1) + dot;
668 if (mode == CODEC_MODE_ENCODE || mode == CODEC_MODE_BOTH) {
669 res |= writeReg(0x25, volume);
670 }
671 if (mode == CODEC_MODE_DECODE || mode == CODEC_MODE_BOTH) {
672 res |= writeReg(0x38, volume);
673 }
674
675 return res;
676 }
677
679 error_t res = RESULT_OK;
680 uint8_t reg = 0;
681
682 if (gain > ES8374_PGA_GAIN_MIN && gain < ES8374_PGA_GAIN_MAX) {
683 res = readReg(0x21, &reg);
684 reg &= 0xfb;
685 reg |= gain << 2;
686 res = writeReg(0x21, reg); // MIC PGA
687 } else {
688 res = 0xff;
689 AD_LOGI("invalid microphone gain!");
690 }
691
692 return res;
693 }
694
696 error_t res = RESULT_OK;
697 uint8_t reg;
698
699 res |= writeReg(0x00, 0x3F); // IC Rst start
700 res |= writeReg(0x00, 0x03); // IC Rst stop
701 res |= writeReg(0x01, 0x7F); // IC clk on
702
703 res |= readReg(0x0F, &reg);
704 reg &= 0x7f;
705 reg |= (ms_mode << 7);
706 res |= writeReg(0x0f, reg); // CODEC IN I2S SLAVE MODE
707
708 res |= writeReg(0x6F, 0xA0); // pll set:mode enable
709 res |= writeReg(0x72, 0x41); // pll set:mode set
710 res |= writeReg(0x09, 0x01); // pll set:reset on ,set start
711 res |= writeReg(0x0C, 0x22); // pll set:k
712 res |= writeReg(0x0D, 0x2E); // pll set:k
713 res |= writeReg(0x0E, 0xC6); // pll set:k
714 res |= writeReg(0x0A, 0x3A); // pll set:
715 res |= writeReg(0x0B, 0x07); // pll set:n
716 res |= writeReg(0x09, 0x41); // pll set:reset off ,set stop
717
718 res |= i2sConfigClock(cfg);
719
720 res |= writeReg(0x24, 0x08); // adc set
721 res |= writeReg(0x36, 0x00); // dac set
722 res |= writeReg(0x12, 0x30); // timming set
723 res |= writeReg(0x13, 0x20); // timming set
724
725 // TODO
726 res |= configFmt(CODEC_MODE_ENCODE, fmt);
727 res |= configFmt(CODEC_MODE_DECODE, fmt);
728
729 res |= writeReg(0x21, 0x50); // adc set: SEL LIN1 CH+PGAGAIN=0DB
730 res |= writeReg(0x22, 0xFF); // adc set: PGA GAIN=0DB
731 res |= writeReg(0x21, 0x14); // adc set: SEL LIN1 CH+PGAGAIN=18DB
732 res |= writeReg(0x22, 0x55); // pga = +15db
733 res |=
734 writeReg(0x08, 0x21); // set class d divider = 33, to avoid the
735 // high frequency tone on laudspeaker
736 res |= writeReg(0x00, 0x80); // IC START
737
738 res |= setAdcDacVolume(CODEC_MODE_ENCODE, 0, 0); // 0db
739 res |= setAdcDacVolume(CODEC_MODE_DECODE, 0, 0); // 0db
740
741 res |= writeReg(0x14, 0x8A); // IC START
742 res |= writeReg(0x15, 0x40); // IC START
743 res |= writeReg(0x1A, 0xA0); // monoout set
744 res |= writeReg(0x1B, 0x19); // monoout set
745 res |= writeReg(0x1C, 0x90); // spk set
746 res |= writeReg(0x1D, 0x01); // spk set
747 res |= writeReg(0x1F, 0x00); // spk set
748 res |= writeReg(0x1E, 0x20); // spk on
749 res |= writeReg(0x28, 0x00); // alc set
750 res |= writeReg(0x25, 0x00); // ADCVOLUME on
751 res |= writeReg(0x38, 0x00); // DACVOLUME on
752 res |= writeReg(0x37, 0x30); // dac set
753 res |= writeReg(0x6D,
754 0x60); // SEL:GPIO1=DMIC CLK OUT+SEL:GPIO2=PLL CLK OUT
755 res |= writeReg(0x71, 0x05); // for automute setting
756 res |= writeReg(0x73, 0x70);
757
758 res |= configOutputDevice(); // 0x3c Enable DAC and Enable
759 // Lout/Rout/1/2
760 res |= configInputDevice(); // 0x00 LINSEL & RINSEL, LIN1/RIN1 as
761 // ADC Input; DSSEL,use one DS Reg11;
762 // DSR, LINPUT1-RINPUT1
763 res |= setVoiceVolume(0);
764
765 res |= writeReg(0x37, 0x00); // dac set
766
767 return res;
768 }
769
770 protected:
773 bool initialized = false;
774};
775
776} // namespace audio_driver
@ MCLK_DIV_20
Definition CodecConstants.h:31
@ MCLK_DIV_30
Definition CodecConstants.h:33
@ MCLK_DIV_33
Definition CodecConstants.h:21
@ MCLK_DIV_1
Definition CodecConstants.h:8
@ MCLK_DIV_24
Definition CodecConstants.h:20
@ MCLK_DIV_44
Definition CodecConstants.h:23
@ MCLK_DIV_18
Definition CodecConstants.h:18
@ MCLK_DIV_11
Definition CodecConstants.h:15
@ MCLK_DIV_16
Definition CodecConstants.h:17
@ MCLK_DIV_3
Definition CodecConstants.h:10
@ MCLK_DIV_22
Definition CodecConstants.h:19
@ MCLK_DIV_32
Definition CodecConstants.h:34
@ MCLK_DIV_9
Definition CodecConstants.h:14
@ MCLK_DIV_34
Definition CodecConstants.h:35
@ MCLK_DIV_72
Definition CodecConstants.h:26
@ MCLK_DIV_66
Definition CodecConstants.h:25
@ MCLK_DIV_14
Definition CodecConstants.h:38
@ MCLK_DIV_5
Definition CodecConstants.h:27
@ MCLK_DIV_4
Definition CodecConstants.h:11
@ MCLK_DIV_8
Definition CodecConstants.h:13
@ MCLK_DIV_2
Definition CodecConstants.h:9
@ MCLK_DIV_7
Definition CodecConstants.h:36
@ MCLK_DIV_6
Definition CodecConstants.h:12
@ MCLK_DIV_36
Definition CodecConstants.h:22
@ MCLK_DIV_48
Definition CodecConstants.h:24
@ MCLK_DIV_10
Definition CodecConstants.h:28
@ MCLK_DIV_17
Definition CodecConstants.h:30
@ MCLK_DIV_13
Definition CodecConstants.h:37
@ MCLK_DIV_15
Definition CodecConstants.h:29
@ MCLK_DIV_12
Definition CodecConstants.h:16
@ MCLK_DIV_25
Definition CodecConstants.h:32
@ LCLK_DIV_1408
Definition CodecConstants.h:54
@ LCLK_DIV_750
Definition CodecConstants.h:66
@ LCLK_DIV_1536
Definition CodecConstants.h:55
@ LCLK_DIV_272
Definition CodecConstants.h:62
@ LCLK_DIV_1500
Definition CodecConstants.h:70
@ LCLK_DIV_375
Definition CodecConstants.h:63
@ LCLK_DIV_512
Definition CodecConstants.h:49
@ LCLK_DIV_1496
Definition CodecConstants.h:69
@ LCLK_DIV_1152
Definition CodecConstants.h:53
@ LCLK_DIV_256
Definition CodecConstants.h:47
@ LCLK_DIV_128
Definition CodecConstants.h:45
@ LCLK_DIV_2304
Definition CodecConstants.h:57
@ LCLK_DIV_136
Definition CodecConstants.h:60
@ LCLK_DIV_192
Definition CodecConstants.h:46
@ LCLK_DIV_768
Definition CodecConstants.h:51
@ LCLK_DIV_250
Definition CodecConstants.h:61
@ LCLK_DIV_384
Definition CodecConstants.h:48
@ LCLK_DIV_1000
Definition CodecConstants.h:67
@ LCLK_DIV_1024
Definition CodecConstants.h:52
@ LCLK_DIV_2112
Definition CodecConstants.h:56
@ LCLK_DIV_576
Definition CodecConstants.h:50
@ LCLK_DIV_544
Definition CodecConstants.h:65
@ LCLK_DIV_1088
Definition CodecConstants.h:68
@ LCLK_DIV_500
Definition CodecConstants.h:64
@ LCLK_DIV_125
Definition CodecConstants.h:59
#define RESULT_OK
Definition DriverCommon.h:74
#define RESULT_FAIL
Definition DriverCommon.h:75
#define AD_LOGD(...)
Definition Logger.h:12
#define AD_LOGI(...)
Definition Logger.h:13
#define AD_LOGW(...)
Definition Logger.h:14
Header-only driver class for the ES8374 codec chip.
Definition ES8374.h:51
error_t writeReg(uint8_t reg_add, uint8_t data)
Definition ES8374.h:214
static constexpr int ES8374_ADDR
Definition ES8374.h:53
error_t ctrlStateActive(codec_mode_t mode, bool ctrl_state_active)
Control ES8374 codec chip.
Definition ES8374.h:149
error_t setBitsPerSample(codec_mode_t mode, sample_bits_t bit_per_sample)
Configure ES8374 data sample bits.
Definition ES8374.h:235
error_t init(codec_config_t *cfg, codec_mode_t codec_mode)
Initialize ES8374 codec chip.
Definition ES8374.h:117
error_t setD2sePga(es_d2se_pga_t gain)
Definition ES8374.h:678
void setAddress(int addr)
Defines the I2C device address.
Definition ES8374.h:60
error_t deinit(void)
Deinitialize ES8374 codec chip.
Definition ES8374.h:143
error_t setVoiceMute(bool enable)
Mute or unmute ES8374 DAC.
Definition ES8374.h:74
error_t start(codec_mode_t mode)
Start ES8374 codec chip.
Definition ES8374.h:287
error_t initReg(codec_mode_t ms_mode, i2s_format_t fmt, es_i2s_clock_t cfg)
Definition ES8374.h:695
int i2c_addr
Definition ES8374.h:772
error_t configI2S(codec_mode_t mode, I2SDefinition *iface)
Configure ES8374 codec mode and I2S interface.
Definition ES8374.h:101
error_t setAdcDacVolume(int mode, int volume, int dot)
Definition ES8374.h:655
void setWire(i2c_bus_handle_t handle)
Defines the I2C bus instance to be used.
Definition ES8374.h:57
error_t configFmt(codec_mode_t mode, i2s_format_t fmt)
Configure ES8374 I2S format.
Definition ES8374.h:259
error_t configInputDevice()
Set ES8374 ADC input mode.
Definition ES8374.h:626
void readAll()
Print all ES8374 registers.
Definition ES8374.h:65
i2c_bus_handle_t i2c_handle
Definition ES8374.h:771
error_t getVoiceMute(void)
Get ES8374 DAC mute status.
Definition ES8374.h:88
error_t setVoiceVolume(int volume)
Set voice volume (0~100)
Definition ES8374.h:180
error_t stop(codec_mode_t mode)
Stop ES8374 codec chip.
Definition ES8374.h:341
error_t setMicGain(es_mic_gain_t gain)
Set ES8374 mic gain.
Definition ES8374.h:640
error_t getVoiceVolume(int *volume)
Get voice volume (0~100)
Definition ES8374.h:197
bool initialized
Definition ES8374.h:773
int readReg(uint8_t reg_add, uint8_t *regv)
Definition ES8374.h:219
error_t configOutputDevice()
Set ES8374 DAC output mode.
Definition ES8374.h:608
error_t i2sConfigClock(es_i2s_clock_t cfg)
Configure I2S clock in MASTER mode.
Definition ES8374.h:385
Namespace for audio driver components.
Definition AudioBoard.h:5
es_d2se_pga_t
Definition ES8374.h:39
@ ES8374_PGA_GAIN_MAX
Definition ES8374.h:43
@ ES8374_PGA_GAIN_DIS
Definition ES8374.h:41
@ ES8374_PGA_GAIN_EN
Definition ES8374.h:42
@ ES8374_PGA_GAIN_MIN
Definition ES8374.h:40
sample_bits_t
Definition DriverCommon.h:235
@ BIT_LENGTH_24BITS
Definition DriverCommon.h:240
@ BIT_LENGTH_32BITS
Definition DriverCommon.h:241
@ BIT_LENGTH_16BITS
Definition DriverCommon.h:237
codec_mode_t
Definition DriverCommon.h:290
@ CODEC_MODE_ENCODE
Definition DriverCommon.h:293
@ CODEC_MODE_BOTH
Definition DriverCommon.h:295
@ CODEC_MODE_DECODE
Definition DriverCommon.h:294
@ CODEC_MODE_LINE_IN
Definition DriverCommon.h:296
int error_t
Definition DriverCommon.h:150
es_mic_gain_t
Definition DriverCommon.h:305
@ MIC_GAIN_15DB
Definition DriverCommon.h:312
@ MIC_GAIN_MIN
Definition DriverCommon.h:306
@ MIC_GAIN_24DB
Definition DriverCommon.h:315
i2s_format_t
Definition DriverCommon.h:250
void * i2c_bus_handle_t
Definition DriverCommon.h:155
I2S interface parameters for the audio codec.
Definition DriverCommon.h:384
i2s_format_t fmt
Definition DriverCommon.h:386
i2s_master_slave_t mode
Definition DriverCommon.h:385
sample_bits_t bits
Definition DriverCommon.h:388
Top-level codec configuration passed to the audio driver at init.
Definition DriverCommon.h:397
I2SDefinition i2s
Definition DriverCommon.h:400
i2s clock configuration structure for es8388 and es8374
Definition CodecConstants.h:78
es_lclk_div_t lclk_div
Definition CodecConstants.h:80
es_sclk_div_t sclk_div
Definition CodecConstants.h:79