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
93
96
97
100
101
106
107
110
111
112
113
114
117
118
121
122
123
126
127
130
131
132
133
136
137
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
162
163
168
169
170
171
172
173
174
180
184
185
189
190
191
192
193
197
203
204
210
224
225
230
234
235
236
237
238
239
240
241
242
243
244
245
246
250
251
252
253
254
255
256
257
258
259
260
267
268
269
270
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
325
338
339
345
358
359
360
361
362
363
364
365
366
367
380
381
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
416
417
418
419
422
423
424
425
426
427
428
429
432
433
434
435
436
437
438
439
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
531
537
538
542
548
549
553
557
558
561
564
565
568
571
572
575
578
579
582
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
608
609
610
611
612
613
614
615
616
617
618
619
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
647
648
651
652
655
656
/* ... */
/* ... */
#include "stm324xg_eval_audio.h"
#include "stm324xg_eval_io.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
const uint32_t I2SFreq[8] = {8000, 11025, 16000, 22050, 32000, 44100, 48000, 96000};
const uint32_t I2SPLLN[8] = {256, 429, 213, 429, 426, 271, 258, 344};
const uint32_t I2SPLLR[8] = {5, 4, 4, 4, 4, 6, 3, 1};
/* ... */
/* ... */
AUDIO_DrvTypeDef *audio_drv;
I2S_HandleTypeDef haudio_i2s;
/* ... */
/* ... */
static void I2Sx_Init(uint32_t AudioFreq);
static void I2Sx_DeInit(void);
static void CODEC_Reset(void);
/* ... */
/* ... */
/* ... */
uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
{
uint8_t ret = AUDIO_ERROR;
I2Sx_DeInit();
BSP_AUDIO_OUT_ClockConfig(&haudio_i2s, AudioFreq, NULL);
/* ... */
haudio_i2s.Instance = AUDIO_I2Sx;
if(HAL_I2S_GetState(&haudio_i2s) == HAL_I2S_STATE_RESET)
{
BSP_AUDIO_OUT_MspInit(&haudio_i2s, NULL);
}if (HAL_I2S_GetState(&haudio_i2s) == HAL_I2S_STATE_RESET) { ... }
I2Sx_Init(AudioFreq);
CODEC_Reset();
if(((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID)
{
audio_drv = &cs43l22_drv;
ret = AUDIO_OK;
}if (((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID) { ... }
else
{
ret = AUDIO_ERROR;
}else { ... }
if(ret == AUDIO_OK)
{
audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq);
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
void BSP_AUDIO_OUT_DeInit(void)
{
I2Sx_DeInit();
BSP_AUDIO_OUT_MspDeInit(&haudio_i2s, NULL);
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size)
{
if(audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) { ... }
else
{
HAL_I2S_Transmit_DMA(&haudio_i2s, pBuffer, DMA_MAX(Size/AUDIODATA_SIZE));
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size)
{
HAL_I2S_Transmit_DMA(&haudio_i2s, pData, Size);
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Pause(void)
{
if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0) { ... }
else
{
HAL_I2S_DMAPause(&haudio_i2s);
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Resume(void)
{
if(audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0) { ... }
else
{
HAL_I2S_DMAResume(&haudio_i2s);
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
{
HAL_I2S_DMAStop(&haudio_i2s);
if(audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) { ... }
else
{
if(Option == CODEC_PDWN_HW)
{
HAL_Delay(1);
BSP_IO_WritePin(AUDIO_RESET_PIN, RESET);
}if (Option == CODEC_PDWN_HW) { ... }
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
{
if(audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
{
if(audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
{
if(audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
{
BSP_AUDIO_OUT_ClockConfig(&haudio_i2s, AudioFreq, NULL);
I2Sx_Init(AudioFreq);
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
{
RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct;
uint8_t index = 0, freqindex = 0xFF;
for(index = 0; index < 8; index++)
{
if(I2SFreq[index] == AudioFreq)
{
freqindex = index;
}if (I2SFreq[index] == AudioFreq) { ... }
}for (index = 0; index < 8; index++) { ... }
HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct);
if(freqindex != 0xFF)
{
/* ... */
RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = I2SPLLN[freqindex];
RCC_ExCLKInitStruct.PLLI2S.PLLI2SR = I2SPLLR[freqindex];
HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
}if (freqindex != 0xFF) { ... }
else
{
/* ... */
RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = 258;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SR = 3;
HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
}else { ... }
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
{
static DMA_HandleTypeDef hdma_i2sTx;
GPIO_InitTypeDef GPIO_InitStruct;
AUDIO_I2Sx_CLK_ENABLE();
AUDIO_I2Sx_SCK_SD_WS_CLK_ENABLE();
GPIO_InitStruct.Pin = AUDIO_I2Sx_WS_PIN | AUDIO_I2Sx_SCK_PIN | AUDIO_I2Sx_SD_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = AUDIO_I2Sx_SCK_SD_WS_AF;
HAL_GPIO_Init(AUDIO_I2Sx_SCK_SD_WS_GPIO_PORT, &GPIO_InitStruct);
AUDIO_I2Sx_MCK_CLK_ENABLE();
GPIO_InitStruct.Pin = AUDIO_I2Sx_MCK_PIN;
HAL_GPIO_Init(AUDIO_I2Sx_MCK_GPIO_PORT, &GPIO_InitStruct);
AUDIO_I2Sx_DMAx_CLK_ENABLE();
if(hi2s->Instance == AUDIO_I2Sx)
{
hdma_i2sTx.Init.Channel = AUDIO_I2Sx_DMAx_CHANNEL;
hdma_i2sTx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_i2sTx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2sTx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2sTx.Init.PeriphDataAlignment = AUDIO_I2Sx_DMAx_PERIPH_DATA_SIZE;
hdma_i2sTx.Init.MemDataAlignment = AUDIO_I2Sx_DMAx_MEM_DATA_SIZE;
hdma_i2sTx.Init.Mode = DMA_NORMAL;
hdma_i2sTx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_i2sTx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
hdma_i2sTx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_i2sTx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_i2sTx.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma_i2sTx.Instance = AUDIO_I2Sx_DMAx_STREAM;
__HAL_LINKDMA(hi2s, hdmatx, hdma_i2sTx);
HAL_DMA_DeInit(&hdma_i2sTx);
HAL_DMA_Init(&hdma_i2sTx);
}if (hi2s->Instance == AUDIO_I2Sx) { ... }
HAL_NVIC_SetPriority(AUDIO_I2Sx_DMAx_IRQ, AUDIO_IRQ_PREPRIO, 0);
HAL_NVIC_EnableIRQ(AUDIO_I2Sx_DMAx_IRQ);
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
{
GPIO_InitTypeDef GPIO_InitStruct;
AUDIO_I2Sx_CLK_DISABLE();
GPIO_InitStruct.Pin = AUDIO_I2Sx_WS_PIN | AUDIO_I2Sx_SCK_PIN | AUDIO_I2Sx_SD_PIN;
HAL_GPIO_DeInit(AUDIO_I2Sx_SCK_SD_WS_GPIO_PORT, GPIO_InitStruct.Pin);
GPIO_InitStruct.Pin = AUDIO_I2Sx_MCK_PIN;
HAL_GPIO_DeInit(AUDIO_I2Sx_MCK_GPIO_PORT, GPIO_InitStruct.Pin);
}{ ... }
/* ... */
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
{
/* ... */
BSP_AUDIO_OUT_TransferComplete_CallBack();
}{ ... }
/* ... */
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
/* ... */
BSP_AUDIO_OUT_HalfTransfer_CallBack();
}{ ... }
/* ... */
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
{
BSP_AUDIO_OUT_Error_CallBack();
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
{
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
{
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_Error_CallBack(void)
{
}{ ... }
/* ... */
/* ... */
static void I2Sx_Init(uint32_t AudioFreq)
{
haudio_i2s.Instance = AUDIO_I2Sx;
__HAL_I2S_DISABLE(&haudio_i2s);
haudio_i2s.Init.Mode = I2S_MODE_MASTER_TX;
haudio_i2s.Init.Standard = I2S_STANDARD;
haudio_i2s.Init.DataFormat = I2S_DATAFORMAT_16B;
haudio_i2s.Init.AudioFreq = AudioFreq;
haudio_i2s.Init.CPOL = I2S_CPOL_LOW;
haudio_i2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
HAL_I2S_Init(&haudio_i2s);
}{ ... }
/* ... */
static void I2Sx_DeInit(void)
{
haudio_i2s.Instance = AUDIO_I2Sx;
__HAL_I2S_DISABLE(&haudio_i2s);
HAL_I2S_DeInit(&haudio_i2s);
}{ ... }
/* ... */
static void CODEC_Reset(void)
{
BSP_IO_Init();
BSP_IO_ConfigPin(AUDIO_RESET_PIN, IO_MODE_OUTPUT);
BSP_IO_WritePin(AUDIO_RESET_PIN, RESET);
HAL_Delay(CODEC_RESET_DELAY);
BSP_IO_WritePin(AUDIO_RESET_PIN, SET);
HAL_Delay(CODEC_RESET_DELAY);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */