-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstm32f4_discovery_audio_codec.c
1650 lines (1414 loc) · 59.9 KB
/
stm32f4_discovery_audio_codec.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 stm32f4_discovery_audio_codec.c
* @author MCD Application Team
* @version V1.1.0
* @date 28-October-2011
* @brief This file includes the low layer driver for CS43L22 Audio Codec
* available on STM32F4-Discovery Kit.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/*==============================================================================================================================
User NOTES
1. How To use this driver:
--------------------------
- This driver supports STM32F4xx devices on STM32F4-Discovery Kit.
- Configure the options in file stm32f4_discovery_audio_codec.h in the section CONFIGURATION.
Refer to the sections 2 and 3 to have more details on the possible configurations.
- Call the function EVAL_AUDIO_Init(
OutputDevice: physical output mode (OUTPUT_DEVICE_SPEAKER,
OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_AUTO or
OUTPUT_DEVICE_BOTH)
Volume: initial volume to be set (0 is min (mute), 100 is max (100%)
AudioFreq: Audio frequency in Hz (8000, 16000, 22500, 32000 ...)
this parameter is relative to the audio file/stream type.
)
This function configures all the hardware required for the audio application (codec, I2C, I2S,
GPIOs, DMA and interrupt if needed). This function returns 0 if configuration is OK.
if the returned value is different from 0 or the function is stuck then the communication with
the codec (try to un-plug the power or reset device in this case).
+ OUTPUT_DEVICE_SPEAKER: only speaker will be set as output for the audio stream.
+ OUTPUT_DEVICE_HEADPHONE: only headphones will be set as output for the audio stream.
+ OUTPUT_DEVICE_AUTO: Selection of output device is made through external switch (implemented
into the audio jack on the evaluation board). When the Headphone is connected it is used
as output. When the headphone is disconnected from the audio jack, the output is
automatically switched to Speaker.
+ OUTPUT_DEVICE_BOTH: both Speaker and Headphone are used as outputs for the audio stream
at the same time.
- Call the function EVAL_AUDIO_Play(
pBuffer: pointer to the audio data file address
Size: size of the buffer to be sent in Bytes
)
to start playing (for the first time) from the audio file/stream.
- Call the function EVAL_AUDIO_PauseResume(
Cmd: AUDIO_PAUSE (or 0) to pause playing or AUDIO_RESUME (or
any value different from 0) to resume playing.
)
Note. After calling EVAL_AUDIO_PauseResume() function for pause, only EVAL_AUDIO_PauseResume() should be called
for resume (it is not allowed to call EVAL_AUDIO_Play() in this case).
Note. This function should be called only when the audio file is played or paused (not stopped).
- For each mode, you may need to implement the relative callback functions into your code.
The Callback functions are named EVAL_AUDIO_XXX_CallBack() and only their prototypes are declared in
the stm32f4_discovery_audio_codec.h file. (refer to the example for more details on the callbacks implementations)
- To Stop playing, to modify the volume level or to mute, use the functions
EVAL_AUDIO_Stop(), EVAL_AUDIO_VolumeCtl() and EVAL_AUDIO_Mute().
- The driver API and the callback functions are at the end of the stm32f4_discovery_audio_codec.h file.
Driver architecture:
--------------------
This driver is composed of three main layers:
o High Audio Layer: consists of the function API exported in the stm32f4_discovery_audio_codec.h file
(EVAL_AUDIO_Init(), EVAL_AUDIO_Play() ...)
o Codec Control layer: consists of the functions API controlling the audio codec (CS43L22) and
included as local functions in file stm32f4_discovery_audio_codec.c (Codec_Init(), Codec_Play() ...)
o Media Access Layer (MAL): which consists of functions allowing to access the media containing/
providing the audio file/stream. These functions are also included as local functions into
the stm32f4_discovery_audio_codec.c file (Audio_MAL_Init(), Audio_MAL_Play() ...)
Each set of functions (layer) may be implemented independently of the others and customized when
needed.
2. Modes description:
---------------------
+ AUDIO_MAL_MODE_NORMAL : is suitable when the audio file is in a memory location.
+ AUDIO_MAL_MODE_CIRCULAR: is suitable when the audio data are read either from a
memory location or from a device at real time (double buffer could be used).
3. DMA interrupts description:
------------------------------
+ EVAL_AUDIO_IT_TC_ENABLE: Enable this define to use the DMA end of transfer interrupt.
then, a callback should be implemented by user to perform specific actions
when the DMA has finished the transfer.
+ EVAL_AUDIO_IT_HT_ENABLE: Enable this define to use the DMA end of half transfer interrupt.
then, a callback should be implemented by user to perform specific actions
when the DMA has reached the half of the buffer transfer (generally, it is useful
to load the first half of buffer while DMA is loading from the second half).
+ EVAL_AUDIO_IT_ER_ENABLE: Enable this define to manage the cases of error on DMA transfer.
4. Known Limitations:
---------------------
1- When using the Speaker, if the audio file quality is not high enough, the speaker output
may produce high and uncomfortable noise level. To avoid this issue, to use speaker
output properly, try to increase audio file sampling rate (typically higher than 48KHz).
This operation will lead to larger file size.
2- 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 (EVAL_AUDIO_Init() or EVAL_AUDIO_Stop()) and when Volume control modification is
performed (EVAL_AUDIO_VolumeCtl() or EVAL_AUDIO_Mute()). When the audio data is played, no communication is
required with the audio codec.
3- 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.
4- Mono audio streaming is not supported (in order to play mono audio streams, each data should be sent twice
on the I2S or should be duplicated on the source buffer. Or convert the stream in stereo before playing).
5- Supports only 16-bit audio data size.
===============================================================================================================================*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery_audio_codec.h"
/** @addtogroup Utilities
* @{
*/
/** @addtogroup STM32F4_DISCOVERY
* @{
*/
/** @addtogroup STM32F4_DISCOVERY_AUDIO_CODEC
* @brief This file includes the low layer driver for CS43L22 Audio Codec
* available on STM32F4-Discovery Kit.
* @{
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Private_Types
* @{
*/
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Private_Defines
* @{
*/
/* Mask for the bit EN of the I2S CFGR register */
#define I2S_ENABLE_MASK 0x0400
/* Delay for the Codec to be correctly reset */
#define CODEC_RESET_DELAY 0x4FFF
/* Codec audio Standards */
#ifdef I2S_STANDARD_PHILLIPS
#define CODEC_STANDARD 0x04
#define I2S_STANDARD I2S_Standard_Phillips
#elif defined(I2S_STANDARD_MSB)
#define CODEC_STANDARD 0x00
#define I2S_STANDARD I2S_Standard_MSB
#elif defined(I2S_STANDARD_LSB)
#define CODEC_STANDARD 0x08
#define I2S_STANDARD I2S_Standard_LSB
#else
#error "Error: No audio communication standard selected !"
#endif /* I2S_STANDARD */
/* The 7 bits Codec address (sent through I2C interface) */
#define CODEC_ADDRESS 0x94 /* b00100111 */
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Private_Variables
* @{
*/
/* This structure is declared global because it is handled by two different functions */
DMA_InitTypeDef DMA_InitStructure;
DMA_InitTypeDef AUDIO_MAL_DMA_InitStructure;
uint32_t AudioTotalSize = 0xFFFF; /* This variable holds the total size of the audio file */
uint32_t AudioRemSize = 0xFFFF; /* This variable holds the remaining data in audio file */
uint16_t *CurrentPos ; /* This variable holds the current position of audio pointer */
__IO uint32_t CODECTimeout = CODEC_LONG_TIMEOUT;
__IO uint8_t OutputDev = 0;
__IO uint32_t CurrAudioInterface = AUDIO_INTERFACE_I2S; //AUDIO_INTERFACE_DAC
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Private_Function_Prototypes
* @{
*/
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Private_Functions
* @{
*/
static void Audio_MAL_IRQHandler(void);
/*-----------------------------------
Audio Codec functions
------------------------------------------*/
/* High Layer codec functions */
static uint32_t Codec_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq);
static uint32_t Codec_DeInit(void);
static uint32_t Codec_Play(void);
static uint32_t Codec_PauseResume(uint32_t Cmd);
static uint32_t Codec_Stop(uint32_t Cmd);
static uint32_t Codec_VolumeCtrl(uint8_t Volume);
static uint32_t Codec_Mute(uint32_t Cmd);
/* Low layer codec functions */
static void Codec_CtrlInterface_Init(void);
static void Codec_CtrlInterface_DeInit(void);
static void Codec_AudioInterface_Init(uint32_t AudioFreq);
static void Codec_AudioInterface_DeInit(void);
static void Codec_Reset(void);
static uint32_t Codec_WriteRegister(uint8_t RegisterAddr, uint8_t RegisterValue);
static uint32_t Codec_ReadRegister(uint8_t RegisterAddr);
static void Codec_GPIO_Init(void);
static void Codec_GPIO_DeInit(void);
static void Delay(__IO uint32_t nCount);
/*----------------------------------------------------------------------------*/
/*-----------------------------------
MAL (Media Access Layer) functions
------------------------------------------*/
/* Peripherals configuration functions */
static void Audio_MAL_Init(void);
static void Audio_MAL_DeInit(void);
static void Audio_MAL_PauseResume(uint32_t Cmd, uint32_t Addr);
static void Audio_MAL_Stop(void);
/*----------------------------------------------------------------------------*/
/* DMA Stream definitions */
uint32_t AUDIO_MAL_DMA_CLOCK = AUDIO_I2S_DMA_CLOCK;
DMA_Stream_TypeDef * AUDIO_MAL_DMA_STREAM = AUDIO_I2S_DMA_STREAM ;
uint32_t AUDIO_MAL_DMA_DREG = AUDIO_I2S_DMA_DREG;
uint32_t AUDIO_MAL_DMA_CHANNEL = AUDIO_I2S_DMA_CHANNEL;
uint32_t AUDIO_MAL_DMA_IRQ = AUDIO_I2S_DMA_IRQ ;
uint32_t AUDIO_MAL_DMA_FLAG_TC = AUDIO_I2S_DMA_FLAG_TC;
uint32_t AUDIO_MAL_DMA_FLAG_HT = AUDIO_I2S_DMA_FLAG_HT;
uint32_t AUDIO_MAL_DMA_FLAG_FE = AUDIO_I2S_DMA_FLAG_FE;
uint32_t AUDIO_MAL_DMA_FLAG_TE = AUDIO_I2S_DMA_FLAG_TE;
uint32_t AUDIO_MAL_DMA_FLAG_DME = AUDIO_I2S_DMA_FLAG_DME;
/**
* @brief Set the current audio interface (I2S or DAC).
* @param Interface: AUDIO_INTERFACE_I2S or AUDIO_INTERFACE_DAC
* @retval None
*/
void EVAL_AUDIO_SetAudioInterface(uint32_t Interface)
{
CurrAudioInterface = Interface;
if (CurrAudioInterface == AUDIO_INTERFACE_I2S)
{
/* DMA Stream definitions */
AUDIO_MAL_DMA_CLOCK = AUDIO_I2S_DMA_CLOCK;
AUDIO_MAL_DMA_STREAM = AUDIO_I2S_DMA_STREAM;
AUDIO_MAL_DMA_DREG = AUDIO_I2S_DMA_DREG;
AUDIO_MAL_DMA_CHANNEL = AUDIO_I2S_DMA_CHANNEL;
AUDIO_MAL_DMA_IRQ = AUDIO_I2S_DMA_IRQ ;
AUDIO_MAL_DMA_FLAG_TC = AUDIO_I2S_DMA_FLAG_TC;
AUDIO_MAL_DMA_FLAG_HT = AUDIO_I2S_DMA_FLAG_HT;
AUDIO_MAL_DMA_FLAG_FE = AUDIO_I2S_DMA_FLAG_FE;
AUDIO_MAL_DMA_FLAG_TE = AUDIO_I2S_DMA_FLAG_TE;
AUDIO_MAL_DMA_FLAG_DME = AUDIO_I2S_DMA_FLAG_DME;
}
else if (Interface == AUDIO_INTERFACE_DAC)
{
/* DMA Stream definitions */
AUDIO_MAL_DMA_CLOCK = AUDIO_DAC_DMA_CLOCK;
AUDIO_MAL_DMA_STREAM = AUDIO_DAC_DMA_STREAM;
AUDIO_MAL_DMA_DREG = AUDIO_DAC_DMA_DREG;
AUDIO_MAL_DMA_CHANNEL = AUDIO_DAC_DMA_CHANNEL;
AUDIO_MAL_DMA_IRQ = AUDIO_DAC_DMA_IRQ ;
AUDIO_MAL_DMA_FLAG_TC = AUDIO_DAC_DMA_FLAG_TC;
AUDIO_MAL_DMA_FLAG_HT = AUDIO_DAC_DMA_FLAG_HT;
AUDIO_MAL_DMA_FLAG_FE = AUDIO_DAC_DMA_FLAG_FE;
AUDIO_MAL_DMA_FLAG_TE = AUDIO_DAC_DMA_FLAG_TE;
AUDIO_MAL_DMA_FLAG_DME = AUDIO_DAC_DMA_FLAG_DME;
}
}
/**
* @brief Configure the audio peripherals.
* @param OutputDevice: OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE,
* OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO .
* @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
* @param AudioFreq: Audio frequency used to play the audio stream.
* @retval 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
{
/* Perform low layer Codec initialization */
if (Codec_Init(OutputDevice, VOLUME_CONVERT(Volume), AudioFreq) != 0)
{
return 1;
}
else
{
/* I2S data transfer preparation:
Prepare the Media to be used for the audio transfer from memory to I2S peripheral */
Audio_MAL_Init();
/* Return 0 when all operations are OK */
return 0;
}
}
/**
* @brief Deinitializes all the resources used by the codec (those initialized
* by EVAL_AUDIO_Init() function).
* @param None
* @retval 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_DeInit(void)
{
/* DeInitialize the Media layer */
Audio_MAL_DeInit();
/* DeInitialize Codec */
Codec_DeInit();
return 0;
}
/**
* @brief Starts playing audio stream from a data buffer for a determined size.
* @param pBuffer: Pointer to the buffer
* @param Size: Number of audio data BYTES.
* @retval 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_Play(uint16_t* pBuffer, uint32_t Size)
{
/* Set the total number of data to be played (count in half-word) */
AudioTotalSize = Size;
/* Call the audio Codec Play function */
Codec_Play();
/* Update the Media layer and enable it for play */
Audio_MAL_Play((uint32_t)pBuffer, (uint32_t)(DMA_MAX(Size/4)));
/* Update the remaining number of data to be played */
AudioRemSize = (Size/2) - DMA_MAX(AudioTotalSize);
/* Update the current audio pointer position */
CurrentPos = pBuffer + DMA_MAX(AudioTotalSize);
return 0;
}
/**
* @brief This function Pauses or Resumes the audio file stream. In case
* of using DMA, the DMA Pause feature is used. In all cases the I2S
* peripheral is disabled.
*
* @WARNING When calling EVAL_AUDIO_PauseResume() function for pause, only
* this function should be called for resume (use of EVAL_AUDIO_Play()
* function for resume could lead to unexpected behavior).
*
* @param Cmd: AUDIO_PAUSE (or 0) to pause, AUDIO_RESUME (or any value different
* from 0) to resume.
* @retval 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_PauseResume(uint32_t Cmd)
{
/* Call the Audio Codec Pause/Resume function */
if (Codec_PauseResume(Cmd) != 0)
{
return 1;
}
else
{
/* Call the Media layer pause/resume function */
Audio_MAL_PauseResume(Cmd, 0);
/* Return 0 if all operations are OK */
return 0;
}
}
/**
* @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 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_Stop(uint32_t Option)
{
/* Call Audio Codec Stop function */
if (Codec_Stop(Option) != 0)
{
return 1;
}
else
{
/* Call Media layer Stop function */
Audio_MAL_Stop();
/* Update the remaining data number */
AudioRemSize = AudioTotalSize;
/* Return 0 when all operations are correctly done */
return 0;
}
}
/**
* @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 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_VolumeCtl(uint8_t Volume)
{
/* Call the codec volume control function with converted volume value */
return (Codec_VolumeCtrl(VOLUME_CONVERT(Volume)));
}
/**
* @brief Enables or disables the MUTE mode by software
* @param Command: could be AUDIO_MUTE_ON to mute sound or AUDIO_MUTE_OFF to
* unmute the codec and restore previous volume level.
* @retval 0 if correct communication, else wrong communication
*/
uint32_t EVAL_AUDIO_Mute(uint32_t Cmd)
{
/* Call the Codec Mute function */
return (Codec_Mute(Cmd));
}
/**
* @brief This function handles main Media layer interrupt.
* @param None
* @retval 0 if correct communication, else wrong communication
*/
static void Audio_MAL_IRQHandler(void)
{
#ifndef AUDIO_MAL_MODE_NORMAL
uint16_t *pAddr = (uint16_t *)CurrentPos;
uint32_t Size = AudioRemSize;
#endif /* AUDIO_MAL_MODE_NORMAL */
#ifdef AUDIO_MAL_DMA_IT_TC_EN
/* Transfer complete interrupt */
if (DMA_GetFlagStatus(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_TC) != RESET)
{
#ifdef AUDIO_MAL_MODE_NORMAL
/* Check if the end of file has been reached */
if (AudioRemSize > 0)
{
/* Wait the DMA Stream to be effectively disabled */
while (DMA_GetCmdStatus(AUDIO_MAL_DMA_STREAM) != DISABLE)
{}
/* Clear the Interrupt flag */
DMA_ClearFlag(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_TC);
/* Re-Configure the buffer address and size */
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) CurrentPos;
DMA_InitStructure.DMA_BufferSize = (uint32_t) (DMA_MAX(AudioRemSize));
/* Configure the DMA Stream with the new parameters */
DMA_Init(AUDIO_MAL_DMA_STREAM, &DMA_InitStructure);
/* Enable the I2S DMA Stream*/
DMA_Cmd(AUDIO_MAL_DMA_STREAM, ENABLE);
/* Update the current pointer position */
CurrentPos += DMA_MAX(AudioRemSize);
/* Update the remaining number of data to be played */
AudioRemSize -= DMA_MAX(AudioRemSize);
/* Enable the I2S DMA Stream*/
DMA_Cmd(AUDIO_MAL_DMA_STREAM, ENABLE);
}
else
{
/* Disable the I2S DMA Stream*/
DMA_Cmd(AUDIO_MAL_DMA_STREAM, DISABLE);
/* Clear the Interrupt flag */
DMA_ClearFlag(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_TC);
/* Manage the remaining file size and new address offset: This function
should be coded by user (its prototype is already declared in stm32f4_discovery_audio_codec.h) */
EVAL_AUDIO_TransferComplete_CallBack((uint32_t)CurrentPos, 0);
}
#elif defined(AUDIO_MAL_MODE_CIRCULAR)
/* Manage the remaining file size and new address offset: This function
should be coded by user (its prototype is already declared in stm32f4_discovery_audio_codec.h) */
EVAL_AUDIO_TransferComplete_CallBack(pAddr, Size);
/* Clear the Interrupt flag */
DMA_ClearFlag(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_TC);
#endif /* AUDIO_MAL_MODE_NORMAL */
}
#endif /* AUDIO_MAL_DMA_IT_TC_EN */
#ifdef AUDIO_MAL_DMA_IT_HT_EN
/* Half Transfer complete interrupt */
if (DMA_GetFlagStatus(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_HT) != RESET)
{
/* Manage the remaining file size and new address offset: This function
should be coded by user (its prototype is already declared in stm32f4_discovery_audio_codec.h) */
EVAL_AUDIO_HalfTransfer_CallBack((uint32_t)pAddr, Size);
/* Clear the Interrupt flag */
DMA_ClearFlag(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_HT);
}
#endif /* AUDIO_MAL_DMA_IT_HT_EN */
#ifdef AUDIO_MAL_DMA_IT_TE_EN
/* FIFO Error interrupt */
if ((DMA_GetFlagStatus(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_TE) != RESET) || \
(DMA_GetFlagStatus(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_FE) != RESET) || \
(DMA_GetFlagStatus(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_DME) != RESET))
{
/* Manage the error generated on DMA FIFO: This function
should be coded by user (its prototype is already declared in stm32f4_discovery_audio_codec.h) */
EVAL_AUDIO_Error_CallBack((uint32_t*)&pAddr);
/* Clear the Interrupt flag */
DMA_ClearFlag(AUDIO_MAL_DMA_STREAM, AUDIO_MAL_DMA_FLAG_TE | AUDIO_MAL_DMA_FLAG_FE | \
AUDIO_MAL_DMA_FLAG_DME);
}
#endif /* AUDIO_MAL_DMA_IT_TE_EN */
}
/**
* @brief This function handles main I2S interrupt.
* @param None
* @retval 0 if correct communication, else wrong communication
*/
void Audio_MAL_I2S_IRQHandler(void)
{
Audio_MAL_IRQHandler();
}
/**
* @brief This function handles main DAC interrupt.
* @param None
* @retval 0 if correct communication, else wrong communication
*/
void Audio_MAL_DAC_IRQHandler(void)
{
Audio_MAL_IRQHandler();
}
/**
* @brief I2S interrupt management
* @param None
* @retval None
*/
void Audio_I2S_IRQHandler(void)
{
/* Check on the I2S TXE flag */
if (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) != RESET)
{
if (CurrAudioInterface == AUDIO_INTERFACE_DAC)
{
/* Wirte data to the DAC interface */
DAC_SetChannel1Data(DAC_Align_12b_L, EVAL_AUDIO_GetSampleCallBack());
}
/* Send dummy data on I2S to avoid the underrun condition */
SPI_I2S_SendData(CODEC_I2S, EVAL_AUDIO_GetSampleCallBack());
}
}
/*========================
CS43L22 Audio Codec Control Functions
==============================*/
/**
* @brief Initializes the audio codec and all related interfaces (control
* interface: I2C and audio interface: I2S)
* @param OutputDevice: can be OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE,
* OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO .
* @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
* @param AudioFreq: Audio frequency used to play the audio stream.
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
{
uint32_t counter = 0;
/* Configure the Codec related IOs */
Codec_GPIO_Init();
/* Reset the Codec Registers */
Codec_Reset();
/* Initialize the Control interface of the Audio Codec */
Codec_CtrlInterface_Init();
/* Keep Codec powered OFF */
counter += Codec_WriteRegister(0x02, 0x01);
counter += Codec_WriteRegister(0x04, 0xAF); /* SPK always OFF & HP always ON */
OutputDev = 0xAF;
/* Clock configuration: Auto detection */
counter += Codec_WriteRegister(0x05, 0x81);
/* Set the Slave Mode and the audio Standard */
counter += Codec_WriteRegister(0x06, CODEC_STANDARD);
/* Set the Master volume */
Codec_VolumeCtrl(Volume);
if (CurrAudioInterface == AUDIO_INTERFACE_DAC)
{
/* Enable the PassThrough on AIN1A and AIN1B */
counter += Codec_WriteRegister(0x08, 0x01);
counter += Codec_WriteRegister(0x09, 0x01);
/* Route the analog input to the HP line */
counter += Codec_WriteRegister(0x0E, 0xC0);
/* Set the Passthough volume */
counter += Codec_WriteRegister(0x14, 0x00);
counter += Codec_WriteRegister(0x15, 0x00);
}
/* Power on the Codec */
counter += Codec_WriteRegister(0x02, 0x9E);
/* Additional configuration for the CODEC. These configurations are done to reduce
the time needed for the Codec to power off. If these configurations are removed,
then a long delay should be added between powering off the Codec and switching
off the I2S peripheral MCLK clock (which is the operating clock for Codec).
If this delay is not inserted, then the codec will not shut down properly and
it results in high noise after shut down. */
/* Disable the analog soft ramp */
counter += Codec_WriteRegister(0x0A, 0x00);
if (CurrAudioInterface != AUDIO_INTERFACE_DAC)
{
/* Disable the digital soft ramp */
counter += Codec_WriteRegister(0x0E, 0x04);
}
/* Disable the limiter attack level */
counter += Codec_WriteRegister(0x27, 0x00);
/* Adjust Bass and Treble levels */
counter += Codec_WriteRegister(0x1F, 0x0F);
/* Adjust PCM volume level */
counter += Codec_WriteRegister(0x1A, 0x0A);
counter += Codec_WriteRegister(0x1B, 0x0A);
/* Configure the I2S peripheral */
Codec_AudioInterface_Init(AudioFreq);
/* Return communication control value */
return counter;
}
/**
* @brief Restore the audio codec state to default state and free all used
* resources.
* @param None
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_DeInit(void)
{
uint32_t counter = 0;
/* Reset the Codec Registers */
Codec_Reset();
/* Keep Codec powered OFF */
counter += Codec_WriteRegister(0x02, 0x01);
/* Deinitialize all use GPIOs */
Codec_GPIO_DeInit();
/* Disable the Codec control interface */
Codec_CtrlInterface_DeInit();
/* Deinitialize the Codec audio interface (I2S) */
Codec_AudioInterface_DeInit();
/* Return communication control value */
return counter;
}
/**
* @brief Start the audio Codec play feature.
* @note For this codec no Play options are required.
* @param None
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_Play(void)
{
/*
No actions required on Codec level for play command
*/
/* Return communication control value */
return 0;
}
/**
* @brief Pauses and resumes playing on the audio codec.
* @param Cmd: AUDIO_PAUSE (or 0) to pause, AUDIO_RESUME (or any value different
* from 0) to resume.
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_PauseResume(uint32_t Cmd)
{
uint32_t counter = 0;
/* Pause the audio file playing */
if (Cmd == AUDIO_PAUSE)
{
/* Mute the output first */
counter += Codec_Mute(AUDIO_MUTE_ON);
/* Put the Codec in Power save mode */
counter += Codec_WriteRegister(0x02, 0x01);
}
else /* AUDIO_RESUME */
{
/* Unmute the output first */
counter += Codec_Mute(AUDIO_MUTE_OFF);
counter += Codec_WriteRegister(0x04, OutputDev);
/* Exit the Power save mode */
counter += Codec_WriteRegister(0x02, 0x9E);
}
return counter;
}
/**
* @brief Stops audio Codec playing. It powers down the codec.
* @param CodecPdwnMode: selects the power down mode.
* - CODEC_PDWN_SW: only mutes the audio codec. When resuming from this
* mode the codec keeps the previous initialization
* (no need to re-Initialize the codec registers).
* - CODEC_PDWN_HW: Physically power down the codec. When resuming from this
* mode, the codec is set to default configuration
* (user should re-Initialize the codec in order to
* play again the audio stream).
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_Stop(uint32_t CodecPdwnMode)
{
uint32_t counter = 0;
/* Mute the output first */
Codec_Mute(AUDIO_MUTE_ON);
if (CodecPdwnMode == CODEC_PDWN_SW)
{
/* Power down the DAC and the speaker (PMDAC and PMSPK bits)*/
counter += Codec_WriteRegister(0x02, 0x9F);
}
else /* CODEC_PDWN_HW */
{
/* Power down the DAC components */
counter += Codec_WriteRegister(0x02, 0x9F);
/* Wait at least 100us */
Delay(0xFFF);
/* Reset The pin */
GPIO_WriteBit(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, Bit_RESET);
}
return counter;
}
/**
* @brief Sets higher or lower the codec volume level.
* @param Volume: a byte value from 0 to 255 (refer to codec registers
* description for more details).
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_VolumeCtrl(uint8_t Volume)
{
uint32_t counter = 0;
if (Volume > 0xE6)
{
/* Set the Master volume */
counter += Codec_WriteRegister(0x20, Volume - 0xE7);
counter += Codec_WriteRegister(0x21, Volume - 0xE7);
}
else
{
/* Set the Master volume */
counter += Codec_WriteRegister(0x20, Volume + 0x19);
counter += Codec_WriteRegister(0x21, Volume + 0x19);
}
return counter;
}
/**
* @brief Enables or disables the mute feature on the audio codec.
* @param Cmd: AUDIO_MUTE_ON to enable the mute or AUDIO_MUTE_OFF to disable the
* mute mode.
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_Mute(uint32_t Cmd)
{
uint32_t counter = 0;
/* Set the Mute mode */
if (Cmd == AUDIO_MUTE_ON)
{
counter += Codec_WriteRegister(0x04, 0xFF);
}
else /* AUDIO_MUTE_OFF Disable the Mute */
{
counter += Codec_WriteRegister(0x04, OutputDev);
}
return counter;
}
/**
* @brief Resets the audio codec. It restores the default configuration of the
* codec (this function shall be called before initializing the codec).
* @note This function calls an external driver function: The IO Expander driver.
* @param None
* @retval None
*/
static void Codec_Reset(void)
{
/* Power Down the codec */
GPIO_WriteBit(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, Bit_RESET);
/* wait for a delay to insure registers erasing */
Delay(CODEC_RESET_DELAY);
/* Power on the codec */
GPIO_WriteBit(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, Bit_SET);
}
/**
* @brief Writes a Byte to a given register into the audio codec through the
control interface (I2C)
* @param RegisterAddr: The address (location) of the register to be written.
* @param RegisterValue: the Byte value to be written into destination register.
* @retval 0 if correct communication, else wrong communication
*/
static uint32_t Codec_WriteRegister(uint8_t RegisterAddr, uint8_t RegisterValue)
{
uint32_t result = 0;
/*!< While the bus is busy */
CODECTimeout = CODEC_LONG_TIMEOUT;
while(I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BUSY))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Start the config sequence */
I2C_GenerateSTART(CODEC_I2C, ENABLE);
/* Test on EV5 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(CODEC_I2C, CODEC_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Transmit the first address for write operation */
I2C_SendData(CODEC_I2C, RegisterAddr);
/* Test on EV8 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Prepare the register value to be sent */
I2C_SendData(CODEC_I2C, RegisterValue);
/*!< Wait till all data have been physically transferred on the bus */
CODECTimeout = CODEC_LONG_TIMEOUT;
while(!I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BTF))
{
if((CODECTimeout--) == 0) Codec_TIMEOUT_UserCallback();
}
/* End the configuration sequence */
I2C_GenerateSTOP(CODEC_I2C, ENABLE);
#ifdef VERIFY_WRITTENDATA
/* Verify that the data has been correctly written */
result = (Codec_ReadRegister(RegisterAddr) == RegisterValue)? 0:1;
#endif /* VERIFY_WRITTENDATA */
/* Return the verifying value: 0 (Passed) or 1 (Failed) */
return result;
}
/**
* @brief Reads and returns the value of an audio codec register through the
* control interface (I2C).
* @param RegisterAddr: Address of the register to be read.
* @retval Value of the register to be read or dummy value if the communication
* fails.
*/
static uint32_t Codec_ReadRegister(uint8_t RegisterAddr)
{
uint32_t result = 0;
/*!< While the bus is busy */
CODECTimeout = CODEC_LONG_TIMEOUT;
while(I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BUSY))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Start the config sequence */
I2C_GenerateSTART(CODEC_I2C, ENABLE);
/* Test on EV5 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(CODEC_I2C, CODEC_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Transmit the register address to be read */
I2C_SendData(CODEC_I2C, RegisterAddr);
/* Test on EV8 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BTF) == RESET)
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/*!< Send START condition a second time */
I2C_GenerateSTART(CODEC_I2C, ENABLE);
/*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while(!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/*!< Send Codec address for read */
I2C_Send7bitAddress(CODEC_I2C, CODEC_ADDRESS, I2C_Direction_Receiver);
/* Wait on ADDR flag to be set (ADDR is still not cleared at this level */
CODECTimeout = CODEC_FLAG_TIMEOUT;