forked from ValdemarOrn/Teensy4i2s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_AK4619VN.cpp
787 lines (623 loc) · 19.5 KB
/
control_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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
#include <Arduino.h>
#include <Wire.h>
#include "control_AK4619VN.h"
void AK4619VN::init() {
// Enable the Audio codec
pinMode(22, OUTPUT); //PWN on
digitalWrite(22, HIGH);
delay(1000);
AK4619VN::begin(18, 19); // SDA, SCL for Teensy 4.1
/* pin 6 : SDIN
pin 7 : SDOUT
pin 23 : MCLK
pin 21 : BICK
pin 20 : SYNC */
//Set CODEC to reset state for initialization
uint8_t error = 0;
error = AK4619VN::setRstState(true);
if(error){
Serial.println("Unable to set codec reset state");
}
//Enable PWR of DAC1&2 and ADC1&2
error = AK4619VN::pwrMgm(true, true, true, true);
if(error){
Serial.println("Unable to set codec PWR");
}
// Set Slot start (LR (false) or Slot length), BICK Edge falling/rising, SDOut speed slow/fast
//error = AK4619VN::audioFormatMode(AK4619VN::AK_I2S_STEREO, false, false);
//error = AK4619VN::audioFormatMode(AK4619VN::AK_MSB_STEREO, false, false);
//error = AK4619VN::audioFormatMode(AK4619VN::AK_TDM256_I2S_32B, false, false);
error = AK4619VN::audioFormatMode(AK4619VN::AK_TDM128_I2S_32B, false, false);
//error = AK4619VN::audioFormatMode(AK4619VN::AK_TDM128_MSB_32B, true, false);
if(error){
Serial.println("Unable to set audio format mode.");
}
// Set TDM mode and Slot Length for DAC 1&2 and ADC1&2
error = AK4619VN::audioFormatSlotLen(AK4619VN::AK_SLOT, AK4619VN::AK_32BIT, AK4619VN::AK_24BIT);
if(error){
Serial.println("Unable to set slot length.");
}
// Set sample rate to 96kHz
//error = AK4619VN::sysClkSet(AK4619VN::AK_256FS_96KS);
//error = AK4619VN::sysClkSet(AK4619VN::AK_256FS_8_48KS);
error = AK4619VN::sysClkSet(AK4619VN::AK_128FS_192KS);
if(error){
Serial.println("Unable to set system clock mode.");
}
// Set all 4 ADC gains to 0dB
error = AK4619VN::micGain(AK4619VN::AK_MIC_GAIN_0DB, AK4619VN::AK_MIC_GAIN_0DB, AK4619VN::AK_MIC_GAIN_0DB, AK4619VN::AK_MIC_GAIN_0DB);
if(error){
Serial.println("Unable to set codec mic input gain.");
}
// // Set all 4 ADC digital volume to 0dB
// error = AK4619VN::inputGain(AK4619VN::AK_IN_GAIN_0DB, AK4619VN::AK_IN_GAIN_0DB, AK4619VN::AK_IN_GAIN_0DB, AK4619VN::AK_IN_GAIN_0DB);
// if(error){
// Serial.println("Unable to set codec digital input input gain.");
// }
//Set all DAC1 LR gains to 0dB
error = AK4619VN::outputGain(false, AK4619VN::AK_DAC1B, AK4619VN::AK_OUT_GAIN_0DB);
if(error){
Serial.println("Unable to set DAC1 gain.");
}
//Set all DAC2 LR gains to 0dB
error = AK4619VN::outputGain(false, AK4619VN::AK_DAC2B, AK4619VN::AK_OUT_GAIN_0DB);
if(error){
Serial.println("Unable to set DAC2 gain.");
}
//Set Input config to Single ended 1 on both ADCs
error = AK4619VN::inputConf(AK4619VN::AK_IN_SE1, AK4619VN::AK_IN_SE1, AK4619VN::AK_IN_SE1, AK4619VN::AK_IN_SE1);
if(error){
Serial.println("Unable to set DAC input configuration.");
}
//DAC2 to SDOUT2, DAC1 to SDOUT1
error = AK4619VN::outputConf(AK4619VN::AK_OUT_SDIN2, AK4619VN::AK_OUT_SDIN1);
//error = AK4619VN::outputConf(AK4619VN::AK_OUT_SDOUT1, AK4619VN::AK_OUT_SDOUT1);
if(error){
Serial.println("Unable to set DAC input configuration.");
}
//Release Reset state
error = AK4619VN::setRstState(false);
if(error){
Serial.println("Unable to clear codec reset state.");
}
delay(500);
Serial.println("Audio Codec Setup completed:");
delay(500);
//Verify settings
AK4619VN::printRegs(0x0, 21);
delay(1000);
}
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->setSDA(SDA);
m_i2c->setSCL(SCL);
m_i2c->begin();
}
void AK4619VN::begin(void){
m_i2c->begin();
}
#if not defined ESP_ARDUINO_VERSION_MAJOR
// Write single register
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
}
// Read single register
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
}
// Read multiple registers
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
//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: (Input Data length)
* 00 - 24bit
* 01 - 20bit
* 10 - 16bit
* 11 - 32bit
*
* ODL: (Output Data length)
* Same as IDL except
* 11 - N/A
*
* FS: (Frequency Sample)
* 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 only allows 128fs
*/
uint8_t AK4619VN::audioFormatSlotLen(slot_start_t SLOT, 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 = (SLOT << 4 | IDL << 2 | ODL);
regval &= 0xF0;
regval |= tempval;
error = readReg(AUDFORM2, ®val);
if(error){
return error;
}
//return (writeReg(AUDFORM2, 0b00011100));
return (writeReg(AUDFORM2, tempval));
}
//Set audio format mode
uint8_t AK4619VN::audioFormatMode(audio_iface_format_t FORMAT, bool BICK_RISING, bool SDOUT_FAST_MODE){
uint8_t regval = 0;
uint8_t error = 0;
error = readReg(AUDFORM1, ®val);
if(error){
return error;
}
//Clear upper 6 bits, shift FORMAT by 2 and set it
//regval &= 0x000000000;
regval = (FORMAT << 2 | BICK_RISING << 1 | SDOUT_FAST_MODE);
// 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, regval);
if(error){
return error;
}
return (writeReg(AUDFORM1, regval));
}
// 0000 0XXX SYSCLKSET 5 options
uint8_t AK4619VN::sysClkSet(clk_fs_t FS){
uint8_t regval = 0;
uint8_t error = 0;
regval = FS;
error = writeReg(SYSCLKSET, regval);
if(error){
return error;
}
error = readReg(AUDFORM1, ®val);
if(error){
return error;
}
return (writeReg(AUDFORM1, regval));
}
// Set timing digital vollume, ADC mutes and HPF filter
/* atspad - time interval digital volume 4/fs 21ms @ 48kHz (default= false) or 16/fs 128ms @ 48kHz (true)
* ad2mute - ADC2 Digital soft mute, default = false
* ad1mute - ADC1 Digital soft mute, default = false
* ad2hpfn - ADC2 DC offset cancel HPF, default enabled = false
* ad1hpfn - ADC1 DC offset cancel HPF, default enabled = false
*/
uint8_t AK4619VN::muteADCHPF(bool atspad, bool ad2mute, bool ad1mute, bool ad1hpfn, bool ad2hpfn){
uint8_t regval = 0;
uint8_t error = 0;
regval = (atspad << 7 | ad2mute << 6 | ad1mute << 5| ad1hpfn << 2 | ad2hpfn << 1);
error = writeReg(ADCMUTEHPF, regval);
if(error){
return error;
}
return (writeReg(ADCMUTEHPF, regval));
}
uint8_t AK4619VN::micGain(mic_gain_t MGN1L, mic_gain_t MGN1R, mic_gain_t MGN2L, mic_gain_t MGN2R){
uint8_t regval0 = 0;
uint8_t regval1 = 0;
uint8_t error = 0;
regval0 = (MGN1L << 4 | MGN1R);
regval1 = (MGN2L << 4 | MGN2R);
error = writeReg(MICGAIN1, regval0);
if(error){
return error;
}
return (writeReg(MICGAIN2, regval1));
}
uint8_t AK4619VN::inputGainChange(bool relative, bool MGN1L, bool MGN1R, bool MGN2L, bool MGN2R, 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(MGN1L) gains[0] += gain;
if(MGN1R) gains[1] += gain;
if(MGN2L) gains[2] += gain;
if(MGN2R) 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(MGN1L) gains[0] = gain;
if(MGN1R) gains[1] = gain;
if(MGN2L) gains[2] = gain;
if(MGN2R) 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));
}
// 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;
}
}
// Print Current set 8 bit Registers to Serial
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;
}
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("(");
Serial.print(regvals[idx], HEX);
Serial.print(")");
Serial.print("\t");
Serial.println(controlParams[idx]);
//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
/*
Name: readReg
Description:
Read a register from the AK4619VN::
Input Parameters:
reg - Register address.
Return:
Register value.
*/
// uint8_t AK4619VN::readReg(uint8_t reg) {
// int data;
// m_i2c->beginTransmission(m_addr);
// if (m_i2c->write(reg) == 0) {
// log_e("Error writing register address 0x%02x.", reg);
// return 0;
// }
// if (m_i2c->endTransmission(false) != 0) {
// log_e("Error ending transmission.");
// return 0;
// }
// if (!m_i2c->requestFrom(m_addr, (uint8_t)1)) {
// log_e("Error requesting data.");
// return 0;
// }
// if ((data = m_i2c->read()) < 0) {
// log_e("Error reading data.");
// return 0;
// }
// return (uint8_t)data;
// }
/*
Name: writeReg
Description:
Write a register to the ES8388 AK4619VN::
Input Parameters:
reg - Register address.
data - Data to write.
*/
// void AK4619VN::writeReg(uint8_t reg, uint8_t data) {
// m_i2c->beginTransmission(_addr);
// if (m_i2c->write(reg) == 0) {
// log_e("Error writing register address 0x%02x.", reg);
// return;
// }
// if (m_i2c->write(data) == 0) {
// log_e("Error writing data 0x%02x.", data);
// return;
// }
// if (m_i2c->endTransmission(true) != 0) {
// log_e("Error ending transmission.");
// return;
// }
// }
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