1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
29
30
35
36
39
40
43
44
47
48
50
51
52
53
56
57
60
61
64
65
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
95
96
99
100
103
104
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
133
137
141
145
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
200
205
206
211
212
213
214
215
216
217
218
219
220
221
222
223
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
338
344
345
346
347
348
354
358
359
366
367
368
369
370
371
377
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
406
411
416
421
425
426
427
428
429
434
438
439
446
447
448
449
450
451
452
453
455
456
457
458
459
460
463
464
467
468
471
472
/* ... */
#include "cs43l22.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
#define VOLUME_CONVERT(Volume) (((Volume) > 100)? 255:((uint8_t)(((Volume) * 255) / 100)))
/* ... */
#if !defined (VERIFY_WRITTENDATA)
#endif
/* ... */
/* ... */
/* ... */
/* ... */
AUDIO_DrvTypeDef cs43l22_drv =
{
cs43l22_Init,
cs43l22_DeInit,
cs43l22_ReadID,
cs43l22_Play,
cs43l22_Pause,
cs43l22_Resume,
cs43l22_Stop,
cs43l22_SetFrequency,
cs43l22_SetVolume,
cs43l22_SetMute,
cs43l22_SetOutputMode,
cs43l22_Reset,
...};
static uint8_t Is_cs43l22_Stop = 1;
volatile uint8_t OutputDev = 0;
/* ... */
/* ... */
static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
/* ... */
/* ... */
/* ... */
uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
{
uint32_t counter = 0;
AUDIO_IO_Init();
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x01);
switch (OutputDevice)
{
case OUTPUT_DEVICE_SPEAKER:
OutputDev = 0xFA;
break;
case OUTPUT_DEVICE_SPEAKER:
case OUTPUT_DEVICE_HEADPHONE:
OutputDev = 0xAF;
break;
case OUTPUT_DEVICE_HEADPHONE:
case OUTPUT_DEVICE_BOTH:
OutputDev = 0xAA;
break;
case OUTPUT_DEVICE_BOTH:
case OUTPUT_DEVICE_AUTO:
OutputDev = 0x05;
break;
case OUTPUT_DEVICE_AUTO:
default:
OutputDev = 0x05;
break; default
}switch (OutputDevice) { ... }
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_CLOCKING_CTL, 0x81);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_INTERFACE_CTL1, CODEC_STANDARD);
counter += cs43l22_SetVolume(DeviceAddr, Volume);
if(OutputDevice != OUTPUT_DEVICE_HEADPHONE)
{
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PLAYBACK_CTL2, 0x06);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_SPEAKER_A_VOL, 0x00);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_SPEAKER_B_VOL, 0x00);
}if (OutputDevice != OUTPUT_DEVICE_HEADPHONE) { ... }
/* ... */
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_ANALOG_ZC_SR_SETT, 0x00);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x04);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_LIMIT_CTL1, 0x00);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_TONE_CTL, 0x0F);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PCMA_VOL, 0x0A);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PCMB_VOL, 0x0A);
return counter;
}{ ... }
/* ... */
void cs43l22_DeInit(void)
{
AUDIO_IO_DeInit();
}{ ... }
/* ... */
uint32_t cs43l22_ReadID(uint16_t DeviceAddr)
{
uint8_t Value;
AUDIO_IO_Init();
Value = AUDIO_IO_Read(DeviceAddr, CS43L22_CHIPID_ADDR);
Value = (Value & CS43L22_ID_MASK);
return((uint32_t) Value);
}{ ... }
/* ... */
uint32_t cs43l22_Play(uint16_t DeviceAddr, uint16_t* pBuffer, uint16_t Size)
{
uint32_t counter = 0;
if(Is_cs43l22_Stop == 1)
{
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x06);
counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_OFF);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9E);
Is_cs43l22_Stop = 0;
}if (Is_cs43l22_Stop == 1) { ... }
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_Pause(uint16_t DeviceAddr)
{
uint32_t counter = 0;
counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_ON);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x01);
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_Resume(uint16_t DeviceAddr)
{
uint32_t counter = 0;
volatile uint32_t index = 0x00;
counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_OFF);
for(index = 0x00; index < 0xFF; index++);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9E);
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_Stop(uint16_t DeviceAddr, uint32_t CodecPdwnMode)
{
uint32_t counter = 0;
counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_ON);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x04);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9F);
Is_cs43l22_Stop = 1;
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_SetVolume(uint16_t DeviceAddr, uint8_t Volume)
{
uint32_t counter = 0;
uint8_t convertedvol = VOLUME_CONVERT(Volume);
if(convertedvol > 0xE6)
{
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_A_VOL, convertedvol - 0xE7);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_B_VOL, convertedvol - 0xE7);
}if (convertedvol > 0xE6) { ... }
else
{
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_A_VOL, convertedvol + 0x19);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_B_VOL, convertedvol + 0x19);
}else { ... }
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_SetFrequency(uint16_t DeviceAddr, uint32_t AudioFreq)
{
return 0;
}{ ... }
/* ... */
uint32_t cs43l22_SetMute(uint16_t DeviceAddr, uint32_t Cmd)
{
uint32_t counter = 0;
if(Cmd == AUDIO_MUTE_ON)
{
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xFF);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_A_VOL, 0x01);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_B_VOL, 0x01);
}if (Cmd == AUDIO_MUTE_ON) { ... }
else
{
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_A_VOL, 0x00);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_B_VOL, 0x00);
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev);
}else { ... }
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_SetOutputMode(uint16_t DeviceAddr, uint8_t Output)
{
uint32_t counter = 0;
switch (Output)
{
case OUTPUT_DEVICE_SPEAKER:
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xFA);
OutputDev = 0xFA;
break;
case OUTPUT_DEVICE_SPEAKER:
case OUTPUT_DEVICE_HEADPHONE:
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xAF);
OutputDev = 0xAF;
break;
case OUTPUT_DEVICE_HEADPHONE:
case OUTPUT_DEVICE_BOTH:
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xAA);
OutputDev = 0xAA;
break;
case OUTPUT_DEVICE_BOTH:
case OUTPUT_DEVICE_AUTO:
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0x05);
OutputDev = 0x05;
break;
case OUTPUT_DEVICE_AUTO:
default:
counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0x05);
OutputDev = 0x05;
break;default
}switch (Output) { ... }
return counter;
}{ ... }
/* ... */
uint32_t cs43l22_Reset(uint16_t DeviceAddr)
{
return 0;
}{ ... }
/* ... */
static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
{
uint32_t result = 0;
AUDIO_IO_Write(Addr, Reg, Value);
#ifdef VERIFY_WRITTENDATA
result = (AUDIO_IO_Read(Addr, Reg) == Value)? 0:1;/* ... */
#endif
return result;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */