-
Notifications
You must be signed in to change notification settings - Fork 2
/
es8374.cpp
656 lines (579 loc) · 19.8 KB
/
es8374.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* author : Shuichi TAKANO
* since : Sun Jan 24 2021 23:56:09
*/
#include "es8374.h"
#include <stdint.h>
#include <i2c.h>
#include <algorithm>
#include <stdio.h>
namespace
{
struct Clock
{
enum LCLKDiv
{
LCLK_DIV_MIN = -1,
LCLK_DIV_128 = 0,
LCLK_DIV_192 = 1,
LCLK_DIV_256 = 2,
LCLK_DIV_384 = 3,
LCLK_DIV_512 = 4,
LCLK_DIV_576 = 5,
LCLK_DIV_768 = 6,
LCLK_DIV_1024 = 7,
LCLK_DIV_1152 = 8,
LCLK_DIV_1408 = 9,
LCLK_DIV_1536 = 100,
LCLK_DIV_2112 = 101,
LCLK_DIV_2304 = 102,
LCLK_DIV_125 = 16,
LCLK_DIV_136 = 17,
LCLK_DIV_250 = 18,
LCLK_DIV_272 = 19,
LCLK_DIV_375 = 20,
LCLK_DIV_500 = 21,
LCLK_DIV_544 = 22,
LCLK_DIV_750 = 23,
LCLK_DIV_1000 = 24,
LCLK_DIV_1088 = 25,
LCLK_DIV_1496 = 26,
LCLK_DIV_1500 = 27,
};
enum MCLKDiv
{
MCLK_DIV_MIN = -1,
MCLK_DIV_1 = 1,
MCLK_DIV_2 = 2,
MCLK_DIV_3 = 3,
MCLK_DIV_4 = 4,
MCLK_DIV_6 = 5,
MCLK_DIV_8 = 6,
MCLK_DIV_9 = 7,
MCLK_DIV_11 = 8,
MCLK_DIV_12 = 9,
MCLK_DIV_16 = 10,
MCLK_DIV_18 = 11,
MCLK_DIV_22 = 12,
MCLK_DIV_24 = 13,
MCLK_DIV_33 = 14,
MCLK_DIV_36 = 15,
MCLK_DIV_44 = 16,
MCLK_DIV_48 = 17,
MCLK_DIV_66 = 18,
MCLK_DIV_72 = 19,
MCLK_DIV_5 = 20,
MCLK_DIV_10 = 21,
MCLK_DIV_15 = 22,
MCLK_DIV_17 = 23,
MCLK_DIV_20 = 24,
MCLK_DIV_25 = 25,
MCLK_DIV_30 = 26,
MCLK_DIV_32 = 27,
MCLK_DIV_34 = 28,
MCLK_DIV_7 = 29,
MCLK_DIV_13 = 30,
MCLK_DIV_14 = 31,
};
LCLKDiv lclkDiv = LCLK_DIV_256;
MCLKDiv mclkDiv = MCLK_DIV_4;
};
struct Config
{
enum ADCMode
{
ES8374_ADC_INPUT_LINE1 = 0,
ES8374_ADC_INPUT_LINE2 = 1,
ES8374_ADC_INPUT_ALL = 2,
ES8374_ADC_INPUT_DIFFERENCE = 3,
};
enum DACMode
{
ES8374_DAC_OUTPUT_LINE1 = 0,
ES8374_DAC_OUTPUT_LINE2 = 1,
ES8374_DAC_OUTPUT_ALL = 2,
};
enum Mode
{
ES8374_MODE_ENCODE = 1,
ES8374_MODE_DECODE = 2,
ES8374_MODE_BOTH = 3,
ES8374_MODE_LINE_IN = 4,
};
struct Interface
{
enum Mode
{
ES8374_MODE_SLAVE = 0,
ES8374_MODE_MASTER = 1,
};
enum Format
{
ES8374_I2S_NORMAL = 0,
ES8374_I2S_LEFT = 1,
ES8374_I2S_RIGHT = 2,
ES8374_I2S_DSP = 3,
};
enum Samples
{
ES8374_08K_SAMPLES = 0,
ES8374_11K_SAMPLES = 1,
ES8374_16K_SAMPLES = 2,
ES8374_22K_SAMPLES = 3,
ES8374_24K_SAMPLES = 4,
ES8374_32K_SAMPLES = 5,
ES8374_44K_SAMPLES = 6,
ES8374_48K_SAMPLES = 7,
};
enum Bits
{
ES8374_BIT_LENGTH_16BITS = 1,
ES8374_BIT_LENGTH_24BITS = 2,
ES8374_BIT_LENGTH_32BITS = 3,
};
Mode mode = ES8374_MODE_SLAVE;
Format format = ES8374_I2S_NORMAL;
Samples samples = ES8374_44K_SAMPLES;
Bits bits = ES8374_BIT_LENGTH_16BITS;
};
ADCMode adcMode = ES8374_ADC_INPUT_LINE1;
DACMode dacMode = ES8374_DAC_OUTPUT_LINE1;
Mode mode = ES8374_MODE_BOTH;
Interface interface;
public:
Config() {}
};
enum BitLength
{
BIT_LENGTH_MIN = -1,
BIT_LENGTH_16BITS = 0x03,
BIT_LENGTH_18BITS = 0x02,
BIT_LENGTH_20BITS = 0x01,
BIT_LENGTH_24BITS = 0x00,
BIT_LENGTH_32BITS = 0x04,
};
enum
{
MIC_GAIN_MIN = -1,
MIC_GAIN_0DB = 0,
MIC_GAIN_3DB = 3,
MIC_GAIN_6DB = 6,
MIC_GAIN_9DB = 9,
MIC_GAIN_12DB = 12,
MIC_GAIN_15DB = 15,
MIC_GAIN_18DB = 18,
MIC_GAIN_21DB = 21,
MIC_GAIN_24DB = 24,
};
enum
{
D2SE_PGA_GAIN_MIN = -1,
D2SE_PGA_GAIN_DIS = 0,
D2SE_PGA_GAIN_EN = 1,
D2SE_PGA_GAIN_MAX = 2,
};
enum
{
ES8374_CTRL_STOP = 0x00,
ES8374_CTRL_START = 0x01,
};
struct Manager
{
static constexpr int I2CAddr = 0x10;
Clock clock_;
Config config_;
public:
void init()
{
stop(config_.mode);
init_reg(config_.interface.mode,
config_.interface.format,
BIT_LENGTH_16BITS,
clock_,
config_.dacMode,
config_.adcMode);
setMICGain(MIC_GAIN_18DB);
setD2sePga(D2SE_PGA_GAIN_EN);
codecConfigI2S(config_.interface.format,
config_.interface.bits);
}
int readReg(uint8_t regAddr)
{
i2c_init(I2C_DEVICE_0, I2CAddr, 7, 100000);
uint8_t buf[] = {regAddr};
uint8_t rbuf[1] = {};
i2c_recv_data(I2C_DEVICE_0, buf, sizeof(buf),
rbuf, 1);
// printf("readReg(%02x: %02x)\n", regAddr, rbuf[0]);
return rbuf[0];
}
void writeReg(uint8_t regAddr, uint8_t data)
{
// printf("writeReg(%02x: %02x)\n", regAddr, data);
i2c_init(I2C_DEVICE_0, I2CAddr, 7, 100000);
uint8_t buf[] = {regAddr, data};
i2c_send_data(I2C_DEVICE_0, buf, sizeof(buf));
}
void start(Config::Mode mode)
{
if (mode == Config::Mode::ES8374_MODE_LINE_IN)
{
auto reg = readReg(0x1a); // set monomixer
reg |= 0x60;
reg |= 0x20;
reg &= 0xf7;
writeReg(0x1a, reg);
reg = readReg(0x1c); // set spk mixer
reg |= 0x40;
writeReg(0x1c, reg);
writeReg(0x1D, 0x02); // spk set
writeReg(0x1F, 0x00); // spk set
writeReg(0x1E, 0xA0); // spk on
}
if (mode == Config::Mode::ES8374_MODE_ENCODE || mode == Config::Mode::ES8374_MODE_BOTH || mode == Config::Mode::ES8374_MODE_LINE_IN)
{
auto reg = readReg(0x21); // power up adc and input
reg &= 0x3f;
writeReg(0x21, reg);
reg = readReg(0x10); // power up adc and input
reg &= 0x3f;
writeReg(0x10, reg);
}
if (mode == Config::Mode::ES8374_MODE_DECODE || mode == Config::Mode::ES8374_MODE_BOTH || mode == Config::Mode::ES8374_MODE_LINE_IN)
{
auto reg = readReg(0x1a); // disable lout
reg |= 0x08;
writeReg(0x1a, reg);
reg &= 0xdf;
writeReg(0x1a, reg);
writeReg(0x1D, 0x12); // mute speaker
writeReg(0x1E, 0x20); // disable class d
reg = readReg(0x15); // power up dac
reg &= 0xdf;
writeReg(0x15, reg);
reg = readReg(0x1a); // disable lout
reg |= 0x20;
writeReg(0x1a, reg);
reg &= 0xf7;
writeReg(0x1a, reg);
writeReg(0x1D, 0x02); // mute speaker
writeReg(0x1E, 0xa0); // disable class d
setVoiceMute(false);
}
}
void stop(Config::Mode mode)
{
if (mode == Config::Mode::ES8374_MODE_LINE_IN)
{
auto reg = readReg(0x1a); // disable lout
reg |= 0x08;
writeReg(0x1a, reg);
reg &= 0x9f;
writeReg(0x1a, reg);
writeReg(0x1D, 0x12); // mute speaker
writeReg(0x1E, 0x20); // disable class d
reg = readReg(0x1c); // disable spkmixer
reg &= 0xbf;
writeReg(0x1c, reg);
writeReg(0x1F, 0x00); // spk set
}
if (mode == Config::Mode::ES8374_MODE_DECODE ||
mode == Config::Mode::ES8374_MODE_BOTH)
{
setVoiceMute(true);
auto reg = readReg(0x1a); // disable lout
reg |= 0x08;
writeReg(0x1a, reg);
reg &= 0xdf;
writeReg(0x1a, reg);
writeReg(0x1D, 0x12); // mute speaker
writeReg(0x1E, 0x20); // disable class d
reg = readReg(0x15); // power up dac
reg |= 0x20;
writeReg(0x15, reg);
}
if (mode == Config::Mode::ES8374_MODE_ENCODE ||
mode == Config::Mode::ES8374_MODE_BOTH)
{
auto reg = readReg(0x10); // power up adc and input
reg |= 0xc0;
writeReg(0x10, reg);
reg = readReg(0x21); // power up adc and input
reg |= 0xc0;
writeReg(0x21, reg);
}
}
void setVoiceMute(bool enable)
{
auto reg = readReg(0x36);
reg &= 0xdf;
reg = (reg | (enable << 5));
writeReg(0x36, reg);
}
void init_reg(Config::Interface::Mode ms_mode,
Config::Interface::Format fmt,
BitLength bitLength,
const Clock &cfg,
Config::DACMode out_channel,
Config::ADCMode in_channel)
{
writeReg(0x00, 0x3F); // IC Rst start
writeReg(0x00, 0x03); // IC Rst stop
writeReg(0x01, 0x7F); // IC clk on // M ORG 7F
auto reg = readReg(0x0F);
reg &= 0x7f;
reg |= (ms_mode << 7);
writeReg(0x0f, reg); // CODEC IN I2S SLAVE MODE
writeReg(0x6F, 0xA0); // pll set:mode enable
writeReg(0x72, 0x41); // pll set:mode set
// writeReg(0x09,0x01)// pll set:reset on ,set start
// writeReg(0x0C,0x22)// pll set:k
// writeReg(0x0D,0x2E)// pll set:k
// writeReg(0x0E,0xC6)// pll set:k
// writeReg(0x0A,0x3A)// pll set:
// writeReg(0x0B,0x07)// pll set:n
// writeReg(0x09,0x41)// pll set:reset off ,set stop
writeReg(0x09, 0x01); // pll set:reset on ,set start
writeReg(0x0C, 0x08); // pll set:k
writeReg(0x0D, 0x13); // pll set:k
writeReg(0x0E, 0xE0); // pll set:k
writeReg(0x0A, 0x8A); // pll set:
writeReg(0x0B, 0x08); // pll set:n
writeReg(0x09, 0x41); // pll set:reset off ,set stop
i2sConfigClock(cfg);
writeReg(0x24, 0x08); // adc set
writeReg(0x36, 0x00); // dac set
writeReg(0x12, 0x30); // timming set
writeReg(0x13, 0x20); // timming set
configI2SADCFormat(fmt);
setADCBitsPerSample(bitLength);
configI2SDACFormat(fmt);
setDACBitsPerSample(bitLength);
writeReg(0x21, 0x50); // adc set: SEL LIN1 CH+PGAGAIN=0DB
writeReg(0x22, 0xFF); // adc set: PGA GAIN=0DB
writeReg(0x21, 0x14); // adc set: SEL LIN1 CH+PGAGAIN=18DB
writeReg(0x22, 0x55); // pga = +15db
writeReg(0x08, 0x21); // set class d divider = 33, to avoid the high frequency tone on laudspeaker
writeReg(0x00, 0x80); // IC START
setADCVolume(0, 0); // 0dB
setDACVolume(0, 0); // 0dB
writeReg(0x14, 0x8A); // IC START
writeReg(0x15, 0x40); // IC START
writeReg(0x1A, 0xA0); // monoout set
writeReg(0x1B, 0x19); // monoout set
writeReg(0x1C, 0x90); // spk set
writeReg(0x1D, 0x01); // spk set
writeReg(0x1F, 0x00); // spk set
writeReg(0x1E, 0x20); // spk on
writeReg(0x28, 0x70); // alc set 0x70
writeReg(0x26, 0x4E); // alc set
writeReg(0x27, 0x10); // alc set
writeReg(0x29, 0x00); // alc set
writeReg(0x2B, 0x00); // alc set
writeReg(0x25, 0x00); // ADCVOLUME on
writeReg(0x38, 0x00); // DACVOLUME on
writeReg(0x37, 0x30); // dac set
writeReg(0x6D, 0x60); // SEL:GPIO1=DMIC CLK OUT+SEL:GPIO2=PLL CLK OUT
writeReg(0x71, 0x05); // for automute setting
writeReg(0x73, 0x70);
configDACOutput(out_channel); // 0x3c Enable DAC and Enable Lout/Rout/1/2
configADCInput(in_channel); // 0x00 LINSEL & RINSEL, LIN1/RIN1 as ADC Input DSSEL,use one DS Reg11 DSR, LINPUT1-RINPUT1
setVoiceVolume(0);
writeReg(0x37, 0x00); // dac set
}
void i2sConfigClock(const Clock &cfg)
{
auto reg = readReg(0x0F); // power up adc and input
reg &= 0xe0;
reg |= cfg.mclkDiv;
writeReg(0x0f, reg);
auto div = [&] {
switch (cfg.lclkDiv)
{
case Clock::LCLKDiv::LCLK_DIV_128:
return 128;
case Clock::LCLKDiv::LCLK_DIV_192:
return 192;
case Clock::LCLKDiv::LCLK_DIV_256:
return 256;
case Clock::LCLKDiv::LCLK_DIV_384:
return 384;
case Clock::LCLKDiv::LCLK_DIV_512:
return 512;
case Clock::LCLKDiv::LCLK_DIV_576:
return 576;
case Clock::LCLKDiv::LCLK_DIV_768:
return 768;
case Clock::LCLKDiv::LCLK_DIV_1024:
return 1024;
case Clock::LCLKDiv::LCLK_DIV_1152:
return 1152;
case Clock::LCLKDiv::LCLK_DIV_1408:
return 1408;
case Clock::LCLKDiv::LCLK_DIV_1536:
return 1536;
case Clock::LCLKDiv::LCLK_DIV_2112:
return 2112;
case Clock::LCLKDiv::LCLK_DIV_2304:
return 2304;
case Clock::LCLKDiv::LCLK_DIV_125:
return 125;
case Clock::LCLKDiv::LCLK_DIV_136:
return 136;
case Clock::LCLKDiv::LCLK_DIV_250:
return 250;
case Clock::LCLKDiv::LCLK_DIV_272:
return 272;
case Clock::LCLKDiv::LCLK_DIV_375:
return 375;
case Clock::LCLKDiv::LCLK_DIV_500:
return 500;
case Clock::LCLKDiv::LCLK_DIV_544:
return 544;
case Clock::LCLKDiv::LCLK_DIV_750:
return 750;
case Clock::LCLKDiv::LCLK_DIV_1000:
return 1000;
case Clock::LCLKDiv::LCLK_DIV_1088:
return 1088;
case Clock::LCLKDiv::LCLK_DIV_1496:
return 1496;
case Clock::LCLKDiv::LCLK_DIV_1500:
return 1500;
default:
return 1;
}
}();
printf("div = %d, %d\n", div, cfg.lclkDiv);
auto dacratio_h = div >> 8;
auto dacratio_l = div & 255;
writeReg(0x06, dacratio_h); // ADCFsMode,singel SPEED,RATIO=256
writeReg(0x07, dacratio_l); // ADCFsMode,singel SPEED,RATIO=256
}
void configI2SADCFormat(Config::Interface::Format fmt)
{
auto reg = readReg(0x10);
reg &= 0xfc;
writeReg(0x10, (reg | fmt));
}
void configI2SDACFormat(Config::Interface::Format fmt)
{
auto reg = readReg(0x11);
reg &= 0xfc;
writeReg(0x11, (reg | fmt));
}
void setADCBitsPerSample(BitLength bit_per_sample)
{
auto bits = bit_per_sample & 0x0f;
auto reg = readReg(0x10);
reg &= 0xe3;
writeReg(0x10, (reg | (bits << 2)));
}
void setDACBitsPerSample(BitLength bit_per_sample)
{
auto bits = bit_per_sample & 0x0f;
auto reg = readReg(0x11);
reg &= 0xe3;
writeReg(0x11, (reg | (bits << 2)));
}
int makeVolumeVal(int volume, int dot)
{
return (std::min(96, std::max(0, -volume)) << 1) + (dot >= 5 ? 1 : 0);
}
void setADCVolume(int volume, int dot)
{
writeReg(0x25, makeVolumeVal(volume, dot));
}
void setDACVolume(int volume, int dot)
{
writeReg(0x38, makeVolumeVal(volume, dot));
}
void configDACOutput(Config::DACMode output)
{
writeReg(0x1d, 0x02);
auto reg = readReg(0x1c); // set spk mixer
reg |= 0x80;
writeReg(0x1c, reg);
writeReg(0x1D, 0x02); // spk set
writeReg(0x1F, 0x00); // spk set
writeReg(0x1E, 0xA0); // spk on
}
void configADCInput(Config::ADCMode input)
{
auto reg = readReg(0x21);
reg = (reg & 0xcf) | 0x14;
writeReg(0x21, reg);
writeReg(0x21, 0x24);
}
void codecConfigI2S(Config::Interface::Format interface_fmt,
Config::Interface::Bits interface_bits)
{
configI2SADCFormat(interface_fmt);
auto bl = [&] {
switch (interface_bits)
{
default:
case Config::Interface::ES8374_BIT_LENGTH_16BITS:
return BIT_LENGTH_16BITS;
case Config::Interface::ES8374_BIT_LENGTH_24BITS:
return BIT_LENGTH_24BITS;
case Config::Interface::ES8374_BIT_LENGTH_32BITS:
return BIT_LENGTH_32BITS;
}
}();
setADCBitsPerSample(bl);
setDACBitsPerSample(bl);
}
void setMICGain(int gain)
{
if ((gain > MIC_GAIN_MIN) && (gain < MIC_GAIN_24DB))
{
auto gain_n = int(gain / 3);
auto reg = (gain_n | (gain_n << 4));
writeReg(0x22, reg); // MIC PGA
}
}
void setD2sePga(int gain)
{
if ((gain > D2SE_PGA_GAIN_MIN) && (gain < D2SE_PGA_GAIN_MAX))
{
auto reg = readReg(0x21);
reg &= 0xfb;
reg |= gain << 2;
writeReg(0x21, reg); // MIC PGA
}
}
void setVoiceVolume(int volume)
{
if (volume < 0)
{
volume = 192;
}
else if (volume > 96)
{
volume = 0;
}
else
{
volume = 192 - volume * 2;
}
writeReg(0x38, volume);
}
static Manager &instance()
{
static Manager inst;
return inst;
}
};
} // namespace
void initES8374()
{
auto &m = Manager::instance();
printf("initES8374\n");
m.init();
m.start(Config::Mode::ES8374_MODE_DECODE);
//m.start(Config::Mode::ES8374_MODE_BOTH);
// m.setDACVolume(0, 0);
m.setVoiceVolume(96);
// m.writeReg(0x1e, 0xa4);
}