-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32l4r9i_discovery_audio.c
2081 lines (1852 loc) · 72 KB
/
stm32l4r9i_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 stm32l4r9i_discovery_audio.c
* @author MCD Application Team
* @brief This file provides a set of functions needed to manage the
* Audio driver for the STM32L4R9I_DISCOVERY board.
******************************************************************************
* @attention
*
* Copyright (c) 2017 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/*==============================================================================
User NOTES
1. How To use this driver:
--------------------------
+ This driver supports STM32L4xx devices on STM32L4R9I_DISCOVERY (MB1311) Discovery boards.
a) to play an audio file through headset. All functions names start by BSP_AUDIO_OUT_xxx.
b) to record an audio file through digital microphones (MP34DT01TR ST mems)
or analog microphone (headset microphone). All functions names start by BSP_AUDIO_IN_xxx.
a) PLAY A FILE:
==============
+ Call the function BSP_AUDIO_OUT_Init(
OutputDevice: physical output mode (only OUTPUT_DEVICE_HEADPHONE).
Volume : Initial volume to be set (0 is min (mute), 100 is max (100%)
AudioFreq : Audio frequency in Hz (8000, 16000, 22500, 32000...)
this parameter is relative to the audio file/stream type.
)
This function configures all the hardware required for the audio application (codec, I2C, SAI,
GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if configuration is OK.
If the returned value is different from AUDIO_OK or the function is stuck then the communication with
the audio codec has failed.
- OUTPUT_DEVICE_HEADPHONE: only Headphone output is available on this board.
+ Call the function BSP_AUDIO_OUT_RegisterCallbacks to register user callbacks
required to manage audio data streaming towards the audio codec (ErrorCallback(),
HalfTransfer_CallBack() and TransferComplete_CallBack()).
+ Call the function BSP_AUDIO_OUT_Play() to start audio playback (for the first time).
+ Call the function BSP_AUDIO_OUT_Pause() to pause audio playback.
+ Call the function BSP_AUDIO_OUT_Resume() to resume audio playback.
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 audio playback.
+ To modify the volume level, the sampling frequency, the device output mode,
the mute status or the audio configuration or the stop, use the functions: BSP_AUDIO_OUT_SetVolume(),
AUDIO_OUT_SetFrequency(), BSP_AUDIO_OUT_SetOutputMode(), BSP_AUDIO_OUT_SetMute()and
BSP_AUDIO_OUT_ChangeAudioConfig().
Driver architecture:
--------------------
+ This driver provides the audio layer high level API: it consists in functions
exported in the stm32l4r9i_discovery_audio.h file (e.g. BSP_AUDIO_OUT_Init(),
BSP_AUDIO_OUT_Play(), ...).
+ This driver also includes the Media Access Layer (MAL): it consists in
functions allowing to access setup the audio devices. These functions
are included as local functions into the stm32l4r9i_discovery_audio.c file
(e.g. AUDIO_SAIx_Init()).
Known Limitations:
------------------
1- Communication with the audio codec (through I2C) may be corrupted if it is interrupted by some
user interrupt routines (in this case, interrupts could be disabled just before the start of
communication then re-enabled when it is over). Note that this communication is only done at
the configuration phase (BSP_AUDIO_OUT_Init() or BSP_AUDIO_OUT_Stop()) and when Volume control modification is
performed (BSP_AUDIO_OUT_SetVolume() or BSP_AUDIO_OUT_SetMute()or BSP_AUDIO_OUT_SetOutputMode()).
When the audio data is played, no communication is required with the audio codec.
2- Parsing of audio file is not implemented (in order to determine audio file properties: Mono/Stereo, Data size,
File size, Audio Frequency, Audio Data header size ...). The configuration is fixed for the given audio file.
3- Supports only 16-bits audio data size.
b) RECORD A FILE:
================
+ Call the function BSP_AUDIO_IN_InitEx(
InputDevice: physical input mode (INPUT_DEVICE_DIGITAL_MIC
INPUT_DEVICE_DIGITAL_MIC1, INPUT_DEVICE_DIGITAL_MIC2
or INPUT_DEVICE_ANALOG_MIC)
AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...)
)
This function configures all the hardware required for the audio application (DFSDM or SAI,
GPIOs, DMA and interrupt if needed). This function returns AUDIO_OK if the
configuration completes successfully.
- INPUT_DEVICE_DIGITAL_MIC: Record from digital microphones mounted on board.
- INPUT_DEVICE_DIGITAL_MIC1: Record from digital microphone 1 mounted on board (left microphone).
- INPUT_DEVICE_DIGITAL_MIC2: Record from digital microphone 2 mounted on board (right microphone).
- INPUT_DEVICE_ANALOG_MIC: Record from headset microphone.
+ Call the function BSP_AUDIO_IN_RegisterCallbacks to register user callbacks
used to stream audio data toward the record buffer (ErrorCallback(),
HalfTransfer_CallBack() and TransferComplete_CallBack()).
+ Call the function BSP_AUDIO_IN_Record(
pbuf Main buffer pointer for the recorded data storing
size Current size of the recorded buffer
)
to start recording from the microphone.
+ Call the function BSP_AUDIO_IN_Stop() to stop recording.
==============================================================================*/
/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "stm32l4r9i_discovery_audio.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32L4R9I_DISCOVERY
* @{
*/
/** @defgroup STM32L4R9I_DISCOVERY_AUDIO STM32L4R9I_DISCOVERY AUDIO
* @brief This file includes the low layer driver for cs42l51 Audio Codec
* available on STM32L4R9I_DISCOVERY board (MB1311).
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/** @defgroup STM32L4R9I_DISCOVERY_AUDIO_Private_Types Private Types
* @{
*/
typedef struct
{
AUDIO_DrvTypeDef * AudioDrv; /* Audio codec driver */
uint32_t OutputDevice; /* Output device */
uint32_t Frequency; /* Playback frequency */
uint32_t Volume; /* Playback volume */
Audio_CallbackTypeDef CbError; /* pointer to the callback function invoked when error occurs */
Audio_CallbackTypeDef CbHalfTransfer; /* pointer to the callback function invoked when half transfer occurs */
Audio_CallbackTypeDef CbTransferComplete; /* pointer to the callback function invoked when transfer complete occurs */
} AUDIO_OUT_TypeDef;
typedef struct
{
AUDIO_DrvTypeDef * AudioDrv; /* Audio codec driver */
DFSDM_Channel_HandleTypeDef hDfsdmLeftChannel; /* DFSDM channel handle used for left channel */
DFSDM_Channel_HandleTypeDef hDfsdmRightChannel; /* DFSDM channel handle used for right channel */
DMA_HandleTypeDef hDmaDfsdmLeft; /* DMA handle used for DFSDM regular conversions on left channel */
DMA_HandleTypeDef hDmaDfsdmRight; /* DMA handle used for DFSDM regular conversions on right channel */
int32_t * LeftRecBuff; /* Buffers for left samples */
int32_t * RightRecBuff; /* Buffers for right samples */
uint32_t InputDevice; /* Input device */
uint32_t Frequency; /* Record Frequency */
uint32_t BitResolution; /* Record bit resolution */
uint32_t ChannelNbr; /* Record Channel Number */
uint16_t * pRecBuf; /* Pointer to record user buffer */
uint32_t RecSize; /* Size to record in mono, double size to record in stereo */
Audio_CallbackTypeDef CbError; /* pointer to the callback function invoked when a DMA transfer fails */
Audio_CallbackTypeDef CbHalfTransfer; /* pointer to the callback function invoked when half of the DMA transfer is completed */
Audio_CallbackTypeDef CbTransferComplete; /* pointer to the callback function invoked when the DMA transfer is completed */
} AUDIO_IN_TypeDef;
/**
* @}
*/
/* Private macros ------------------------------------------------------------*/
/** @defgroup STM32L4R9I_DISCOVERY_AUDIO_Private_Macros Private Macros
* @{
*/
/*### PLAY ###*/
/* SCK(kHz) = SAI_CK_x/(SAIClockDivider*2*256) */
#define SAIClockDivider(__FREQUENCY__) \
(__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 24 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 4 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 12 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 2 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 6 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 1 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 4 : 2 \
/*### RECORD ###*/
#define DFSDMOverSampling(__FREQUENCY__) \
(__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 256 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 256 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 128 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 128 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 64 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 64 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 32 : 16 \
#define DFSDMClockDivider(__FREQUENCY__) \
(__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 24 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 4 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 24 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 4 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 24 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 4 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 32 : 32 \
#define DFSDMFilterOrder(__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_SINC4_ORDER \
: (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? DFSDM_FILTER_SINC4_ORDER : DFSDM_FILTER_SINC5_ORDER \
#define DFSDMRightBitShift(__FREQUENCY__) \
(__FREQUENCY__ == AUDIO_FREQUENCY_8K) ? 7 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_11K) ? 7 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_16K) ? 4 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_22K) ? 4 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_32K) ? 7 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_44K) ? 7 \
: (__FREQUENCY__ == AUDIO_FREQUENCY_48K) ? 3 : 4 \
/* Saturate the record PCM sample */
#define SaturaLH(N, L, H) (((N)<(L))?(L):(((N)>(H))?(H):(N)))
/**
* @}
*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup STM32L4R9I_DISCOVERY_AUDIO_Private_Variables Private Variables
* @{
*/
/* Audio output context information */
static AUDIO_OUT_TypeDef hAudioOut = {0};
/* Audio input context information */
static AUDIO_IN_TypeDef hAudioIn = {0};
/* SAI DMA handle */
static DMA_HandleTypeDef hDmaSaiTx;
static DMA_HandleTypeDef hDmaSaiRx;
static uint32_t DmaLeftRecHalfBuffCplt;
static uint32_t DmaLeftRecBuffCplt;
static uint32_t DmaRightRecHalfBuffCplt;
static uint32_t DmaRightRecBuffCplt;
/**
* @}
*/
/* Exported variables ---------------------------------------------------------*/
/** @defgroup STM32L4R9I_DISCOVERY_AUDIO_Exported_Variables Exported Variables
* @{
*/
/* SAIx handle */
SAI_HandleTypeDef BSP_AUDIO_hSai_Tx;
SAI_HandleTypeDef BSP_AUDIO_hSai_Rx;
/* DFSDM filter handle */
DFSDM_Filter_HandleTypeDef BSP_AUDIO_hDfsdmLeftFilter;
DFSDM_Filter_HandleTypeDef BSP_AUDIO_hDfsdmRightFilter;
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup STM32L4R9I_DISCOVERY_AUDIO_Private_Functions Private Functions
* @{
*/
static uint8_t AUDIO_SAIx_Init(uint32_t AudioFreq);
static uint8_t AUDIO_SAIx_DeInit(void);
static uint8_t AUDIO_DFSDMx_Init(uint32_t AudioFreq);
static uint8_t AUDIO_DFSDMx_DeInit(void);
static uint8_t AUDIO_SAIPLLConfig(uint32_t AudioFreq);
/**
* @}
*/
/* Exported functions --------------------------------------------------------*/
/** @addtogroup STM32L4R9I_DISCOVERY_AUDIO_Exported_Functions
* @{
*/
/**
* @brief Configures the audio codec related peripherals.
* @param OutputDevice: OUTPUT_DEVICE_HEADPHONE.
* @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
* @param AudioFreq: Audio frequency used to play the audio stream.
* @retval BSP AUDIO status
* @note The SAI PLL input clock must be configure in the user application.
* The SAI PLL configuration done within this function assumes that
* the SAI PLL input clock runs at 8 MHz.
*/
uint8_t BSP_AUDIO_OUT_Init(uint16_t OutputDevice,
uint8_t Volume,
uint32_t AudioFreq)
{
/* Initialize the audio output context */
hAudioOut.AudioDrv = &cs42l51_drv;
hAudioOut.OutputDevice = OutputDevice;
hAudioOut.Frequency = AudioFreq;
hAudioOut.Volume = Volume;
hAudioOut.CbError = (Audio_CallbackTypeDef)NULL;
hAudioOut.CbHalfTransfer = (Audio_CallbackTypeDef)NULL;
hAudioOut.CbTransferComplete = (Audio_CallbackTypeDef)NULL;
/* Check if input device is currently used */
if(hAudioIn.InputDevice != 0)
{
/* If input device is currently used, SAI PLL is already initialized */
/* Check that AudioFreq for record and playback is the same */
if(hAudioIn.Frequency != hAudioOut.Frequency)
{
return AUDIO_ERROR;
}
}
else
{
/* Configure the SAI PLL according to the requested audio frequency */
if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
/* If input device is analogic mic, SAI is already initialized */
if(hAudioIn.InputDevice != INPUT_DEVICE_ANALOG_MIC)
{
/* SAI data transfer preparation: prepare the Media to be used for the audio
transfer from memory to SAI peripheral. */
if (AUDIO_SAIx_Init(AudioFreq) != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
/* Initialize the audio codec internal registers */
if (hAudioOut.AudioDrv->Init(AUDIO_I2C_ADDRESS,
(hAudioOut.OutputDevice | hAudioIn.InputDevice),
Volume,
AudioFreq) != 0)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief De-Initializes audio codec related peripherals
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_DeInit(void)
{
if(hAudioIn.InputDevice == INPUT_DEVICE_ANALOG_MIC)
{
/* Reset playback path on audio codec */
if(hAudioIn.AudioDrv->Init(AUDIO_I2C_ADDRESS,
hAudioIn.InputDevice,
(uint8_t) hAudioOut.Volume,
hAudioIn.Frequency) != 0)
{
return AUDIO_ERROR;
}
}
else
{
/* De-initializes SAI interface */
if(AUDIO_SAIx_DeInit() != AUDIO_OK)
{
return AUDIO_ERROR;
}
/* DeInit audio codec */
hAudioOut.AudioDrv->DeInit();
}
/* Disable SAI PLL if no more device is used */
if(hAudioIn.InputDevice == 0)
{
if (AUDIO_SAIx_PLL_DISABLE() != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
/* Reset the audio output context */
memset(&hAudioOut, 0, sizeof(hAudioOut));
return AUDIO_OK;
}
/**
* @brief Starts playing audio stream from a data buffer for a determined size.
* @param pData: pointer on PCM samples buffer
* @param Size: Number of audio data HALF WORD.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_Play(uint16_t* pData, uint32_t Size)
{
/* Initiate a DMA transfer of PCM samples towards the serial audio interface */
if (HAL_SAI_Transmit_DMA(&BSP_AUDIO_hSai_Tx, (uint8_t *)pData, DMA_MAX(Size))!= HAL_OK)
{
return AUDIO_ERROR;
}
/* Call the audio Codec Play function */
if (hAudioOut.AudioDrv->Play(AUDIO_I2C_ADDRESS, pData, Size) != 0)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief Sends n-Bytes on the SAI interface.
* @param pData: pointer on PCM samples buffer
* @param Size: number of data to be written
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_ChangeBuffer(uint16_t *pData, uint16_t Size)
{
/* Initiate a DMA transfer of PCM samples towards the serial audio interface */
if (HAL_SAI_Transmit_DMA(&BSP_AUDIO_hSai_Tx, (uint8_t *)pData, Size)!= HAL_OK)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief This function Pauses the audio file stream. In case
* of using DMA, the DMA Pause feature is used.
* @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 AUDIO status
*/
uint8_t BSP_AUDIO_OUT_Pause(void)
{
/* Call the Audio Codec Pause function */
if (hAudioOut.AudioDrv->Pause(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}
/* Pause DMA transfer of PCM samples towards the serial audio interface */
if (HAL_SAI_DMAPause(&BSP_AUDIO_hSai_Tx)!= HAL_OK)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief This function Resumes the audio file stream.
* @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 AUDIO status
*/
uint8_t BSP_AUDIO_OUT_Resume(void)
{
/* Call the Audio Codec Resume function */
if (hAudioOut.AudioDrv->Resume(AUDIO_I2C_ADDRESS) != 0)
{
return AUDIO_ERROR;
}
/* Resume DMA transfer of PCM samples towards the serial audio interface */
if (HAL_SAI_DMAResume(&BSP_AUDIO_hSai_Tx)!= HAL_OK)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief Stops audio playing and Power down the Audio Codec.
* @param Option: could be one of the following parameters
* - CODEC_PDWN_SW: for software power off (by writing registers).
* Then no need to reconfigure the Codec after power on.
* - CODEC_PDWN_HW: completely shut down the codec (physically).
* Then need to reconfigure the Codec after power on.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_Stop(uint32_t Option)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(Option);
/* Call Audio Codec Stop function */
if (hAudioOut.AudioDrv->Stop(AUDIO_I2C_ADDRESS, Option) != 0)
{
return AUDIO_ERROR;
}
/* Wait at least 100ms */
HAL_Delay(100);
/* Stop DMA transfer of PCM samples towards the serial audio interface */
if (HAL_SAI_DMAStop(&BSP_AUDIO_hSai_Tx)!= HAL_OK)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief Controls the current audio volume level.
* @param Volume: Volume level to be set in percentage from 0% to 100% (0 for
* Mute and 100 for Max volume level).
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_SetVolume(uint8_t Volume)
{
/* Call the codec volume control function with converted volume value */
if (hAudioOut.AudioDrv->SetVolume(AUDIO_I2C_ADDRESS, Volume) != 0)
{
return AUDIO_ERROR;
}
hAudioOut.Volume = Volume;
return AUDIO_OK;
}
/**
* @brief Enables or disables the MUTE mode by software
* @param Cmd: Could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to
* unmute the codec and restore previous volume level.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_SetMute(uint32_t Cmd)
{
/* Call the Codec Mute function */
if (hAudioOut.AudioDrv->SetMute(AUDIO_I2C_ADDRESS, Cmd) != 0)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief Switch dynamically (while audio file is being played) the output
* target (speaker or headphone).
* @param Output: The audio output target: OUTPUT_DEVICE_SPEAKER,
* OUTPUT_DEVICE_HEADPHONE or OUTPUT_DEVICE_BOTH
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_SetOutputMode(uint8_t Output)
{
/* Call the Codec output device function */
if (hAudioOut.AudioDrv->SetOutputMode(AUDIO_I2C_ADDRESS, Output) != 0)
{
return AUDIO_ERROR;
}
return AUDIO_OK;
}
/**
* @brief Updates the audio frequency.
* @param AudioFreq: Audio frequency used to play the audio stream.
* @note The SAI PLL input clock must be configure in the user application.
* The SAI PLL configuration done within this function assumes that
* the SAI PLL input clock runs at 8 MHz.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_OUT_SetFrequency(uint32_t AudioFreq)
{
uint8_t TxData[2] = {0x00, 0x00};
/* Configure the SAI PLL according to the requested audio frequency */
if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK)
{
return AUDIO_ERROR;
}
/* Disable SAI peripheral to allow access to SAI internal registers */
__HAL_SAI_DISABLE(&BSP_AUDIO_hSai_Tx);
__HAL_SAI_DISABLE(&BSP_AUDIO_hSai_Rx);
/* Update the SAI audio frequency configuration */
BSP_AUDIO_hSai_Tx.Init.Mckdiv = SAIClockDivider(AudioFreq);
HAL_SAI_Init(&BSP_AUDIO_hSai_Tx);
BSP_AUDIO_hSai_Rx.Init.Mckdiv = SAIClockDivider(AudioFreq);
HAL_SAI_Init(&BSP_AUDIO_hSai_Rx);
/* Enable SAI peripheral to generate MCLK */
__HAL_SAI_ENABLE(&BSP_AUDIO_hSai_Tx);
/* Transmit one byte to start FS generation */
if(HAL_SAI_Transmit(&BSP_AUDIO_hSai_Tx, TxData, 2, 1000) != HAL_OK)
{
return AUDIO_ERROR;
}
hAudioOut.Frequency = AudioFreq;
return AUDIO_OK;
}
/**
* @brief Changes the Audio Out Configuration.
* @param AudioOutOption: specifies the audio out new configuration
* This parameter can be any value of @ref BSP_Audio_Out_Option
* @note This API should be called after the BSP_AUDIO_OUT_Init() to adjust the
* audio out configuration.
* @retval None
*/
void BSP_AUDIO_OUT_ChangeAudioConfig(uint32_t AudioOutOption)
{
uint8_t TxData[2] = {0x00, 0x00};
/********** Playback Buffer circular/normal mode **********/
if(AudioOutOption & BSP_AUDIO_OUT_CIRCULARMODE)
{
/* Deinitialize the Stream to update DMA mode */
HAL_DMA_DeInit(BSP_AUDIO_hSai_Tx.hdmatx);
/* Update the SAI audio Transfer DMA mode */
BSP_AUDIO_hSai_Tx.hdmatx->Init.Mode = DMA_CIRCULAR;
/* Configure the DMA Stream with new Transfer DMA mode */
HAL_DMA_Init(BSP_AUDIO_hSai_Tx.hdmatx);
}
else /* BSP_AUDIO_OUT_NORMALMODE */
{
/* Deinitialize the Stream to update DMA mode */
HAL_DMA_DeInit(BSP_AUDIO_hSai_Tx.hdmatx);
/* Update the SAI audio Transfer DMA mode */
BSP_AUDIO_hSai_Tx.hdmatx->Init.Mode = DMA_NORMAL;
/* Configure the DMA Stream with new Transfer DMA mode */
HAL_DMA_Init(BSP_AUDIO_hSai_Tx.hdmatx);
}
/********** Playback Buffer stereo/mono mode **********/
if(AudioOutOption & BSP_AUDIO_OUT_STEREOMODE)
{
/* Disable SAI peripheral to allow access to SAI internal registers */
__HAL_SAI_DISABLE(&BSP_AUDIO_hSai_Tx);
/* Update the SAI audio frame slot configuration */
BSP_AUDIO_hSai_Tx.Init.MonoStereoMode = SAI_STEREOMODE;
HAL_SAI_Init(&BSP_AUDIO_hSai_Tx);
/* Enable SAI peripheral to generate MCLK */
__HAL_SAI_ENABLE(&BSP_AUDIO_hSai_Tx);
/* Transmit one byte to start FS generation */
HAL_SAI_Transmit(&BSP_AUDIO_hSai_Tx, TxData, 2, 1000);
}
else /* BSP_AUDIO_OUT_MONOMODE */
{
/* Disable SAI peripheral to allow access to SAI internal registers */
__HAL_SAI_DISABLE(&BSP_AUDIO_hSai_Tx);
/* Update the SAI audio frame slot configuration */
BSP_AUDIO_hSai_Tx.Init.MonoStereoMode = SAI_MONOMODE;
HAL_SAI_Init(&BSP_AUDIO_hSai_Tx);
/* Enable SAI peripheral to generate MCLK */
__HAL_SAI_ENABLE(&BSP_AUDIO_hSai_Tx);
/* Transmit one byte to start FS generation */
HAL_SAI_Transmit(&BSP_AUDIO_hSai_Tx, TxData, 2, 1000);
}
}
/**
* @brief register user callback functions
* @param ErrorCallback: pointer to the error callback function
* @param HalfTransferCallback: pointer to the half transfer callback function
* @param TransferCompleteCallback: pointer to the transfer complete callback function
* @retval None
*/
void BSP_AUDIO_OUT_RegisterCallbacks(Audio_CallbackTypeDef ErrorCallback,
Audio_CallbackTypeDef HalfTransferCallback,
Audio_CallbackTypeDef TransferCompleteCallback)
{
hAudioOut.CbError = ErrorCallback;
hAudioOut.CbHalfTransfer = HalfTransferCallback;
hAudioOut.CbTransferComplete = TransferCompleteCallback;
}
/**
* @brief Tx Transfer completed callbacks.
* @param hsai: SAI handle
* @retval None
*/
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
{
/* Invoke the registered 'TransferComplete' function (if any) */
if (hAudioOut.CbTransferComplete != (Audio_CallbackTypeDef)NULL)
{
hAudioOut.CbTransferComplete();
}
}
/**
* @brief Tx Half Transfer completed callbacks.
* @param hsai: SAI handle
* @retval None
*/
void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
{
/* Invoke the registered 'HalfTransfer' callback function (if any) */
if (hAudioOut.CbHalfTransfer != (Audio_CallbackTypeDef)NULL)
{
hAudioOut.CbHalfTransfer();
}
}
/**
* @brief SAI error callbacks.
* @param hsai: SAI handle
* @retval None
*/
void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai)
{
/* Invoke the registered 'ErrorCallback' callback function (if any) */
if (hAudioOut.CbError != (Audio_CallbackTypeDef)NULL)
{
hAudioOut.CbError();
}
/* Invoke the registered 'ErrorCallback' callback function (if any) */
if (hAudioIn.CbError != (Audio_CallbackTypeDef)NULL)
{
hAudioIn.CbError();
}
}
/**
* @}
*/
/** @addtogroup STM32L4R9I_DISCOVERY_AUDIO_Exported_Functions
* @{
*/
/**
* @brief Initializes microphone related peripherals.
* @note This function assumes that the SAI input clock (through PLL_M)
* is already configured and ready to be used.
* @param AudioFreq: Audio frequency to be configured for the SAI peripheral.
* @param BitRes: Audio frequency to be configured for the SAI peripheral.
* @param ChnlNbr: Audio frequency to be configured for the SAI peripheral.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
{
return BSP_AUDIO_IN_InitEx(INPUT_DEVICE_DIGITAL_MIC, AudioFreq, BitRes, ChnlNbr);
}
/**
* @brief Initialize wave recording.
* @param InputDevice: INPUT_DEVICE_DIGITAL_MIC, INPUT_DEVICE_DIGITAL_MIC1,
* INPUT_DEVICE_DIGITAL_MIC2 or INPUT_DEVICE_ANALOG_MIC.
* @param AudioFreq: Audio frequency to be configured.
* @param BitRes: Audio bit resolution to be configured..
* @param ChnlNbr: Number of channel to be configured.
* @retval AUDIO_OK if correct communication, else wrong communication
*/
uint8_t BSP_AUDIO_IN_InitEx(uint16_t InputDevice, uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
{
/* Update the audio input context */
hAudioIn.AudioDrv = &cs42l51_drv;
hAudioIn.InputDevice = InputDevice;
hAudioIn.Frequency = AudioFreq;
hAudioIn.BitResolution = BitRes;
hAudioIn.ChannelNbr = ChnlNbr;
hAudioIn.CbError = (Audio_CallbackTypeDef)NULL;
hAudioIn.CbHalfTransfer = (Audio_CallbackTypeDef)NULL;
hAudioIn.CbTransferComplete = (Audio_CallbackTypeDef)NULL;
/* Check channel number according device : only record mono with analog mic and stereo with digital mic are allowed */
if(((InputDevice == INPUT_DEVICE_DIGITAL_MIC) && (ChnlNbr == 1)) ||
((InputDevice == INPUT_DEVICE_DIGITAL_MIC1) && (ChnlNbr == 2)) ||
((InputDevice == INPUT_DEVICE_DIGITAL_MIC2) && (ChnlNbr == 2)) ||
((InputDevice == INPUT_DEVICE_ANALOG_MIC) && (ChnlNbr == 2)))
{
return AUDIO_ERROR;
}
/* Check if output device is currently used */
if(hAudioOut.OutputDevice != 0)
{
/* If output device is currently used, SAI PLL is already initialized */
/* Check that AudioFreq for record and playback is the same */
if(hAudioIn.Frequency != hAudioOut.Frequency)
{
return AUDIO_ERROR;
}
}
else
{
/* Configure the SAI PLL according to the requested audio frequency */
if (AUDIO_SAIPLLConfig(AudioFreq) != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
if(InputDevice != INPUT_DEVICE_ANALOG_MIC)
{
/* Initializes the Digital Filter for Sigma-Delta Modulators interface */
if(AUDIO_DFSDMx_Init(AudioFreq) != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
else
{ /* INPUT_DEVICE_ANALOG_MIC */
/* If output device is currently used, SAI is already initialized */
if(hAudioOut.OutputDevice == 0)
{
/* SAI data transfer preparation: prepare the Media to be used for the audio
transfer from SAI peripheral to memory. */
if(AUDIO_SAIx_Init(AudioFreq) != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
/* Initialize the audio codec internal registers */
if(hAudioIn.AudioDrv->Init(AUDIO_I2C_ADDRESS,
(hAudioOut.OutputDevice | hAudioIn.InputDevice),
hAudioOut.Volume,
AudioFreq) != 0)
{
return AUDIO_ERROR;
}
}
/* Initialise transfer control flag */
DmaLeftRecHalfBuffCplt = 0;
DmaLeftRecBuffCplt = 0;
DmaRightRecHalfBuffCplt = 0;
DmaRightRecBuffCplt = 0;
return AUDIO_OK;
}
/**
* @brief De-Initializes microphone related peripherals.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_IN_DeInit(void)
{
if(hAudioIn.InputDevice != INPUT_DEVICE_ANALOG_MIC)
{
/* De-initializes the Digital Filter for Sigma-Delta Modulators interface */
if (AUDIO_DFSDMx_DeInit() != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
else
{ /* INPUT_DEVICE_ANALOG_MIC */
/* Check if output device is currently used */
if(hAudioOut.OutputDevice != 0)
{
/* Reset record path on audio codec */
if(hAudioOut.AudioDrv->Init(AUDIO_I2C_ADDRESS,
hAudioOut.OutputDevice,
(uint8_t) hAudioOut.Volume,
hAudioOut.Frequency) != 0)
{
return AUDIO_ERROR;
}
}
else
{
/* De-initializes SAI interface */
if(AUDIO_SAIx_DeInit() != AUDIO_OK)
{
return AUDIO_ERROR;
}
/* DeInit audio codec */
hAudioIn.AudioDrv->DeInit();
}
}
/* Disable SAI PLL if no more device is used */
if(hAudioOut.OutputDevice == 0)
{
if (AUDIO_SAIx_PLL_DISABLE() != AUDIO_OK)
{
return AUDIO_ERROR;
}
}
/* Reset the audio input context */
memset(&hAudioIn, 0, sizeof(hAudioIn));
return AUDIO_OK;
}
/**
* @brief Starts audio recording.
* @param pbuf: Main buffer pointer for the recorded data storing
* @param size: Current size of the recorded buffer
* @note The Right channel is start at first with synchro on start of Left channel
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_IN_Record(uint16_t* pbuf, uint32_t size)
{
hAudioIn.pRecBuf = pbuf;
hAudioIn.RecSize = size;
if(hAudioIn.InputDevice != INPUT_DEVICE_ANALOG_MIC)
{
if((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1)
{
/* Allocate hAudioIn.LeftRecBuff buffer */
#if defined(BSP_AUDIO_USE_RTOS)
hAudioIn.LeftRecBuff = (int32_t *)k_malloc((size / hAudioIn.ChannelNbr) * sizeof(int32_t));
#else
hAudioIn.LeftRecBuff = (int32_t *)malloc((size / hAudioIn.ChannelNbr) * sizeof(int32_t));
#endif
if(hAudioIn.LeftRecBuff == NULL)
{
return AUDIO_ERROR;
}
}
if((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2)
{
/* Allocate hAudioIn.RightRecBuff buffer */
#if defined(BSP_AUDIO_USE_RTOS)
hAudioIn.RightRecBuff = (int32_t *)k_malloc((size / hAudioIn.ChannelNbr) * sizeof(int32_t));
#else
hAudioIn.RightRecBuff = (int32_t *)malloc((size / hAudioIn.ChannelNbr) * sizeof(int32_t));
#endif
if(hAudioIn.RightRecBuff == NULL)
{
return AUDIO_ERROR;
}
}
if((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC2) == INPUT_DEVICE_DIGITAL_MIC2)
{
/* Call the Media layer start function for right channel */
if(HAL_DFSDM_FilterRegularStart_DMA(&BSP_AUDIO_hDfsdmRightFilter,
(int32_t*)hAudioIn.RightRecBuff,
(hAudioIn.RecSize/hAudioIn.ChannelNbr)) != HAL_OK)
{
return AUDIO_ERROR;
}
}
if((hAudioIn.InputDevice & INPUT_DEVICE_DIGITAL_MIC1) == INPUT_DEVICE_DIGITAL_MIC1)
{
/* Call the Media layer start function for left channel */
if(HAL_DFSDM_FilterRegularStart_DMA(&BSP_AUDIO_hDfsdmLeftFilter,
(int32_t*)hAudioIn.LeftRecBuff,
(hAudioIn.RecSize/hAudioIn.ChannelNbr)) != HAL_OK)
{
return AUDIO_ERROR;
}
}
}
else
{ /* INPUT_DEVICE_ANALOG_MIC */
/* Call the audio Codec Play function */
if (hAudioIn.AudioDrv->Play(AUDIO_I2C_ADDRESS, pbuf, size) != 0)
{
return AUDIO_ERROR;
}
/* Start the process receive DMA */
if(HAL_OK != HAL_SAI_Receive_DMA(&BSP_AUDIO_hSai_Rx, (uint8_t*)pbuf, size))
{
return AUDIO_ERROR;
}
}
return AUDIO_OK;
}
/**
* @brief Updates the audio frequency.
* @param AudioFreq: Audio frequency used to record the audio stream.
* @note This API should be called after the BSP_AUDIO_IN_Init() to adjust the
* audio frequency.
* @retval BSP AUDIO status
*/
uint8_t BSP_AUDIO_IN_SetFrequency(uint32_t AudioFreq)
{