-
Notifications
You must be signed in to change notification settings - Fork 4
/
stm32h747i_discovery_audio.c
2930 lines (2614 loc) · 88.3 KB
/
stm32h747i_discovery_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
/**
******************************************************************************
* @file stm32h747i_discovery_audio.c
* @author MCD Application Team
* @brief This file provides the Audio driver for the STM32H747I_DISCO
* board.
@verbatim
How To use this driver:
-----------------------
+ This driver supports stm32h7xx devices on STM32H747I-DISCO (MB1248) boards.
+ Call the function BSP_AUDIO_OUT_Init() for AUDIO OUT initialization:
Instance: Select the output instance. It can only be 0 (SAI)
AudioInit: Audio Out structure to select the following parameters
- Device: Select the output device (headphone, speaker, hdmi ..)
- SampleRate: Select the output sample rate (8Khz .. 96Khz)
- BitsPerSample: Select the output resolution (16 or 32bits per sample)
- ChannelsNbr: Select the output channels number(1 for mono, 2 for stereo)
- Volume: Select the output volume(0% .. 100%)
This function configures all the hardware required for the audio application (codec, I2C, SAI,
GPIOs, DMA and interrupt if needed). This function returns BSP_ERROR_NONE if configuration is OK.
If the returned value is different from BSP_ERROR_NONE or the function is stuck then the communication with
the codec has failed (try to un-plug the power or reset device in this case).
User can update the SAI or the clock configurations by overriding the weak MX functions MX_SAI1_Block_B_Init(),
MX_SAI4_Block_A_Init(),MX_SAI1_ClockConfig(),and MX_SAI4_ClockConfig()
User can override the default MSP configuration and register his own MSP callbacks (defined at application level)
by calling BSP_AUDIO_OUT_RegisterMspCallbacks() function
User can restore the default MSP configuration by calling BSP_AUDIO_OUT_RegisterDefaultMspCallbacks()
To use these two functions, user have to enable USE_HAL_SAI_REGISTER_CALLBACKS within stm32h7xx_hal_conf.h file
+ Call the function BSP_AUDIO_OUT_Play() to play audio stream:
Instance: Select the output instance. It can only be 0 (SAI)
pBuf: pointer to the audio data file address
NbrOfBytes: Total size of the buffer to be sent in Bytes
+ 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).
+ Call the function BSP_AUDIO_OUT_Stop() to stop playing.
+ Call the function BSP_AUDIO_OUT_Mute() to mute the player.
+ Call the function BSP_AUDIO_OUT_UnMute() to unmute the player.
+ Call the function BSP_AUDIO_OUT_IsMute() to get the mute state(BSP_AUDIO_MUTE_ENABLED or BSP_AUDIO_MUTE_DISABLED).
+ Call the function BSP_AUDIO_OUT_SetDevice() to update the AUDIO OUT device.
+ Call the function BSP_AUDIO_OUT_GetDevice() to get the AUDIO OUT device.
+ Call the function BSP_AUDIO_OUT_SetSampleRate() to update the AUDIO OUT sample rate.
+ Call the function BSP_AUDIO_OUT_GetSampleRate() to get the AUDIO OUT sample rate.
+ Call the function BSP_AUDIO_OUT_SetBitsPerSample() to update the AUDIO OUT resolution.
+ Call the function BSP_AUDIO_OUT_GetBitPerSample() to get the AUDIO OUT resolution.
+ Call the function BSP_AUDIO_OUT_SetChannelsNbr() to update the AUDIO OUT number of channels.
+ Call the function BSP_AUDIO_OUT_GetChannelsNbr() to get the AUDIO OUT number of channels.
+ Call the function BSP_AUDIO_OUT_SetVolume() to update the AUDIO OUT volume.
+ Call the function BSP_AUDIO_OUT_GetVolume() to get the AUDIO OUT volume.
+ Call the function BSP_AUDIO_OUT_GetState() to get the AUDIO OUT state.
+ BSP_AUDIO_OUT_SetDevice(), BSP_AUDIO_OUT_SetSampleRate(), BSP_AUDIO_OUT_SetBitsPerSample() and
BSP_AUDIO_OUT_SetChannelsNbr() cannot be called while the state is AUDIO_OUT_STATE_PLAYING.
+ 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 stm32h747i_discovery_audio.h file. (refer to the example for more details on the callbacks implementations)
+ Call the function BSP_AUDIO_IN_Init() for AUDIO IN initialization:
Instance : Select the input instance. Can be 0 (SAI) or 1 (PDM)
AudioInit: Audio In structure to select the following parameters
- Device: Select the input device (analog, digital micx)
- SampleRate: Select the input sample rate (8Khz .. 96Khz)
- BitsPerSample: Select the input resolution (16 or 32bits per sample)
- ChannelsNbr: Select the input channels number(1 for mono, 2 for stereo)
- Volume: Select the input volume(0% .. 100%)
This function configures all the hardware required for the audio application (codec, I2C, SAI
GPIOs, DMA and interrupt if needed). This function returns BSP_ERROR_NONE if configuration is OK.
If the returned value is different from BSP_ERROR_NONE or the function is stuck then the communication with
the codec or the MFX has failed (try to un-plug the power or reset device in this case).
User can update the SAI or the clock configurations by overriding the weak MX functions MX_SAIx_Init()and
MX_SAIx_ClockConfig() .
User can override the default MSP configuration and register his own MSP callbacks (defined at application level)
by calling BSP_AUDIO_IN_RegisterMspCallbacks() function
User can restore the default MSP configuration by calling BSP_AUDIO_IN_RegisterDefaultMspCallbacks()
To use these two functions, user have to enable USE_HAL_SAI_REGISTER_CALLBACKS within stm32h7xx_hal_conf.h file .
+ Call the function BSP_AUDIO_IN_Record() to record audio stream. The recorded data are stored to user buffer in raw
(L, R, L, R ...)
Instance : Select the input instance. Can be 0 (SAI) or 1 (PDM)
pBuf: pointer to user buffer
NbrOfBytes: Total size of the buffer to be sent in Bytes
+ Call the function BSP_AUDIO_IN_Pause() to pause recording
+ Call the function BSP_AUDIO_IN_Resume() to resume recording.
+ Call the function BSP_AUDIO_IN_Stop() to stop recording.
+ Call the function BSP_AUDIO_IN_SetDevice() to update the AUDIO IN device.
+ Call the function BSP_AUDIO_IN_GetDevice() to get the AUDIO IN device.
+ Call the function BSP_AUDIO_IN_SetSampleRate() to update the AUDIO IN sample rate.
+ Call the function BSP_AUDIO_IN_GetSampleRate() to get the AUDIO IN sample rate.
+ Call the function BSP_AUDIO_IN_SetBitPerSample() to update the AUDIO IN resolution.
+ Call the function BSP_AUDIO_IN_GetBitPerSample() to get the AUDIO IN resolution.
+ Call the function BSP_AUDIO_IN_SetChannelsNbr() to update the AUDIO IN number of channels.
+ Call the function BSP_AUDIO_IN_GetChannelsNbr() to get the AUDIO IN number of channels.
+ Call the function BSP_AUDIO_IN_SetVolume() to update the AUDIO IN volume.
+ Call the function BSP_AUDIO_IN_GetVolume() to get the AUDIO IN volume.
+ Call the function BSP_AUDIO_IN_GetState() to get the AUDIO IN state.
+ For each mode, you may need to implement the relative callback functions into your code.
The Callback functions are named AUDIO_IN_XXX_CallBack() and only their prototypes are declared in
the stm32h747i_discovery_audio.h file. (refer to the example for more details on the callbacks implementations)
+ The driver API and the callback functions are at the end of the stm32h747i_discovery_audio.h file.
Known Limitations:
------------------
1- If the TDM Format used to play 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.
@endverbatim
******************************************************************************
* @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.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h747i_discovery_audio.h"
#include "stm32h747i_discovery_bus.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H747I_DISCO
* @{
*/
/** @defgroup STM32H747I_DISCO_AUDIO AUDIO
* @brief This file includes the low layer driver for wm8994 Audio Codec
* available on STM32H747I-DISCO discovery board(MB1381).
* @{
*/
/** @defgroup STM32H747I_DISCO_AUDIO_Private_Variables Private Variables
* @{
*/
static AUDIO_Drv_t *Audio_Drv = NULL;
/* PDM filters params */
static PDM_Filter_Handler_t PDM_FilterHandler[2];
static PDM_Filter_Config_t PDM_FilterConfig[2];
/**
* @}
*/
/** @defgroup STM32H747I_DISCO_AUDIO_Exported_Variables Exported Variables
* @{
*/
void *Audio_CompObj = NULL;
/* Play handle */
SAI_HandleTypeDef haudio_out_sai;
/*record handle*/
SAI_HandleTypeDef haudio_in_sai;
/* Play context */
AUDIO_OUT_Ctx_t Audio_Out_Ctx[AUDIO_OUT_INSTANCES_NBR] = {0};
/* Recording context */
AUDIO_IN_Ctx_t Audio_In_Ctx[AUDIO_IN_INSTANCES_NBR] = {0};
/**
* @}
*/
/** @defgroup STM32H747I_DISCO_AUDIO_Private_Function_Prototypes Private Function Prototypes
* @{
*/
/* SAI Msp config */
static void SAI_MspInit(SAI_HandleTypeDef *hsai);
static void SAI_MspDeInit(SAI_HandleTypeDef *hsai);
/* SAI callbacks */
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1U)
static void SAI_TxCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_RxCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_ErrorCallback(SAI_HandleTypeDef *hsai);
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 1U) */
#if (USE_AUDIO_CODEC_WM8994 == 1)
static int32_t WM8994_Probe(void);
#endif
/**
* @}
*/
/** @defgroup STM32H747I_DISCO_AUDIO_OUT_Exported_Functions AUDIO_OUT Exported Functions
* @{
*/
/**
* @brief Configures the audio peripherals.
* @param Instance : AUDIO_OUT Instance. It can only be 0 (SAI)
* @param AudioInit : AUDIO_OUT init Structure
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_Init(uint32_t Instance, BSP_AUDIO_Init_t* AudioInit)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Fill Audio_Out_Ctx structure */
Audio_Out_Ctx[Instance].Device = AudioInit->Device;
Audio_Out_Ctx[Instance].Instance = Instance;
Audio_Out_Ctx[Instance].SampleRate = AudioInit->SampleRate;
Audio_Out_Ctx[Instance].BitsPerSample = AudioInit->BitsPerSample;
Audio_Out_Ctx[Instance].ChannelsNbr = AudioInit->ChannelsNbr;
Audio_Out_Ctx[Instance].Volume = AudioInit->Volume;
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_RESET;
#if (USE_AUDIO_CODEC_WM8994 == 1)
if(WM8994_Probe() != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
#endif
if(ret == BSP_ERROR_NONE)
{
/* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */
if(MX_SAI1_ClockConfig(&haudio_out_sai, AudioInit->SampleRate) != HAL_OK)
{
ret = BSP_ERROR_CLOCK_FAILURE;
}
else
{
/* SAI data transfer preparation:
Prepare the Media to be used for the audio transfer from memory to SAI peripheral */
haudio_out_sai.Instance = AUDIO_OUT_SAIx;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1U)
/* Register the SAI MSP Callbacks */
if(Audio_Out_Ctx[Instance].IsMspCallbacksValid == 0U)
{
if(BSP_AUDIO_OUT_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
#else
SAI_MspInit(&haudio_out_sai);
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 1U) */
if(ret == BSP_ERROR_NONE)
{
MX_SAI_Config_t mx_sai_config;
/* Prepare haudio_out_sai handle */
mx_sai_config.AudioFrequency = AudioInit->SampleRate;
mx_sai_config.AudioMode = SAI_MODEMASTER_TX;
mx_sai_config.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
mx_sai_config.MonoStereoMode = (AudioInit->ChannelsNbr == 1U) ? SAI_MONOMODE : SAI_STEREOMODE;
mx_sai_config.DataSize = (AudioInit->BitsPerSample == AUDIO_RESOLUTION_32B) ? SAI_DATASIZE_32 : SAI_DATASIZE_16;
mx_sai_config.FrameLength = 128;
mx_sai_config.ActiveFrameLength = 64;
mx_sai_config.OutputDrive = SAI_OUTPUTDRIVE_ENABLE;
mx_sai_config.Synchro = SAI_ASYNCHRONOUS;
mx_sai_config.SynchroExt = SAI_SYNCEXT_DISABLE;
mx_sai_config.SlotActive = CODEC_AUDIOFRAME_SLOT_02;
/* SAI peripheral initialization: this __weak function can be redefined by the application */
if(MX_SAI1_Block_A_Init(&haudio_out_sai, &mx_sai_config) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1U)
/* Register SAI TC, HT and Error callbacks */
else if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_TX_COMPLETE_CB_ID, SAI_TxCpltCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_TX_HALFCOMPLETE_CB_ID, SAI_TxHalfCpltCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_ERROR_CB_ID, SAI_ErrorCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#endif
else
{
#if (USE_AUDIO_CODEC_WM8994 == 1)
WM8994_Init_t codec_init;
codec_init.Resolution = (AudioInit->BitsPerSample == AUDIO_RESOLUTION_32B) ? 3 : 0;
/* Fill codec_init structure */
codec_init.Frequency = AudioInit->SampleRate;
codec_init.InputDevice = WM8994_IN_NONE;
codec_init.OutputDevice = AudioInit->Device;
/* Convert volume before sending to the codec */
codec_init.Volume = VOLUME_OUT_CONVERT(AudioInit->Volume);
/* Initialize the codec internal registers */
if(Audio_Drv->Init(Audio_CompObj, &codec_init) != 0)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
#endif
if(ret == BSP_ERROR_NONE)
{
/* Update BSP AUDIO OUT state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_STOP;
}
}
}
}
}
}
return ret;
}
/**
* @brief De-initializes the audio out peripheral.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @retval None
*/
int32_t BSP_AUDIO_OUT_DeInit(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 0U)
SAI_MspDeInit(&haudio_out_sai);
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 0U) */
/* Initialize the haudio_out_sai Instance parameter */
haudio_out_sai.Instance = AUDIO_OUT_SAIx;
/* Call the Media layer stop function */
if(Audio_Drv->DeInit(Audio_CompObj) != 0)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if(HAL_SAI_DeInit(&haudio_out_sai) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Update BSP AUDIO OUT state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_RESET;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Initializes the Audio Codec audio out instance (SAI).
* @param hsai SAI handle
* @param MXConfig SAI configuration structure
* @note Being __weak it can be overwritten by the application
* @retval HAL status
*/
__weak HAL_StatusTypeDef MX_SAI1_Block_A_Init(SAI_HandleTypeDef* hsai, MX_SAI_Config_t *MXConfig)
{
HAL_StatusTypeDef ret = HAL_OK;
/* Disable SAI peripheral to allow access to SAI internal registers */
__HAL_SAI_DISABLE(hsai);
/* Configure SAI1_Block_A */
hsai->Init.MonoStereoMode = MXConfig->MonoStereoMode;
hsai->Init.AudioFrequency = MXConfig->AudioFrequency;
hsai->Init.AudioMode = MXConfig->AudioMode;
hsai->Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
hsai->Init.Protocol = SAI_FREE_PROTOCOL;
hsai->Init.DataSize = MXConfig->DataSize;
hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
hsai->Init.ClockStrobing = MXConfig->ClockStrobing;
hsai->Init.Synchro = MXConfig->Synchro;
hsai->Init.OutputDrive = MXConfig->OutputDrive;
hsai->Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF;
hsai->Init.SynchroExt = MXConfig->SynchroExt;
hsai->Init.CompandingMode = SAI_NOCOMPANDING;
hsai->Init.TriState = SAI_OUTPUT_NOTRELEASED;
hsai->Init.Mckdiv = 0;
hsai->Init.MckOverSampling = SAI_MCK_OVERSAMPLING_DISABLE;
hsai->Init.MckOutput = SAI_MCK_OUTPUT_DISABLE;
hsai->Init.PdmInit.Activation = DISABLE;
hsai->Init.PdmInit.ClockEnable = 0;
hsai->Init.PdmInit.MicPairsNbr = 0;
/* Configure SAI_Block_x Frame */
hsai->FrameInit.FrameLength = MXConfig->FrameLength;
hsai->FrameInit.ActiveFrameLength = MXConfig->ActiveFrameLength;
hsai->FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
/* Configure SAI Block_x Slot */
hsai->SlotInit.FirstBitOffset = 0;
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
hsai->SlotInit.SlotNumber = 4;
hsai->SlotInit.SlotActive = MXConfig->SlotActive;
if(HAL_SAI_Init(hsai) != HAL_OK)
{
ret = HAL_ERROR;
}
__HAL_SAI_ENABLE(hsai);
return ret;
}
/**
* @brief SAI clock Config.
* @param hsai SAI handle
* @param SampleRate Audio frequency used to play the audio stream.
* @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 HAL status
*/
__weak HAL_StatusTypeDef MX_SAI1_ClockConfig(SAI_HandleTypeDef *hsai, uint32_t SampleRate)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hsai);
HAL_StatusTypeDef ret = HAL_OK;
RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
/* Set the PLL configuration according to the audio frequency */
if((SampleRate == AUDIO_FREQUENCY_11K) || (SampleRate == AUDIO_FREQUENCY_22K) || (SampleRate == AUDIO_FREQUENCY_44K))
{
rcc_ex_clk_init_struct.PLL2.PLL2P = 38;
rcc_ex_clk_init_struct.PLL2.PLL2N = 429;
}
else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_32K, AUDIO_FREQUENCY_48K, AUDIO_FREQUENCY_96K */
{
rcc_ex_clk_init_struct.PLL2.PLL2P = 7;
rcc_ex_clk_init_struct.PLL2.PLL2N = 344;
}
rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SAI1;
rcc_ex_clk_init_struct.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLL2;
rcc_ex_clk_init_struct.PLL2.PLL2Q = 1;
rcc_ex_clk_init_struct.PLL2.PLL2R = 1;
rcc_ex_clk_init_struct.PLL2.PLL2M = 25;
if(HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct) != HAL_OK)
{
ret = HAL_ERROR;
}
return ret;
}
/**
* @brief SAI clock Config.
* @param hsai SAI handle
* @param SampleRate Audio frequency used to play the audio stream.
* @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 HAL status
*/
__weak HAL_StatusTypeDef MX_SAI4_ClockConfig(SAI_HandleTypeDef *hsai, uint32_t SampleRate)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hsai);
HAL_StatusTypeDef ret = HAL_OK;
RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
/* Set the PLL configuration according to the audio frequency */
if((SampleRate == AUDIO_FREQUENCY_11K) || (SampleRate == AUDIO_FREQUENCY_22K) || (SampleRate == AUDIO_FREQUENCY_44K))
{
rcc_ex_clk_init_struct.PLL2.PLL2P = 38;
rcc_ex_clk_init_struct.PLL2.PLL2N = 429;
}
else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_32K, AUDIO_FREQUENCY_48K, AUDIO_FREQUENCY_96K */
{
rcc_ex_clk_init_struct.PLL2.PLL2P = 7;
rcc_ex_clk_init_struct.PLL2.PLL2N = 344;
}
/* SAI clock config */
rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SAI4A;
rcc_ex_clk_init_struct.Sai4AClockSelection = RCC_SAI4ACLKSOURCE_PLL2;
rcc_ex_clk_init_struct.PLL2.PLL2Q = 1;
rcc_ex_clk_init_struct.PLL2.PLL2R = 1;
rcc_ex_clk_init_struct.PLL2.PLL2M = 25;
if(HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct) != HAL_OK)
{
ret = HAL_ERROR;
}
return ret;
}
/**
* @brief Default BSP AUDIO OUT Msp Callbacks
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @retval BSP status
*/
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1U)
int32_t BSP_AUDIO_OUT_RegisterDefaultMspCallbacks (uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
__HAL_SAI_RESET_HANDLE_STATE(&haudio_out_sai);
/* Register MspInit/MspDeInit Callbacks */
if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_MSPINIT_CB_ID,SAI_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_MSPDEINIT_CB_ID,SAI_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
Audio_Out_Ctx[Instance].IsMspCallbacksValid = 1;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP AUDIO OUT Msp Callback registering
* @param Instance AUDIO OUT Instance. It can only be 0
* @param CallBacks pointer to MspInit/MspDeInit callbacks functions
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_RegisterMspCallbacks (uint32_t Instance, BSP_AUDIO_OUT_Cb_t *CallBacks)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
__HAL_SAI_RESET_HANDLE_STATE(&haudio_out_sai);
/* Register MspInit/MspDeInit Callbacks */
if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_MSPINIT_CB_ID, CallBacks->pMspSaiInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_MSPDEINIT_CB_ID,CallBacks->pMspSaiDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
Audio_Out_Ctx[Instance].IsMspCallbacksValid = 1;
}
}
/* Return BSP status */
return ret;
}
#endif /*(USE_HAL_SAI_REGISTER_CALLBACKS == 1U)*/
/**
* @brief Starts playing audio stream from a data buffer for a determined size.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @param pData pointer on data address
* @param NbrOfBytes Size of total samples in bytes
* BitsPerSample: 16 or 32
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_Play(uint32_t Instance, uint8_t* pData, uint32_t NbrOfBytes)
{
int32_t ret = BSP_ERROR_NONE;
if((Instance >= AUDIO_OUT_INSTANCES_NBR) || (((NbrOfBytes / (Audio_Out_Ctx[Instance].BitsPerSample/8U)) > 0xFFFFU)))
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if((Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_STOP) || (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET))
{
if(HAL_SAI_Transmit_DMA(&haudio_out_sai, pData, (uint16_t)(NbrOfBytes /(Audio_Out_Ctx[Instance].BitsPerSample/8U))) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
if(ret == BSP_ERROR_NONE)
{
if(Audio_Drv->Play(Audio_CompObj) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update BSP AUDIO OUT state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_PLAYING;
}
}
}
else
{
ret = BSP_ERROR_BUSY;
}
/* Return BSP status */
return ret;
}
/**
* @brief This function Pauses the audio file stream. In case
* of using DMA, the DMA Pause feature is used.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @note 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 BSP status
*/
int32_t BSP_AUDIO_OUT_Pause(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Call the Media layer pause function */
if(HAL_SAI_DMAPause(&haudio_out_sai) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
if(ret == BSP_ERROR_NONE)
{
/* Call the Audio Codec Pause/Resume function */
if(Audio_Drv->Pause(Audio_CompObj) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update BSP AUDIO OUT state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_PAUSE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Resumes the audio file stream.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @note 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 BSP status
*/
int32_t BSP_AUDIO_OUT_Resume(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Call the Media layer pause/resume function */
if(HAL_SAI_DMAResume(&haudio_out_sai) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
if(ret == BSP_ERROR_NONE)
{
if(Audio_Drv->Resume(Audio_CompObj) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update BSP AUDIO OUT state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_PLAYING;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Stops audio playing and Power down the Audio Codec.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_Stop(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_PLAYING)
{
/* Call the Media layer stop function */
if(Audio_Drv->Stop(Audio_CompObj, CODEC_PDWN_SW) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
if(HAL_SAI_DMAStop(&haudio_out_sai)!= HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
if( ret==BSP_ERROR_NONE)
{
/* Update BSP AUDIO OUT state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_STOP;
}
}
}
else
{
ret = BSP_ERROR_BUSY;
}
/* Return BSP status */
return ret;
}
/**
* @brief Controls the current audio volume level.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @param Volume Volume level to be set in percentage from 0% to 100% (0 for
* Mute and 100 for Max volume level).
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_SetVolume(uint32_t Instance, uint32_t Volume)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Call the codec volume control function with converted volume value */
if(Audio_Drv->SetVolume(Audio_CompObj, AUDIO_VOLUME_OUTPUT, VOLUME_OUT_CONVERT(Volume)) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if(Volume == 0U)
{
/* Update Mute State */
Audio_Out_Ctx[Instance].IsMute = BSP_AUDIO_MUTE_ENABLED;
}
else
{
/* Update Mute State */
Audio_Out_Ctx[Instance].IsMute = BSP_AUDIO_MUTE_DISABLED;
}
Audio_Out_Ctx[Instance].Volume = Volume;
}
/* Return BSP status */
return ret;
}
/**
* @brief Get the current audio volume level.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @param Volume pointer to volume to be returned
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_GetVolume(uint32_t Instance, uint32_t *Volume)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
*Volume = Audio_Out_Ctx[Instance].Volume;
}
/* Return BSP status */
return ret;
}
/**
* @brief Enables the MUTE
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_Mute(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Call the Codec Mute function */
if(Audio_Drv->SetMute(Audio_CompObj, CODEC_MUTE_ON) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update Mute State */
Audio_Out_Ctx[Instance].IsMute = BSP_AUDIO_MUTE_ENABLED;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Disables the MUTE mode
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_UnMute(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Call the Codec Mute function */
if(Audio_Drv->SetMute(Audio_CompObj, CODEC_MUTE_OFF) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update Mute State */
Audio_Out_Ctx[Instance].IsMute = BSP_AUDIO_MUTE_DISABLED;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Check whether the MUTE mode is enabled or not
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @param IsMute pointer to mute state
* @retval Mute status
*/
int32_t BSP_AUDIO_OUT_IsMute(uint32_t Instance, uint32_t *IsMute)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
*IsMute = Audio_Out_Ctx[Instance].IsMute;
}
/* Return BSP status */
return ret;
}
/**
* @brief Switch dynamically (while audio file is played) the output target
* (speaker or headphone).
* @param Instance AUDIO OUT Instance. It can only be 0 (SAI) or 1 (I2S)
* @param Device The audio output device
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_SetDevice(uint32_t Instance, uint32_t Device)
{
int32_t ret = BSP_ERROR_NONE;
UNUSED(Device);
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_STOP)
{
ret = BSP_ERROR_BUSY;
}
else
{
/* Nothing to do because there is only one device (AUDIO_OUT_DEVICE_HEADPHONE) */
}
/* Return BSP status */
return ret;
}
/**
* @brief Get the Output Device
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @param Device The audio output device
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_GetDevice(uint32_t Instance, uint32_t *Device)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Get Audio_Out_Ctx Device */
*Device = Audio_Out_Ctx[Instance].Device;
}
/* Return BSP status */
return ret;
}
/**
* @brief Updates the audio frequency.
* @param Instance : AUDIO OUT Instance. It can only be 0 (SAI)
* @param SampleRate 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.
* @retval BSP status
*/
int32_t BSP_AUDIO_OUT_SetSampleRate(uint32_t Instance, uint32_t SampleRate)
{
int32_t ret = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_STOP)
{
ret = BSP_ERROR_BUSY;
}
/* Check if record on instance 0 is on going and corresponding sample rate */
else if ((Audio_In_Ctx[0].State != AUDIO_IN_STATE_RESET) &&
(Audio_In_Ctx[0].SampleRate != SampleRate))
{
ret = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}/* Check if sample rate is modified */
else if (Audio_Out_Ctx[Instance].SampleRate == SampleRate)
{
/* Nothing to do */
}
else
{
/* Update the SAI audio frequency configuration */
haudio_out_sai.Init.AudioFrequency = SampleRate;
/* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */
if(MX_SAI1_ClockConfig(&haudio_out_sai, SampleRate) != HAL_OK)
{