forked from Infineon/audio-codec-wm8960
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmtb_wm8960.cpp
903 lines (779 loc) · 30.6 KB
/
mtb_wm8960.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/******************************************************************************
* File Name: mtb_wm8960.c
*
* Description: This file contains the implementation of the WM8960 driver.
*
* Related Document: See README.md
*
*
*******************************************************************************
* $ Copyright 2021-YEAR Cypress Semiconductor $
*******************************************************************************/
#include "mtb_wm8960.h"
#define WM8960_TIMEOUT_MS (100u)
#define REGISTER_MAP_SIZE (56u)
typedef bool (* _mtb_wm8960_update_data_t)(mtb_wm8960_reg_t reg, uint16_t value);
typedef enum
{
_WM8960_SYSCLK_FREQ_12288000_HZ = 12288000,
_WM8960_SYSCLK_FREQ_11289600_HZ = 11289600
} _mtb_wm8960_sysclk_freq_t;
typedef struct
{
uint8_t features;
mtb_wm8960_reg_t reg;
uint16_t value;
} _mtb_wm8960_operation_t;
#ifdef ARDUINO
static TwoWire* i2c_ptr = nullptr;
#endif
static uint8_t enabled_features;
static bool pll_enabled = false;
static uint32_t write_retry_count = 1;
/* The WM8960 audio codec does not allow reading registers from the device so we
* store a cached copy with default of the register map in the driver and is updated
* on every write.
*/
static uint16_t wm8960_register_map[REGISTER_MAP_SIZE];
//--------------------------------------------------------------------------------------------------
// _mtb_wm8960_config_default
//--------------------------------------------------------------------------------------------------
static bool _mtb_wm8960_config_default(uint8_t features)
{
bool result;
uint16_t value;
/* Enable VREF and set VMID=50K */
value = (WM8960_PWR_MGMT1_VREF_UP | WM8960_PWR_MGMT1_VMIDSEL_50K);
if (features & WM8960_FEATURE_MICROPHONE1
|| features & WM8960_FEATURE_MICROPHONE2
|| features & WM8960_FEATURE_MICROPHONE3)
{
/* AINL, AINR, ADCL, ADCR and MICB */
value |= (WM8960_PWR_MGMT1_AINL_UP | WM8960_PWR_MGMT1_AINR_UP |
WM8960_PWR_MGMT1_ADCL_UP | WM8960_PWR_MGMT1_ADCR_UP |
WM8960_PWR_MGMT1_MICB_UP);
}
result = mtb_wm8960_write(WM8960_REG_PWR_MGMT1, value);
if (! result)
{
WM8960_LOG("Enable VREF and set VMID=50K");
return result;
}
/* Enable DACL, DACR, LOUT1 and ROUT1 */
if (features & WM8960_FEATURE_HEADPHONE || features & WM8960_FEATURE_SPEAKER)
{
value = 0;
if (features & WM8960_FEATURE_HEADPHONE){
value |= WM8960_PWR_MGMT2_DACL_UP | WM8960_PWR_MGMT2_DACR_UP | WM8960_PWR_MGMT2_LOUT1_UP | WM8960_PWR_MGMT2_ROUT1_UP;
}
if (features & WM8960_FEATURE_SPEAKER){
value |= WM8960_PWR_MGMT2_DACL_UP | WM8960_PWR_MGMT2_DACR_UP | WM8960_PWR_MGMT2_SPKL_UP | WM8960_PWR_MGMT2_SPKR_UP;
}
result = mtb_wm8960_write(WM8960_REG_PWR_MGMT2, value);
if (! result)
{
WM8960_LOG("Enable DACL, DACR, LOUT1 and ROUT1");
return result;
}
}
/* Enable DACL, DACR, LOUT2 and ROUT2 */
if (features & WM8960_FEATURE_SPEAKER)
{
// R49 Class D Control 011110111 - Enable Class D Speaker Outputs
result = mtb_wm8960_write(WM8960_REG_CLASS_D_CTL1, 0xF7);
if (! result)
{
WM8960_LOG("Enable Class D");
return result;
}
}
uint16_t pwr_mgmt3 = 0;
/* Enable left output mixer and right output mixer */
if (features & WM8960_FEATURE_HEADPHONE || features & WM8960_FEATURE_SPEAKER)
{
pwr_mgmt3 |= (WM8960_PWR_MGMT3_LOMIX_UP | WM8960_PWR_MGMT3_ROMIX_UP);
}
/* Enable left and right channel input PGA */
uint16_t mic_sig_path = WM8960_ADCL_ADCR_SIG_PTH_MIC2B_CON; ;
if (features & WM8960_FEATURE_MICROPHONE1
|| features & WM8960_FEATURE_MICROPHONE2
|| features & WM8960_FEATURE_MICROPHONE3)
{
pwr_mgmt3 |= (WM8960_PWR_MGMT3_RMIC_UP | WM8960_PWR_MGMT3_LMIC_UP);
// determine mic_sig_path
if (features & WM8960_FEATURE_MICROPHONE1){
mic_sig_path |= WM8960_ADCL_ADCR_SIG_PTH_MN1_CON;
}
if (features & WM8960_FEATURE_MICROPHONE2){
mic_sig_path |= WM8960_ADCL_ADCR_SIG_PTH_MP2_CON;
}
if (features & WM8960_FEATURE_MICROPHONE3){
mic_sig_path |= WM8960_ADCL_ADCR_SIG_PTH_MP3_CON;
}
}
result = mtb_wm8960_write(WM8960_REG_PWR_MGMT3, pwr_mgmt3);
if (! result)
{
WM8960_LOG("WM8960_REG_PWR_MGMT3");
return result;
}
static const _mtb_wm8960_operation_t operations[] =
{
/* LINPUT1 to PGA (LMN1), Connect left input PGA to left input boost (LMIC2B), Left PGA
Boost = 0dB */
{ .features = WM8960_FEATURE_MICROPHONES, .reg = WM8960_REG_ADCL_SIG_PTH,
.value = mic_sig_path },
/* RINPUT1 to PGA (RMN1), Connect right input PGA to right input boost (RMIC2B), Right PGA
Boost = 0dB */
{ .features = WM8960_FEATURE_MICROPHONES, .reg = WM8960_REG_ADCR_SIG_PTH,
.value = mic_sig_path },
/* Unmute left input PGA (LINMUTE), Left Input PGA Vol = 0dB, Volume Update */
{ .features = WM8960_FEATURE_MICROPHONES, .reg = WM8960_REG_LEFT_IN_VOL,
.value = (WM8960_LEFT_RIGHT_IN_VOL_IPVU | WM8960_LEFT_RIGHT_IN_VOL_INVOL_0dB) },
/* Unmute right input PGA (RINMUTE), Right Input PGA Vol = 0dB, Volume Update */
{ .features = WM8960_FEATURE_MICROPHONES, .reg = WM8960_REG_RIGHT_IN_VOL,
.value = (WM8960_LEFT_RIGHT_IN_VOL_IPVU | WM8960_LEFT_RIGHT_IN_VOL_INVOL_0dB) },
/* Left ADC Vol = 0dB, Volume Update */
{ .features = WM8960_FEATURE_MICROPHONES, .reg = WM8960_REG_LEFT_ADC_VOL,
.value = (WM8960_LEFT_RIGHT_ADC_VOL_ADCVU_UP | WM8960_LEFT_RIGHT_ADC_VOL_ADCVOL_0dB) },
/* Right ADC Vol = 0dB, Volume Update */
{ .features = WM8960_FEATURE_MICROPHONES, .reg = WM8960_REG_RIGHT_ADC_VOL,
.value = (WM8960_LEFT_RIGHT_ADC_VOL_ADCVU_UP | WM8960_LEFT_RIGHT_ADC_VOL_ADCVOL_0dB) },
/* Left DAC to left output mixed enabled (LD2LO), 0dB */
{ .features = WM8960_FEATURE_HEADPHONE, .reg = WM8960_REG_LEFT_OUT_MIX,
.value = WM8960_LEFT_OUT_MIX_LD2LO_EN },
/* Right DAC to right output mixed enabled (RD2RO), 0dB */
{ .features = WM8960_FEATURE_HEADPHONE, .reg = WM8960_REG_RIGHT_OUT_MIX,
.value = WM8960_RIGHT_OUT_MIX_RD2RO_EN },
/* LOUT1 Volume = 0dB, volume updated */
{ .features = WM8960_FEATURE_HEADPHONE, .reg = WM8960_REG_LOUT1_VOL,
.value = (WM8960_LOUT1_ROUT1_VOL_OUT1VU | WM8960_LOUT1_ROUT1_VOL_OUT1VOL_0dB) },
/* ROUT1 Volume = 0dB, volume updated */
{ .features = WM8960_FEATURE_HEADPHONE, .reg = WM8960_REG_ROUT1_VOL,
.value = (WM8960_LOUT1_ROUT1_VOL_OUT1VU | WM8960_LOUT1_ROUT1_VOL_OUT1VOL_0dB) },
/* Unmute DAC digital soft mute */
{ .features = WM8960_FEATURE_HEADPHONE, .reg = WM8960_REG_CTR1,
.value = WM8960_CTR1_DACMU_NO },
/* Left DAC to left output mixed enabled (LD2LO), 0dB */
{ .features = WM8960_FEATURE_SPEAKER, .reg = WM8960_REG_LEFT_OUT_MIX,
.value = WM8960_LEFT_OUT_MIX_LD2LO_EN },
/* Right DAC to right output mixed enabled (RD2RO), 0dB */
{ .features = WM8960_FEATURE_SPEAKER, .reg = WM8960_REG_RIGHT_OUT_MIX,
.value = WM8960_RIGHT_OUT_MIX_RD2RO_EN },
/* LOUT2 Volume = 0dB, volume updated */
{ .features = WM8960_FEATURE_SPEAKER, .reg = WM8960_REG_LOUT2_VOL,
.value = (WM8960_LOUT1_ROUT1_VOL_OUT1VU | WM8960_LOUT1_ROUT1_VOL_OUT1VOL_0dB) },
/* ROUT2 Volume = 0dB, volume updated */
{ .features = WM8960_FEATURE_SPEAKER, .reg = WM8960_REG_ROUT2_VOL,
.value = (WM8960_LOUT1_ROUT1_VOL_OUT1VU | WM8960_LOUT1_ROUT1_VOL_OUT1VOL_0dB) },
/* Unmute DAC digital soft mute */
{ .features = WM8960_FEATURE_SPEAKER, .reg = WM8960_REG_CTR1,
.value = WM8960_CTR1_DACMU_NO },
};
for (uint32_t i = 0; i < sizeof(operations) / sizeof(_mtb_wm8960_operation_t); i++)
{
_mtb_wm8960_operation_t operation = operations[i];
if ((features & operation.features) == operation.features)
{
result = mtb_wm8960_write(operation.reg, operation.value);
if (! result)
{
WM8960_LOG("operation");
return result;
}
}
}
enabled_features = features;
return result;
}
//--------------------------------------------------------------------------------------------------
// _mtb_wm8960_setup_pll
//--------------------------------------------------------------------------------------------------
static bool _mtb_wm8960_setup_pll(uint32_t mclk_hz,
mtb_wm8960_adc_dac_sample_rate_t sample_rate)
{
WM8960_LOG("_mtb_wm8960_setup_pll");
bool result;
uint8_t PLLN;
uint32_t PLLK;
bool use_prescale = false;
uint32_t sys_clk_hz;
/* Based on Table 40 on pg 61 in the WM8960 datasheet we infer
* the SysClk frequency based on the selected sample rate.
*/
switch (sample_rate)
{
case WM8960_ADC_DAC_SAMPLE_RATE_48_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_32_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_24_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_16_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_12_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_8_KHZ:
default:
sys_clk_hz = _WM8960_SYSCLK_FREQ_12288000_HZ;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_44_1_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_22_05_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_11_025_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_8_018_KHZ:
sys_clk_hz = _WM8960_SYSCLK_FREQ_11289600_HZ;
break;
}
/* Based on the PLL section on pg 63/64 the Sysclk divider
* after the PLL must be 2. See Figure 36.
* f2 = 4 (Fixed divider in PLL) * 2 (SYSCLKDIV) * sysclk_hz
* f1 = mclk_hz
* R = f2 / f1
* PLLN = int (R)
* PLLK = int (2^24 (R - PLLN))
*/
uint32_t f2 = (4 * 2 * sys_clk_hz);
uint32_t f1 = mclk_hz;
float R = (float)f2 / (float)f1;
/* As per description of R52 the PLLN value must be between
* 5 and 13. If the R value is less than 5 we use a prescale
* divder (PLLPRESCALE) that divides the mclk_hz frequency by
* 2 before its goes into the PLL.
*
* The datasheet also documents the peak stability is at
* PLLN = 8 (pg 64) so if the R value is close to 5 and if using
* the prescale divider gets us closer to 8 we enable the prescale
* divider.
*/
if ((R <= 5) || ((R*2 - 8) < (8 - R)))
{
use_prescale = true;
R *= (float)2;
}
/* If the value of R is not within the permitted range then
* mclk_hz freq cannot be used to generate a valid sysclk.
*/
if ((R <= 5) || (R >= 13))
{
return false;
}
PLLN = (uint8_t)R;
PLLK = (uint32_t)((float)0x1000000 * (R - (float)PLLN));
uint16_t prescale_mask = (use_prescale)
? WM8960_PLL_N_PLLPRESCALE_EN
: WM8960_PLL_N_PLLPRESCALE_DI;
/* Update the PLLN register */
result = mtb_wm8960_write(WM8960_REG_PLL_N, PLLN | prescale_mask | WM8960_PLL_N_SDM_FRAC);
if (! result)
{
return result;
}
/* Update PLLK1 with PLLK[23:16] */
result = mtb_wm8960_write(WM8960_REG_PLL_K1, PLLK >> 16 & 0xFF);
if (! result)
{
return result;
}
/* Update PLLK2 with PLLK[15:8] */
result = mtb_wm8960_write(WM8960_REG_PLL_K2, PLLK >> 8 & 0xFF);
if (! result)
{
return result;
}
/* Update PLLK3 with PLLK[7:0] */
result = mtb_wm8960_write(WM8960_REG_PLL_K3, PLLK & 0xFF);
if (! result)
{
return result;
}
/* Enable the PLL in the power management register. */
result = mtb_wm8960_set(WM8960_REG_PWR_MGMT2, WM8960_PWR_MGMT2_PLL_EN_UP);
return result;
}
//--------------------------------------------------------------------------------------------------
// _mtb_wm8960_adjust_volume
//--------------------------------------------------------------------------------------------------
static bool _mtb_wm8960_adjust_volume(uint8_t volume, mtb_wm8960_reg_t left_vol_reg,
mtb_wm8960_reg_t right_vol_reg,
uint16_t update_bit, uint16_t volume_bits_mask)
{
WM8960_LOG("_mtb_wm8960_adjust_volume");
/* Volume adjustment is done based on documentation datasheet(rev 4.4) pg 45 */
bool result;
uint16_t data;
result = mtb_wm8960_read(left_vol_reg, &data);
if (! result)
{
return result;
}
/* Clear the update and volume bits */
data &= (~update_bit & ~volume_bits_mask);
/* Set the new volume bits */
data |= volume;
result = mtb_wm8960_write(left_vol_reg, data);
if (! result)
{
return result;
}
result = mtb_wm8960_read(right_vol_reg, &data);
if (! result)
{
return result;
}
/* Clear old volume bits */
data &= ~volume_bits_mask;
/* Set the new volume bits */
data |= (volume | update_bit);
/* Set right volume and update bits */
result = mtb_wm8960_write(right_vol_reg, data);
return result;
}
#ifdef ARDUINO
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_set_wire
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_set_wire(TwoWire* i2c_inst){
WM8960_LOG("mtb_wm8960_set_wire");
/* Assign I2C hal object */
i2c_ptr = i2c_inst;
return i2c_ptr!=nullptr;
}
#endif
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_set_write_retry_count
//--------------------------------------------------------------------------------------------------
void mtb_wm8960_set_write_retry_count(uint32_t count){
write_retry_count = count;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_init
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_init(uint8_t features)
{
// use Wire and start it
WM8960_LOG("mtb_wm8960_init");
#ifdef ARDUINO
if (i2c_ptr==nullptr){
i2c_ptr = &Wire;
WM8960_LOG("Wire.begin()");
i2c_ptr->begin();
i2c_ptr->setClock(10000);
}
#endif
bool result = mtb_wm8960_write(WM8960_REG_RESET, 0);
if (!result)
{
return false;
}
//delay(100);
/* Call default configuration */
if (features != WM8960_FEATURE_NONE)
{
result = _mtb_wm8960_config_default(features);
if (!result) {
WM8960_LOG("_mtb_wm8960_config_default ERROR");
return result;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_free
//--------------------------------------------------------------------------------------------------
void mtb_wm8960_free(void)
{
WM8960_LOG("mtb_wm8960_free");
#ifdef ARDUINO
i2c_ptr = NULL;
#endif
pll_enabled = false;
enabled_features = WM8960_FEATURE_NONE;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_activate
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_activate(void)
{
WM8960_LOG("mtb_wm8960_activate");
bool result;
uint16_t value;
/******************** PWR_MGMT1 *******************/
result = mtb_wm8960_read(WM8960_REG_PWR_MGMT1, &value);
if (! result)
{
return result;
}
/* Clear VMIDSEL */
value &= ~WM8960_PWR_MGMT1_VMIDSEL_5K;
/* Set VMID=50K for playback and recording */
value |= (WM8960_PWR_MGMT1_VMIDSEL_50K);
if (enabled_features & WM8960_FEATURE_MICROPHONE1
|| enabled_features & WM8960_FEATURE_MICROPHONE2
|| enabled_features & WM8960_FEATURE_MICROPHONE3)
{
/* AINL, AINR, ADCL, ADCR and MICB */
value |= (WM8960_PWR_MGMT1_AINL_UP | WM8960_PWR_MGMT1_AINR_UP |
WM8960_PWR_MGMT1_ADCL_UP | WM8960_PWR_MGMT1_ADCR_UP |
WM8960_PWR_MGMT1_MICB_UP);
}
result = mtb_wm8960_write(WM8960_REG_PWR_MGMT1, value);
if (! result)
{
return result;
}
/******************** PWR_MGMT2 *******************/
value = 0;
if (pll_enabled)
{
value = WM8960_PWR_MGMT2_PLL_EN_UP;
}
if ((enabled_features & WM8960_FEATURE_HEADPHONE) == WM8960_FEATURE_HEADPHONE)
{
value |= (WM8960_PWR_MGMT2_DACL_UP | WM8960_PWR_MGMT2_DACR_UP |
WM8960_PWR_MGMT2_LOUT1_UP | WM8960_PWR_MGMT2_ROUT1_UP);
}
if ((enabled_features & WM8960_FEATURE_SPEAKER) == WM8960_FEATURE_SPEAKER)
{
value |= (WM8960_PWR_MGMT2_DACL_UP | WM8960_PWR_MGMT2_DACR_UP |
WM8960_PWR_MGMT2_SPKL_UP | WM8960_PWR_MGMT2_SPKR_UP);
}
result = mtb_wm8960_set(WM8960_REG_PWR_MGMT2, value);
if (! result)
{
return result;
}
/******************** PWR_MGMT3 *******************/
value = 0;
/* Enable left output mixer and right output mixer */
if (enabled_features & WM8960_FEATURE_HEADPHONE || enabled_features & WM8960_FEATURE_SPEAKER)
{
value |= (WM8960_PWR_MGMT3_LOMIX_UP | WM8960_PWR_MGMT3_ROMIX_UP);
}
/* Enable left and right channel input PGA */
if (enabled_features & WM8960_FEATURE_MICROPHONE1
|| enabled_features & WM8960_FEATURE_MICROPHONE2
|| enabled_features & WM8960_FEATURE_MICROPHONE3)
{
value |= (WM8960_PWR_MGMT3_RMIC_UP | WM8960_PWR_MGMT3_LMIC_UP);
}
result = mtb_wm8960_set(WM8960_REG_PWR_MGMT3, value);
return result;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_deactivate
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_deactivate(void)
{
WM8960_LOG("mtb_wm8960_deactivate");
bool result;
uint16_t value = 0;
/******************** PWR_MGMT3 *******************/
/* Disable left output mixer and right output mixer */
if (enabled_features & WM8960_FEATURE_HEADPHONE || enabled_features &WM8960_FEATURE_SPEAKER)
{
value |= (WM8960_PWR_MGMT3_LOMIX_UP | WM8960_PWR_MGMT3_ROMIX_UP);
}
/* Disable left and right channel input PGA */
if (enabled_features & WM8960_FEATURE_MICROPHONE1
|| enabled_features & WM8960_FEATURE_MICROPHONE2
|| enabled_features & WM8960_FEATURE_MICROPHONE3)
{
value |= (WM8960_PWR_MGMT3_RMIC_UP | WM8960_PWR_MGMT3_LMIC_UP);
}
result = mtb_wm8960_clear(WM8960_REG_PWR_MGMT3, value);
if (! result)
{
return result;
}
/******************** PWR_MGMT2 *******************/
result = mtb_wm8960_read(WM8960_REG_PWR_MGMT2, &value);
if (! result)
{
return result;
}
/* If PLL is enabled, disable the PLL. */
if (pll_enabled)
{
value &= ~(WM8960_PWR_MGMT2_PLL_EN_UP);
}
if ((enabled_features & WM8960_FEATURE_HEADPHONE) == WM8960_FEATURE_HEADPHONE)
{
/* Disable DACL, DACR, LOUT1 and ROUT1 */
value &= ~(WM8960_PWR_MGMT2_DACL_UP | WM8960_PWR_MGMT2_DACR_UP |
WM8960_PWR_MGMT2_LOUT1_UP | WM8960_PWR_MGMT2_ROUT1_UP);
}
if ((enabled_features & WM8960_FEATURE_SPEAKER) == WM8960_FEATURE_SPEAKER)
{
/* Disable DACL, DACR, LOUT1 and ROUT1 */
value &= ~(WM8960_PWR_MGMT2_DACL_UP | WM8960_PWR_MGMT2_DACR_UP |
WM8960_PWR_MGMT2_SPKL_UP | WM8960_PWR_MGMT2_SPKR_UP);
}
result = mtb_wm8960_write(WM8960_REG_PWR_MGMT2, value);
if (! result)
{
return result;
}
/******************** PWR_MGMT1 *******************/
result = mtb_wm8960_read(WM8960_REG_PWR_MGMT1, &value);
if (! result)
{
return result;
}
/* Clear VMIDSEL */
value &= ~WM8960_PWR_MGMT1_VMIDSEL_5K;
/* Set VMID=250K for low power standby */
value |= WM8960_PWR_MGMT1_VMIDSEL_250K;
if (enabled_features & WM8960_FEATURE_MICROPHONE1
|| enabled_features & WM8960_FEATURE_MICROPHONE2
|| enabled_features & WM8960_FEATURE_MICROPHONE3)
{
/* Disable AINL, AINR, ADCL, ADCR and MICB */
value &= ~(WM8960_PWR_MGMT1_AINL_UP | WM8960_PWR_MGMT1_AINR_UP |
WM8960_PWR_MGMT1_ADCL_UP | WM8960_PWR_MGMT1_ADCR_UP |
WM8960_PWR_MGMT1_MICB_UP);
}
result = mtb_wm8960_write(WM8960_REG_PWR_MGMT1, value);
return result;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_write
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_write_ex(mtb_wm8960_reg_t enum_reg, uint16_t value)
{
uint8_t reg = enum_reg;
uint8_t data[2];
data[0] = (reg<<1)|((uint8_t)((value>>8)&0x0001)); //RegAddr
data[1] = (uint8_t)(value & 0x00FF); //RegValue
bool result = i2c_write(WM8960_I2C_ADDRESS, data);
if (result) {
if (reg == WM8960_REG_RESET){
static const uint16_t wm8960_default_register_map[REGISTER_MAP_SIZE] =
{
0x0097, 0x0097, 0x0000, 0x0000, 0x0000, 0x0008, 0x0000, 0x000a, // R0~R7
0x01c0, 0x0000, 0x00ff, 0x00ff, 0x0000, 0x0000, 0x0000, 0x0000, // R8~R15
0x0000, 0x007b, 0x0100, 0x0032, 0x0000, 0x00c3, 0x00c3, 0x01c0, // R16~R23
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // R24~R31
0x0100, 0x0100, 0x0050, 0x0050, 0x0050, 0x0050, 0x0000, 0x0000, // R32~R39
0x0000, 0x0000, 0x0040, 0x0000, 0x0000, 0x0050, 0x0050, 0x0000, // R40~R47
0x0000, 0x0037, 0x004d, 0x0080, 0x0008, 0x0031, 0x0026, 0x00e9, // R48~R55
};
memcpy(&wm8960_register_map, &wm8960_default_register_map, REGISTER_MAP_SIZE);
} else {
// Update register map
wm8960_register_map[reg] = value;
}
}
return result;
}
#ifdef ARDUINO
bool i2c_write(uint8_t address, uint8_t data[2]) {
i2c_ptr->beginTransmission(address);
i2c_ptr->write(data, 2);
int result = i2c_ptr->endTransmission(true);
return result == 0;
}
#endif
bool mtb_wm8960_write(mtb_wm8960_reg_t enum_reg, uint16_t value){
char msg[80];
snprintf(msg, 80, "mtb_wm8960_write 0x%x = 0x%x", enum_reg, value );
WM8960_LOG(msg);
bool result = false;
uint32_t count=0;
while(!result){
result = mtb_wm8960_write_ex(enum_reg, value);
if (count>0 && count++ > write_retry_count){
break;
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_read
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_read(mtb_wm8960_reg_t reg, uint16_t* value)
{
if (reg >= REGISTER_MAP_SIZE)
{
return false;
}
*value = wm8960_register_map[reg];
return true;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_set
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_set(mtb_wm8960_reg_t reg, uint16_t mask)
{
bool result;
uint16_t data;
result = mtb_wm8960_read(reg, &data);
if (result)
{
result = mtb_wm8960_write((mtb_wm8960_reg_t)reg, data | mask);
}
return result;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_clear
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_clear(mtb_wm8960_reg_t reg, uint16_t mask)
{
bool result;
uint16_t data;
result = mtb_wm8960_read(reg, &data);
if (result)
{
result = mtb_wm8960_write(reg, data & ~mask);
}
return result;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_adjust_input_volume
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_adjust_input_volume(uint8_t volume)
{
WM8960_LOG("mtb_wm8960_adjust_input_volume");
if (volume > WM8960_LEFT_RIGHT_IN_VOL_INVOL_30dB)
{
return false;
}
return _mtb_wm8960_adjust_volume(volume, WM8960_REG_LEFT_IN_VOL, WM8960_REG_RIGHT_IN_VOL,
WM8960_LEFT_RIGHT_IN_VOL_IPVU,
WM8960_LEFT_RIGHT_IN_VOL_INVOL_30dB);
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_adjust_heaphone_output_volume
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_adjust_heaphone_output_volume(uint8_t volume)
{
WM8960_LOG("mtb_wm8960_adjust_heaphone_output_volume");
if (volume > WM8960_LOUT1_ROUT1_VOL_OUT1VOL_6dB)
{
return false;
}
return _mtb_wm8960_adjust_volume(volume, WM8960_REG_LOUT1_VOL, WM8960_REG_ROUT1_VOL,
WM8960_LOUT1_ROUT1_VOL_OUT1VU,
WM8960_LOUT1_ROUT1_VOL_OUT1VOL_6dB);
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_adjust_heaphone_output_volume
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_adjust_speaker_output_volume(uint8_t volume)
{
WM8960_LOG("mtb_wm8960_adjust_heaphone_output_volume");
if (volume > WM8960_LOUT1_ROUT1_VOL_OUT1VOL_6dB)
{
return false;
}
return _mtb_wm8960_adjust_volume(volume, WM8960_REG_LOUT2_VOL, WM8960_REG_ROUT2_VOL,
WM8960_LOUT1_ROUT1_VOL_OUT1VU,
WM8960_LOUT1_ROUT1_VOL_OUT1VOL_6dB);
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_set_output_volume
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_set_output_volume(uint8_t volume){
bool result = true;
if (enabled_features & WM8960_FEATURE_SPEAKER == WM8960_FEATURE_SPEAKER )
result = result && mtb_wm8960_adjust_speaker_output_volume(volume);
if (enabled_features & WM8960_FEATURE_HEADPHONE == WM8960_FEATURE_HEADPHONE)
result = result && mtb_wm8960_adjust_heaphone_output_volume(volume);
return result;
}
//--------------------------------------------------------------------------------------------------
// mtb_wm8960_configure_clocking
//--------------------------------------------------------------------------------------------------
bool mtb_wm8960_configure_clocking(uint32_t mclk_hz, bool enable_pll,
mtb_wm8960_adc_dac_sample_rate_t sample_rate,
mtb_wm8960_word_length_t word_length,
mtb_wm8960_mode_t mode)
{
WM8960_LOG("mtb_wm8960_configure_clocking");
bool result;
uint16_t dac_div_mask;
uint16_t adc_div_mask;
/* By default the mclk is used as sysclk (no pll) */
uint16_t clk_sel_mask = WM8960_CLK1_CLKSEL_MCLK;
/* By default the sysclk div is set to 1 */
uint16_t sysclk_div_mask = WM8960_CLK1_SYSCLKDIV_BY_1;
/* Only two clock frequencies are supported for mclk if PLL is not being used*/
if (!enable_pll && (mclk_hz != _WM8960_SYSCLK_FREQ_12288000_HZ) &&
(mclk_hz != _WM8960_SYSCLK_FREQ_11289600_HZ))
{
return false;
}
/* Check if using internal PLL */
if (enable_pll)
{
result = _mtb_wm8960_setup_pll(mclk_hz, sample_rate);
if (! result)
{
return result;
}
clk_sel_mask = WM8960_CLK1_CLKSEL_PLL;
sysclk_div_mask = WM8960_CLK1_SYSCLKDIV_BY_2;
}
switch (sample_rate)
{
case WM8960_ADC_DAC_SAMPLE_RATE_48_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_44_1_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_1;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_1;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_32_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_1_5;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_1_5;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_24_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_22_05_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_2;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_2;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_16_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_3;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_3;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_11_025_KHZ:
case WM8960_ADC_DAC_SAMPLE_RATE_12_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_4;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_4;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_8_018_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_5_5;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_5_5;
break;
case WM8960_ADC_DAC_SAMPLE_RATE_8_KHZ:
dac_div_mask = WM8960_CLK1_DACDIV_BY_6;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_6;
break;
default:
dac_div_mask = WM8960_CLK1_DACDIV_BY_1;
adc_div_mask = WM8960_CLK1_ADCDIV_BY_1;
break;
}
pll_enabled = enable_pll;
/* Set Clocking 1 */
result = mtb_wm8960_write(WM8960_REG_CLK1,
adc_div_mask |
dac_div_mask |
sysclk_div_mask |
clk_sel_mask);
if (! result)
{
return result;
}
result = mtb_wm8960_write(WM8960_REG_AUDIO_INTF0,
(uint16_t)mode |
(uint16_t)word_length |
WM8960_AUDIO_INTF0_FORMAT_I2S_MODE);
return result;
}
bool mtb_wm8960_dump(){
WM8960_LOG("mtb_wm8960_dump");
char msg[80];
for (int j=0x1;j<=0x37;j++){
uint16_t data;
if (!mtb_wm8960_read((mtb_wm8960_reg_t)j, &data)){
WM8960_LOG("mtb_wm8960_dump ERROR");
return false;
}
snprintf(msg, 80, "%x: %x",j, data);
WM8960_LOG(msg);
}
return true;
}