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
82
83
86
87
92
93
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
129
130
133
134
135
136
137
138
139
140
141
142
145
146
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
171
172
177
178
179
180
186
190
191
196
197
198
199
200
204
210
211
217
232
233
238
242
243
244
245
246
247
248
249
250
251
252
253
254
258
259
260
261
262
263
264
265
266
267
268
275
276
277
278
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
311
321
322
323
329
342
343
349
362
363
370
383
384
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
448
449
450
451
452
453
454
455
456
460
461
462
463
464
465
466
467
468
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
534
535
536
537
538
539
540
541
546
547
548
549
550
551
552
553
554
555
556
557
559
560
561
565
571
572
576
582
583
587
591
592
595
598
599
602
605
606
609
612
613
616
617
623
624
625
626
627
628
629
630
631
634
635
636
637
638
639
640
641
642
643
644
645
646
652
653
654
655
656
657
658
663
664
665
666
667
668
669
670
671
672
673
674
677
678
679
680
681
682
683
684
685
686
687
688
691
692
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
723
724
725
726
727
728
729
730
731
734
735
736
737
738
739
740
741
742
743
744
750
751
752
753
754
755
756
757
758
759
760
761
762
763
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
787
788
789
790
791
792
793
794
795
796
800
801
802
803
804
805
806
807
808
809
815
816
817
818
819
820
821
822
823
824
830
831
832
833
834
835
836
837
844
845
850
851
852
853
854
855
859
864
865
869
875
876
880
886
887
894
895
896
897
898
899
900
901
902
903
904
905
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
978
979
980
981
982
983
984
985
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1005
1006
1007
1010
1016
1017
1020
1026
1027
1030
1035
1036
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1150
1151
1152
1153
1154
1155
1157
1158
1159
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1253
1254
1257
1258
1261
1262
1265
1266
/* ... */
/* ... */
#include "stm324x9i_eval_audio.h"
/* ... */
/* ... */
/* ... */
/* ... */
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 void 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);
/* ... */
/* ... */
/* ... */
uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
{
uint8_t ret = AUDIO_ERROR;
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) { ... }
SAIx_Init(AudioFreq);
if((wm8994_drv.ReadID(AUDIO_I2C_ADDRESS)) == WM8994_ID)
{
audio_drv = &wm8994_drv;
ret = AUDIO_OK;
}if ((wm8994_drv.ReadID(AUDIO_I2C_ADDRESS)) == WM8994_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)
{
SAIx_DeInit();
BSP_AUDIO_OUT_MspDeInit(&haudio_out_sai, 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_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*)pBuffer, DMA_MAX(Size / AUDIODATA_SIZE));
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
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)
{
if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}if (audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0) { ... }
else
{
HAL_SAI_DMAPause(&haudio_out_sai);
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_SAI_DMAResume(&haudio_out_sai);
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
{
HAL_SAI_DMAStop(&haudio_out_sai);
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);
}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_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_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);
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef *hsai, uint32_t AudioFreq, void *Params)
{
RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct;
HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct);
if((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K))
{
/* ... */
RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI_PLLI2S;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = 429;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SQ = 2;
RCC_ExCLKInitStruct.PLLI2SDivQ = 19;
HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
}if ((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K)) { ... }
else
{
/* ... */
RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI_PLLI2S;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SN = 344;
RCC_ExCLKInitStruct.PLLI2S.PLLI2SQ = 7;
RCC_ExCLKInitStruct.PLLI2SDivQ = 1;
HAL_RCCEx_PeriphCLKConfig(&RCC_ExCLKInitStruct);
}else { ... }
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspInit(SAI_HandleTypeDef *hsai, void *Params)
{
static DMA_HandleTypeDef hdma_saiTx;
GPIO_InitTypeDef GPIO_InitStruct;
AUDIO_SAIx_CLK_ENABLE();
AUDIO_SAIx_MCLK_SCK_SD_FS_ENABLE();
GPIO_InitStruct.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SCK_PIN | AUDIO_SAIx_SD_PIN | AUDIO_SAIx_MCK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = AUDIO_SAIx_MCLK_SCK_SD_FS_AF;
HAL_GPIO_Init(AUDIO_SAIx_MCLK_SCK_SD_FS_GPIO_PORT, &GPIO_InitStruct);
AUDIO_SAIx_DMAx_CLK_ENABLE();
if(hsai->Instance == AUDIO_SAIx)
{
hdma_saiTx.Init.Channel = AUDIO_SAIx_DMAx_CHANNEL;
hdma_saiTx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_saiTx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_saiTx.Init.MemInc = DMA_MINC_ENABLE;
hdma_saiTx.Init.PeriphDataAlignment = AUDIO_SAIx_DMAx_PERIPH_DATA_SIZE;
hdma_saiTx.Init.MemDataAlignment = AUDIO_SAIx_DMAx_MEM_DATA_SIZE;
hdma_saiTx.Init.Mode = DMA_NORMAL;
hdma_saiTx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_saiTx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
hdma_saiTx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_saiTx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_saiTx.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma_saiTx.Instance = AUDIO_SAIx_DMAx_STREAM;
__HAL_LINKDMA(hsai, hdmatx, hdma_saiTx);
HAL_DMA_DeInit(&hdma_saiTx);
HAL_DMA_Init(&hdma_saiTx);
}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);
gpio_init_structure.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SCK_PIN | AUDIO_SAIx_SD_PIN | AUDIO_SAIx_MCK_PIN;
HAL_GPIO_DeInit(AUDIO_SAIx_MCLK_SCK_SD_FS_GPIO_PORT, gpio_init_structure.Pin);
AUDIO_SAIx_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
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)
{
}{ ... }
/* ... */
/* ... */
static void SAIx_Init(uint32_t AudioFreq)
{
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_ENABLED;
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_ENABLED;
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;
HAL_SAI_Init(&haudio_out_sai);
__HAL_SAI_ENABLE(&haudio_out_sai);
}{ ... }
/* ... */
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;
}{ ... }
/* ... */
void BSP_AUDIO_IN_DeInit(void)
{
I2Sx_DeInit();
BSP_AUDIO_IN_MspDeInit(&haudio_in_i2s, NULL);
TIMx_DeInit();
}{ ... }
/* ... */
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;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t* PDMBuf, uint16_t* PCMBuf)
{
uint8_t AppPDM[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);
AppPDM[(index*2)+1] = Channel_Demux[byte1 & CHANNEL_DEMUX_MASK] | Channel_Demux[byte2 & CHANNEL_DEMUX_MASK] << 4;
AppPDM[(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*)&AppPDM[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_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
{
static DMA_HandleTypeDef hdma_i2sRx;
GPIO_InitTypeDef GPIO_InitStruct;
TIMx_Init();
AUDIO_I2Sx_CLK_ENABLE();
AUDIO_I2Sx_SD_GPIO_CLK_ENABLE();
AUDIO_I2Sx_SCK_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = AUDIO_I2Sx_SCK_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_AF;
HAL_GPIO_Init(AUDIO_I2Sx_SCK_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = AUDIO_I2Sx_SD_PIN;
GPIO_InitStruct.Alternate = AUDIO_I2Sx_SD_AF;
HAL_GPIO_Init(AUDIO_I2Sx_SD_GPIO_PORT, &GPIO_InitStruct);
AUDIO_I2Sx_DMAx_CLK_ENABLE();
if(hi2s->Instance == AUDIO_I2Sx)
{
hdma_i2sRx.Init.Channel = AUDIO_I2Sx_DMAx_CHANNEL;
hdma_i2sRx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2sRx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2sRx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2sRx.Init.PeriphDataAlignment = AUDIO_I2Sx_DMAx_PERIPH_DATA_SIZE;
hdma_i2sRx.Init.MemDataAlignment = AUDIO_I2Sx_DMAx_MEM_DATA_SIZE;
hdma_i2sRx.Init.Mode = DMA_CIRCULAR;
hdma_i2sRx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_i2sRx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_i2sRx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_i2sRx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_i2sRx.Init.PeriphBurst = DMA_MBURST_SINGLE;
hdma_i2sRx.Instance = AUDIO_I2Sx_DMAx_STREAM;
__HAL_LINKDMA(hi2s, hdmarx, hdma_i2sRx);
HAL_DMA_DeInit(&hdma_i2sRx);
HAL_DMA_Init(&hdma_i2sRx);
}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();
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_TransferComplete_CallBack(void)
{
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
{
/* ... */
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_Error_Callback(void)
{
/* ... */
}{ ... }
/* ... */
/* ... */
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_HIGH;
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);
}{ ... }
/* ... */
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_InitStruct;
AUDIO_TIMx_CLK_ENABLE();
AUDIO_TIMx_GPIO_CLK_ENABLE();
Enable peripherals and GPIO Clocks
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = AUDIO_TIMx_AF;
GPIO_InitStruct.Pin = AUDIO_TIMx_IN_GPIO_PIN;
HAL_GPIO_Init(AUDIO_TIMx_GPIO, &GPIO_InitStruct);
GPIO_InitStruct.Pin = AUDIO_TIMx_OUT_GPIO_PIN;
HAL_GPIO_Init(AUDIO_TIMx_GPIO, &GPIO_InitStruct);
}{ ... }
/* ... */
static void TIMx_IC_MspDeInit(TIM_HandleTypeDef *htim)
{
AUDIO_TIMx_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
static void TIMx_Init(void)
{
TIM_IC_InitTypeDef sICConfig;
TIM_OC_InitTypeDef sOCConfig;
TIM_ClockConfigTypeDef sCLKSourceConfig;
TIM_SlaveConfigTypeDef sSlaveConfig;
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
sICConfig.ICPolarity = TIM_ICPOLARITY_FALLING;
sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
sICConfig.ICPrescaler = TIM_ICPSC_DIV1;
sICConfig.ICFilter = 0;
HAL_TIM_IC_ConfigChannel(&haudio_tim, &sICConfig, AUDIO_TIMx_IN_CHANNEL);
sCLKSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE1;
sCLKSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
sCLKSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
sCLKSourceConfig.ClockFilter = 0;
HAL_TIM_ConfigClockSource(&haudio_tim, &sCLKSourceConfig);
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
sSlaveConfig.TriggerPrescaler = TIM_CLOCKPRESCALER_DIV1;
sSlaveConfig.TriggerFilter = 0;
HAL_TIM_SlaveConfigSynchronization(&haudio_tim, &sSlaveConfig);
sOCConfig.OCMode = TIM_OCMODE_PWM1;
sOCConfig.OCIdleState = TIM_OCIDLESTATE_SET;
sOCConfig.Pulse = 1;
sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sOCConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sOCConfig.OCFastMode = TIM_OCFAST_DISABLE;
sOCConfig.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&haudio_tim, &sOCConfig, 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);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */