-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
357 lines (300 loc) · 9.92 KB
/
device.c
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
#include "device.h"
#include "display.h"
#include "interrupts.h"
#define PCM3070_Device_Address 0x18 //7-bit PCM3070 I2C address
TSBState sbState;
void SBInit(void)
{
sbState.mode = analog;
sbState.nInput = 1;
sbState.nSurround = SURROUND_NONE;
sbState.nVolume = 8;
sbState.bMute = false;
sbState.bShutdown = true;
sbState.bDisplayOn = false;
sbState.nDisplayTimeCount = 0;
//set PCA9535 pins all as output
unsigned char out[] = {0x00, 0x00};
I2C_multiWrite(0x20, 0x06, 2, out); //setting Port0 and Port1 as OUTPUT
}
const char LDAC[]={0x9C,0xB0,0xC4,0xD8,0xEC,0xF4,0xFA,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10};
const char RDAC[]={0x9C,0xB0,0xC4,0xD8,0xEC,0xF4,0xFA,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10};
//const char pVol28[] = {0x00, 0x00, 0x00, 0x02, 0x06, 0x0B, 0x16, 0x20, 0x2D, 0x40, 0x47, 0x50, 0x5A, 0x65, 0x71, 0x7F};
//const char pVol29[] = {0x10, 0x33, 0xA3, 0x06, 0x66, 0x61, 0xB5, 0x13, 0x4E, 0x00, 0xCF, 0x92, 0x67, 0x6E, 0xCF, 0xB2};
//const char pVol2A[] = {0x62, 0xCF, 0xD7, 0x1B, 0x66, 0x88, 0x43, 0x73, 0xFB, 0x00, 0x26, 0x3B, 0x03, 0xE3, 0x54, 0x60};
void SetVolume(int nVolume)
{
if (nVolume > 15) nVolume = 15;
if (nVolume <= 0) nVolume = 0;
//unsigned char mstVolume[] = {pVol28[nVolume], pVol29[nVolume], pVol2A[nVolume]};
unsigned char mstVolume[] = {LDAC[nVolume], RDAC[nVolume]};
I2C_singleWrite(PCM3070_Device_Address,0x00, 0x00);
I2C_multiWrite(PCM3070_Device_Address, 0x41, 2, mstVolume);
//I2C_singleWrite(PCM3070_Device_Address,0x00, 0x0B); //page 11
//I2C_multiWrite(PCM3070_Device_Address, 0x28, 3, mstVolume); //Master Volume control
//I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08);
//I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
//I2C_singleWrite(PCM3070_Device_Address,0x00, 0x0B); //page 11
//I2C_multiWrite(PCM3070_Device_Address, 0x28, 3, mstVolume);
}
struct TRegisterData
{
char cRegister;
char cData;
};
void LoadRegisters(const TRegisterData *pRegisterData)
{
unsigned int n = 0;
TRegisterData registerData = pRegisterData[0];
while ((registerData.cRegister != 0xFF) && (registerData.cData != 0xFF))
{
I2C_singleWrite(PCM3070_Device_Address, registerData.cRegister, registerData.cData);
registerData = pRegisterData[n++];
}
}
const TRegisterData pToAnalogMode[] =
{
{0x00, 0x00},
{0x52, 0x88},
{0x51, 0x00},
{0x40, 0x0C},
{0x3F, 0x14},
{0x05, 0x11},
{0x04, 0x07}, //set BCLK as PLL input clock
{0x05, 0x93}, //Set PLL parameter R=3
{0x06, 0x04}, //Set PLL parameter J = 4
{0x07, 0x23},
{0x08, 0x28}, //Set PLL parameter D = 9000
{0x00, 0x00},
{0x21, 0x44},
{0x1F, 0x14}, //sec_bclk path
{0x37, 0x14},
{0x34, 0x20},
{0x20, 0x0E}, //interface path
{0x1D, 0x01}, //disable digital loopback
{0x1B, 0x00}, //set wclk as output
{0x1E, 0x82}, //power up Bclk divider, and set ratio=2
{0x3C, 0x80},
{0x3D, 0x00},
{0x00, 0x08},
{0x01, 0x04},
{0x00, 0x2C},
{0x01, 0x04},
{0x00, 0x01}, //adjust PGA gain for Analog mode
{0x3B, 0x00},
{0x3C, 0x00},
{0x00, 0x00}, //power up both ADCs
{0x51, 0xC0},
{0x52, 0x00}, //unMute both ADCs
{0x3F, 0xD4}, //power up both DACs
{0x40, 0x00}, //unMute both DACs
{0x00, 0x01}, //Set LineOUT volume at 0dB
{0x12, 0x00},
{0x13, 0x00},
//{0x00, 0x08}, //configure the Input Mux in miniDSP process flow
//{0x1E, 0x02},
//{0x00, 0x08},
//{0x01, 0x05},
//{0x00, 0x08},
//{0x1E, 0x02},
{0xFF, 0xFF}
};
//this function is used when switch from I2S mode to Analog mode
void PCM3070_toAnalogMode(void)
{
LoadRegisters(pToAnalogMode);
Delay_ms(200);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08); //configure the Input Mux in miniDSP process flow
I2C_singleWrite(PCM3070_Device_Address, 0x1E, 0x02);
I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08);
I2C_singleWrite(PCM3070_Device_Address, 0x1E, 0x02);
}
const TRegisterData pToI2SMode[] =
{
//select page 0
{0x00, 0x00},
//Mute ADCs
{0x52, 0x88},
//power down ADCs
{0x51, 0x00},
//Mute DACs
{0x40, 0x0C},
//power down both DACs
{0x3F, 0x14},
//power down PLL
{0x05, 0x11},
//configure PLL settings
{0x04, 0x03}, //Select MCLK as the PLL input clock
{0x05, 0x91}, //set R = 1
{0x06, 0x04}, //set J = 4
{0x07, 0x00},
{0x08, 0x00}, //Set D = 0000
//configure Audio interface
{0x00, 0x00},
{0x21, 0x00},
{0x1F, 0x12}, //sec_bclk path
{0x37, 0x14},
{0x34, 0x20},
{0x20, 0x00}, //interface path
{0x1D, 0x01},//disable digital loopback
{0x1E, 0x02}, //power down Bclk divider, and set ratio=2
{0x1B, 0x00},
{0x3C, 0x80},
{0x3D, 0x00},
{0x00, 0x08},
{0x01, 0x04},
{0x00, 0x2C},
{0x01, 0x04},
{0x00, 0x00},
//power up both ADCs
{0x51, 0xC0},
//unMute both ADCs
{0x52, 0x00},
//power up both DACs
{0x3F, 0xD4},
//unMute both DACs
{0x40, 0x00},
//Set LineOUT volume at -3 dB
{0x00, 0x01},
{0x12, 0x3D},
{0x13, 0x3D},
{0x3C, 0x00},//Set DAC Gain
{0x3B, 0x00},
//configure the Input Mux in miniDSP process flow
//{0x00, 0x08},
//{0x1E, 0x01},
//{0x00, 0x08},
//{0x01, 0x05}, //flip buffers
//{0x00, 0x08},
//{0x1E, 0x01},
{0xFF, 0xFF}
};
//this function is used when switch from Analog mode to I2S mode
void PCM3070_toI2SMode(void)
{
LoadRegisters(pToI2SMode);
Delay_ms(200);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08);
I2C_singleWrite(PCM3070_Device_Address, 0x1E, 0x01);
I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08);
I2C_singleWrite(PCM3070_Device_Address, 0x1E, 0x01);
}
void PCM3070_USB_IN(void)
{
SetShutdown(true);
sbState.mode = usb;
//switch from Analog mode to I2S mode
PCM3070_toI2SMode();
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x01); //Increase USB Lineout gain
I2C_singleWrite(PCM3070_Device_Address, 0x12, 0x06);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x01);
I2C_singleWrite(PCM3070_Device_Address, 0x13, 0x06);
//configure the on-board hardware switch
P3OUT_bit.P3OUT_7 = 0; //switch_EN, enable the switch
P3OUT_bit.P3OUT_6 = 0; //switch_Logic low, USB path
P2OUT_bit.P2OUT_2 = 0; //CKSEL, DIR9001 in PLL mode
SetShutdown(sbState.bShutdown);
}
void PCM3070_SPDIF_IN(void)
{
SetShutdown(true);
sbState.mode = spdif;
//switch from analog mode to I2S mode
PCM3070_toI2SMode();
//configure the on-board hardware switch
P3OUT_bit.P3OUT_7 = 0; //switch_EN, enable the switch
P3OUT_bit.P3OUT_6 = 1; //switch_Logic high, SPDIF path
P2OUT_bit.P2OUT_2 = 0; //CKSEL, DIR9001 in PLL mode
SetShutdown(sbState.bShutdown);
}
void SetAnalogMode(int nInput)
{
SetShutdown(true);
unsigned char pMux[] = {0x40, 0x10, 0x04};
unsigned char pgaGain[] = {0x00, 0x00, 0x0C};
unsigned char cInput = 0x40;
unsigned char gInput = {0x00};
const TRegisterData pSwitchAnalog[] = {
{0x00, 0x01},
{0x36, 0x40},
{0x39, 0x40},
{0xff, 0xff}};
//configure GPIO pins
P3OUT_bit.P3OUT_7 = 1; //switch_EN, Disable the switch
P2OUT_bit.P2OUT_2 = 1; //CKSEL, non-PLL mode,only generate clocks for PCM3070
//switch from I2S mode to Analog mode
PCM3070_toAnalogMode();
LoadRegisters(pSwitchAnalog);
cInput = pMux[nInput - 1];
I2C_singleWrite(PCM3070_Device_Address,0x34, cInput);
I2C_singleWrite(PCM3070_Device_Address,0x37, cInput);
gInput = pgaGain[nInput - 1];
I2C_singleWrite(PCM3070_Device_Address,0x00, 0x01);
I2C_singleWrite(PCM3070_Device_Address,0x3B, gInput); //Increase PGA gain for HP IN
I2C_singleWrite(PCM3070_Device_Address,0x3C, gInput);
Delay_ms(100);
SetShutdown(sbState.bShutdown);
}
void SetMute(bool bMute)
{
unsigned char cData = 0x00;
if (bMute) cData = 0x0c;
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x00); //configure PCM3070
I2C_singleWrite(PCM3070_Device_Address, 0x40, cData);
}
void SetShutdown(bool bShutdown)
{
// shutdown TPA3110
if (bShutdown)
P3OUT_bit.P3OUT_0 = 0; //Bring TPA3110 into Shutdown
else
P3OUT_bit.P3OUT_0 = 1; //Bring TPA3110 out of Shutdown
}
void SetSurround(int nSurround)
{
//unsigned char pLargeBass[] = {0x33, 0x33, 0x33};
//unsigned char pSmallBass[] = {0x20, 0x00, 0x00};
//unsigned char *pBass = pSmallBass;
//if (nSurround & SURROUND_0) //large bass
// pBass = pLargeBass;
SetShutdown(true);
if (nSurround & SURROUND_0) //Surround_0 toggles EQ
{
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x2C); //EQ MUX
I2C_singleWrite(PCM3070_Device_Address, 0x0A, 0x01);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x2C);
I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x2C);
I2C_singleWrite(PCM3070_Device_Address, 0x0A, 0x01);
}
else{
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x2C); //EQ MUX
I2C_singleWrite(PCM3070_Device_Address, 0x0A, 0x02);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x2C);
I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x2C);
I2C_singleWrite(PCM3070_Device_Address, 0x0A, 0x02);
}
unsigned char srsTrue = 0x02;
unsigned char srsFalse = 0x01;
if (nSurround & SURROUND_1) //Surround_1 toggles SRS WOW HD
{
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0xB);
I2C_singleWrite(PCM3070_Device_Address, 0x2E, srsTrue);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08);
I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0xB);
I2C_singleWrite(PCM3070_Device_Address, 0x2E, srsTrue);
}
else{
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0xB);
I2C_singleWrite(PCM3070_Device_Address, 0x2E, srsFalse);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0x08);
I2C_singleWrite(PCM3070_Device_Address, 0x01, 0x05);
I2C_singleWrite(PCM3070_Device_Address, 0x00, 0xB);
I2C_singleWrite(PCM3070_Device_Address, 0x2E, srsFalse);
}
SetShutdown(sbState.bShutdown);
}