-
Notifications
You must be signed in to change notification settings - Fork 1
/
AK4619VN.cpp
563 lines (431 loc) · 12.8 KB
/
AK4619VN.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
#include "AK4619VN.h"
#ifndef ESP_ARDUINO_VERSION_MAJOR
#warning This library is tested only with the Arduino ESP32 platform, version 1.x.x and 2.x.x
#endif
AK4619VN::AK4619VN(TwoWire *i2c, uint8_t i2cAddress) {
if(i2c == NULL){
while(1);
}
m_i2c = i2c;
m_addr = i2cAddress;
}
void AK4619VN::begin(uint8_t SDA, uint8_t SCL ) {
m_i2c->begin(SDA, SCL);
}
void AK4619VN::begin(void){
m_i2c->begin();
}
//Set reset state
/*
* Use to set CODEC in reset state.
* true = reset state
* false = normal operation
* CODEC should be configured in reset state,
* release it after configuration
*/
uint8_t AK4619VN::setRstState(bool state){
uint8_t regval = 0;
uint8_t error = 0;
error = readReg(PWRMGM, ®val);
if(error){
return error;
}
regval |= !state;
return (writeReg(PWRMGM, regval));
}
//Power MGM of codec DAC&ADC
uint8_t AK4619VN::pwrMgm(bool ADC2, bool ADC1, bool DAC2, bool DAC1){
uint8_t regval = 0;
uint8_t rstState = 0;
uint8_t error = 0;
regval = (ADC2 << 5 | ADC1 << 4 | DAC2 << 2 | DAC1 << 1);
//Check for RESET state of CODEC
error = readReg(PWRMGM, &rstState);
if(error){
return error;
}
// if( !((rstState >> 0) & 1) ){
// Serial.println("Codec is in reset state");
// }
return (writeReg(PWRMGM, regval));
}
//Set audio format, I&O data length and MCLK and sample freq
/*
* slot = 0 - LRCK edge | 1 - Slot length
* IDL:
* 00 - 24bit
* 01 - 20bit
* 10 - 16bit
* 11 - 32bit
*
* ODL:
* Samle as IDL except
* 11 - N/A
*
* FS:
* bits MCLK fs range
* 000 - 256fs 8 kHz ≦ fs ≦ 48 kHz
* 001 - 256fs fs = 96 kHz
* 010 - 384fs 8 kHz ≦ fs ≦ 48 kHz
* 011 - 512fs 8 kHz ≦ fs ≦ 48 kHz
* 1xx - 128fs fs = 192 kHz
* ** For all FS BICK in range 32, 48, 64, 128, 256fs
* ** except 1xx which exclude 256fs
*/
uint8_t AK4619VN::audioFormatSlotLen(data_bit_length_t IDL, data_bit_length_t ODL){
uint8_t regval = 0;
uint8_t error = 0;
error = readReg(AUDFORM2, ®val);
if(error){
return error;
}
uint8_t tempval = (IDL << 2 | ODL);
regval &= 0xF0;
regval |= tempval;
return (writeReg(AUDFORM2, regval));
}
//Set audio format mode
uint8_t AK4619VN::audioFormatMode(audio_iface_format_t FORMAT){
uint8_t regval0 = 0;
uint8_t regval1 = 0;
uint8_t error = 0;
error = readReg(AUDFORM1, ®val0);
if(error){
return error;
}
error = readReg(AUDFORM2, ®val1);
if(error){
return error;
}
//Clear upper 6 bits, shift FORMAT by 2 and set it
regval0 &= 0x03;
int temp = FORMAT << 2;
regval0 |= temp;
regval1 &= ~(0x08); //Mask 4th bit and NOT it
int tempval = (FORMAT & 0x01); // Set tempval to 1st bit of FORMAT
regval1 |= (tempval << 3); // Set 4th bit to tempval
error = writeReg(AUDFORM1, regval0);
if(error){
return error;
}
return (writeReg(AUDFORM2, regval1));
}
uint8_t AK4619VN::sysClkSet(clk_fs_t FS, bool BICKEdg, bool SDOPH){
uint8_t regval = 0;
uint8_t error = 0;
error = writeReg(SYSCLKSET, FS);
if(error){
return error;
}
uint8_t tempval = (BICKEdg << 1 | SDOPH);
error = readReg(AUDFORM1, ®val);
if(error){
return error;
}
regval &= 0xFC;
regval |= (tempval & 0x03);
return (writeReg(AUDFORM1, regval));
}
uint8_t AK4619VN::inputGain(input_gain_t ADC1L, input_gain_t ADC1R, input_gain_t ADC2L, input_gain_t ADC2R){
uint8_t regval0 = 0;
uint8_t regval1 = 0;
uint8_t error = 0;
regval0 = (ADC1L << 4 | ADC1R);
regval1 = (ADC2L << 4 | ADC2R);
error = writeReg(MICGAIN1, regval0);
if(error){
return error;
}
return (writeReg(MICGAIN2, regval1));
}
uint8_t AK4619VN::inputGainChange(bool relative, bool ADC1L, bool ADC1R, bool ADC2L, bool ADC2R, int8_t gain){
uint8_t regvals[2] = {0};
int8_t gains[4] ={0};
uint8_t error = 0;
if(relative){
error = readRegMulti(MICGAIN1, 2, regvals); //Read input gain regvals
if(error){
return error;
}
//Get current gains in regs
gains[0] = (regvals[0] >> 4) & 0x0F; // Shift right by 4 bits and mask with 0x0F
gains[1] = regvals[0] & 0x0F; // Mask with 0x0F to get the lower 4 bits
gains[2] = (regvals[1] >> 4) & 0x0F;
gains[3] = regvals[1] & 0x0F;
if(ADC1L) gains[0] += gain;
if(ADC1R) gains[1] += gain;
if(ADC2L) gains[2] += gain;
if(ADC2R) gains[3] += gain;
//Check for min/max
for(uint8_t i=0; i<=3;i++){
if (gains[i] < 0)
gains[i] = 0;
else if (gains[i] > 11)
gains[i] = 11;
}
regvals[0] = ((uint8_t)gains[0] << 4 | (uint8_t)gains[1]);
regvals[1] = ((uint8_t)gains[2] << 4 | (uint8_t)gains[3]);
error = writeReg(MICGAIN1, regvals[0]);
if(error){
return error;
}
return (writeReg(MICGAIN2, regvals[1]));
}
else{
if (gain < 0)
gain = 0;
else if (gain > 11)
gain = 11;
if(ADC1L) gains[0] = gain;
if(ADC1R) gains[1] = gain;
if(ADC2L) gains[2] = gain;
if(ADC2R) gains[3] = gain;
regvals[0] = (gains[0] << 4 | gains[1]);
regvals[1] = (gains[2] << 4 | gains[3]);
error = writeReg(MICGAIN1, regvals[0]);
if(error){
return error;
}
return (writeReg(MICGAIN2, regvals[1]));
}
return 0;
}
//Set DAC output gain
/*
* relative - change gain relatively by gainVal
* channel - DACxB - change LR
* DACxL/RL - change only one
* gainVal - 0x00 - MAX +24dB
* 0xFF - Muted
* Steps in 0.5dB
* 0dB = 0x30 - CODEC default
*/
uint8_t AK4619VN::outputGain(bool relative, output_gain_t channel, int16_t gainVal){
uint8_t regvals[4] = {0};
uint8_t error = 0;
if(relative){
error = readRegMulti(DAC1LVOL, 4, regvals); //Read output gain regvals
if(error){
return error;
}
switch(channel){
case AK4619VN::AK_DAC1B :
regvals[0] = modifyGainRange(gainVal, regvals[0]);
regvals[1] = modifyGainRange(gainVal, regvals[1]);
error = writeReg(DAC1LVOL, regvals[0]);
if(error){
return error;
}
error = writeReg(DAC1RVOL, regvals[1]);
if(error){
return error;
}
break;
case AK4619VN::AK_DAC2B :
regvals[2] = modifyGainRange(gainVal, regvals[2]);
regvals[3] = modifyGainRange(gainVal, regvals[3]);
error = writeReg(DAC2LVOL, regvals[2]);
if(error){
return error;
}
error = writeReg(DAC2RVOL, regvals[3]);
if(error){
return error;
}
break;
case AK4619VN::AK_DAC1L :
regvals[0] = modifyGainRange(gainVal, regvals[0]);
return (writeReg(DAC1LVOL, regvals[0]));
break;
case AK4619VN::AK_DAC1R :
regvals[1] = modifyGainRange(gainVal, regvals[1]);
return (writeReg(DAC1RVOL, regvals[1]));
break;
case AK4619VN::AK_DAC2L :
regvals[2] = modifyGainRange(gainVal, regvals[2]);
return (writeReg(DAC2LVOL, regvals[2]));
break;
case AK4619VN::AK_DAC2R :
regvals[3] = modifyGainRange(gainVal, regvals[3]);
return (writeReg(DAC2RVOL, regvals[3]));
break;
default:
break;
}
}
else{
switch(channel){
case AK4619VN::AK_DAC1B :
regvals[0] = checkGainRange(gainVal);
regvals[1] = checkGainRange(gainVal);
error = writeReg(DAC1LVOL, regvals[0]);
if(error){
Serial.print("DAC1LVOL Err: ");
Serial.println(error, DEC);
return error;
}
error = writeReg(DAC1RVOL, regvals[1]);
if(error){
Serial.print("DAC1RVOL Err: ");
Serial.println(error, DEC);
return error;
}
break;
case AK4619VN::AK_DAC2B :
regvals[2] = checkGainRange(gainVal);
regvals[3] = checkGainRange(gainVal);
error = writeReg(DAC2LVOL, regvals[2]);
if(error){
Serial.print("DAC2LVOL Err: ");
Serial.println(error, DEC);
return error;
}
error = writeReg(DAC2RVOL, regvals[3]);
if(error){
Serial.print("DAC2RVOL Err: ");
Serial.println(error, DEC);
return error;
}
break;
case AK4619VN::AK_DAC1L :
regvals[0] = checkGainRange(gainVal);
return (writeReg(DAC1LVOL, regvals[0]));
break;
case AK4619VN::AK_DAC1R :
regvals[1] = checkGainRange(gainVal);
return (writeReg(DAC1RVOL, regvals[1]));
break;
case AK4619VN::AK_DAC2L :
regvals[2] = checkGainRange(gainVal);
return (writeReg(DAC2LVOL, regvals[2]));
break;
case AK4619VN::AK_DAC2R :
regvals[3] = checkGainRange(gainVal);
return (writeReg(DAC2RVOL, regvals[3]));
break;
default:
break;
}
}
return 0;
}
//ADC input configuration
uint8_t AK4619VN::inputConf(intput_conf_t ADC1L, intput_conf_t ADC1R, intput_conf_t ADC2L, intput_conf_t ADC2R){
uint8_t regval = 0;
regval = ( ADC1L << 6 | ADC1R << 4 | ADC2L << 2 | ADC2R);
return (writeReg(ADCAIN, regval));
}
//DAC input configuration
uint8_t AK4619VN::outputConf(output_conf_t DAC2, output_conf_t DAC1){
uint8_t regval = 0;
regval = ( DAC2 << 2 | DAC1);
return (writeReg(DACDIN, regval));
}
uint8_t AK4619VN::printRegs(uint8_t startReg, uint8_t len){
uint8_t regvals[128] = {0};
uint8_t error = 0;
error = readRegMulti(startReg, len, regvals);
if(error){
return error;
}
//TODO remove for DBG only
for(int idx = 0; idx < len; idx++){
Serial.print(idx+startReg, HEX);
Serial.print(": \t");
for (int8_t i = 7; i >= 0; i--) {
uint8_t bit = (regvals[idx] >> i) & 1;
Serial.print(bit);
if (i % 4 == 0)
Serial.print(" ");
}
Serial.print("\t");
Serial.print(regvals[idx], HEX);
Serial.println(); // Prints a new line after printing the bits
}
return 0;
}
#if ESP_ARDUINO_VERSION_MAJOR == 1
uint8_t AK4619VN::writeReg(uint8_t deviceReg, uint8_t regVal) {
m_i2c->beginTransmission(m_addr);
m_i2c->write(deviceReg);
m_i2c->write(regVal);
m_i2c->endTransmission(true);
return (m_i2c->lastError());
}
uint8_t AK4619VN::readReg(uint8_t deviceReg, uint8_t * regVal) {
m_i2c->beginTransmission(m_addr);
m_i2c->write(deviceReg);
m_i2c->endTransmission(true);
m_i2c->beginTransmission(m_addr);
m_i2c->requestFrom((int8_t)m_addr, 1);
* regVal = m_i2c->read();
m_i2c->endTransmission(true);
return (m_i2c->lastError());
}
uint8_t AK4619VN::readRegMulti(uint8_t startReg, uint8_t len, uint8_t * vals) {
m_i2c->beginTransmission(m_addr);
m_i2c->write(startReg);
m_i2c->endTransmission(false);
m_i2c->requestFrom((int8_t)m_addr, (int8_t)len);
for (uint8_t i = 0; i <= (len - 1); i++) {
vals[i] = m_i2c->read();
}
return (m_i2c->lastError());
}
#endif
#if ESP_ARDUINO_VERSION_MAJOR == 2
uint8_t AK4619VN::writeReg(uint8_t deviceReg, uint8_t regVal) {
m_i2c->beginTransmission(m_addr);
m_i2c->write(deviceReg);
m_i2c->write(regVal);
return(m_i2c->endTransmission(true)); // Send STOP
}
uint8_t AK4619VN::readReg(uint8_t deviceReg, uint8_t * regVal) {
m_i2c->beginTransmission(m_addr);
m_i2c->write(deviceReg);
m_i2c->endTransmission(true);
uint8_t numbytes = 0;
numbytes = m_i2c->requestFrom(m_addr, (uint8_t)1, (uint8_t)false);
if((bool)numbytes){
Wire.readBytes(regVal, numbytes);
}
return(m_i2c->endTransmission(true)); //Send STOP
}
uint8_t AK4619VN::readRegMulti(uint8_t startReg, uint8_t len, uint8_t * vals) {
m_i2c->beginTransmission(m_addr);
m_i2c->write(startReg);
m_i2c->endTransmission(false);
uint8_t numbytes = 0;
numbytes = m_i2c->requestFrom((uint8_t)m_addr, len, (uint8_t)false);
if((bool)numbytes){
Wire.readBytes(vals, numbytes);
}
return(m_i2c->endTransmission(true)); //Send STOP
}
#endif
//Modify regVal by inVal, check for under/overflow and adjust regVal to min or max
uint8_t AK4619VN::modifyGainRange(int16_t inVal, uint8_t regVal){
if (inVal > 0 && (0xFF - regVal) < inVal) {
// Overflow occurred, return maximum value
return 0xFF;
} else if (inVal < 0 && regVal < (-inVal)) {
// Underflow occurred, return minimum value
return 0;
} else {
// No overflow or underflow, perform the update
return regVal + inVal;
}
}
//Check if inVal is in range of uint8_t
uint8_t AK4619VN::checkGainRange(int16_t inVal){
if (inVal > 0xFF) {
// Value exceeds the maximum of uint8_t
return 0xFF;
} else if (inVal < 0) {
// Value is negative, return minimum value of uint8_t
return 0;
} else {
// Value is within the range of uint8_t, return as is
return (uint8_t)inVal;
}
}