ARMEBS4  revision-26.06.2015
stm32f4_discovery_audio_codec.h
Go to the documentation of this file.
1 /**
2  ******************************************************************************
3  * @file stm32f4_discovery_audio_codec.h
4  * @author MCD Application Team
5  * @version V1.1.0
6  * @date 28-October-2011
7  * @brief This file contains all the functions prototypes for the
8  * stm32f4_discovery_audio_codec.c driver.
9  ******************************************************************************
10  * @attention
11  *
12  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
15  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18  *
19  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
20  ******************************************************************************
21  */
22 
23 /* Define to prevent recursive inclusion -------------------------------------*/
24 #ifndef __STM32F4_DISCOVERY_AUDIOCODEC_H
25 #define __STM32F4_DISCOVERY_AUDIOCODEC_H
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32f4xx.h"
29 #include "stm32f4xx_gpio.h"
30 
31 /** @addtogroup Utilities
32  * @{
33  */
34 
35 
36 /** @addtogroup STM32F4_DISCOVERY
37  * @{
38  */
39 
40 /** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC
41  * @{
42  */
43 
44 
45 /** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Types
46  * @{
47  */
48 
49 /** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Constants
50  * @{
51  */
52 
53 /*------------------------------------
54  CONFIGURATION: Audio Codec Driver Configuration parameters
55  ----------------------------------------*/
56 /* Audio Transfer mode (I2S Interrupt) */
57 //#define I2S_INTERRUPT /* Uncomment this line to enable audio transfert with I2S interrupt*/
58 
59 /* Audio Transfer mode (DMA, Interrupt or Polling) */
60 #define AUDIO_MAL_MODE_NORMAL /* Uncomment this line to enable the audio
61  Transfer using DMA */
62 /* #define AUDIO_MAL_MODE_CIRCULAR */ /* Uncomment this line to enable the audio
63  Transfer using DMA */
64 
65 /* For the DMA modes select the interrupt that will be used */
66 #define AUDIO_MAL_DMA_IT_TC_EN /* Uncomment this line to enable DMA Transfer Complete interrupt */
67 /* #define AUDIO_MAL_DMA_IT_HT_EN */ /* Uncomment this line to enable DMA Half Transfer Complete interrupt */
68 /* #define AUDIO_MAL_DMA_IT_TE_EN */ /* Uncomment this line to enable DMA Transfer Error interrupt */
69 
70 /* Select the interrupt preemption priority and subpriority for the DMA interrupt */
71 #define EVAL_AUDIO_IRQ_PREPRIO 0 /* Select the preemption priority level(0 is the highest) */
72 #define EVAL_AUDIO_IRQ_SUBRIO 0 /* Select the sub-priority level (0 is the highest) */
73 
74 /* Uncomment the following line to use the default Codec_TIMEOUT_UserCallback()
75  function implemented in stm32f4_discovery_audio_codec.c file.
76  Codec_TIMEOUT_UserCallback() function is called whenever a timeout condition
77  occurs during communication (waiting on an event that doesn't occur, bus
78  errors, busy devices ...). */
79 /* #define USE_DEFAULT_TIMEOUT_CALLBACK */
80 
81 /* Enable this define to use the I2S DMA for writing into DAC register */
82 //#define DAC_USE_I2S_DMA
83 /*----------------------------------------------------------------------------*/
84 
85 /*------------------------------------
86  OPTIONAL Configuration defines parameters
87  ----------------------------------------*/
88 /* I2C clock speed configuration (in Hz)
89  WARNING:
90  Make sure that this define is not already declared in other files (ie.
91  stm322xg_eval.h file). It can be used in parallel by other modules. */
92 #ifndef I2C_SPEED
93  #define I2C_SPEED 100000
94 #endif /* I2C_SPEED */
95 
96 /* Uncomment defines below to select standard for audio communication between
97  Codec and I2S peripheral */
98 #define I2S_STANDARD_PHILLIPS
99 /* #define I2S_STANDARD_MSB */
100 /* #define I2S_STANDARD_LSB */
101 
102 /* Uncomment the defines below to select if the Master clock mode should be
103  enabled or not */
104 #define CODEC_MCLK_ENABLED
105 /* #deine CODEC_MCLK_DISABLED */
106 
107 /* Uncomment this line to enable verifying data sent to codec after each write
108  operation */
109 #define VERIFY_WRITTENDATA
110 /*----------------------------------------------------------------------------*/
111 
112 /*-----------------------------------
113  Hardware Configuration defines parameters
114  -----------------------------------------*/
115 /* Audio Reset Pin definition */
116 #define AUDIO_RESET_GPIO_CLK RCC_AHB1Periph_GPIOD
117 #define AUDIO_RESET_PIN GPIO_Pin_4
118 #define AUDIO_RESET_GPIO GPIOD
119 
120 /* I2S peripheral configuration defines */
121 #define CODEC_I2S SPI3
122 #define CODEC_I2S_CLK RCC_APB1Periph_SPI3
123 #define CODEC_I2S_ADDRESS 0x40003C0C
124 #define CODEC_I2S_GPIO_AF GPIO_AF_SPI3
125 #define CODEC_I2S_IRQ SPI3_IRQn
126 #define CODEC_I2S_GPIO_CLOCK (RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOA)
127 #define CODEC_I2S_WS_PIN GPIO_Pin_4
128 #define CODEC_I2S_SCK_PIN GPIO_Pin_10
129 #define CODEC_I2S_SD_PIN GPIO_Pin_12
130 #define CODEC_I2S_MCK_PIN GPIO_Pin_7
131 #define CODEC_I2S_WS_PINSRC GPIO_PinSource4
132 #define CODEC_I2S_SCK_PINSRC GPIO_PinSource10
133 #define CODEC_I2S_SD_PINSRC GPIO_PinSource12
134 #define CODEC_I2S_MCK_PINSRC GPIO_PinSource7
135 #define CODEC_I2S_GPIO GPIOC
136 #define CODEC_I2S_WS_GPIO GPIOA
137 #define CODEC_I2S_MCK_GPIO GPIOC
138 #define Audio_I2S_IRQ_Handler SPI3_IRQ_Handler
139 
140 
141  #define AUDIO_MAL_DMA_PERIPH_DATA_SIZE DMA_PeripheralDataSize_HalfWord
142  #define AUDIO_MAL_DMA_MEM_DATA_SIZE DMA_MemoryDataSize_HalfWord
143  #define DMA_MAX_SZE 0xFFFF
144 
145 
146  #define DAC_DHR12L1_ADDRESS 0x4000740C
147  #define DAC_DHR12R1_ADDRESS 0x40007408
148  #define DAC_DHR8R1_ADDRESS 0x40007410
149  #define AUDIO_DAC_CHANNEL DAC_Channel_1
150 
151  /* I2S DMA Stream definitions */
152  #define AUDIO_I2S_DMA_CLOCK RCC_AHB1Periph_DMA1
153  #define AUDIO_I2S_DMA_STREAM DMA1_Stream7
154  #define AUDIO_I2S_DMA_DREG CODEC_I2S_ADDRESS
155  #define AUDIO_I2S_DMA_CHANNEL DMA_Channel_0
156  #define AUDIO_I2S_DMA_IRQ DMA1_Stream7_IRQn
157  #define AUDIO_I2S_DMA_FLAG_TC DMA_FLAG_TCIF7
158  #define AUDIO_I2S_DMA_FLAG_HT DMA_FLAG_HTIF7
159  #define AUDIO_I2S_DMA_FLAG_FE DMA_FLAG_FEIF7
160  #define AUDIO_I2S_DMA_FLAG_TE DMA_FLAG_TEIF7
161  #define AUDIO_I2S_DMA_FLAG_DME DMA_FLAG_DMEIF7
162 
163  #define Audio_MAL_I2S_IRQ_Handler DMA1_Stream7_IRQ_Handler
164 
165 
166  /* DAC DMA Stream definitions */
167  #define AUDIO_DAC_DMA_CLOCK RCC_AHB1Periph_DMA1
168  #define AUDIO_DAC_DMA_STREAM DMA1_Stream0
169  #define AUDIO_DAC_DMA_DREG DAC_DHR12L1_ADDRESS
170  #define AUDIO_DAC_DMA_CHANNEL DMA_Channel_0
171  #define AUDIO_DAC_DMA_IRQ DMA1_Stream0_IRQn
172  #define AUDIO_DAC_DMA_FLAG_TC DMA_FLAG_TCIF0
173  #define AUDIO_DAC_DMA_FLAG_HT DMA_FLAG_HTIF0
174  #define AUDIO_DAC_DMA_FLAG_FE DMA_FLAG_FEIF0
175  #define AUDIO_DAC_DMA_FLAG_TE DMA_FLAG_TEIF0
176  #define AUDIO_DAC_DMA_FLAG_DME DMA_FLAG_DMEIF0
177 
178  #define Audio_MAL_DAC_IRQ_Handler DMA1_Stream0_IRQ_Handler
179 
180 
181 /* I2C peripheral configuration defines (control interface of the audio codec) */
182 #define CODEC_I2C I2C1
183 #define CODEC_I2C_CLK RCC_APB1Periph_I2C1
184 #define CODEC_I2C_GPIO_CLOCK RCC_AHB1Periph_GPIOB
185 #define CODEC_I2C_GPIO_AF GPIO_AF_I2C1
186 #define CODEC_I2C_GPIO GPIOB
187 #define CODEC_I2C_SCL_PIN GPIO_Pin_6
188 #define CODEC_I2C_SDA_PIN GPIO_Pin_9
189 #define CODEC_I2S_SCL_PINSRC GPIO_PinSource6
190 #define CODEC_I2S_SDA_PINSRC GPIO_PinSource9
191 
192 /* Maximum Timeout values for flags and events waiting loops. These timeouts are
193  not based on accurate values, they just guarantee that the application will
194  not remain stuck if the I2C communication is corrupted.
195  You may modify these timeout values depending on CPU frequency and application
196  conditions (interrupts routines ...). */
197 #define CODEC_FLAG_TIMEOUT ((uint32_t)0x1000)
198 #define CODEC_LONG_TIMEOUT ((uint32_t)(300 * CODEC_FLAG_TIMEOUT))
199 /*----------------------------------------------------------------------------*/
200 
201 /*-----------------------------------
202  Audio Codec User defines
203  -----------------------------------------*/
204 /* Audio interface : I2S or DAC */
205 #define AUDIO_INTERFACE_I2S 1
206 #define AUDIO_INTERFACE_DAC 2
207 
208 /* Codec output DEVICE */
209 #define OUTPUT_DEVICE_SPEAKER 1
210 #define OUTPUT_DEVICE_HEADPHONE 2
211 #define OUTPUT_DEVICE_BOTH 3
212 #define OUTPUT_DEVICE_AUTO 4
213 
214 /* Volume Levels values */
215 #define DEFAULT_VOLMIN 0x00
216 #define DEFAULT_VOLMAX 0xFF
217 #define DEFAULT_VOLSTEP 0x04
218 
219 #define AUDIO_PAUSE 0
220 #define AUDIO_RESUME 1
221 
222 /* Codec POWER DOWN modes */
223 #define CODEC_PDWN_HW 1
224 #define CODEC_PDWN_SW 2
225 
226 /* MUTE commands */
227 #define AUDIO_MUTE_ON 1
228 #define AUDIO_MUTE_OFF 0
229 /*----------------------------------------------------------------------------*/
230 /**
231  * @}
232  */
233 
234 /** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Macros
235  * @{
236  */
237 #define VOLUME_CONVERT(x) ((Volume > 100)? 100:((uint8_t)((Volume * 255) / 100)))
238 #define DMA_MAX(x) (((x) <= DMA_MAX_SZE)? (x):DMA_MAX_SZE)
239 
240 /**
241  * @}
242  */
243 
244 /** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Functions
245  * @{
246  */
247 void EVAL_AUDIO_SetAudioInterface(uint32_t Interface);
248 uint32_t EVAL_AUDIO_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq);
249 uint32_t EVAL_AUDIO_DeInit(void);
250 uint32_t EVAL_AUDIO_Play(uint16_t* pBuffer, uint32_t Size);
251 uint32_t EVAL_AUDIO_PauseResume(uint32_t Cmd);
252 uint32_t EVAL_AUDIO_Stop(uint32_t CodecPowerDown_Mode);
253 uint32_t EVAL_AUDIO_VolumeCtl(uint8_t Volume);
254 uint32_t EVAL_AUDIO_Mute(uint32_t Command);
255 void Audio_MAL_Play(uint32_t Addr, uint32_t Size);
256 void DAC_Config(void);
257 
258 /* User Callbacks: user has to implement these functions in his code if
259  they are needed. -----------------------------------------------------------*/
260 
261 uint16_t EVAL_AUDIO_GetSampleCallBack(void);
262 
263 /* This function is called when the requested data has been completely transferred.
264  In Normal mode (when the define AUDIO_MAL_MODE_NORMAL is enabled) this function
265  is called at the end of the whole audio file.
266  In circular mode (when the define AUDIO_MAL_MODE_CIRCULAR is enabled) this
267  function is called at the end of the current buffer transmission. */
268 void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size);
269 
270 /* This function is called when half of the requested buffer has been transferred
271  This callback is useful in Circular mode only (when AUDIO_MAL_MODE_CIRCULAR
272  define is enabled)*/
273 void EVAL_AUDIO_HalfTransfer_CallBack(uint32_t pBuffer, uint32_t Size);
274 
275 /* This function is called when an Interrupt due to transfer error on or peripheral
276  error occurs. */
277 void EVAL_AUDIO_Error_CallBack(void* pData);
278 
279 /* Codec_TIMEOUT_UserCallback() function is called whenever a timeout condition
280  occurs during communication (waiting on an event that doesn't occur, bus
281  errors, busy devices ...) on the Codec control interface (I2C).
282  You can use the default timeout callback implementation by uncommenting the
283  define USE_DEFAULT_TIMEOUT_CALLBACK in stm32f4_discovery_audio_codec.h file.
284  Typically the user implementation of this callback should reset I2C peripheral
285  and re-initialize communication or in worst case reset all the application. */
286 uint32_t Codec_TIMEOUT_UserCallback(void);
287 
288 #endif /* __STM32F4_DISCOVERY_AUDIOCODEC_H */
289 
290 /**
291  * @}
292  */
293 
294 /**
295  * @}
296  */
297 
298 /**
299  * @}
300  */
301 
302 /**
303  * @}
304  */
305 
306 
307 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
This file contains all the functions prototypes for the GPIO firmware library.
CMSIS Cortex-M4 Device Peripheral Access Layer Header File. This file contains all the peripheral reg...