-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32n6570_discovery_audio.c
3610 lines (3286 loc) · 110 KB
/
stm32n6570_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 stm32n6570_discovery_audio.c
* @author MCD Application Team
* @brief This file provides the Audio driver for the STM32N6570-DK board.
******************************************************************************
* @attention
*
* Copyright (c) 2023 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.
*
******************************************************************************
@verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
+ This driver supports STM32N6xx devices on STM32N6570-DK (MB1939) board.
+ 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).
- SampleRate: Select the output sample rate (8Khz .. 96Khz).
- BitsPerSample: Select the output resolution (16 or 24bits 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_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_SAI_REGISTER_CALLBACKS
within stm32n6xx_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 BSP_AUDIO_OUT_XXX_CallBack() and only their prototypes are declared in
the stm32n6570_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 (MDF)
AudioInit: Audio In structure to select the following parameters:
- Device: Select the input device (analog, digital microphone).
- SampleRate: Select the input sample rate (8Khz .. 96Khz).
- BitsPerSample: Select the input resolution (16bits per sample).
- ChannelsNbr: Select the input channels number(1 for mono).
- Volume: Select the input volume(0% .. 100%).
This function configures all the hardware required for the audio application (codec, I2C, SAI, MDF,
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_SAIx_Init() and
MX_SAIx_ClockConfig()
User can update the MDF or the clock configurations by overriding the weak MX functions MX_MDFx_Init() and
MX_MDFx_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_MDF_REGISTER_CALLBACKS within stm32n6xx_hal_conf.h file
+ Call the function BSP_AUDIO_IN_Record() to record audio stream.
Instance : Select the input instance. Can be 0 (SAI) or 1 (MDF).
pBuf: pointer to user buffer.
NbrOfBytes: Total size of the buffer to be received 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 BSP_AUDIO_IN_XXX_CallBack() and only their prototypes are declared in
the stm32n6570_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 stm32n6570_discovery_audio.h file.
@endverbatim
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32n6570_discovery_audio.h"
#include "stm32n6570_discovery_bus.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32N6570_DISCOVERY
* @{
*/
/** @defgroup STM32N6570_DISCOVERY_AUDIO AUDIO
* @{
*/
/** @defgroup STM32N6570_DISCOVERY_AUDIO_Private_Macros AUDIO Private Macros
* @{
*/
#define SaturaLH(N, L, H) (((N)<(L))?(L):(((N)>(H))?(H):(N)))
#define MDF_DECIMATION_RATIO(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (64U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (64U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (32U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (32U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (16U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (16U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (16U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_96K)) ? (8U) : (4U)
#define MDF_GAIN(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (-4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (-6) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (2) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (2) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (10) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (10) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (10) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_96K)) ? (18) : (24)
#define MDF_CIC_MODE(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (MDF_ONE_FILTER_SINC4) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_96K)) ? (MDF_ONE_FILTER_SINC4) : (MDF_ONE_FILTER_SINC4)
#define MDF_PROC_CLOCK_DIVIDER(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (2U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (1U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (2U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (1U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (2U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (1U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (2U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_96K)) ? (2U) : (1U)
#define MDF_OUTPUT_CLOCK_DIVIDER(__FREQUENCY__) \
((__FREQUENCY__) == (AUDIO_FREQUENCY_8K)) ? (12U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_11K)) ? (4U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_16K)) ? (12U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_22K)) ? (4U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_32K)) ? (12U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_44K)) ? (4U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_48K)) ? (8U) \
: ((__FREQUENCY__) == (AUDIO_FREQUENCY_96K)) ? (16U) : (16U)
/**
* @}
*/
/** @defgroup STM32N6570_DISCOVERY_AUDIO_Exported_Variables AUDIO Exported Variables
* @{
*/
/* Audio in and out context */
AUDIO_OUT_Ctx_t Audio_Out_Ctx[AUDIO_OUT_INSTANCES_NBR] = {{AUDIO_OUT_DEVICE_HEADPHONE,
AUDIO_FREQUENCY_8K,
AUDIO_RESOLUTION_16B,
50U,
2U,
AUDIO_MUTE_DISABLED,
AUDIO_OUT_STATE_RESET,
0U}};
AUDIO_IN_Ctx_t Audio_In_Ctx[AUDIO_IN_INSTANCES_NBR] = {{AUDIO_IN_DEVICE_ANALOG_MIC,
AUDIO_FREQUENCY_8K,
AUDIO_RESOLUTION_16B,
1U,
NULL,
0U,
50U,
AUDIO_IN_STATE_RESET,
0U},
{AUDIO_IN_DEVICE_DIGITAL_MIC,
AUDIO_FREQUENCY_8K,
AUDIO_RESOLUTION_16B,
1U,
NULL,
0U,
50U,
AUDIO_IN_STATE_RESET,
0U}};
/* Audio component object */
void *Audio_CompObj = NULL;
/* Audio driver */
AUDIO_Drv_t *Audio_Drv = NULL;
/* Play */
SAI_HandleTypeDef haudio_out_sai = {NULL};
/* Record */
SAI_HandleTypeDef haudio_in_sai = {NULL};
MDF_HandleTypeDef haudio_in_mdf = {NULL};
/* Audio in and out DMA handles used by SAI and MDF */
DMA_HandleTypeDef hDmaSaiTx = {NULL}, hDmaSaiRx = {NULL}, hDmaMdf = {NULL};
/**
* @}
*/
/** @defgroup STM32N6570_DISCOVERY_AUDIO_Private_Variables AUDIO Private Variables
* @{
*/
/* Queue variables declaration */
static DMA_QListTypeDef SAITxQueue, SAIRxQueue, MdfRxQueue;
/* Audio MDF filter configuration */
static MDF_FilterConfigTypeDef Audio_MdfFilterConfig;
/* Audio in MDF internal buffer */
static int32_t Audio_DigMicRecBuff[DEFAULT_AUDIO_IN_BUFFER_SIZE];
/**
* @}
*/
/** @defgroup STM32N6570_DISCOVERY_AUDIO_Private_Function_Prototypes AUDIO Private Function Prototypes
* @{
*/
#if defined(USE_AUDIO_CODEC_WM8904)
static int32_t WM8904_Probe(void);
#else /* USE_AUDIO_CODEC_WM8904 */
static int32_t CS42L51_Probe(void);
static int32_t CS42L51_PowerUp(void);
static int32_t CS42L51_PowerDown(void);
#endif /* USE_AUDIO_CODEC_WM8904 */
static void SAI_MspInit(SAI_HandleTypeDef *hsai);
static void SAI_MspDeInit(SAI_HandleTypeDef *hsai);
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
static void SAI_TxCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_ErrorCallback(SAI_HandleTypeDef *hsai);
static void SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai);
static void SAI_RxCpltCallback(SAI_HandleTypeDef *hsai);
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 1) */
static void MDF_MspInit(MDF_HandleTypeDef *hmdf);
static void MDF_MspDeInit(MDF_HandleTypeDef *hmdf);
#if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
static void MDF_AcqCpltCallback(MDF_HandleTypeDef *hmdf);
static void MDF_AcqHalfCpltCallback(MDF_HandleTypeDef *hmdf);
static void MDF_ErrorCallback(MDF_HandleTypeDef *hmdf);
#endif /* (USE_HAL_MDF_REGISTER_CALLBACKS == 1) */
/**
* @}
*/
/** @addtogroup STM32N6570_DISCOVERY_AUDIO_OUT_Exported_Functions
* @{
*/
/**
* @brief Initialize the audio out peripherals.
* @param Instance Audio out instance.
* @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 status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
else if ((AudioInit->BitsPerSample == AUDIO_RESOLUTION_32B)
|| (AudioInit->BitsPerSample == AUDIO_RESOLUTION_8B))
{
status = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
else if ((Audio_In_Ctx[0].State != AUDIO_IN_STATE_RESET) &&
((Audio_In_Ctx[0].SampleRate != AudioInit->SampleRate) ||
(Audio_In_Ctx[0].BitsPerSample != AudioInit->BitsPerSample)))
{
status = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
else
{
/* Fill audio out context structure */
Audio_Out_Ctx[Instance].Device = AudioInit->Device;
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;
/* Un-reset audio codec if not currently used by audio in instances */
if (Audio_In_Ctx[0].State == AUDIO_IN_STATE_RESET)
{
#if defined(USE_AUDIO_CODEC_CS42L51)
(void)CS42L51_PowerUp();
if (CS42L51_Probe() != BSP_ERROR_NONE)
#else /* USE_AUDIO_CODEC_CS42L51 */
if (WM8904_Probe() != BSP_ERROR_NONE)
#endif /* USE_AUDIO_CODEC_CS42L51 */
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
}
if (status == BSP_ERROR_NONE)
{
/* Set SAI instance */
haudio_out_sai.Instance = AUDIO_OUT_SAI;
/* Configure the SAI clock according to the requested audio frequency if not already done by other instances */
if (Audio_In_Ctx[0].State == AUDIO_IN_STATE_RESET)
{
if (MX_SAI1_ClockConfig(&haudio_out_sai, AudioInit->SampleRate) != HAL_OK)
{
status = BSP_ERROR_CLOCK_FAILURE;
}
}
if (status == BSP_ERROR_NONE)
{
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 0)
SAI_MspInit(&haudio_out_sai);
#else
/* Register the SAI MSP Callbacks */
if (Audio_Out_Ctx[Instance].IsMspCallbacksValid == 0U)
{
if (BSP_AUDIO_OUT_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
}
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 0) */
}
if (status == BSP_ERROR_NONE)
{
/* Prepare SAI peripheral initialization */
MX_SAI_Config_t mxSaiInit;
mxSaiInit.AudioFrequency = AudioInit->SampleRate;
mxSaiInit.AudioMode = SAI_MODEMASTER_TX;
mxSaiInit.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
mxSaiInit.MonoStereoMode = (AudioInit->ChannelsNbr == 1U) ? SAI_MONOMODE : SAI_STEREOMODE;
if (AudioInit->BitsPerSample == AUDIO_RESOLUTION_24B)
{
mxSaiInit.DataSize = SAI_DATASIZE_24;
mxSaiInit.FrameLength = 64;
mxSaiInit.ActiveFrameLength = 32;
}
else
{
mxSaiInit.DataSize = SAI_DATASIZE_16;
mxSaiInit.FrameLength = 32;
mxSaiInit.ActiveFrameLength = 16;
}
mxSaiInit.OutputDrive = SAI_OUTPUTDRIVE_ENABLE;
mxSaiInit.Synchro = SAI_ASYNCHRONOUS;
mxSaiInit.SynchroExt = SAI_SYNCEXT_DISABLE;
mxSaiInit.SlotActive = SAI_SLOTACTIVE_0 | SAI_SLOTACTIVE_1;
/* SAI peripheral initialization */
if (MX_SAI1_Init(&haudio_out_sai, &mxSaiInit) != HAL_OK)
{
status = 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)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_TX_HALFCOMPLETE_CB_ID, SAI_TxHalfCpltCallback)
!= HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_SAI_RegisterCallback(&haudio_out_sai, HAL_SAI_ERROR_CB_ID, SAI_ErrorCallback) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 1) */
else
{
/* Initialize audio codec */
#if defined(USE_AUDIO_CODEC_WM8904)
WM8904_Init_t codec_init;
codec_init.InputDevice = (Audio_In_Ctx[0].State != AUDIO_IN_STATE_RESET)
? WM8904_IN_MIC1 : WM8904_IN_NONE;
codec_init.OutputDevice = WM8904_OUT_HEADPHONE;
codec_init.Resolution = WM8904_RESOLUTION_16B;
#else /* USE_AUDIO_CODEC_WM8904 */
CS42L51_Init_t codec_init;
codec_init.InputDevice = (Audio_In_Ctx[0].State != AUDIO_IN_STATE_RESET)
? CS42L51_IN_MIC1 : CS42L51_IN_NONE;
codec_init.OutputDevice = CS42L51_OUT_HEADPHONE;
codec_init.Resolution = CS42L51_RESOLUTION_16B; /* Not used */
#endif /* USE_AUDIO_CODEC_WM8904 */
codec_init.Frequency = AudioInit->SampleRate;
codec_init.Volume = AudioInit->Volume;
if (Audio_Drv->Init(Audio_CompObj, &codec_init) < 0)
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update audio out context state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_STOP;
}
}
}
}
}
return status;
}
/**
* @brief De-initialize the audio out peripherals.
* @param Instance Audio out instance.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_DeInit(uint32_t Instance)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_RESET)
{
/* SAI peripheral de-initialization */
if (HAL_SAI_DeInit(&haudio_out_sai) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
/* De-initialize audio codec if not currently used by audio in instance 0 */
else
{
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 0)
SAI_MspDeInit(&haudio_out_sai);
#endif /* (USE_HAL_SAI_REGISTER_CALLBACKS == 0) */
if (Audio_In_Ctx[0].State == AUDIO_IN_STATE_RESET)
{
if (Audio_Drv->DeInit(Audio_CompObj) < 0)
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
#if defined(USE_AUDIO_CODEC_CS42L51)
else
{
(void)CS42L51_PowerDown();
}
#endif /* USE_AUDIO_CODEC_CS42L51 */
}
}
if (status == BSP_ERROR_NONE)
{
/* Update audio out context */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_RESET;
Audio_Out_Ctx[Instance].IsMute = 0U;
}
}
else
{
/* Nothing to do */
}
return status;
}
/**
* @brief Start playing audio stream from a data buffer for a determined size.
* @param Instance Audio out instance.
* @param pData Pointer on data buffer.
* @param NbrOfBytes Size of buffer in bytes. Maximum size is 65535 bytes.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_Play(uint32_t Instance, uint8_t *pData, uint32_t NbrOfBytes)
{
int32_t status = BSP_ERROR_NONE;
uint16_t NbrOfDmaDatas;
if ((Instance >= AUDIO_OUT_INSTANCES_NBR) || (pData == NULL))
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_STOP)
{
status = BSP_ERROR_BUSY;
}
else
{
/* Compute number of DMA data to tranfser according resolution */
if (Audio_Out_Ctx[Instance].BitsPerSample == AUDIO_RESOLUTION_16B)
{
NbrOfDmaDatas = (uint16_t)(NbrOfBytes / 2U);
}
else /* AUDIO_RESOLUTION_24b */
{
NbrOfDmaDatas = (uint16_t)(NbrOfBytes / 4U);
}
/* Initiate a DMA transfer of audio samples towards the serial audio interface */
if (HAL_SAI_Transmit_DMA(&haudio_out_sai, pData, NbrOfDmaDatas) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
/* Call the audio codec play function */
else if (Audio_Drv->Play(Audio_CompObj) < 0)
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update audio out state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_PLAYING;
}
}
return status;
}
/**
* @brief Pause playback of audio stream.
* @param Instance Audio out instance.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_Pause(uint32_t Instance)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_PLAYING)
{
status = BSP_ERROR_BUSY;
}
/* Call the audio codec pause function */
else if (Audio_Drv->Pause(Audio_CompObj) < 0)
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
/* Pause DMA transfer of audio samples towards the serial audio interface */
else if (HAL_SAI_DMAPause(&haudio_out_sai) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Update audio out state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_PAUSE;
}
return status;
}
/**
* @brief Resume playback of audio stream.
* @param Instance Audio out instance.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_Resume(uint32_t Instance)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_PAUSE)
{
status = BSP_ERROR_BUSY;
}
/* Call the audio codec resume function */
else if (Audio_Drv->Resume(Audio_CompObj) < 0)
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
/* Resume DMA transfer of audio samples towards the serial audio interface */
else if (HAL_SAI_DMAResume(&haudio_out_sai) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Update audio out state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_PLAYING;
}
return status;
}
/**
* @brief Stop playback of audio stream.
* @param Instance Audio out instance.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_Stop(uint32_t Instance)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_STOP)
{
/* Nothing to do */
}
else if ((Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_PLAYING) &&
(Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_PAUSE))
{
status = BSP_ERROR_BUSY;
}
/* Call the audio codec stop function */
#if defined(USE_AUDIO_CODEC_WM8904)
else if (Audio_Drv->Stop(Audio_CompObj, WM8904_PDWN_SW) < 0)
#else /* USE_AUDIO_CODEC_WM8904 */
else if (Audio_Drv->Stop(Audio_CompObj, CS42L51_PDWN_SW) < 0)
#endif /* USE_AUDIO_CODEC_WM8904 */
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
/* Stop DMA transfer of audio samples towards the serial audio interface */
else if (HAL_SAI_DMAStop(&haudio_out_sai) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Update audio out state */
Audio_Out_Ctx[Instance].State = AUDIO_OUT_STATE_STOP;
}
return status;
}
/**
* @brief Mute playback of audio stream.
* @param Instance Audio out instance.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_Mute(uint32_t Instance)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
/* Check audio out mute status */
else if (Audio_Out_Ctx[Instance].IsMute == 1U)
{
/* Nothing to do */
}
/* Call the audio codec mute function */
#if defined(USE_AUDIO_CODEC_WM8904)
else if (Audio_Drv->SetMute(Audio_CompObj, WM8904_MUTE_ON) < 0)
#else /* USE_AUDIO_CODEC_WM8904 */
else if (Audio_Drv->SetMute(Audio_CompObj, CS42L51_MUTE_ON) < 0)
#endif /* USE_AUDIO_CODEC_WM8904 */
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update audio out mute status */
Audio_Out_Ctx[Instance].IsMute = 1U;
}
return status;
}
/**
* @brief Unmute playback of audio stream.
* @param Instance Audio out instance.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_UnMute(uint32_t Instance)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
/* Check audio out mute status */
else if (Audio_Out_Ctx[Instance].IsMute == 0U)
{
/* Nothing to do */
}
/* Call the audio codec mute function */
#if defined(USE_AUDIO_CODEC_WM8904)
else if (Audio_Drv->SetMute(Audio_CompObj, WM8904_MUTE_OFF) < 0)
#else /* USE_AUDIO_CODEC_WM8904 */
else if (Audio_Drv->SetMute(Audio_CompObj, CS42L51_MUTE_OFF) < 0)
#endif /* USE_AUDIO_CODEC_WM8904 */
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update audio out mute status */
Audio_Out_Ctx[Instance].IsMute = 0U;
}
return status;
}
/**
* @brief Check audio out mute status.
* @param Instance Audio out instance.
* @param IsMute Pointer to mute status. Value is 1 for mute, 0 for unmute status.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_IsMute(uint32_t Instance, uint32_t *IsMute)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
/* Get the current audio out mute status */
else
{
*IsMute = Audio_Out_Ctx[Instance].IsMute;
}
return status;
}
/**
* @brief Set audio out volume.
* @param Instance Audio out instance.
* @param Volume Volume level in percentage from 0% to 100%.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_SetVolume(uint32_t Instance, uint32_t Volume)
{
int32_t status = BSP_ERROR_NONE;
if ((Instance >= AUDIO_OUT_INSTANCES_NBR) || (Volume > 100U))
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
else
{
/* Call the audio codec volume control function */
if (Audio_Drv->SetVolume(Audio_CompObj, VOLUME_OUTPUT, (uint8_t) Volume) < 0)
{
status = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Store volume on audio out context */
Audio_Out_Ctx[Instance].Volume = Volume;
}
}
return status;
}
/**
* @brief Get audio out volume.
* @param Instance Audio out instance.
* @param Volume Pointer to volume level in percentage from 0% to 100%.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_GetVolume(uint32_t Instance, uint32_t *Volume)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
/* Get the current audio out volume */
else
{
*Volume = Audio_Out_Ctx[Instance].Volume;
}
return status;
}
/**
* @brief Set audio out sample rate.
* @param Instance Audio out instance.
* @param SampleRate Sample rate of the audio out stream.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_SetSampleRate(uint32_t Instance, uint32_t SampleRate)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_STOP)
{
status = 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))
{
status = BSP_ERROR_FEATURE_NOT_SUPPORTED;
}
/* Check if sample rate is modified */
else if (Audio_Out_Ctx[Instance].SampleRate == SampleRate)
{
/* Nothing to do */
}
else
{
/* Update SAI1 clock config */
haudio_out_sai.Init.AudioFrequency = SampleRate;
if (MX_SAI1_ClockConfig(&haudio_out_sai, SampleRate) != HAL_OK)
{
status = BSP_ERROR_CLOCK_FAILURE;
}
/* Re-initialize SAI1 with new sample rate */
else if (HAL_SAI_Init(&haudio_out_sai) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
/* Store new sample rate on audio out context */
else
{
Audio_Out_Ctx[Instance].SampleRate = SampleRate;
}
}
return status;
}
/**
* @brief Get audio out sample rate.
* @param Instance Audio out instance.
* @param SampleRate Pointer to sample rate of the audio out stream.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_GetSampleRate(uint32_t Instance, uint32_t *SampleRate)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
/* Get the current audio out sample rate */
else
{
*SampleRate = Audio_Out_Ctx[Instance].SampleRate;
}
return status;
}
/**
* @brief Set audio out device.
* @param Instance Audio out instance.
* @param Device Device of the audio out stream.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_SetDevice(uint32_t Instance, uint32_t Device)
{
int32_t status = BSP_ERROR_NONE;
UNUSED(Device);
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State != AUDIO_OUT_STATE_STOP)
{
status = BSP_ERROR_BUSY;
}
else
{
/* Nothing to do because there is only one device (AUDIO_OUT_HEADPHONE) */
}
return status;
}
/**
* @brief Get audio out device.
* @param Instance Audio out instance.
* @param Device Pointer to device of the audio out stream.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_GetDevice(uint32_t Instance, uint32_t *Device)
{
int32_t status = BSP_ERROR_NONE;
if (Instance >= AUDIO_OUT_INSTANCES_NBR)
{
status = BSP_ERROR_WRONG_PARAM;
}
/* Check audio out state */
else if (Audio_Out_Ctx[Instance].State == AUDIO_OUT_STATE_RESET)
{
status = BSP_ERROR_BUSY;
}
/* Get the current audio out device */
else
{
*Device = Audio_Out_Ctx[Instance].Device;
}
return status;
}
/**
* @brief Set bits per sample for the audio out stream.
* @param Instance Audio out instance.
* @param BitsPerSample Bits per sample of the audio out stream.
* @retval BSP status.
*/
int32_t BSP_AUDIO_OUT_SetBitsPerSample(uint32_t Instance, uint32_t BitsPerSample)