-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32h7b3i_discovery_audio.c
4985 lines (4549 loc) · 162 KB
/
stm32h7b3i_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 stm32h7b3i_discovery_audio.c
* @author MCD Application Team
* @brief This file provides the Audio driver for the STM32H7B3I_DK
* board.
@verbatim
How To use this driver:
-----------------------
+ This driver supports stm32h7xx devices on STM32H7B3I_DK (MB1332) boards.
+ Call the function BSP_AUDIO_OUT_Init() for AUDIO OUT initialization:
Instance: Select the output instance. It can only be 0 (SAI) or 1 (I2S)
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, I2S ,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 I2S/SAI or the clock configurations by overriding the weak MX functions MX_I2S6_Init(),
MX_I2S6_ClockConfig(),MX_SAI1_Block_A_Init() and MX_SAI1_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_I2S_REGISTER_CALLBACKS and 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) or 1 (I2S)
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 STM32H7B3I_DK_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), 1 (I2S) or 2 (DFSDM)
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, I2S, DFSDM
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 DFSDM/SAI or the clock configurations by overriding the weak MX functions MX_SAIx_Init(),
MX_SAIx_ClockConfig(), MX_DFSDMx_Init() and MX_DFSDMx_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 and/or USE_HAL_DFSDM_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), 1 (I2S) or 2 (DFSDM)
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.
+ Call the function BSP_AUDIO_IN_RecordChannels() to record audio stream. The recorded data are stored to user buffers separately
(L, L, ...) (R, R ...). User has to process his data at application level.
Instance : Select the input instance. Can be 2 (DFSDM)
pBuf: pointer to user buffers table
NbrOfBytes: Total size of the buffer to be sent in Bytes
+ Call the function BSP_AUDIO_IN_PauseChannels() to pause recording:
Instance : Select the input instance. Can be 2 (DFSDM)
Device: Select the input device (digital micX)
+ Call the function BSP_AUDIO_IN_ResumeChannels() to resume recording.
Instance : Select the input instance. Can be 2 (DFSDM)
Device: Select the input device (digital micX)
+ Call the function BSP_AUDIO_IN_StopChannels() to stop recording.
Instance : Select the input instance. Can be 2 (DFSDM)
Device: Select the input device (digital micX)
+ 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 stm32h7b3i_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 stm32h7b3i_discovery_audio.h file.
@endverbatim
******************************************************************************
* @attention
*
* Copyright (c) 2019 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 "stm32h7b3i_discovery_audio.h"
#include "stm32h7b3i_discovery_bus.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H7B3I_DK
* @{
*/
/** @defgroup STM32H7B3I_DK_AUDIO AUDIO
* @brief This file includes the low layer driver for cs42l51 Audio Codec
* available on STM32H7B3I_DK board(MB1332).
* @{
*/
/** @defgroup STM32H7B3I_DK_AUDIO_Private_Defines AUDIO Private Defines
* @{
*/
/**
* @}
*/
/** @defgroup STM32H7B3I_DK_AUDIO_Private_Macros AUDIO Private Macros
* @{
*/
/*### RECORD ###*/
#define DFSDM_OVER_SAMPLING(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (256U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (256U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (128U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (128U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (64U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (64U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (32U) : (16U)
#define DFSDM_CLOCK_DIVIDER(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (24U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (4U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (24U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (4U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (24U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (4U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (32U) : (32U)
#define DFSDM_FILTER_ORDER(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (DFSDM_FILTER_SINC3_ORDER) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (DFSDM_FILTER_SINC3_ORDER) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (DFSDM_FILTER_SINC3_ORDER) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (DFSDM_FILTER_SINC3_ORDER) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (DFSDM_FILTER_SINC4_ORDER) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (DFSDM_FILTER_SINC3_ORDER) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (DFSDM_FILTER_SINC4_ORDER) : (DFSDM_FILTER_SINC5_ORDER)
#define DFSDM_MIC_BIT_SHIFT(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (5U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (6U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (3U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (3U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (6U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (0U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (2U) : (2U)
/* Saturate the record PCM sample */
#define SaturaLH(N, L, H) (((N)<(L))?(L):(((N)>(H))?(H):(N)))
/**
* @}
*/
/** @defgroup STM32H7B3I_DK_AUDIO_Exported_Variables AUDIO Exported Variables
* @{
*/
/* Audio in and out component object */
void *Audio_CompObj = NULL;
/* Play */
SAI_HandleTypeDef haudio_out_sai = {0};
I2S_HandleTypeDef haudio_out_i2s = {0};
AUDIO_OUT_Ctx_t Audio_Out_Ctx[AUDIO_OUT_INSTANCES_NBR];
/* Record */
DFSDM_Filter_HandleTypeDef haudio_in_dfsdm_filter[DFSDM_MIC_NUMBER];
DFSDM_Channel_HandleTypeDef haudio_in_dfsdm_channel[DFSDM_MIC_NUMBER];
SAI_HandleTypeDef haudio_in_sai = {0};
I2S_HandleTypeDef haudio_in_i2s = {0};
AUDIO_IN_Ctx_t Audio_In_Ctx[AUDIO_IN_INSTANCES_NBR];
/**
* @}
*/
/** @defgroup STM32H7B3I_DK_AUDIO_Private_Variables AUDIO Private Variables
* @{
*/
/* Audio in and out driver */
static AUDIO_Drv_t *Audio_Drv = NULL;
/* Recording DFSDM DMA handles */
static DMA_HandleTypeDef hDmaDfsdm[DFSDM_MIC_NUMBER];
/* Recording Buffer Trigger */
static __IO uint32_t RecBuffTrigger = 0;
static __IO uint32_t RecBuffHalf = 0;
ALIGN_32BYTES(static int32_t MicRecBuff[2][DEFAULT_AUDIO_IN_BUFFER_SIZE]);
static __IO uint32_t MicBuffIndex[DFSDM_MIC_NUMBER];
/**
* @}
*/
/** @defgroup STM32H7B3I_DK_AUDIO_Private_Function_Prototypes AUDIO 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 == 1)
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 == 1) */
/* I2S Msp config */
static void I2S_MspInit(I2S_HandleTypeDef *hi2s);
static void I2S_MspDeInit(I2S_HandleTypeDef *hi2s);
/* I2S callbacks */
#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1)
static void I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s);
static void I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s);
static void I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s);
static void I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s);
static void I2S_ErrorCallback(I2S_HandleTypeDef *hi2s);
#endif /* (USE_HAL_I2S_REGISTER_CALLBACKS == 1) */
/* DFSDM Channel Msp config */
static void DFSDM_ChannelMspInit(DFSDM_Channel_HandleTypeDef *hDfsdmChannel);
static void DFSDM_ChannelMspDeInit(DFSDM_Channel_HandleTypeDef *hDfsdmChannel);
/* DFSDM Filter Msp config */
static void DFSDM_FilterMspInit(DFSDM_Filter_HandleTypeDef *hDfsdmFilter);
static void DFSDM_FilterMspDeInit(DFSDM_Filter_HandleTypeDef *hDfsdmFilter);
/* DFSDM Filter conversion callbacks */
#if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
static void DFSDM_FilterRegConvHalfCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
static void DFSDM_FilterRegConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
#endif /* (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1) */
#if (USE_AUDIO_CODEC_CS42L51 == 1)
static int32_t CS42L51_Probe(void);
static int32_t CS42L51_PowerUp(void);
static int32_t CS42L51_PowerDown(void);
#endif
/**
* @}
*/
/** @defgroup STM32H7B3I_DK_AUDIO_OUT_Exported_Functions AUDIO OUT Exported Functions
* @{
*/
/**
* @brief Configures the audio peripherals.
* @param Instance AUDIO OUT Instance. It can only be 0 (SAI) or 1 (I2S)
* @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_CS42L51 == 1)
if ((Audio_In_Ctx[0].State == AUDIO_IN_STATE_RESET) && (Audio_In_Ctx[1].State == AUDIO_IN_STATE_RESET))
{
(void)CS42L51_PowerUp();
if (CS42L51_Probe() != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
#endif
if (ret == BSP_ERROR_NONE)
{
if (Instance == 0U)
{
/* 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 == 1)
/* Register the 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
}
}
else
{
/* PLL clock is set depending by the AudioFreq (44.1khz vs 48khz groups) */
if (MX_I2S6_ClockConfig(&haudio_out_i2s, AudioInit->SampleRate) != HAL_OK)
{
ret = BSP_ERROR_CLOCK_FAILURE;
}
else
{
/* I2S data transfer preparation:
Prepare the Media to be used for the audio transfer from memory to I2S peripheral */
haudio_out_i2s.Instance = AUDIO_OUT_I2Sx;
#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1)
/* Register the MSP Callbacks */
if (Audio_Out_Ctx[Instance].IsMspCallbacksValid == 0U)
{
if (BSP_AUDIO_OUT_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
#else
I2S_MspInit(&haudio_out_i2s);
#endif
}
}
if (ret == BSP_ERROR_NONE)
{
if (Instance == 0U)
{
MX_SAI_Config 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_FALLINGEDGE;
mx_sai_config.MonoStereoMode = (AudioInit->ChannelsNbr == 1U) ? SAI_MONOMODE : SAI_STEREOMODE;
if (AudioInit->BitsPerSample == AUDIO_RESOLUTION_24B)
{
mx_sai_config.DataSize = SAI_DATASIZE_24;
mx_sai_config.FrameLength = 64;
mx_sai_config.ActiveFrameLength = 32;
}
else
{
mx_sai_config.DataSize = SAI_DATASIZE_16;
mx_sai_config.FrameLength = 32;
mx_sai_config.ActiveFrameLength = 16;
}
mx_sai_config.OutputDrive = SAI_OUTPUTDRIVE_ENABLE;
mx_sai_config.Synchro = SAI_ASYNCHRONOUS;
mx_sai_config.SynchroExt = SAI_SYNCEXT_DISABLE;
mx_sai_config.SlotActive = SAI_SLOTACTIVE_0 | SAI_SLOTACTIVE_1;
/* 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 == 1)
/* 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
{
MX_I2S_Config mx_i2s_config;
mx_i2s_config.AudioMode = I2S_MODE_MASTER_TX;
mx_i2s_config.SampleRate = AudioInit->SampleRate;
/* I2S peripheral initialization: this __weak function can be redefined by the application */
if (MX_I2S6_Init(&haudio_out_i2s, &mx_i2s_config) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
else if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_TX_COMPLETE_CB_ID, I2S_TxCpltCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_TX_HALF_COMPLETE_CB_ID, I2S_TxHalfCpltCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_ERROR_CB_ID, I2S_ErrorCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
#endif
}
if (ret == BSP_ERROR_NONE)
{
#if (USE_AUDIO_CODEC_CS42L51 == 1)
CS42L51_Init_t codec_init;
/* Fill codec_init structure */
codec_init.InputDevice = ((Audio_In_Ctx[0].State == AUDIO_IN_STATE_RESET) && (Audio_In_Ctx[1].State == AUDIO_IN_STATE_RESET)) ? \
CS42L51_IN_NONE : CS42L51_IN_LINE1;
codec_init.OutputDevice = CS42L51_OUT_HEADPHONE;
codec_init.Frequency = AudioInit->SampleRate;
codec_init.Resolution = CS42L51_RESOLUTION_16b; /* Not used */
codec_init.Volume = AudioInit->Volume;
if (Instance == 0U)
{
/* Initialize the codec internal registers */
if (Audio_Drv->Init(Audio_CompObj, &codec_init) < 0)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
else
{
/* Receive fake I2S data in order to generate MCLK needed by CS42L51 to set its registers */
if (HAL_I2S_Transmit_DMA(&haudio_out_i2s, ((uint16_t *)0x38000000), 16) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Initialize the codec internal registers */
if (Audio_Drv->Init(Audio_CompObj, &codec_init) < 0)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Stop receiving fake I2S data */
if (HAL_I2S_DMAStop(&haudio_out_i2s) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
}
}
#endif
/* 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) or 1 (I2S)
* @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 (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
ret = BSP_ERROR_BUSY;
}
else
{
/* Reset audio codec if not currently used by audio in instances */
if ((Audio_In_Ctx[0].State == AUDIO_IN_STATE_RESET) || (Audio_In_Ctx[1].State == AUDIO_IN_STATE_RESET))
{
(void)CS42L51_PowerDown();
}
if (Instance == 0U)
{
if (HAL_SAI_DeInit(&haudio_out_sai) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 0)
SAI_MspDeInit(&haudio_out_sai);
#endif
}
else
{
if (HAL_I2S_DeInit(&haudio_out_i2s) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_I2S_REGISTER_CALLBACKS == 0)
I2S_MspDeInit(&haudio_out_i2s);
#endif
}
if (ret == BSP_ERROR_NONE)
{
/* Call the Media layer stop function */
if (Audio_Drv->DeInit(Audio_CompObj) < 0)
{
ret = BSP_ERROR_COMPONENT_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 confiruration 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 *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.MckOutput = SAI_MCK_OUTPUT_ENABLE;
hsai->Init.MckOverSampling = SAI_MCK_OVERSAMPLING_DISABLE;
hsai->Init.PdmInit.Activation = DISABLE;
/* 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;
if ((MXConfig->DataSize == AUDIO_RESOLUTION_24B) || (MXConfig->DataSize == AUDIO_RESOLUTION_32B))
{
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
}
else
{
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
}
hsai->SlotInit.SlotNumber = 2;
hsai->SlotInit.SlotActive = MXConfig->SlotActive;
if (HAL_SAI_Init(hsai) != HAL_OK)
{
ret = HAL_ERROR;
}
return ret;
}
/**
* @brief Initializes the Audio audio out peripheral (I2S).
* @param hi2s I2S handle
* @param MXConfig I2S confiruration structure
* @note Being __weak it can be overwritten by the application
* @retval HAL status
*/
__weak HAL_StatusTypeDef MX_I2S6_Init(I2S_HandleTypeDef *hi2s, MX_I2S_Config *MXConfig)
{
HAL_StatusTypeDef ret = HAL_OK;
/* Disable I2S peripheral to allow access to I2S internal registers */
__HAL_I2S_DISABLE(hi2s);
/* I2S peripheral configuration */
hi2s->Init.Mode = MXConfig->AudioMode;
hi2s->Init.Standard = I2S_STANDARD_PHILIPS;
hi2s->Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s->Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
hi2s->Init.AudioFreq = MXConfig->SampleRate;
hi2s->Init.CPOL = I2S_CPOL_LOW;
hi2s->Init.FirstBit = I2S_FIRSTBIT_MSB;
hi2s->Init.WSInversion = I2S_WS_INVERSION_DISABLE;
hi2s->Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_RIGHT;
hi2s->Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_DISABLE;
if (HAL_I2S_Init(hi2s) != HAL_OK)
{
ret = HAL_ERROR;
}
/* Enable SAI peripheral */
__HAL_I2S_ENABLE(hi2s);
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);
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 = 24;
rcc_ex_clk_init_struct.PLL2.PLL2Q = 24;
rcc_ex_clk_init_struct.PLL2.PLL2N = 271;
}
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.PLL2Q = 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.PLL2R = 1;
rcc_ex_clk_init_struct.PLL2.PLL2M = 24;
rcc_ex_clk_init_struct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_0;
rcc_ex_clk_init_struct.PLL2.PLL2VCOSEL = RCC_PLL2VCOMEDIUM;
rcc_ex_clk_init_struct.PLL2.PLL2FRACN = 0;
return HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
}
/**
* @brief I2S clock Config.
* @param hi2s I2S 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_I2S6_ClockConfig(I2S_HandleTypeDef *hi2s, uint32_t SampleRate)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hi2s);
RCC_PeriphCLKInitTypeDef rcc_ex_clk_init_struct;
HAL_RCCEx_GetPeriphCLKConfig(&rcc_ex_clk_init_struct);
/* PLLI2S_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
/* 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 = 24; /* PLLP for DFSDM clock if used in same time with I2S */
rcc_ex_clk_init_struct.PLL2.PLL2Q = 24; /* PLLQ for I2S clock */
rcc_ex_clk_init_struct.PLL2.PLL2N = 271;
}
else /* AUDIO_FREQUENCY_8K, AUDIO_FREQUENCY_16K, AUDIO_FREQUENCY_32K, AUDIO_FREQUENCY_48K, AUDIO_FREQUENCY_96K */
{
rcc_ex_clk_init_struct.PLL2.PLL2P = 7; /* PLLP for DFSDM clock if used in same time with I2S */
rcc_ex_clk_init_struct.PLL2.PLL2Q = 7; /* PLLQ for I2S clock */
rcc_ex_clk_init_struct.PLL2.PLL2N = 344;
}
rcc_ex_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_SPI6 | RCC_PERIPHCLK_SAI1;
rcc_ex_clk_init_struct.Spi6ClockSelection = RCC_SPI6CLKSOURCE_PLL2;
rcc_ex_clk_init_struct.PLL2.PLL2R = 1;
rcc_ex_clk_init_struct.PLL2.PLL2M = 24;
rcc_ex_clk_init_struct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_0;
rcc_ex_clk_init_struct.PLL2.PLL2VCOSEL = RCC_PLL2VCOMEDIUM;
rcc_ex_clk_init_struct.PLL2.PLL2FRACN = 0;
return HAL_RCCEx_PeriphCLKConfig(&rcc_ex_clk_init_struct);
}
/**
* @brief Default BSP AUDIO OUT Msp Callbacks
* @param Instance AUDIO OUT Instance. It can only be 0 (SAI) or 1 (I2S)
* @retval BSP status
*/
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1) || (USE_HAL_I2S_REGISTER_CALLBACKS == 1)
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
{
if (Instance == 0U)
{
__HAL_SAI_RESET_HANDLE_STATE(&haudio_out_sai);
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
/* 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;
}
#endif
}
else
{
__HAL_I2S_RESET_HANDLE_STATE(&haudio_out_i2s);
#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1)
/* Register MspInit/MspDeInit Callbacks */
if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_MSPINIT_CB_ID, I2S_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_MSPDEINIT_CB_ID, I2S_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
Audio_Out_Ctx[Instance].IsMspCallbacksValid = 1;
}
#endif
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP AUDIO OUT Msp Callback registering
* @param Instance AUDIO OUT Instance. It can only be 0 (SAI) or 1 (I2S)
* @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
{
if (Instance == 0U)
{
__HAL_SAI_RESET_HANDLE_STATE(&haudio_out_sai);
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
/* 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;
}
#endif
}
else
{
__HAL_I2S_RESET_HANDLE_STATE(&haudio_out_i2s);
#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1)
/* Register MspInit/MspDeInit Callbacks */
if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_MSPINIT_CB_ID, CallBacks->pMspI2sInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_I2S_RegisterCallback(&haudio_out_i2s, HAL_I2S_MSPDEINIT_CB_ID, CallBacks->pMspI2sDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
Audio_Out_Ctx[Instance].IsMspCallbacksValid = 1;
}
#endif
}
}
/* Return BSP status */
return ret;
}
#endif /*(USE_HAL_SAI_REGISTER_CALLBACKS == 1) || (USE_HAL_I2S_REGISTER_CALLBACKS == 1)*/
/**
* @brief Starts playing audio stream from a data buffer for a determined size.
* @param Instance AUDIO OUT Instance. It can only be 0 (SAI) or 1 (I2S)
* @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)
{
ret = BSP_ERROR_BUSY;
}
else
{
if (Instance == 0U)
{
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;
}
}
else
{
if (HAL_I2S_Transmit_DMA(&haudio_out_i2s, (uint16_t *)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) < 0)
{
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 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) or 1 (I2S)
* @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;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_PLAYING)
{
ret = BSP_ERROR_BUSY;
}
/* Call the audio codec pause function */
else if (Audio_Drv->Pause(Audio_CompObj) < 0)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Pause DMA transfer of audio samples towards the serial audio interface */
if (Instance == 0U)
{
if (HAL_SAI_DMAPause(&haudio_out_sai) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
else
{
if (HAL_I2S_DMAPause(&haudio_out_i2s) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;