Select one of the symbols to view example projects that use it.
 
Outline
#include "stm324x9i_eval_audio.h"
audio_drv
haudio_out_sai
haudio_in_i2s
haudio_tim
PDM_FilterHandler
PDM_FilterConfig
Channel_Demux
AudioInVolume
BSP_AUDIO_OUT_Init(uint16_t, uint8_t, uint32_t)
BSP_AUDIO_OUT_DeInit()
BSP_AUDIO_OUT_Play(uint16_t *, uint32_t)
BSP_AUDIO_OUT_ChangeBuffer(uint16_t *, uint16_t)
BSP_AUDIO_OUT_Pause()
BSP_AUDIO_OUT_Resume()
BSP_AUDIO_OUT_Stop(uint32_t)
BSP_AUDIO_OUT_SetVolume(uint8_t)
BSP_AUDIO_OUT_SetMute(uint32_t)
BSP_AUDIO_OUT_SetOutputMode(uint8_t)
BSP_AUDIO_OUT_SetFrequency(uint32_t)
BSP_AUDIO_OUT_SetAudioFrameSlot(uint32_t)
BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef *, uint32_t, void *)
BSP_AUDIO_OUT_MspInit(SAI_HandleTypeDef *, void *)
BSP_AUDIO_OUT_MspDeInit(SAI_HandleTypeDef *, void *)
HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *)
HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *)
HAL_SAI_ErrorCallback(SAI_HandleTypeDef *)
BSP_AUDIO_OUT_TransferComplete_CallBack()
BSP_AUDIO_OUT_HalfTransfer_CallBack()
BSP_AUDIO_OUT_Error_CallBack()
SAIx_Init(uint32_t)
SAIx_DeInit()
BSP_AUDIO_IN_Init(uint32_t, uint32_t, uint32_t)
BSP_AUDIO_IN_DeInit()
BSP_AUDIO_IN_Record(uint16_t *, uint32_t)
BSP_AUDIO_IN_Stop()
BSP_AUDIO_IN_Pause()
BSP_AUDIO_IN_Resume()
BSP_AUDIO_IN_SetVolume(uint8_t)
BSP_AUDIO_IN_PDMToPCM(uint16_t *, uint16_t *)
HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *)
HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *)
HAL_I2S_ErrorCallback(I2S_HandleTypeDef *)
BSP_AUDIO_IN_ClockConfig(I2S_HandleTypeDef *, void *)
BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *, void *)
BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *, void *)
BSP_AUDIO_IN_TransferComplete_CallBack()
BSP_AUDIO_IN_HalfTransfer_CallBack()
BSP_AUDIO_IN_Error_Callback()
PDMDecoder_Init(uint32_t, uint32_t, uint32_t)
I2Sx_Init(uint32_t)
I2Sx_DeInit()
TIMx_IC_MspInit(TIM_HandleTypeDef *)
TIMx_IC_MspDeInit(TIM_HandleTypeDef *)
TIMx_Init()
TIMx_DeInit()
Files
loading...
CodeScopeSTM32 Libraries and SamplesSTM324x9I_EVALstm324x9i_eval_audio.c
 
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
118
119
120
121
122
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
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
882
883
884
885
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
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
974
975
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
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
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
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file stm324x9i_eval_audio.c * @author MCD Application Team * @brief This file provides the Audio driver for the STM324x9I-EVAL evaluation board. ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /*============================================================================== User NOTES How To use this driver: ----------------------- + This driver supports STM32F4xx devices on STM324x9I-EVAL (MB1045) Evaluation boards. + Call the function BSP_AUDIO_OUT_Init( OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH) Volume : Initial volume to be set (0 is min (mute), 100 is max (100%) AudioFreq : Audio frequency in Hz (8000, 16000, 22500, 32000...) this parameter is relative to the audio file/stream type. ) This function configures all the hardware required for the audio application (codec, I2C, SAI, GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if configuration is OK. If the returned value is different from AUDIO_OK or the function is stuck then the communication with the codec or the IOExpander has failed (try to un-plug the power or reset device in this case). - OUTPUT_DEVICE_SPEAKER : only speaker will be set as output for the audio stream. - OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream. - OUTPUT_DEVICE_BOTH : both Speaker and Headphone are used as outputs for the audio stream at the same time. + Call the function BSP_EVAL_AUDIO_OUT_Play( pBuffer: pointer to the audio data file address Size : size of the buffer to be sent in Bytes ) to start playing (for the first time) from the audio file/stream. + Call the function BSP_AUDIO_OUT_Pause() to pause playing + Call the function BSP_AUDIO_OUT_Resume() to resume playing. Note. After calling BSP_AUDIO_OUT_Pause() function for pause, only BSP_AUDIO_OUT_Resume() should be called for resume (it is not allowed to call BSP_AUDIO_OUT_Play() in this case). Note. This function should be called only when the audio file is played or paused (not stopped). + For each mode, you may need to implement the relative callback functions into your code. The Callback functions are named AUDIO_OUT_XXX_CallBack() and only their prototypes are declared in the stm324x9i_eval_audio.h file. (refer to the example for more details on the callbacks implementations) + To Stop playing, to modify the volume level, the frequency, the audio frame slot, the device output mode the mute or the stop, use the functions: BSP_AUDIO_OUT_SetVolume(), AUDIO_OUT_SetFrequency(), BSP_AUDIO_OUT_SetAudioFrameSlot(), BSP_AUDIO_OUT_SetOutputMode(), BSP_AUDIO_OUT_SetMute() and BSP_AUDIO_OUT_Stop(). + The driver API and the callback functions are at the end of the stm324x9i_eval_audio.h file. Driver architecture: -------------------- + This driver provide the High Audio Layer: consists of the function API exported in the stm324x9i_eval_audio.h file (BSP_AUDIO_OUT_Init(), BSP_AUDIO_OUT_Play() ...) + This driver provide also the Media Access Layer (MAL): which consists of functions allowing to access the media containing/ providing the audio file/stream. Known Limitations: ------------------ 1- If the TDM Format used to paly in parallel 2 audio Stream (the first Stream is configured in codec SLOT0 and second Stream in SLOT1) the Pause/Resume, volume and mute feature will control the both streams. 2- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size, File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file. 3- Supports only Stereo audio streaming. 4- Supports only 16-bits audio data size. ==============================================================================*//* ... */ /* Includes ------------------------------------------------------------------*/ #include "stm324x9i_eval_audio.h" /** @addtogroup BSP * @{ *//* ... */ /** @addtogroup STM324x9I_EVAL * @{ *//* ... */ /** @defgroup STM324x9I_EVAL_AUDIO STM324x9I EVAL AUDIO * @brief This file includes the low layer driver for wm8994 Audio Codec * available on STM324x9I-EVAL evaluation board(MB1045). * @{ *//* ... */ /** @defgroup STM324x9I_EVAL_AUDIO_Private_Variables STM324x9I EVAL AUDIO Private Variables * @{ *//* ... */ AUDIO_DrvTypeDef *audio_drv; SAI_HandleTypeDef haudio_out_sai; I2S_HandleTypeDef haudio_in_i2s; TIM_HandleTypeDef haudio_tim; /* PDM filters params */ 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; /** * @} *//* ... */ /** @defgroup STM324x9I_EVAL_AUDIO_Private_Function_Prototypes STM324x9I EVAL AUDIO Private Function Prototypes * @{ *//* ... */ 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); /** * @} *//* ... */ /** @defgroup STM324x9I_EVAL_AUDIO_out_Private_Functions STM324x9I EVAL AUDIO OUT Private Functions * @{ *//* ... */ /** * @brief Configures the audio peripherals. * @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE, * or OUTPUT_DEVICE_BOTH. * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max)) * @param AudioFreq: Audio frequency used to play the audio stream. * @note The I2S PLL input clock must be done in the user application. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq) { uint8_t ret = AUDIO_ERROR; /* Disable SAI */ SAIx_DeInit(); /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */ BSP_AUDIO_OUT_ClockConfig(&haudio_out_sai, AudioFreq, NULL); /* SAI data transfer preparation: Prepare the Media to be used for the audio transfer from memory to SAI peripheral *//* ... */ haudio_out_sai.Instance = AUDIO_SAIx; if(HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET) { /* Init the SAI MSP: this __weak function can be redefined by the application*/ BSP_AUDIO_OUT_MspInit(&haudio_out_sai, NULL); }if (HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET) { ... } SAIx_Init(AudioFreq); /* wm8994 codec initialization */ if((wm8994_drv.ReadID(AUDIO_I2C_ADDRESS)) == WM8994_ID) { /* Initialize the audio driver structure */ 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) { /* Initialize the codec internal registers */ audio_drv->Init(AUDIO_I2C_ADDRESS, OutputDevice, Volume, AudioFreq); }if (ret == AUDIO_OK) { ... } return ret; }{ ... } /** * @brief De-initialize the audio peripherals. * @retval None *//* ... */ void BSP_AUDIO_OUT_DeInit(void) { SAIx_DeInit(); /* DeInit the SAI MSP : this __weak function can be rewritten by the application */ BSP_AUDIO_OUT_MspDeInit(&haudio_out_sai, NULL); }{ ... } /** * @brief Starts playing audio stream from a data buffer for a determined size. * @param pBuffer: Pointer to the buffer * @param Size: Number of audio data BYTES. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_Play(uint16_t* pBuffer, uint32_t Size) { /* Call the audio Codec Play function */ if(audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) { return AUDIO_ERROR; }if (audio_drv->Play(AUDIO_I2C_ADDRESS, pBuffer, Size) != 0) { ... } else { /* Update the Media layer and enable it for play */ HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*)pBuffer, DMA_MAX(Size / AUDIODATA_SIZE)); return AUDIO_OK; }else { ... } }{ ... } /** * @brief Sends n-Bytes on the SAI interface. * @param pData: pointer on data address * @param Size: number of data to be written *//* ... */ void BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size) { HAL_SAI_Transmit_DMA(&haudio_out_sai, (uint8_t*)pData, Size); }{ ... } /** * @brief This function Pauses the audio file stream. In case * of using DMA, the DMA Pause feature is used. * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play() * function for resume could lead to unexpected behavior). * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_Pause(void) { /* Call the Audio Codec Pause/Resume function */ if(audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0) { return AUDIO_ERROR; }if (audio_drv->Pause(AUDIO_I2C_ADDRESS) != 0) { ... } else { /* Call the Media layer pause function */ HAL_SAI_DMAPause(&haudio_out_sai); /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }else { ... } }{ ... } /** * @brief This function Resumes the audio file stream. * WARNING: When calling BSP_AUDIO_OUT_Pause() function for pause, only * BSP_AUDIO_OUT_Resume() function should be called for resume (use of BSP_AUDIO_OUT_Play() * function for resume could lead to unexpected behavior). * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_Resume(void) { /* Call the Audio Codec Pause/Resume function */ if(audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0) { return AUDIO_ERROR; }if (audio_drv->Resume(AUDIO_I2C_ADDRESS) != 0) { ... } else { /* Call the Media layer pause/resume function */ HAL_SAI_DMAResume(&haudio_out_sai); /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }else { ... } }{ ... } /** * @brief Stops audio playing and Power down the Audio Codec. * @param Option: could be one of the following parameters * - CODEC_PDWN_SW: for software power off (by writing registers). * Then no need to reconfigure the Codec after power on. * - CODEC_PDWN_HW: completely shut down the codec (physically). * Then need to reconfigure the Codec after power on. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option) { /* Call the Media layer stop function */ HAL_SAI_DMAStop(&haudio_out_sai); /* Call Audio Codec Stop function */ 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) { /* Wait at least 100us */ HAL_Delay(1); }if (Option == CODEC_PDWN_HW) { ... } /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }else { ... } }{ ... } /** * @brief Controls the current audio volume level. * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for * Mute and 100 for Max volume level). * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume) { /* Call the codec volume control function with converted volume value */ 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 when all operations are correctly done */ return AUDIO_OK; }else { ... } }{ ... } /** * @brief Enables or disables the MUTE mode by software * @param Cmd: Could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to * unmute the codec and restore previous volume level. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd) { /* Call the Codec Mute function */ 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 when all operations are correctly done */ return AUDIO_OK; }else { ... } }{ ... } /** * @brief Switch dynamically (while audio file is played) the output target * (speaker or headphone). * @param Output: The audio output target: OUTPUT_DEVICE_SPEAKER, * OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output) { /* Call the Codec output device function */ 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 when all operations are correctly done */ return AUDIO_OK; }else { ... } }{ ... } /** * @brief Updates the audio frequency. * @param AudioFreq: Audio frequency used to play the audio stream. * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the * audio frequency. *//* ... */ void BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq) { /* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */ BSP_AUDIO_OUT_ClockConfig(&haudio_out_sai, AudioFreq, NULL); /* Disable SAI peripheral to allow access to SAI internal registers */ __HAL_SAI_DISABLE(&haudio_out_sai); /* Update the SAI audio frequency configuration */ haudio_out_sai.Init.AudioFrequency = AudioFreq; HAL_SAI_Init(&haudio_out_sai); /* Enable SAI peripheral to generate MCLK */ __HAL_SAI_ENABLE(&haudio_out_sai); }{ ... } /** * @brief Updates the Audio frame slot configuration. * @param AudioFrameSlot: specifies the audio Frame slot * @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the * audio frame slot. *//* ... */ void BSP_AUDIO_OUT_SetAudioFrameSlot(uint32_t AudioFrameSlot) { /* Disable SAI peripheral to allow access to SAI internal registers */ __HAL_SAI_DISABLE(&haudio_out_sai); /* Update the SAI audio frame slot configuration */ haudio_out_sai.SlotInit.SlotActive = AudioFrameSlot; HAL_SAI_Init(&haudio_out_sai); /* Enable SAI peripheral to generate MCLK */ __HAL_SAI_ENABLE(&haudio_out_sai); }{ ... } /** * @brief Clock Config. * @param hsai: might be required to set audio peripheral predivider if any. * @param AudioFreq: Audio frequency used to play the audio stream. * @param Params : pointer on additional configuration parameters, can be NULL. * @note This API is called by BSP_AUDIO_OUT_Init() and BSP_AUDIO_OUT_SetFrequency() * Being __weak it can be overwritten by the application * @retval None *//* ... */ __weak void BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef *hsai, uint32_t AudioFreq, void *Params) { RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct; HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct); /* Set the PLL configuration according to the audio frequency */ if((AudioFreq == AUDIO_FREQUENCY_11K) || (AudioFreq == AUDIO_FREQUENCY_22K) || (AudioFreq == AUDIO_FREQUENCY_44K)) { /* Configure PLLSAI prescalers */ /* PLLI2S_VCO: VCO_429M SAI_CLK(first level) = PLLI2S_VCO/PLLI2SQ = 429/2 = 214.5 Mhz SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ = 214.5/19 = 11.289 Mhz *//* ... */ 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 /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_48K), AUDIO_FREQUENCY_96K */ { /* SAI clock config PLLI2S_VCO: VCO_344M SAI_CLK(first level) = PLLI2S_VCO/PLLI2SQ = 344/7 = 49.142 Mhz SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ = 49.142/1 = 49.142 Mhz *//* ... */ 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 { ... } }{ ... } /** * @brief Initialize BSP_AUDIO_OUT MSP. * @param hsai: SAI handle * @param Params : pointer on additional configuration parameters, can be NULL. * @retval None *//* ... */ __weak void BSP_AUDIO_OUT_MspInit(SAI_HandleTypeDef *hsai, void *Params) { static DMA_HandleTypeDef hdma_saiTx; GPIO_InitTypeDef GPIO_InitStruct; /* Enable SAI clock */ AUDIO_SAIx_CLK_ENABLE(); /* Enable GPIO clock */ AUDIO_SAIx_MCLK_SCK_SD_FS_ENABLE(); /* CODEC_SAI pins configuration: FS, SCK, MCK and SD pins ------------------*/ 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); /* Enable the DMA clock */ AUDIO_SAIx_DMAx_CLK_ENABLE(); if(hsai->Instance == AUDIO_SAIx) { /* Configure the hdma_saiTx handle parameters */ 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; /* Associate the DMA handle */ __HAL_LINKDMA(hsai, hdmatx, hdma_saiTx); /* Deinitialize the Stream for new transfer */ HAL_DMA_DeInit(&hdma_saiTx); /* Configure the DMA Stream */ HAL_DMA_Init(&hdma_saiTx); }if (hsai->Instance == AUDIO_SAIx) { ... } /* SAI DMA IRQ Channel configuration */ HAL_NVIC_SetPriority(AUDIO_SAIx_DMAx_IRQ, AUDIO_OUT_IRQ_PREPRIO, 0); HAL_NVIC_EnableIRQ(AUDIO_SAIx_DMAx_IRQ); }{ ... } /** * @brief Deinitialize SAI MSP. * @param hsai: SAI handle * @param Params : pointer on additional configuration parameters, can be NULL. *//* ... */ __weak void BSP_AUDIO_OUT_MspDeInit(SAI_HandleTypeDef *hsai, void *Params) { GPIO_InitTypeDef gpio_init_structure; /* SAI DMA IRQ Channel deactivation */ HAL_NVIC_DisableIRQ(AUDIO_SAIx_DMAx_IRQ); if(hsai->Instance == AUDIO_SAIx) { /* Deinitialize the DMA stream */ HAL_DMA_DeInit(hsai->hdmatx); }if (hsai->Instance == AUDIO_SAIx) { ... } /* Disable SAI peripheral */ __HAL_SAI_DISABLE(hsai); /* Deactivates CODEC_SAI pins FS, SCK, MCK and SD by putting them in input mode */ 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); /* Disable SAI clock */ AUDIO_SAIx_CLK_DISABLE(); /* GPIO pins clock and DMA clock can be shut down in the applic by surcharging this __weak function *//* ... */ }{ ... } /** * @brief Tx Transfer completed callbacks. * @param hsai: SAI handle *//* ... */ void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai) { /* Manage the remaining file size and new address offset: This function should be coded by user (its prototype is already declared in stm324x9i_eval_audio.h) *//* ... */ BSP_AUDIO_OUT_TransferComplete_CallBack(); }{ ... } /** * @brief Tx Half Transfer completed callbacks. * @param hsai: SAI handle *//* ... */ void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai) { /* Manage the remaining file size and new address offset: This function should be coded by user (its prototype is already declared in stm324x9i_eval_audio.h) *//* ... */ BSP_AUDIO_OUT_HalfTransfer_CallBack(); }{ ... } /** * @brief SAI error callbacks. * @param hsai: SAI handle *//* ... */ void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai) { BSP_AUDIO_OUT_Error_CallBack(); }{ ... } /** * @brief Manages the DMA full Transfer complete event. *//* ... */ __weak void BSP_AUDIO_OUT_TransferComplete_CallBack(void) { }{ ... } /** * @brief Manages the DMA Half Transfer complete event. *//* ... */ __weak void BSP_AUDIO_OUT_HalfTransfer_CallBack(void) { }{ ... } /** * @brief Manages the DMA FIFO error event. *//* ... */ __weak void BSP_AUDIO_OUT_Error_CallBack(void) { }{ ... } /******************************************************************************* Static Functions *******************************************************************************//* ... */ /** * @brief Initializes the Audio Codec audio interface (SAI). * @param AudioFreq: Audio frequency to be configured for the SAI peripheral. * @note The default SlotActive configuration is set to CODEC_AUDIOFRAME_SLOT_0123 * and user can update this configuration using *//* ... */ static void SAIx_Init(uint32_t AudioFreq) { /* Initialize the haudio_out_sai Instance parameter */ haudio_out_sai.Instance = AUDIO_SAIx; /* Disable SAI peripheral to allow access to SAI internal registers */ __HAL_SAI_DISABLE(&haudio_out_sai); /* Configure SAI_Block_x LSBFirst: Disabled DataSize: 16 *//* ... */ 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; /* Configure SAI_Block_x Frame Frame Length: 64 Frame active Length: 32 FS Definition: Start frame + Channel Side identification FS Polarity: FS active Low FS Offset: FS asserted one bit before the first bit of slot 0 *//* ... */ 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; /* Configure SAI Block_x Slot Slot First Bit Offset: 0 Slot Size : 16 Slot Number: 4 Slot Active: All slot actives *//* ... */ 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); /* Enable SAI peripheral to generate MCLK */ __HAL_SAI_ENABLE(&haudio_out_sai); }{ ... } /** * @brief Deinitialize the Audio Codec audio interface (SAI). *//* ... */ static void SAIx_DeInit(void) { /* Initialize the haudio_out_sai Instance parameter */ haudio_out_sai.Instance = AUDIO_SAIx; /* Disable SAI peripheral */ __HAL_SAI_DISABLE(&haudio_out_sai); HAL_SAI_DeInit(&haudio_out_sai); }{ ... } /** * @} *//* ... */ /** @defgroup STM324x9I_EVAL_AUDIO_IN_Private_Functions STM324x9I EVAL AUDIO IN Private Functions * @{ *//* ... */ /** * @brief Initializes wave recording. * @note This function assumes that the I2S input clock (through PLL_R in * Devices RevA/Z and through dedicated PLLI2S_R in Devices RevB/Y) * is already configured and ready to be used. * @param AudioFreq: Audio frequency to be configured for the I2S peripheral. * @param BitRes: Audio frequency to be configured for the I2S peripheral. * @param ChnlNbr: Audio frequency to be configured for the I2S peripheral. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr) { /* DeInit the I2S */ I2Sx_DeInit(); /* Configure PLL clock */ BSP_AUDIO_IN_ClockConfig(&haudio_in_i2s, NULL); /* Configure the PDM library */ PDMDecoder_Init(AudioFreq, ChnlNbr, ChnlNbr); /* Configure the I2S peripheral */ haudio_in_i2s.Instance = AUDIO_I2Sx; if(HAL_I2S_GetState(&haudio_in_i2s) == HAL_I2S_STATE_RESET) { /* Initialize the I2S Msp: this __weak function can be rewritten by the application */ BSP_AUDIO_IN_MspInit(&haudio_in_i2s, NULL); }if (HAL_I2S_GetState(&haudio_in_i2s) == HAL_I2S_STATE_RESET) { ... } /* Configure the I2S peripheral */ I2Sx_Init(AudioFreq); /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }{ ... } /** * @brief Deinit the audio IN peripherals. *//* ... */ void BSP_AUDIO_IN_DeInit(void) { /* DeInit the I2S */ I2Sx_DeInit(); /* DeInit the I2S MSP : this __weak function can be rewritten by the applic */ BSP_AUDIO_IN_MspDeInit(&haudio_in_i2s, NULL); /* DeInit the Timer */ TIMx_DeInit(); }{ ... } /** * @brief Starts audio recording. * @param pbuf: Main buffer pointer for the recorded data storing * @param size: Current size of the recorded buffer * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size) { uint32_t ret = AUDIO_ERROR; /* Start the process receive DMA */ HAL_I2S_Receive_DMA(&haudio_in_i2s, pbuf, size); /* Return AUDIO_OK when all operations are correctly done */ ret = AUDIO_OK; return ret; }{ ... } /** * @brief Stops audio recording. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_IN_Stop(void) { uint32_t ret = AUDIO_ERROR; /* Call the Media layer pause function */ HAL_I2S_DMAPause(&haudio_in_i2s); /* TIMx Peripheral clock disable */ AUDIO_TIMx_CLK_DISABLE(); /* Return AUDIO_OK when all operations are correctly done */ ret = AUDIO_OK; return ret; }{ ... } /** * @brief Pauses the audio file stream. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_IN_Pause(void) { /* Call the Media layer pause function */ HAL_I2S_DMAPause(&haudio_in_i2s); /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }{ ... } /** * @brief Resumes the audio file stream. * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_IN_Resume(void) { /* Call the Media layer pause/resume function */ HAL_I2S_DMAResume(&haudio_in_i2s); /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }{ ... } /** * @brief Controls the audio in volume level. * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for * Mute and 100 for Max volume level). * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume) { /* Set the Global variable AudioInVolume */ AudioInVolume = Volume; /* Return AUDIO_OK when all operations are correctly done */ return AUDIO_OK; }{ ... } /** * @brief Converts audio format from PDM to PCM. * @param PDMBuf: Pointer to data PDM buffer * @param PCMBuf: Pointer to data PCM buffer * @retval AUDIO_OK if correct communication, else wrong communication *//* ... */ 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; /* PDM Demux */ 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 to PCM filter */ 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 when all operations are correctly done */ return AUDIO_OK; }{ ... } /** * @brief Rx Transfer completed callbacks. * @param hi2s: I2S handle *//* ... */ void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) { /* Call the record update function to get the next buffer to fill and its size (size is ignored) */ BSP_AUDIO_IN_TransferComplete_CallBack(); }{ ... } /** * @brief Rx Half Transfer completed callbacks. * @param hi2s: I2S handle *//* ... */ void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s) { /* Manage the remaining file size and new address offset: This function should be coded by user (its prototype is already declared in stm324x9i_eval_audio.h) *//* ... */ BSP_AUDIO_IN_HalfTransfer_CallBack(); }{ ... } /** * @brief I2S error callbacks. * @param hi2s: I2S handle *//* ... */ void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) { /* Manage the error generated on DMA FIFO: This function should be coded by user (its prototype is already declared in stm324x9i_eval_audio.h) *//* ... */ BSP_AUDIO_IN_Error_Callback(); }{ ... } /** * @brief Clock Config. * @param hi2s: I2S handle * @param Params : pointer on additional configuration parameters, can be NULL. * @note This API is called by BSP_AUDIO_IN_Init() * Being __weak it can be overwritten by the application *//* ... */ __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); }{ ... } /** * @brief Initialize BSP_AUDIO_IN MSP. * @param hi2s: I2S handle * @param Params : pointer on additional configuration parameters, can be NULL. *//* ... */ __weak void BSP_AUDIO_IN_MspInit(I2S_HandleTypeDef *hi2s, void *Params) { static DMA_HandleTypeDef hdma_i2sRx; GPIO_InitTypeDef GPIO_InitStruct; /* Configure the Timer which clocks the MEMS */ /* Moved inside MSP to allow applic to redefine the TIMx_MspInit */ TIMx_Init(); /* Enable I2S clock */ AUDIO_I2Sx_CLK_ENABLE(); /* Enable SCK and SD GPIO clock */ AUDIO_I2Sx_SD_GPIO_CLK_ENABLE(); AUDIO_I2Sx_SCK_GPIO_CLK_ENABLE(); /* CODEC_I2S pins configuration: WS, SCK and SD pins */ 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); /* Enable the DMA clock */ AUDIO_I2Sx_DMAx_CLK_ENABLE(); if(hi2s->Instance == AUDIO_I2Sx) { /* Configure the hdma_i2sRx handle parameters */ 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; /* Associate the DMA handle */ __HAL_LINKDMA(hi2s, hdmarx, hdma_i2sRx); /* Deinitialize the Stream for new transfer */ HAL_DMA_DeInit(&hdma_i2sRx); /* Configure the DMA Stream */ HAL_DMA_Init(&hdma_i2sRx); }if (hi2s->Instance == AUDIO_I2Sx) { ... } /* I2S DMA IRQ Channel configuration */ HAL_NVIC_SetPriority(AUDIO_I2Sx_DMAx_IRQ, AUDIO_IN_IRQ_PREPRIO, 0); HAL_NVIC_EnableIRQ(AUDIO_I2Sx_DMAx_IRQ); }{ ... } /** * @brief BSP AUDIO IN MSP Init. * @param hi2s: I2S handle * @param Params : pointer on additional configuration parameters, can be NULL. *//* ... */ __weak void BSP_AUDIO_IN_MspDeInit(I2S_HandleTypeDef *hi2s, void *Params) { GPIO_InitTypeDef gpio_init_structure; /* I2S DMA IRQ Channel deactivation */ HAL_NVIC_DisableIRQ(AUDIO_I2Sx_DMAx_IRQ); if(hi2s->Instance == AUDIO_I2Sx) { /* Deinitialize the Stream for new transfer */ HAL_DMA_DeInit(hi2s->hdmarx); }if (hi2s->Instance == AUDIO_I2Sx) { ... } /* Disable I2S block */ __HAL_I2S_DISABLE(hi2s); /* Disable pins: SCK and SD pins */ 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); /* Disable I2S clock */ AUDIO_I2Sx_CLK_DISABLE(); /* GPIO pins clock and DMA clock can be shut down in the applic by surcgarging this __weak function *//* ... */ }{ ... } /** * @brief User callback when record buffer is filled. *//* ... */ __weak void BSP_AUDIO_IN_TransferComplete_CallBack(void) { /* This function should be implemented by the user application. It is called into this driver when the current buffer is filled to prepare the next buffer pointer and its size. *//* ... */ }{ ... } /** * @brief Manages the DMA Half Transfer complete event. *//* ... */ __weak void BSP_AUDIO_IN_HalfTransfer_CallBack(void) { /* This function should be implemented by the user application. It is called into this driver when the current buffer is filled to prepare the next buffer pointer and its size. *//* ... */ }{ ... } /** * @brief Audio IN Error callback function. *//* ... */ __weak void BSP_AUDIO_IN_Error_Callback(void) { /* This function is called when an Interrupt due to transfer error on or peripheral error occurs. *//* ... */ }{ ... } /******************************************************************************* Static Functions *******************************************************************************//* ... */ /** * @brief Initializes the PDM library. * @param AudioFreq: Audio sampling frequency * @param ChnlNbrIn: Number of input audio channels in the PDM buffer * @param ChnlNbrOut: Number of desired output audio channels in the resulting PCM buffer * Number of audio channels (1: mono; 2: stereo) *//* ... */ static void PDMDecoder_Init(uint32_t AudioFreq, uint32_t ChnlNbrIn, uint32_t ChnlNbrOut) { uint32_t index = 0; /* Enable CRC peripheral to unlock the PDM library */ __HAL_RCC_CRC_CLK_ENABLE(); for(index = 0; index < ChnlNbrIn; index++) { /* Init PDM filters */ 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 lib config phase */ 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++) { ... } }{ ... } /** * @brief Initializes the Audio Codec audio interface (I2S) * @note This function assumes that the I2S input clock (through PLL_R in * Devices RevA/Z and through dedicated PLLI2S_R in Devices RevB/Y) * is already configured and ready to be used. * @param AudioFreq: Audio frequency to be configured for the I2S peripheral. *//* ... */ static void I2Sx_Init(uint32_t AudioFreq) { /* Initialize the haudio_in_i2s Instance parameter */ haudio_in_i2s.Instance = AUDIO_I2Sx; /* Disable I2S block */ __HAL_I2S_DISABLE(&haudio_in_i2s); /* I2S2 peripheral configuration */ 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; /* Init the I2S */ HAL_I2S_Init(&haudio_in_i2s); }{ ... } /** * @brief Deinitializes the Audio Codec audio interface (I2S). *//* ... */ static void I2Sx_DeInit(void) { /* Initialize the hAudioInI2s Instance parameter */ haudio_in_i2s.Instance = AUDIO_I2Sx; /* Disable I2S block */ __HAL_I2S_DISABLE(&haudio_in_i2s); /* DeInit the I2S */ HAL_I2S_DeInit(&haudio_in_i2s); }{ ... } /** * @brief Initializes the TIM INput Capture MSP. * @param htim: TIM handle *//* ... */ static void TIMx_IC_MspInit(TIM_HandleTypeDef *htim) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable peripherals and GPIO Clocks --------------------------------------*/ /* TIMx Peripheral clock enable */ AUDIO_TIMx_CLK_ENABLE(); /* Enable GPIO Channels Clock */ AUDIO_TIMx_GPIO_CLK_ENABLE(); Enable peripherals and GPIO Clocks /* Configure I/Os ----------------------------------------------------------*/ /* Common configuration for all channels */ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = AUDIO_TIMx_AF; /* Configure TIM input channel */ GPIO_InitStruct.Pin = AUDIO_TIMx_IN_GPIO_PIN; HAL_GPIO_Init(AUDIO_TIMx_GPIO, &GPIO_InitStruct); /* Configure TIM output channel */ GPIO_InitStruct.Pin = AUDIO_TIMx_OUT_GPIO_PIN; HAL_GPIO_Init(AUDIO_TIMx_GPIO, &GPIO_InitStruct); }{ ... } /** * @brief Initializes the TIM INput Capture MSP. * @param htim: TIM handle *//* ... */ static void TIMx_IC_MspDeInit(TIM_HandleTypeDef *htim) { /* Disable TIMx Peripheral clock */ AUDIO_TIMx_CLK_DISABLE(); /* GPIO pins clock and DMA clock can be shut down in the applic by surcgarging this __weak function *//* ... */ }{ ... } /** * @brief Configure TIM as a clock divider by 2. * I2S_SCK is externally connected to TIMx input channel *//* ... */ static void TIMx_Init(void) { TIM_IC_InitTypeDef sICConfig; TIM_OC_InitTypeDef sOCConfig; TIM_ClockConfigTypeDef sCLKSourceConfig; TIM_SlaveConfigTypeDef sSlaveConfig; /* Configure the TIM peripheral --------------------------------------------*/ /* Set TIMx instance */ haudio_tim.Instance = AUDIO_TIMx; /* Timer Input Capture Configuration Structure declaration */ /* Initialize TIMx peripheral as follow: + Period = 0xFFFF + Prescaler = 0 + ClockDivision = 0 + Counter direction = Up *//* ... */ haudio_tim.Init.Period = 1; haudio_tim.Init.Prescaler = 0; haudio_tim.Init.ClockDivision = 0; haudio_tim.Init.CounterMode = TIM_COUNTERMODE_UP; /* Initialize the TIMx peripheral with the structure above */ TIMx_IC_MspInit(&haudio_tim); HAL_TIM_IC_Init(&haudio_tim); Configure the TIM peripheral /* Configure the Input Capture channel -------------------------------------*/ /* Configure the Input Capture of channel 2 */ 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); /* Select external clock mode 1 */ sCLKSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE1; sCLKSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED; sCLKSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1; sCLKSourceConfig.ClockFilter = 0; HAL_TIM_ConfigClockSource(&haudio_tim, &sCLKSourceConfig); /* Select Input Channel as input trigger */ 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); /* Output Compare PWM Mode configuration: Channel2 */ 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; /* Initialize the TIM3 Channel2 with the structure above */ HAL_TIM_PWM_ConfigChannel(&haudio_tim, &sOCConfig, AUDIO_TIMx_OUT_CHANNEL); /* Start the TIM3 Channel2 */ HAL_TIM_PWM_Start(&haudio_tim, AUDIO_TIMx_OUT_CHANNEL); /* Start the TIM3 Channel1 */ HAL_TIM_IC_Start(&haudio_tim, AUDIO_TIMx_IN_CHANNEL); }{ ... } /** * @brief Configure TIM as a clock divider by 2. * I2S_SCK is externally connected to TIMx input channel *//* ... */ static void TIMx_DeInit(void) { haudio_tim.Instance = AUDIO_TIMx; /* Stop the TIM3 Channel2 */ HAL_TIM_PWM_Stop(&haudio_tim, AUDIO_TIMx_OUT_CHANNEL); /* Stop the TIM3 Channel1 */ HAL_TIM_IC_Stop(&haudio_tim, AUDIO_TIMx_IN_CHANNEL); HAL_TIM_IC_DeInit(&haudio_tim); /* Initialize the TIMx peripheral with the structure above */ TIMx_IC_MspDeInit(&haudio_tim); }{ ... } /** * @} *//* ... */ /** * @} *//* ... */ /** * @} *//* ... */ /** * @} *//* ... */ /** * @} *//* ... */