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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
120
121
124
125
130
131
134
137
138
141
142
143
144
145
148
149
152
155
156
159
160
161
162
163
164
165
166
167
168
169
170
171
174
175
178
179
180
181
184
185
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
206
207
212
213
215
216
220
221
234
235
239
240
241
242
243
249
250
251
252
256
257
258
259
260
261
262
263
264
265
266
271
275
276
277
278
279
280
281
282
283
284
285
286
287
291
292
293
294
295
296
297
298
299
300
301
308
309
310
311
315
316
317
318
319
320
321
322
323
324
325
332
333
334
335
336
337
338
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
364
377
378
384
397
398
399
400
401
402
403
404
405
406
419
420
426
427
428
429
430
431
432
433
434
435
439
447
448
452
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
482
483
484
485
486
487
490
491
492
493
494
495
496
497
500
501
502
503
504
505
506
507
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
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
582
583
584
585
586
587
588
589
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
615
616
617
620
623
624
627
630
631
634
637
638
641
642
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
667
671
672
673
676
677
680
681
688
689
690
691
692
693
694
695
697
698
699
700
701
706
707
708
709
710
711
712
713
714
720
721
722
723
724
725
726
727
728
729
730
731
732
733
737
738
739
740
741
742
743
744
745
746
747
748
749
750
754
755
756
757
758
759
760
761
762
763
767
768
769
770
771
772
773
774
775
776
782
783
784
785
786
787
788
789
790
791
797
798
799
800
801
802
803
807
808
813
814
818
819
820
821
822
823
827
832
833
837
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
886
887
888
889
890
891
892
893
894
895
896
897
898
899
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
952
953
954
955
956
957
958
959
964
965
966
967
968
969
970
971
972
973
974
975
976
977
979
980
981
984
990
991
994
1000
1001
1004
1009
1010
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
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
1075
1079
1080
1081
1084
1085
1088
1089
1093
1106
1107
1110
1111
1114
1115
1118
1119
/* ... */
/* ... */
#include "stm32f4_discovery_audio.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};
/* ... */
/* ... */
/* ... */
/* ... */
static AUDIO_DrvTypeDef *pAudioDrv;
I2S_HandleTypeDef hAudioOutI2s;
I2S_HandleTypeDef hAudioInI2s;
PDM_Filter_Handler_t PDM_FilterHandler[2];
PDM_Filter_Config_t PDM_FilterConfig[2];
__IO uint16_t AudioInVolume = DEFAULT_AUDIO_IN_VOLUME;
/* ... */
/* ... */
static uint8_t I2S3_Init(uint32_t AudioFreq);
static uint8_t I2S2_Init(uint32_t AudioFreq);
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_OK;
BSP_AUDIO_OUT_ClockConfig(&hAudioOutI2s, AudioFreq, NULL);
/* ... */
hAudioOutI2s.Instance = I2S3;
if(HAL_I2S_GetState(&hAudioOutI2s) == HAL_I2S_STATE_RESET)
{
BSP_AUDIO_OUT_MspInit(&hAudioOutI2s, NULL);
}if (HAL_I2S_GetState(&hAudioOutI2s) == HAL_I2S_STATE_RESET) { ... }
/* ... */
if(I2S3_Init(AudioFreq) != AUDIO_OK)
{
ret = AUDIO_ERROR;
}if (I2S3_Init(AudioFreq) != AUDIO_OK) { ... }
if(ret == AUDIO_OK)
{
if(((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID)
{
pAudioDrv = &cs43l22_drv;
}if (((cs43l22_drv.ReadID(AUDIO_I2C_ADDRESS)) & CS43L22_ID_MASK) == CS43L22_ID) { ... }
else
{
ret = AUDIO_ERROR;
}else { ... }
}if (ret == AUDIO_OK) { ... }
if(ret == AUDIO_OK)
{
pAudioDrv->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(pAudioDrv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) { ... }
else
{
HAL_I2S_Transmit_DMA(&hAudioOutI2s, 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(&hAudioOutI2s, pData, Size);
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Pause(void)
{
if(pAudioDrv->Pause(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->Pause(AUDIO_I2C_ADDRESS) != 0) { ... }
else
{
HAL_I2S_DMAPause(&hAudioOutI2s);
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Resume(void)
{
if(pAudioDrv->Resume(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->Resume(AUDIO_I2C_ADDRESS) != 0) { ... }
else
{
HAL_I2S_DMAResume(&hAudioOutI2s);
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
{
HAL_I2S_DMAStop(&hAudioOutI2s);
if(pAudioDrv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->Stop(AUDIO_I2C_ADDRESS, Option) != 0) { ... }
else
{
if(Option == CODEC_PDWN_HW)
{
HAL_Delay(1);
HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET);
}if (Option == CODEC_PDWN_HW) { ... }
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
{
if(pAudioDrv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
{
if(pAudioDrv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
{
if(pAudioDrv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
{
return AUDIO_ERROR;
}if (pAudioDrv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
{
BSP_AUDIO_OUT_ClockConfig(&hAudioOutI2s, AudioFreq, NULL);
I2S3_Init(AudioFreq);
}{ ... }
/* ... */
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
{
if(hi2s->Instance == I2S3)
{
BSP_AUDIO_OUT_TransferComplete_CallBack();
}if (hi2s->Instance == I2S3) { ... }
}{ ... }
/* ... */
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
if(hi2s->Instance == I2S3)
{
/* ... */
BSP_AUDIO_OUT_HalfTransfer_CallBack();
}if (hi2s->Instance == I2S3) { ... }
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
{
RCC_PeriphCLKInitTypeDef rccclkinit;
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(&rccclkinit);
if ((freqindex & 0x7) == 0)
{
/* ... */
rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
rccclkinit.PLLI2S.PLLI2SN = I2SPLLN[freqindex];
rccclkinit.PLLI2S.PLLI2SR = I2SPLLR[freqindex];
HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
}if ((freqindex & 0x7) == 0) { ... }
else
{
/* ... */
rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
rccclkinit.PLLI2S.PLLI2SN = 258;
rccclkinit.PLLI2S.PLLI2SR = 3;
HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
}else { ... }
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
{
static DMA_HandleTypeDef hdma_i2sTx;
GPIO_InitTypeDef GPIO_InitStruct;
I2S3_CLK_ENABLE();
I2S3_SCK_SD_CLK_ENABLE();
I2S3_WS_CLK_ENABLE();
GPIO_InitStruct.Pin = I2S3_SCK_PIN | I2S3_SD_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = I2S3_SCK_SD_WS_AF;
HAL_GPIO_Init(I2S3_SCK_SD_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = I2S3_WS_PIN ;
HAL_GPIO_Init(I2S3_WS_GPIO_PORT, &GPIO_InitStruct);
I2S3_MCK_CLK_ENABLE();
GPIO_InitStruct.Pin = I2S3_MCK_PIN;
HAL_GPIO_Init(I2S3_MCK_GPIO_PORT, &GPIO_InitStruct);
I2S3_DMAx_CLK_ENABLE();
if(hi2s->Instance == I2S3)
{
hdma_i2sTx.Init.Channel = I2S3_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 = I2S3_DMAx_PERIPH_DATA_SIZE;
hdma_i2sTx.Init.MemDataAlignment = I2S3_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 = I2S3_DMAx_STREAM;
__HAL_LINKDMA(hi2s, hdmatx, hdma_i2sTx);
HAL_DMA_DeInit(&hdma_i2sTx);
HAL_DMA_Init(&hdma_i2sTx);
}if (hi2s->Instance == I2S3) { ... }
HAL_NVIC_SetPriority(I2S3_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0);
HAL_NVIC_EnableIRQ(I2S3_DMAx_IRQ);
}{ ... }
/* ... */
__weak void BSP_AUDIO_OUT_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
{
GPIO_InitTypeDef GPIO_InitStruct;
HAL_NVIC_DisableIRQ(I2S3_DMAx_IRQ);
if(hi2s->Instance == I2S3)
{
HAL_DMA_DeInit(hi2s->hdmatx);
}if (hi2s->Instance == I2S3) { ... }
__HAL_I2S_DISABLE(hi2s);
GPIO_InitStruct.Pin = I2S3_SCK_PIN | I2S3_SD_PIN;
HAL_GPIO_DeInit(I2S3_SCK_SD_GPIO_PORT, GPIO_InitStruct.Pin);
GPIO_InitStruct.Pin = I2S3_WS_PIN;
HAL_GPIO_DeInit(I2S3_WS_GPIO_PORT, GPIO_InitStruct.Pin);
GPIO_InitStruct.Pin = I2S3_MCK_PIN;
HAL_GPIO_DeInit(I2S3_MCK_GPIO_PORT, GPIO_InitStruct.Pin);
I2S3_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
__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 uint8_t I2S3_Init(uint32_t AudioFreq)
{
hAudioOutI2s.Instance = I2S3;
__HAL_I2S_DISABLE(&hAudioOutI2s);
hAudioOutI2s.Init.AudioFreq = AudioFreq;
hAudioOutI2s.Init.ClockSource = I2S_CLOCK_PLL;
hAudioOutI2s.Init.CPOL = I2S_CPOL_LOW;
hAudioOutI2s.Init.DataFormat = I2S_DATAFORMAT_16B;
hAudioOutI2s.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
hAudioOutI2s.Init.Mode = I2S_MODE_MASTER_TX;
hAudioOutI2s.Init.Standard = I2S_STANDARD;
if(HAL_I2S_Init(&hAudioOutI2s) != HAL_OK)
{
return AUDIO_ERROR;
}if (HAL_I2S_Init(&hAudioOutI2s) != HAL_OK) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
/* ... */
/* ... */
uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
{
BSP_AUDIO_IN_ClockConfig(&hAudioInI2s, AudioFreq, NULL);
/* ... */
PDMDecoder_Init(AudioFreq, ChnlNbr, 2);
hAudioInI2s.Instance = I2S2;
if(HAL_I2S_GetState(&hAudioInI2s) == HAL_I2S_STATE_RESET)
{
BSP_AUDIO_IN_MspInit(&hAudioInI2s, NULL);
}if (HAL_I2S_GetState(&hAudioInI2s) == HAL_I2S_STATE_RESET) { ... }
I2S2_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(&hAudioInI2s, pbuf, size);
ret = AUDIO_OK;
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Stop(void)
{
uint32_t ret = AUDIO_ERROR;
HAL_I2S_DMAStop(&hAudioInI2s);
ret = AUDIO_OK;
return ret;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Pause(void)
{
HAL_I2S_DMAPause(&hAudioInI2s);
return AUDIO_OK;
}{ ... }
/* ... */
uint8_t BSP_AUDIO_IN_Resume(void)
{
HAL_I2S_DMAResume(&hAudioInI2s);
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)
{
uint16_t AppPDM[INTERNAL_BUFF_SIZE/2];
uint32_t index = 0;
for(index = 0; index<INTERNAL_BUFF_SIZE/2; index++)
{
AppPDM[index] = HTONS(PDMBuf[index]);
}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++) { ... }
for(index = 0; index < PCM_OUT_SIZE; index++)
{
PCMBuf[(index<<1)+1] = PCMBuf[index<<1];
}for (index = 0; index < PCM_OUT_SIZE; 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();
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t AudioFreq, void *Params)
{
RCC_PeriphCLKInitTypeDef rccclkinit;
HAL_RCCEx_GetPeriphCLKConfig(&rccclkinit);
if ((AudioFreq & 0x7) == 0)
{
rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
rccclkinit.PLLI2S.PLLI2SN = 192;
rccclkinit.PLLI2S.PLLI2SR = 6;
HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
}if ((AudioFreq & 0x7) == 0) { ... }
else
{
rccclkinit.PeriphClockSelection = RCC_PERIPHCLK_I2S;
rccclkinit.PLLI2S.PLLI2SN = 290;
rccclkinit.PLLI2S.PLLI2SR = 2;
HAL_RCCEx_PeriphCLKConfig(&rccclkinit);
}else { ... }
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *hi2s, void *Params)
{
static DMA_HandleTypeDef hdma_i2sRx;
GPIO_InitTypeDef GPIO_InitStruct;
I2S2_CLK_ENABLE();
I2S2_SCK_GPIO_CLK_ENABLE();
I2S2_MOSI_GPIO_CLK_ENABLE();
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Pin = I2S2_SCK_PIN;
GPIO_InitStruct.Alternate = I2S2_SCK_AF;
HAL_GPIO_Init(I2S2_SCK_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = I2S2_MOSI_PIN ;
GPIO_InitStruct.Alternate = I2S2_MOSI_AF;
HAL_GPIO_Init(I2S2_MOSI_GPIO_PORT, &GPIO_InitStruct);
I2S2_DMAx_CLK_ENABLE();
if(hi2s->Instance == I2S2)
{
hdma_i2sRx.Init.Channel = I2S2_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 = I2S2_DMAx_PERIPH_DATA_SIZE;
hdma_i2sRx.Init.MemDataAlignment = I2S2_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 = I2S2_DMAx_STREAM;
__HAL_LINKDMA(hi2s, hdmarx, hdma_i2sRx);
HAL_DMA_DeInit(&hdma_i2sRx);
HAL_DMA_Init(&hdma_i2sRx);
}if (hi2s->Instance == I2S2) { ... }
HAL_NVIC_SetPriority(I2S2_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0);
HAL_NVIC_EnableIRQ(I2S2_DMAx_IRQ);
}{ ... }
/* ... */
__weak void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params)
{
GPIO_InitTypeDef gpio_init_structure;
HAL_NVIC_DisableIRQ(I2S2_DMAx_IRQ);
if(hi2s->Instance == I2S2)
{
HAL_DMA_DeInit(hi2s->hdmarx);
}if (hi2s->Instance == I2S2) { ... }
__HAL_I2S_DISABLE(hi2s);
gpio_init_structure.Pin = I2S2_SCK_PIN;
HAL_GPIO_DeInit(I2S2_SCK_GPIO_PORT, gpio_init_structure.Pin);
gpio_init_structure.Pin = I2S2_MOSI_PIN;
HAL_GPIO_DeInit(I2S2_MOSI_GPIO_PORT, gpio_init_structure.Pin);
I2S2_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 uint8_t I2S2_Init(uint32_t AudioFreq)
{
hAudioInI2s.Instance = I2S2;
__HAL_I2S_DISABLE(&hAudioInI2s);
hAudioInI2s.Init.AudioFreq = 2 * AudioFreq;
hAudioInI2s.Init.ClockSource = I2S_CLOCK_PLL;
hAudioInI2s.Init.CPOL = I2S_CPOL_HIGH;
hAudioInI2s.Init.DataFormat = I2S_DATAFORMAT_16B;
hAudioInI2s.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hAudioInI2s.Init.Mode = I2S_MODE_MASTER_RX;
hAudioInI2s.Init.Standard = I2S_STANDARD_LSB;
if(HAL_I2S_Init(&hAudioInI2s) != HAL_OK)
{
return AUDIO_ERROR;
}if (HAL_I2S_Init(&hAudioInI2s) != HAL_OK) { ... }
else
{
return AUDIO_OK;
}else { ... }
}{ ... }
/* ... */
/* ... */
/* ... */
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
{
/* ... */
if(hi2s->Instance == I2S3)
{
BSP_AUDIO_OUT_Error_CallBack();
}if (hi2s->Instance == I2S3) { ... }
if(hi2s->Instance == I2S2)
{
BSP_AUDIO_IN_Error_Callback();
}if (hi2s->Instance == I2S2) { ... }
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */