-
Notifications
You must be signed in to change notification settings - Fork 5
/
my_stm32f4_discovery_audio_codec.h
309 lines (254 loc) · 12.3 KB
/
my_stm32f4_discovery_audio_codec.h
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
/**
******************************************************************************
* @file stm32f4_discovery_audio_codec.h
* @author Xavier Halgand & MCD Application Team
* @version
* @date october 2012
* @brief This file contains all the functions prototypes for the
* stm32f4_discovery_audio_codec.c driver.
******************************************************************************
* @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>
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4_DISCOVERY_AUDIOCODEC_H
#define __STM32F4_DISCOVERY_AUDIOCODEC_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_rcc.h"
/** @addtogroup Utilities
* @{
*/
/** @addtogroup STM32F4_DISCOVERY
* @{
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC
* @{
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Types
* @{
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Constants
* @{
*/
/*------------------------------------
CONFIGURATION: Audio Codec Driver Configuration parameters
----------------------------------------*/
/* Audio Transfer mode (I2S Interrupt) */
//#define I2S_INTERRUPT /* Uncomment this line to enable audio transfert with I2S interrupt*/
/* Audio Transfer mode (DMA, Interrupt or Polling) */
/* #define AUDIO_MAL_MODE_NORMAL */ /* Uncomment this line to enable the normal audio
Transfer using DMA */
#define AUDIO_MAL_MODE_CIRCULAR /* Uncomment this line to enable the circular audio
Transfer using DMA */
/* For the DMA modes select the interrupt that will be used */
#define AUDIO_MAL_DMA_IT_TC_EN /* Uncomment this line to enable DMA Transfer Complete interrupt */
#define AUDIO_MAL_DMA_IT_HT_EN /* Uncomment this line to enable DMA Half Transfer Complete interrupt */
/* #define AUDIO_MAL_DMA_IT_TE_EN */ /* Uncomment this line to enable DMA Transfer Error interrupt */
/* Select the interrupt preemption priority and subpriority for the DMA interrupt */
#define EVAL_AUDIO_IRQ_PREPRIO 3 /* Select the preemption priority level(0 is the highest) */
#define EVAL_AUDIO_IRQ_SUBRIO 3 /* Select the sub-priority level (0 is the highest) */
/* Uncomment the following line to use the default Codec_TIMEOUT_UserCallback()
function implemented in stm32f4_discovery_audio_codec.c file.
Codec_TIMEOUT_UserCallback() function is called whenever a timeout condition
occurs during communication (waiting on an event that doesn't occur, bus
errors, busy devices ...). */
/* #define USE_DEFAULT_TIMEOUT_CALLBACK */
/* Enable this define to use the I2S DMA for writing into DAC register */
//#define DAC_USE_I2S_DMA
/*----------------------------------------------------------------------------*/
/*------------------------------------
OPTIONAL Configuration defines parameters
----------------------------------------*/
/* I2C clock speed configuration (in Hz)
WARNING:
Make sure that this define is not already declared in other files (ie.
stm322xg_eval.h file). It can be used in parallel by other modules. */
#ifndef I2C_SPEED
#define I2C_SPEED 100000
#endif /* I2C_SPEED */
/* Uncomment defines below to select standard for audio communication between
Codec and I2S peripheral */
#define I2S_STANDARD_PHILLIPS
/* #define I2S_STANDARD_MSB */
/* #define I2S_STANDARD_LSB */
/* Uncomment the defines below to select if the Master clock mode should be
enabled or not */
#define CODEC_MCLK_ENABLED
/* #deine CODEC_MCLK_DISABLED */
/* Uncomment this line to enable verifying data sent to codec after each write
operation */
#define VERIFY_WRITTENDATA
/*----------------------------------------------------------------------------*/
/*-----------------------------------
Hardware Configuration defines parameters
-----------------------------------------*/
/* Audio Reset Pin definition */
#define AUDIO_RESET_GPIO_CLK RCC_AHB1Periph_GPIOD
#define AUDIO_RESET_PIN GPIO_Pin_4
#define AUDIO_RESET_GPIO GPIOD
/* I2S peripheral configuration defines */
#define CODEC_I2S SPI3
#define CODEC_I2S_CLK RCC_APB1Periph_SPI3
#define CODEC_I2S_ADDRESS 0x40003C0C
#define CODEC_I2S_GPIO_AF GPIO_AF_SPI3
#define CODEC_I2S_IRQ SPI3_IRQn
#define CODEC_I2S_GPIO_CLOCK (RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOA)
#define CODEC_I2S_WS_PIN GPIO_Pin_4
#define CODEC_I2S_SCK_PIN GPIO_Pin_10
#define CODEC_I2S_SD_PIN GPIO_Pin_12
#define CODEC_I2S_MCK_PIN GPIO_Pin_7
#define CODEC_I2S_WS_PINSRC GPIO_PinSource4
#define CODEC_I2S_SCK_PINSRC GPIO_PinSource10
#define CODEC_I2S_SD_PINSRC GPIO_PinSource12
#define CODEC_I2S_MCK_PINSRC GPIO_PinSource7
#define CODEC_I2S_GPIO GPIOC
#define CODEC_I2S_WS_GPIO GPIOA
#define CODEC_I2S_MCK_GPIO GPIOC
#define Audio_I2S_IRQHandler SPI3_IRQHandler
#define AUDIO_MAL_DMA_PERIPH_DATA_SIZE DMA_PeripheralDataSize_HalfWord
#define AUDIO_MAL_DMA_MEM_DATA_SIZE DMA_MemoryDataSize_HalfWord
#define DMA_MAX_SZE 0xFFFF
#define DAC_DHR12L1_ADDRESS 0x4000740C
#define DAC_DHR12R1_ADDRESS 0x40007408
#define DAC_DHR8R1_ADDRESS 0x40007410
#define AUDIO_DAC_CHANNEL DAC_Channel_1
/* I2S DMA Stream definitions */
#define AUDIO_I2S_DMA_CLOCK RCC_AHB1Periph_DMA1
#define AUDIO_I2S_DMA_STREAM DMA1_Stream7
#define AUDIO_I2S_DMA_DREG CODEC_I2S_ADDRESS
#define AUDIO_I2S_DMA_CHANNEL DMA_Channel_0
#define AUDIO_I2S_DMA_IRQ DMA1_Stream7_IRQn
#define AUDIO_I2S_DMA_FLAG_TC DMA_FLAG_TCIF7
#define AUDIO_I2S_DMA_FLAG_HT DMA_FLAG_HTIF7
#define AUDIO_I2S_DMA_FLAG_FE DMA_FLAG_FEIF7
#define AUDIO_I2S_DMA_FLAG_TE DMA_FLAG_TEIF7
#define AUDIO_I2S_DMA_FLAG_DME DMA_FLAG_DMEIF7
#define Audio_MAL_I2S_IRQHandler DMA1_Stream7_IRQHandler
/* DAC DMA Stream definitions */
#define AUDIO_DAC_DMA_CLOCK RCC_AHB1Periph_DMA1
#define AUDIO_DAC_DMA_STREAM DMA1_Stream0
#define AUDIO_DAC_DMA_DREG DAC_DHR12L1_ADDRESS
#define AUDIO_DAC_DMA_CHANNEL DMA_Channel_0
#define AUDIO_DAC_DMA_IRQ DMA1_Stream0_IRQn
#define AUDIO_DAC_DMA_FLAG_TC DMA_FLAG_TCIF0
#define AUDIO_DAC_DMA_FLAG_HT DMA_FLAG_HTIF0
#define AUDIO_DAC_DMA_FLAG_FE DMA_FLAG_FEIF0
#define AUDIO_DAC_DMA_FLAG_TE DMA_FLAG_TEIF0
#define AUDIO_DAC_DMA_FLAG_DME DMA_FLAG_DMEIF0
#define Audio_MAL_DAC_IRQHandler DMA1_Stream0_IRQHandler
/* I2C peripheral configuration defines (control interface of the audio codec) */
#define CODEC_I2C I2C1
#define CODEC_I2C_CLK RCC_APB1Periph_I2C1
#define CODEC_I2C_GPIO_CLOCK RCC_AHB1Periph_GPIOB
#define CODEC_I2C_GPIO_AF GPIO_AF_I2C1
#define CODEC_I2C_GPIO GPIOB
#define CODEC_I2C_SCL_PIN GPIO_Pin_6
#define CODEC_I2C_SDA_PIN GPIO_Pin_9
#define CODEC_I2S_SCL_PINSRC GPIO_PinSource6
#define CODEC_I2S_SDA_PINSRC GPIO_PinSource9
/* Maximum Timeout values for flags and events waiting loops. These timeouts are
not based on accurate values, they just guarantee that the application will
not remain stuck if the I2C communication is corrupted.
You may modify these timeout values depending on CPU frequency and application
conditions (interrupts routines ...). */
#define CODEC_FLAG_TIMEOUT ((uint32_t)0x1000)
#define CODEC_LONG_TIMEOUT ((uint32_t)(300 * CODEC_FLAG_TIMEOUT))
/*----------------------------------------------------------------------------*/
/*-----------------------------------
Audio Codec User defines
-----------------------------------------*/
/* Audio interface : I2S or DAC */
#define AUDIO_INTERFACE_I2S 1
#define AUDIO_INTERFACE_DAC 2
/* Codec output DEVICE */
#define OUTPUT_DEVICE_SPEAKER 1
#define OUTPUT_DEVICE_HEADPHONE 2
#define OUTPUT_DEVICE_BOTH 3
#define OUTPUT_DEVICE_AUTO 4
/* Volume Levels values */
#define DEFAULT_VOLMIN 0x00
#define DEFAULT_VOLMAX 0xFF
#define DEFAULT_VOLSTEP 0x04
#define AUDIO_PAUSE 0
#define AUDIO_RESUME 1
/* Codec POWER DOWN modes */
#define CODEC_PDWN_HW 1
#define CODEC_PDWN_SW 2
/* MUTE commands */
#define AUDIO_MUTE_ON 1
#define AUDIO_MUTE_OFF 0
/*----------------------------------------------------------------------------*/
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Macros
* @{
*/
#define VOLUME_CONVERT(x) ((Volume > 100)? 100:((uint8_t)((Volume * 255) / 100)))
#define DMA_MAX(x) (((x) <= DMA_MAX_SZE)? (x):DMA_MAX_SZE)
/**
* @}
*/
/** @defgroup STM32F4_DISCOVERY_AUDIO_CODEC_Exported_Functions
* @{
*/
void EVAL_AUDIO_SetAudioInterface(uint32_t Interface);
uint32_t EVAL_AUDIO_Init(uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq);
uint32_t EVAL_AUDIO_DeInit(void);
uint32_t EVAL_AUDIO_Play(uint16_t* pBuffer, uint32_t Size);
uint32_t EVAL_AUDIO_PauseResume(uint32_t Cmd);
uint32_t EVAL_AUDIO_Stop(uint32_t CodecPowerDown_Mode);
uint32_t EVAL_AUDIO_VolumeCtl(uint8_t Volume);
uint32_t EVAL_AUDIO_Mute(uint32_t Command);
void Audio_MAL_Play(uint32_t Addr, uint32_t Size);
void DAC_Config(void);
/* User Callbacks: user has to implement these functions in his code if
they are needed. -----------------------------------------------------------*/
uint16_t EVAL_AUDIO_GetSampleCallBack(void);
/* This function is called when the requested data has been completely transferred.
In Normal mode (when the define AUDIO_MAL_MODE_NORMAL is enabled) this function
is called at the end of the whole audio file.
In circular mode (when the define AUDIO_MAL_MODE_CIRCULAR is enabled) this
function is called at the end of the current buffer transmission. */
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size);
/* This function is called when half of the requested buffer has been transferred
This callback is useful in Circular mode only (when AUDIO_MAL_MODE_CIRCULAR
define is enabled)*/
void EVAL_AUDIO_HalfTransfer_CallBack(uint32_t pBuffer, uint32_t Size);
/* This function is called when an Interrupt due to transfer error on or peripheral
error occurs. */
void EVAL_AUDIO_Error_CallBack(void* pData);
/* Codec_TIMEOUT_UserCallback() function is called whenever a timeout condition
occurs during communication (waiting on an event that doesn't occur, bus
errors, busy devices ...) on the Codec control interface (I2C).
You can use the default timeout callback implementation by uncommenting the
define USE_DEFAULT_TIMEOUT_CALLBACK in stm32f4_discovery_audio_codec.h file.
Typically the user implementation of this callback should reset I2C peripheral
and re-initialize communication or in worst case reset all the application. */
uint32_t Codec_TIMEOUT_UserCallback(void);
#endif /* __STM32F4_DISCOVERY_AUDIOCODEC_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/