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
109
112
113
116
119
120
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
165
166
169
170
171
172
173
174
175
176
177
178
181
182
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
207
208
213
214
215
216
217
218
224
228
229
236
237
238
239
240
246
261
262
267
271
272
273
274
275
276
277
278
279
280
281
282
283
287
288
289
290
291
292
293
294
295
296
297
304
305
306
307
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
340
350
351
352
358
371
372
378
391
392
399
412
413
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
442
443
444
445
446
447
448
449
450
451
452
453
454
455
458
464
465
469
475
476
480
486
487
491
495
496
499
502
503
506
509
510
513
516
517
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
592
593
594
595
596
597
598
599
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
646
647
648
649
650
651
652
653
654
655
656
657
661
662
663
664
665
666
667
668
669
670
671
672
675
676
682
683
684
685
686
687
688
689
690
693
694
695
696
697
698
699
700
701
702
703
704
705
711
712
713
714
715
716
717
722
723
724
725
726
727
728
729
730
731
732
733
736
737
738
739
740
741
742
743
744
745
746
747
750
751
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
782
783
784
785
786
787
788
794
795
796
797
798
799
800
801
802
803
804
805
806
807
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
831
832
833
834
835
836
837
838
839
840
844
845
846
847
848
849
850
851
852
853
859
860
861
862
863
864
865
866
867
868
871
878
879
885
886
887
888
889
890
891
892
899
900
905
906
907
908
909
910
914
919
920
924
930
931
935
941
942
945
951
952
955
961
962
965
970
971
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
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
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
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
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
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
1215
1216
1217
1218
1219
1220
1222
1223
1224
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1245
1246
1247
1248
1249
1250
1251
1252
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
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1318
1319
1322
1323
1326
1327
/* ... */
/* ... */
#include "stm32446e_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;
uint32_t deviceid = 0x00;
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);
deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS);
if((deviceid) == WM8994_ID)
{
audio_drv = &wm8994_drv;
ret = AUDIO_OK;
}if ((deviceid) == WM8994_ID) { ... }
else
{
ret = AUDIO_ERROR;
}else { ... }
if(ret == AUDIO_OK)
{
audio_drv->Reset(AUDIO_I2C_ADDRESS);
audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq);
}if (ret == AUDIO_OK) { ... }
return ret;
}{ ... }
/* ... */
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);
}{ ... }
/* ... */
void BSP_AUDIO_OUT_DeInit(void)
{
SAIx_DeInit();
BSP_AUDIO_OUT_MspDeInit(&haudio_out_sai, NULL);
}{ ... }
/* ... */
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_SAIx_CLK_ENABLE();
AUDIO_SAIx_MCLK_SCK_ENABLE();
AUDIO_SAIx_SD_FS_ENABLE();
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_SD_FS_AF;
gpio_init_structure.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SD_PIN;
HAL_GPIO_Init(AUDIO_SAIx_SD_FS_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Alternate = AUDIO_SAIx_MCLK_SD_FS_AF;
gpio_init_structure.Pin = AUDIO_SAIx_MCK_PIN;
HAL_GPIO_Init(AUDIO_SAIx_MCLK_SCK_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Alternate = AUDIO_SAIx_SCK_AF;
gpio_init_structure.Pin = AUDIO_SAIx_SCK_PIN ;
HAL_GPIO_Init(AUDIO_SAIx_MCLK_SCK_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);
gpio_init_structure.Pin = AUDIO_SAIx_FS_PIN | AUDIO_SAIx_SD_PIN;
HAL_GPIO_DeInit(AUDIO_SAIx_SD_FS_GPIO_PORT, gpio_init_structure.Pin);
gpio_init_structure.Pin = AUDIO_SAIx_MCK_PIN;
HAL_GPIO_DeInit(AUDIO_SAIx_MCLK_SCK_GPIO_PORT, gpio_init_structure.Pin);
gpio_init_structure.Pin = AUDIO_SAIx_SCK_PIN ;
HAL_GPIO_DeInit(AUDIO_SAIx_MCLK_SCK_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_SAI2;
rcc_ex_clk_init_struct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLSAI;
rcc_ex_clk_init_struct.PLLSAI.PLLSAIM = 8;
rcc_ex_clk_init_struct.PLLSAI.PLLSAIN = 429;
rcc_ex_clk_init_struct.PLLSAI.PLLSAIQ = 2;
rcc_ex_clk_init_struct.PLLSAIDivQ = 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_SAI2;
rcc_ex_clk_init_struct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLSAI;
rcc_ex_clk_init_struct.PLLSAI.PLLSAIM = 8;
rcc_ex_clk_init_struct.PLLSAI.PLLSAIN = 344;
rcc_ex_clk_init_struct.PLLSAI.PLLSAIQ = 7;
rcc_ex_clk_init_struct.PLLSAIDivQ = 1;
HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
}else { ... }
}{ ... }
/* ... */
/* ... */
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_PLLSAI;
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_RISINGEDGE;
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;
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;
}{ ... }
/* ... */
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_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;
static DMA_HandleTypeDef hdma_i2s_rx;
HAL_NVIC_DisableIRQ(AUDIO_I2Sx_DMAx_IRQ);
if(hi2s->Instance == AUDIO_I2Sx)
{
HAL_DMA_DeInit(&hdma_i2s_rx);
}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_ClockConfig(I2S_HandleTypeDef *hi2s, void *Params)
{
RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_I2S_APB1;
rcc_ex_clk_init_struct.PLLI2S.PLLI2SN = 384;
rcc_ex_clk_init_struct.PLLI2S.PLLI2SR = 2;
HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
}{ ... }
/* ... */
/* ... */
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);
__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);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */