-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.h
148 lines (130 loc) · 6.27 KB
/
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
/*
* codec.h - stm32f405_codec board codec interface routines
*
* Cut from stm32f4_discovery_audio_codec.c
*
* Author: Dan Green (danngreen1@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* See http://creativecommons.org/licenses/MIT/ for more information.
*
* -----------------------------------------------------------------------------
*/
#ifndef __codec__
#define __codec__
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_rcc.h"
/* I2C clock speed configuration (in Hz) */
#define I2C_SPEED 50000
/* 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 */
//#define USE_DEFAULT_TIMEOUT_CALLBACK
/*-----------------------------------
Hardware Configuration defines parameters
-----------------------------------------*/
/* I2S peripheral configuration defines */
#define CODEC_I2S SPI2
#define CODEC_I2S_EXT I2S2ext
#define CODEC_I2S_CLK RCC_APB1Periph_SPI2
#define CODEC_I2S_ADDRESS 0x4000380C
#define CODEC_I2S_EXT_ADDRESS 0x4000340C
#define CODEC_I2S_GPIO_AF GPIO_AF_SPI2
#define CODEC_I2S_IRQ SPI2_IRQn
#define CODEC_I2S_EXT_IRQ SPI2_IRQn
#define CODEC_I2S_GPIO_CLOCK (RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOB)
#define CODEC_I2S_WS_PIN GPIO_Pin_12
#define CODEC_I2S_SCK_PIN GPIO_Pin_13
#define CODEC_I2S_SDI_PIN GPIO_Pin_14
#define CODEC_I2S_SDO_PIN GPIO_Pin_15
#define CODEC_I2S_MCK_PIN GPIO_Pin_6
#define CODEC_I2S_WS_PINSRC GPIO_PinSource12
#define CODEC_I2S_SCK_PINSRC GPIO_PinSource13
#define CODEC_I2S_SDI_PINSRC GPIO_PinSource14
#define CODEC_I2S_SDO_PINSRC GPIO_PinSource15
#define CODEC_I2S_MCK_PINSRC GPIO_PinSource6
#define CODEC_I2S_GPIO GPIOB
#define CODEC_I2S_MCK_GPIO GPIOC
#define AUDIO_I2S_IRQHandler SPI2_IRQHandler
#define AUDIO_I2S_EXT_IRQHandler SPI2_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
/* I2S DMA Stream definitions */
#define AUDIO_I2S_DMA_CLOCK RCC_AHB1Periph_DMA1
#define AUDIO_I2S_DMA_STREAM DMA1_Stream4
#define AUDIO_I2S_DMA_DREG CODEC_I2S_ADDRESS
#define AUDIO_I2S_DMA_CHANNEL DMA_Channel_0
#define AUDIO_I2S_DMA_IRQ DMA1_Stream4_IRQn
#define AUDIO_I2S_DMA_FLAG_TC DMA_FLAG_TCIF4
#define AUDIO_I2S_DMA_FLAG_HT DMA_FLAG_HTIF4
#define AUDIO_I2S_DMA_FLAG_FE DMA_FLAG_FEIF4
#define AUDIO_I2S_DMA_FLAG_TE DMA_FLAG_TEIF4
#define AUDIO_I2S_DMA_FLAG_DME DMA_FLAG_DMEIF4
#define AUDIO_I2S_EXT_DMA_STREAM DMA1_Stream3
#define AUDIO_I2S_EXT_DMA_DREG CODEC_I2S_EXT_ADDRESS
#define AUDIO_I2S_EXT_DMA_CHANNEL DMA_Channel_3
#define AUDIO_I2S_EXT_DMA_IRQ DMA1_Stream3_IRQn
#define AUDIO_I2S_EXT_DMA_FLAG_TC DMA_FLAG_TCIF3
#define AUDIO_I2S_EXT_DMA_FLAG_HT DMA_FLAG_HTIF3
#define AUDIO_I2S_EXT_DMA_FLAG_FE DMA_FLAG_FEIF3
#define AUDIO_I2S_EXT_DMA_FLAG_TE DMA_FLAG_TEIF3
#define AUDIO_I2S_EXT_DMA_FLAG_DME DMA_FLAG_DMEIF3
/* I2C peripheral configuration defines (control interface of the audio codec) */
#define CODEC_I2C I2C2
#define CODEC_I2C_CLK RCC_APB1Periph_I2C2
#define CODEC_I2C_GPIO_CLOCK RCC_AHB1Periph_GPIOB
#define CODEC_I2C_GPIO_AF GPIO_AF_I2C2
#define CODEC_I2C_GPIO GPIOB
#define CODEC_I2C_SCL_PIN GPIO_Pin_10
#define CODEC_I2C_SDA_PIN GPIO_Pin_11
#define CODEC_I2S_SCL_PINSRC GPIO_PinSource10
#define CODEC_I2S_SDA_PINSRC GPIO_PinSource11
/* 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
-----------------------------------------*/
/* Codec output DEVICE */
#define OUTPUT_DEVICE_SPEAKER 1
#define OUTPUT_DEVICE_HEADPHONE 2
#define OUTPUT_DEVICE_BOTH 3
#define OUTPUT_DEVICE_AUTO 4
/*----------------------------------------------------------------------------*/
/* High Layer codec functions */
uint32_t Codec_Init(uint32_t AudioFreq);
/* Low layer codec functions */
void Codec_CtrlInterface_Init(void);
void Codec_AudioInterface_Init(uint32_t AudioFreq);
uint32_t Codec_Reset(void);
uint32_t Codec_WriteRegister(uint8_t RegisterAddr, uint16_t RegisterValue);
void Codec_GPIO_Init(void);
void init_i2s_clkin(void);
#endif