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
88
89
92
93
98
99
102
105
106
113
114
115
116
117
120
121
124
125
126
127
128
129
130
131
132
133
134
135
136
139
140
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
185
186
189
190
191
192
193
194
195
196
197
198
199
200
201
204
205
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
231
232
237
238
242
243
256
257
268
269
270
271
272
278
279
280
281
282
283
287
288
289
296
297
298
299
300
305
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
327
328
329
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
358
359
360
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
390
391
405
406
407
408
409
415
416
417
418
419
420
424
425
426
427
428
429
435
436
437
438
439
440
444
445
446
447
448
449
456
457
458
459
460
461
465
466
467
468
469
470
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
575
576
577
578
579
580
581
582
583
584
585
589
595
596
600
606
607
611
615
616
619
622
623
626
629
630
633
636
637
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
720
721
722
723
724
725
726
727
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
779
780
781
782
783
784
785
786
787
788
789
793
794
795
796
797
798
799
800
801
802
805
811
812
813
814
815
816
817
818
819
820
821
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
850
851
852
853
854
855
856
861
862
863
864
865
866
867
871
872
873
874
875
876
877
878
879
882
883
884
885
886
887
888
889
890
891
892
893
896
897
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
929
930
931
932
933
934
935
936
942
943
944
945
946
947
948
949
950
951
952
953
954
955
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
979
980
981
982
983
984
985
986
987
988
992
993
994
995
996
997
998
999
1000
1001
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1019
1020
1021
1022
1023
1024
1025
1026
1027
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1053
1054
1055
1056
1057
1058
1062
1067
1068
1072
1078
1079
1083
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1111
1117
1118
1121
1127
1128
1131
1136
1137
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1221
1222
1223
1224
1225
1226
1227
1228
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1248
1249
1250
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1368
1369
1370
1371
1372
1373
1375
1376
1377
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1471
1472
1475
1476
1479
1480
/* ... */
/* ... */
#include <string.h>
#include "stm32469i_discovery_audio.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
#define OUTPUT_DEVICE_HEADPHONE1 OUTPUT_DEVICE_HEADPHONE
#define OUTPUT_DEVICE_HEADPHONE2 OUTPUT_DEVICE_SPEAKER
/* ... */
/* ... */
#define SAIClockDivider(__FREQUENCY__) \
(__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 12 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 2 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 6 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 1 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 3 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 0 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 2 : 1 \
/* ... */
/* ... */
/* ... */
AUDIO_DrvTypeDef *audio_drv;...
SAI_HandleTypeDef haudio_out_sai;
I2S_HandleTypeDef haudio_in_i2s;
TIM_HandleTypeDef haudio_tim;
PDM_Filter_Handler_t PDM_FilterHandler[2];
PDM_Filter_Config_t PDM_FilterConfig[2];
uint8_t Channel_Demux[128] = {
0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f
...};
uint16_t __IO AudioInVolume = DEFAULT_AUDIO_IN_VOLUME;
/* ... */
/* ... */
static uint8_t SAIx_Init(uint32_t AudioFreq);
static void SAIx_DeInit(void);
static void I2Sx_Init(uint32_t AudioFreq);
static void I2Sx_DeInit(void);
static void TIMx_IC_MspInit(TIM_HandleTypeDef *htim);
static void TIMx_IC_MspDeInit(TIM_HandleTypeDef *htim);
static void TIMx_Init(void);
static void TIMx_DeInit(void);
static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbrIn, uint32_t ChnlNbrOut);
void BSP_AUDIO_OUT_ChangeAudioConfig(uint32_t AudioOutOption);
/* ... */
/* ... */
/* ... */
uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice,
uint8_t Volume,
uint32_t AudioFreq)
{
uint8_t ret = AUDIO_OK;
SAIx_DeInit();
BSP_AUDIO_OUT_ClockConfig(&haudio_out_sai, AudioFreq, NULL);
/* ... */
haudio_out_sai.Instance = AUDIO_SAIx;
if(HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET)
{
BSP_AUDIO_OUT_MspInit(&haudio_out_sai, NULL);
}if (HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET) { ... }
if (SAIx_Init(AudioFreq) != AUDIO_OK)
{
ret = AUDIO_ERROR;
}if (SAIx_Init(AudioFreq) != AUDIO_OK) { ... }
if(ret == AUDIO_OK)
{
if (cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS) == CS43L22_ID)
{
audio_drv = &cs43l22_drv;
}if (cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS) == CS43L22_ID) { ... }
else
{
ret = AUDIO_ERROR;
}else { ... }
}if (ret == AUDIO_OK) { ... }
if(ret == AUDIO_OK)
{
if (audio_drv->Init(AUDIO_I2C_ADDRESS,
OutputDevice,
Volume,
AudioFreq) != AUDIO_OK)
{
ret = AUDIO_ERROR;
}if (audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq) != AUDIO_OK) { ... }
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) { ... }
if(ret == AUDIO_OK)
{
if (HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*) pBuffer, DMA_MAX(Size / AUDIODATA_SIZE))!= HAL_OK)
{
ret = AUDIO_ERROR;
}if (HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*) pBuffer, DMA_MAX(Size / AUDIODATA_SIZE))!= HAL_OK) { ... }
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size)
{
HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*) pData, Size);
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Pause(void)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0) { ... }
if(ret == AUDIO_OK)
{
if (HAL_SAI_DMAPause(&haudio_out_sai)!= HAL_OK)
{
ret = AUDIO_ERROR;
}if (HAL_SAI_DMAPause(&haudio_out_sai)!= HAL_OK) { ... }
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Resume(void)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0) { ... }
if(ret == AUDIO_OK)
{
if (HAL_SAI_DMAResume(&haudio_out_sai)!= HAL_OK)
{
ret = AUDIO_ERROR;
}if (HAL_SAI_DMAResume(&haudio_out_sai)!= HAL_OK) { ... }
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) { ... }
if(ret == AUDIO_OK)
{
if(Option == CODEC_PDWN_HW)
{
HAL_Delay(2);
}if (Option == CODEC_PDWN_HW) { ... }
if (HAL_SAI_DMAStop(&haudio_out_sai)!= HAL_OK)
{
ret = AUDIO_ERROR;
}if (HAL_SAI_DMAStop(&haudio_out_sai)!= HAL_OK) { ... }
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) { ... }
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0) { ... }
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
{
uint8_t ret = AUDIO_OK;
if(audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
{
ret = AUDIO_ERROR;
}if (audio_drv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0) { ... }
return ret;
}{ ... }
/* ... */
void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
{
BSP_AUDIO_OUT_ClockConfig(&haudio_out_sai, AudioFreq, NULL);
__HAL_SAI_DISABLE(&haudio_out_sai);
haudio_out_sai.Init.AudioFrequency = AudioFreq;
HAL_SAI_Init(&haudio_out_sai);
__HAL_SAI_ENABLE(&haudio_out_sai);
}{ ... }
/* ... */
void BSP_AUDIO_OUT_ChangeAudioConfig(uint32_t AudioOutOption)
{
if(AudioOutOption & BSP_AUDIO_OUT_CIRCULARMODE)
{
HAL_DMA_DeInit(haudio_out_sai.hdmatx);
haudio_out_sai.hdmatx->Init.Mode = DMA_CIRCULAR;
HAL_DMA_Init(haudio_out_sai.hdmatx);
}if (AudioOutOption & BSP_AUDIO_OUT_CIRCULARMODE) { ... }
else
{
HAL_DMA_DeInit(haudio_out_sai.hdmatx);
haudio_out_sai.hdmatx->Init.Mode = DMA_NORMAL;
HAL_DMA_Init(haudio_out_sai.hdmatx);
}else { ... }
Playback Buffer circular/normal mode
if(AudioOutOption & BSP_AUDIO_OUT_STEREOMODE)
{
__HAL_SAI_DISABLE(&haudio_out_sai);
haudio_out_sai.Init.MonoStereoMode = SAI_STEREOMODE;
HAL_SAI_Init(&haudio_out_sai);
__HAL_SAI_ENABLE(&haudio_out_sai);
}if (AudioOutOption & BSP_AUDIO_OUT_STEREOMODE) { ... }
else
{
__HAL_SAI_DISABLE(&haudio_out_sai);
haudio_out_sai.Init.MonoStereoMode = SAI_MONOMODE;
HAL_SAI_Init(&haudio_out_sai);
__HAL_SAI_ENABLE(&haudio_out_sai);
}else { ... }
}{ ... }
/* ... */
void BSP_AUDIO_OUT_SetAudioFrameSlot(uint32_t AudioFrameSlot)
{
__HAL_SAI_DISABLE(&haudio_out_sai);
haudio_out_sai.SlotInit.SlotActive = AudioFrameSlot;
HAL_SAI_Init(&haudio_out_sai);
__HAL_SAI_ENABLE(&haudio_out_sai);
}{ ... }
/* ... */
void BSP_AUDIO_OUT_DeInit(void)
{
SAIx_DeInit();
BSP_AUDIO_OUT_MspDeInit(&haudio_out_sai, NULL);
memset(&audio_drv, 0, sizeof(audio_drv));
}{ ... }
/* ... */
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
{
/* ... */
BSP_AUDIO_OUT_TransferComplete_CallBack();
}{ ... }
/* ... */
void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
{
/* ... */
BSP_AUDIO_OUT_HalfTransfer_CallBack();
}{ ... }
/* ... */
void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai)
{
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)
{
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspInit(SAI_HandleTypeDef *hsai, void *Params)
{
static DMA_HandleTypeDef hdma_sai_tx;
GPIO_InitTypeDef gpio_init_structure;
AUDIO_RESET_ENABLE();
gpio_init_structure.Pin = AUDIO_RESET_PIN;
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(AUDIO_RESET_GPIO_PORT, &gpio_init_structure);
HAL_GPIO_WritePin(AUDIO_RESET_GPIO_PORT, AUDIO_RESET_PIN, GPIO_PIN_SET);
AUDIO_SAIx_CLK_ENABLE();
AUDIO_SAIx_MCLK_ENABLE();
AUDIO_SAIx_SCK_SD_FS_ENABLE();
Put CS43L2 codec reset high
gpio_init_structure.Pin = AUDIO_SAIx_MCK_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = AUDIO_SAIx_MCLK_SCK_SD_FS_AF;
HAL_GPIO_Init(AUDIO_SAIx_MCLK_GPIO_PORT, &gpio_init_structure);
CODEC_SAI pins configuration: MCK pin
gpio_init_structure.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SCK_PIN | AUDIO_SAIx_SD_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = AUDIO_SAIx_MCLK_SCK_SD_FS_AF;
HAL_GPIO_Init(AUDIO_SAIx_SCK_SD_FS_GPIO_PORT, &gpio_init_structure);
AUDIO_SAIx_DMAx_CLK_ENABLE();
if(hsai->Instance == AUDIO_SAIx)
{
hdma_sai_tx.Init.Channel = AUDIO_SAIx_DMAx_CHANNEL;
hdma_sai_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_sai_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sai_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_sai_tx.Init.PeriphDataAlignment = AUDIO_SAIx_DMAx_PERIPH_DATA_SIZE;
hdma_sai_tx.Init.MemDataAlignment = AUDIO_SAIx_DMAx_MEM_DATA_SIZE;
hdma_sai_tx.Init.Mode = DMA_CIRCULAR;
hdma_sai_tx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_sai_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
hdma_sai_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_sai_tx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_sai_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma_sai_tx.Instance = AUDIO_SAIx_DMAx_STREAM;
__HAL_LINKDMA(hsai, hdmatx, hdma_sai_tx);
HAL_DMA_DeInit(&hdma_sai_tx);
HAL_DMA_Init(&hdma_sai_tx);
}if (hsai->Instance == AUDIO_SAIx) { ... }
HAL_NVIC_SetPriority(AUDIO_SAIx_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0);
HAL_NVIC_EnableIRQ(AUDIO_SAIx_DMAx_IRQ);
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspDeInit(SAI_HandleTypeDef *hsai, void *Params)
{
GPIO_InitTypeDef gpio_init_structure;
HAL_NVIC_DisableIRQ(AUDIO_SAIx_DMAx_IRQ);
if(hsai->Instance == AUDIO_SAIx)
{
HAL_DMA_DeInit(hsai->hdmatx);
}if (hsai->Instance == AUDIO_SAIx) { ... }
__HAL_SAI_DISABLE(hsai);
HAL_GPIO_WritePin(AUDIO_RESET_GPIO_PORT, AUDIO_RESET_PIN, GPIO_PIN_RESET);
gpio_init_structure.Pin = AUDIO_SAIx_MCK_PIN;
HAL_GPIO_DeInit(AUDIO_SAIx_MCLK_GPIO_PORT, gpio_init_structure.Pin);
gpio_init_structure.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SCK_PIN | AUDIO_SAIx_SD_PIN;
HAL_GPIO_DeInit(AUDIO_SAIx_SCK_SD_FS_GPIO_PORT, gpio_init_structure.Pin);
gpio_init_structure.Pin = AUDIO_RESET_PIN;
HAL_GPIO_DeInit(AUDIO_RESET_GPIO_PORT, gpio_init_structure.Pin);
AUDIO_SAIx_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef *hsai, uint32_t AudioFreq, void *Params)
{
RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
if((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K))
{
/* ... */
rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SAI_PLLI2S;
rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 429;
rcc_ex_clk_init_struct.PLLI2S.PLLI2SQ = 2;
rcc_ex_clk_init_struct.PLLI2SDivQ = 19;
HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
}if ((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K)) { ... }
else
{
/* ... */
rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SAI_PLLI2S;
rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 344;
rcc_ex_clk_init_struct.PLLI2S.PLLI2SQ = 7;
rcc_ex_clk_init_struct.PLLI2SDivQ = 1;
HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
}else { ... }
}{ ... }
/* ... */
/* ... */
static uint8_t SAIx_Init(uint32_t AudioFreq)
{
uint8_t ret = AUDIO_OK;
haudio_out_sai.Instance = AUDIO_SAIx;
__HAL_SAI_DISABLE(&haudio_out_sai);
/* ... */
haudio_out_sai.Init.AudioFrequency = AudioFreq;
haudio_out_sai.Init.ClockSource = SAI_CLKSOURCE_PLLI2S;
haudio_out_sai.Init.AudioMode = SAI_MODEMASTER_TX;
haudio_out_sai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
haudio_out_sai.Init.Protocol = SAI_FREE_PROTOCOL;
haudio_out_sai.Init.DataSize = SAI_DATASIZE_16;
haudio_out_sai.Init.FirstBit = SAI_FIRSTBIT_MSB;
haudio_out_sai.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
haudio_out_sai.Init.Synchro = SAI_ASYNCHRONOUS;
haudio_out_sai.Init.OutputDrive = SAI_OUTPUTDRIVE_ENABLE;
haudio_out_sai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF;
/* ... */
/* ... */
haudio_out_sai.FrameInit.FrameLength = 64;
haudio_out_sai.FrameInit.ActiveFrameLength = 32;
haudio_out_sai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
haudio_out_sai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
haudio_out_sai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
/* ... */
haudio_out_sai.SlotInit.FirstBitOffset = 0;
haudio_out_sai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
haudio_out_sai.SlotInit.SlotNumber = 4;
haudio_out_sai.SlotInit.SlotActive = CODEC_AUDIOFRAME_SLOT_0123;
if (HAL_SAI_Init(&haudio_out_sai) != HAL_OK)
{
ret = AUDIO_ERROR;
}if (HAL_SAI_Init(&haudio_out_sai) != HAL_OK) { ... }
__HAL_SAI_ENABLE(&haudio_out_sai);
return ret;
}{ ... }
/* ... */
static void SAIx_DeInit(void)
{
haudio_out_sai.Instance = AUDIO_SAIx;
__HAL_SAI_DISABLE(&haudio_out_sai);
HAL_SAI_DeInit(&haudio_out_sai);
}{ ... }
/* ... */
/* ... */
/* ... */
uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
{
I2Sx_DeInit();
BSP_AUDIO_IN_ClockConfig(&haudio_in_i2s, NULL);
PDMDecoder_Init(AudioFreq, ChnlNbr, ChnlNbr);
haudio_in_i2s.Instance = AUDIO_I2Sx;
if(HAL_I2S_GetState(&haudio_in_i2s) == HAL_I2S_STATE_RESET)
{
BSP_AUDIO_IN_MspInit(&haudio_in_i2s, NULL);
}if (HAL_I2S_GetState(&haudio_in_i2s) == HAL_I2S_STATE_RESET) { ... }
I2Sx_Init(AudioFreq);
return AUDIO_OK;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size)
{
uint32_t ret = AUDIO_ERROR;
HAL_I2S_Receive_DMA(&haudio_in_i2s, pbuf, size);
ret = AUDIO_OK;
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Stop(void)
{
uint32_t ret = AUDIO_ERROR;
HAL_I2S_DMAPause(&haudio_in_i2s);
AUDIO_TIMx_CLK_DISABLE();
ret = AUDIO_OK;
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Pause(void)
{
HAL_I2S_DMAPause(&haudio_in_i2s);
return AUDIO_OK;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Resume(void)
{
HAL_I2S_DMAResume(&haudio_in_i2s);
return AUDIO_OK;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume)
{
AudioInVolume = Volume;
return AUDIO_OK;
}{ ... }
/* ... */
void BSP_AUDIO_IN_DeInit(void)
{
I2Sx_DeInit();
BSP_AUDIO_IN_MspDeInit(&haudio_in_i2s, NULL);
TIMx_DeInit();
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t* PDMBuf, uint16_t* PCMBuf)
{
uint8_t app_pdm[INTERNAL_BUFF_SIZE*2];
uint8_t byte1 = 0, byte2 = 0;
uint32_t index = 0;
for(index = 0; index<INTERNAL_BUFF_SIZE/2; index++)
{
byte2 = (PDMBuf[index] >> 8)& 0xFF;
byte1 = (PDMBuf[index] & 0xFF);
app_pdm[(index*2)+1] = Channel_Demux[byte1 & CHANNEL_DEMUX_MASK] | Channel_Demux[byte2 & CHANNEL_DEMUX_MASK] << 4;
app_pdm[(index*2)] = Channel_Demux[(byte1 >> 1) & CHANNEL_DEMUX_MASK] | Channel_Demux[(byte2 >> 1) & CHANNEL_DEMUX_MASK] << 4;
}for (index = 0; index
for(index = 0; index < DEFAULT_AUDIO_IN_CHANNEL_NBR; index++)
{
PDM_Filter((uint8_t*)&app_pdm[index], (uint16_t*)&(PCMBuf[index]), &PDM_FilterHandler[index]);
}for (index = 0; index < DEFAULT_AUDIO_IN_CHANNEL_NBR; index++) { ... }
return AUDIO_OK;
}{ ... }
/* ... */
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
{
BSP_AUDIO_IN_TransferComplete_CallBack();
}{ ... }
/* ... */
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
/* ... */
BSP_AUDIO_IN_HalfTransfer_CallBack();
}{ ... }
/* ... */
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
{
/* ... */
BSP_AUDIO_IN_Error_Callback();
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_ClockConfig(I2S_HandleTypeDef *hi2s, void *Params)
{
RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct;
HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct);
RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = 384;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SR = 2;
HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_TransferComplete_CallBack(void)
{
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
{
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_Error_Callback(void)
{
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
{
static DMA_HandleTypeDef hdma_i2s_rx;
GPIO_InitTypeDef gpio_init_structure;
TIMx_Init();
AUDIO_I2Sx_CLK_ENABLE();
AUDIO_I2Sx_SD_GPIO_CLK_ENABLE();
AUDIO_I2Sx_SCK_GPIO_CLK_ENABLE();
gpio_init_structure.Pin = AUDIO_I2Sx_SCK_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FAST;
gpio_init_structure.Alternate = AUDIO_I2Sx_SCK_AF;
HAL_GPIO_Init(AUDIO_I2Sx_SCK_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = AUDIO_I2Sx_SD_PIN;
gpio_init_structure.Alternate = AUDIO_I2Sx_SD_AF;
HAL_GPIO_Init(AUDIO_I2Sx_SD_GPIO_PORT, &gpio_init_structure);
/* ... */
AUDIO_I2Sx_DMAx_CLK_ENABLE();
if(hi2s->Instance == AUDIO_I2Sx)
{
hdma_i2s_rx.Init.Channel = AUDIO_I2Sx_DMAx_CHANNEL;
hdma_i2s_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2s_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2s_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2s_rx.Init.PeriphDataAlignment = AUDIO_I2Sx_DMAx_PERIPH_DATA_SIZE;
hdma_i2s_rx.Init.MemDataAlignment = AUDIO_I2Sx_DMAx_MEM_DATA_SIZE;
hdma_i2s_rx.Init.Mode = DMA_CIRCULAR;
hdma_i2s_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_i2s_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_i2s_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_i2s_rx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_i2s_rx.Init.PeriphBurst = DMA_MBURST_SINGLE;
hdma_i2s_rx.Instance = AUDIO_I2Sx_DMAx_STREAM;
__HAL_LINKDMA(hi2s, hdmarx, hdma_i2s_rx);
HAL_DMA_DeInit(&hdma_i2s_rx);
HAL_DMA_Init(&hdma_i2s_rx);
}if (hi2s->Instance == AUDIO_I2Sx) { ... }
HAL_NVIC_SetPriority(AUDIO_I2Sx_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0);
HAL_NVIC_EnableIRQ(AUDIO_I2Sx_DMAx_IRQ);
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
{
GPIO_InitTypeDef gpio_init_structure;
HAL_NVIC_DisableIRQ(AUDIO_I2Sx_DMAx_IRQ);
if(hi2s->Instance == AUDIO_I2Sx)
{
HAL_DMA_DeInit(hi2s->hdmarx);
}if (hi2s->Instance == AUDIO_I2Sx) { ... }
__HAL_I2S_DISABLE(hi2s);
gpio_init_structure.Pin = AUDIO_I2Sx_SCK_PIN;
HAL_GPIO_DeInit(AUDIO_I2Sx_SCK_GPIO_PORT, gpio_init_structure.Pin);
gpio_init_structure.Pin = AUDIO_I2Sx_SD_PIN;
HAL_GPIO_DeInit(AUDIO_I2Sx_SD_GPIO_PORT, gpio_init_structure.Pin);
AUDIO_I2Sx_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
/* ... */
static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbrIn, uint32_t ChnlNbrOut)
{
uint32_t index = 0;
__HAL_RCC_CRC_CLK_ENABLE();
for(index = 0; index < ChnlNbrIn; index++)
{
PDM_FilterHandler[index].bit_order = PDM_FILTER_BIT_ORDER_LSB;
PDM_FilterHandler[index].endianness = PDM_FILTER_ENDIANNESS_LE;
PDM_FilterHandler[index].high_pass_tap = 2122358088;
PDM_FilterHandler[index].out_ptr_channels = ChnlNbrOut;
PDM_FilterHandler[index].in_ptr_channels = ChnlNbrIn;
PDM_Filter_Init((PDM_Filter_Handler_t *)(&PDM_FilterHandler[index]));
PDM_FilterConfig[index].output_samples_number = AudioFreq/1000;
PDM_FilterConfig[index].mic_gain = 24;
PDM_FilterConfig[index].decimation_factor = PDM_FILTER_DEC_FACTOR_64;
PDM_Filter_setConfig((PDM_Filter_Handler_t *)&PDM_FilterHandler[index], &PDM_FilterConfig[index]);
}for (index = 0; index < ChnlNbrIn; index++) { ... }
}{ ... }
/* ... */
static void I2Sx_Init(uint32_t AudioFreq)
{
haudio_in_i2s.Instance = AUDIO_I2Sx;
__HAL_I2S_DISABLE(&haudio_in_i2s);
haudio_in_i2s.Init.AudioFreq = 4 * AudioFreq;
haudio_in_i2s.Init.ClockSource = I2S_CLOCK_PLL;
haudio_in_i2s.Init.CPOL = I2S_CPOL_LOW;
haudio_in_i2s.Init.DataFormat = I2S_DATAFORMAT_16B;
haudio_in_i2s.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
haudio_in_i2s.Init.Mode = I2S_MODE_MASTER_RX;
haudio_in_i2s.Init.Standard = I2S_STANDARD_LSB;
HAL_I2S_Init(&haudio_in_i2s);
__HAL_I2S_ENABLE(&haudio_in_i2s);
}{ ... }
/* ... */
static void I2Sx_DeInit(void)
{
haudio_in_i2s.Instance = AUDIO_I2Sx;
__HAL_I2S_DISABLE(&haudio_in_i2s);
HAL_I2S_DeInit(&haudio_in_i2s);
}{ ... }
/* ... */
static void TIMx_IC_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef gpio_init_structure;
AUDIO_TIMx_CLK_ENABLE();
AUDIO_TIMx_GPIO_CLK_ENABLE();
Enable peripherals and GPIO Clocks
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Alternate = AUDIO_TIMx_AF;
gpio_init_structure.Pin = AUDIO_TIMx_IN_GPIO_PIN;
HAL_GPIO_Init(AUDIO_TIMx_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = AUDIO_TIMx_OUT_GPIO_PIN;
HAL_GPIO_Init(AUDIO_TIMx_GPIO_PORT, &gpio_init_structure);
}{ ... }
/* ... */
static void TIMx_IC_MspDeInit(TIM_HandleTypeDef *htim)
{
AUDIO_TIMx_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
static void TIMx_Init(void)
{
TIM_IC_InitTypeDef s_ic_config;
TIM_OC_InitTypeDef s_oc_config;
TIM_ClockConfigTypeDef s_clk_source_config;
TIM_SlaveConfigTypeDef s_slave_config;
haudio_tim.Instance = AUDIO_TIMx;
/* ... */
haudio_tim.Init.Period = 1;
haudio_tim.Init.Prescaler = 0;
haudio_tim.Init.ClockDivision = 0;
haudio_tim.Init.CounterMode = TIM_COUNTERMODE_UP;
TIMx_IC_MspInit(&haudio_tim);
HAL_TIM_IC_Init(&haudio_tim);
Configure the TIM peripheral
s_ic_config.ICPolarity = TIM_ICPOLARITY_FALLING;
s_ic_config.ICSelection = TIM_ICSELECTION_DIRECTTI;
s_ic_config.ICPrescaler = TIM_ICPSC_DIV1;
s_ic_config.ICFilter = 0;
HAL_TIM_IC_ConfigChannel(&haudio_tim, &s_ic_config, AUDIO_TIMx_IN_CHANNEL);
s_clk_source_config.ClockSource = TIM_CLOCKSOURCE_ETRMODE1;
s_clk_source_config.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
s_clk_source_config.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
s_clk_source_config.ClockFilter = 0;
HAL_TIM_ConfigClockSource(&haudio_tim, &s_clk_source_config);
s_slave_config.InputTrigger = TIM_TS_TI1FP1;
s_slave_config.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
s_slave_config.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
s_slave_config.TriggerPrescaler = TIM_CLOCKPRESCALER_DIV1;
s_slave_config.TriggerFilter = 0;
HAL_TIM_SlaveConfigSynchronization(&haudio_tim, &s_slave_config);
s_oc_config.OCMode = TIM_OCMODE_PWM1;
s_oc_config.OCIdleState = TIM_OCIDLESTATE_SET;
s_oc_config.Pulse = 1;
s_oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
s_oc_config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
s_oc_config.OCFastMode = TIM_OCFAST_DISABLE;
s_oc_config.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&haudio_tim, &s_oc_config, AUDIO_TIMx_OUT_CHANNEL);
HAL_TIM_PWM_Start(&haudio_tim, AUDIO_TIMx_OUT_CHANNEL);
HAL_TIM_IC_Start(&haudio_tim, AUDIO_TIMx_IN_CHANNEL);
}{ ... }
/* ... */
static void TIMx_DeInit(void)
{
haudio_tim.Instance = AUDIO_TIMx;
HAL_TIM_PWM_Stop(&haudio_tim, AUDIO_TIMx_OUT_CHANNEL);
HAL_TIM_IC_Stop(&haudio_tim, AUDIO_TIMx_IN_CHANNEL);
HAL_TIM_IC_DeInit(&haudio_tim);
TIMx_IC_MspDeInit(&haudio_tim);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */