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
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony
*
* 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.
*/
#include "GPIO/i2c.h"
#include "Audio/audio.h"
#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
static void audio_write_register(uint8_t address, uint8_t value);
void audio_initialize_platform(int plln, int pllr, int i2sdiv, int i2sodd, int __attribute__ ((unused)) rate) {
GPIO_InitTypeDef GPIO_InitStructure;
// Turn on peripherals.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
// Assume I2C is set up
// Configure reset pin.
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// Configure I2S MCK, SCK, SD pins.
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_10 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
// Configure I2S WS pin.
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI3);
audio_reinit_codec();
// Disable I2S.
SPI3 ->I2SCFGR = 0;
// I2S clock configuration
RCC ->CFGR &= ~RCC_CFGR_I2SSRC; // PLLI2S clock used as I2S clock source.
RCC ->PLLI2SCFGR = (pllr << 28) | (plln << 6);
// Enable PLLI2S and wait until it is ready.
RCC ->CR |= RCC_CR_PLLI2SON;
while (!(RCC ->CR & RCC_CR_PLLI2SRDY ))
;
// Configure I2S.
SPI3 ->I2SPR = i2sdiv | (i2sodd << 8) | SPI_I2SPR_MCKOE;
SPI3 ->I2SCFGR = SPI_I2SCFGR_I2SMOD | SPI_I2SCFGR_I2SCFG_1
| SPI_I2SCFGR_I2SE; // Master transmitter, Phillips mode, 16 bit values, clock polarity low, enable.
}
void audio_put_codec_in_reset(void) {
GPIO_ResetBits(GPIOD, GPIO_Pin_4);
}
void audio_reinit_codec(void) {
audio_put_codec_in_reset();
for (volatile int i = 0; i < 0x4fff; i++) {
__asm__ volatile("nop");
}
GPIO_SetBits(GPIOD, GPIO_Pin_4);
// Configure codec.
audio_write_register(0x02, 0x01); // Keep codec powered off.
audio_write_register(0x04, 0xaf); // SPK always off and HP always on.
audio_write_register(0x05, 0x81); // Clock configuration: Auto detection.
audio_write_register(0x06, 0x04); // Set slave mode and Philips audio standard.
// Power on the codec.
audio_write_register(0x02, 0x9e);
// Configure codec for fast shutdown.
audio_write_register(0x0a, 0x00); // Disable the analog soft ramp.
audio_write_register(0x0e, 0x04); // Disable the digital soft ramp.
audio_write_register(0x27, 0x00); // Disable the limiter attack level.
audio_write_register(0x1f, 0x0f); // Adjust bass and treble levels.
audio_write_register(0x1a, 0x0a); // Adjust PCM volume level.
audio_write_register(0x1b, 0x0a);
}
void audio_on() {
audio_write_register(0x02, 0x9e);
SPI3 ->I2SCFGR = SPI_I2SCFGR_I2SMOD | SPI_I2SCFGR_I2SCFG_1
| SPI_I2SCFGR_I2SE; // Master transmitter, Phillips mode, 16 bit values, clock polarity low, enable.
}
void audio_off() {
audio_write_register(0x02, 0x9f);
SPI3 ->I2SCFGR = 0;
}
void audio_set_volume(int volume) {
audio_write_register(0x20, (volume + 0x19) & 0xff);
audio_write_register(0x21, (volume + 0x19) & 0xff);
}
void audio_output_sample(int16_t sample) {
while (!(SPI3 ->SR & SPI_SR_TXE ))
;
SPI3 ->DR = sample;
}
void audio_output_sample_without_blocking(int16_t sample) {
SPI3 ->DR = sample;
}
void audio_play_with_callback(AudioCallbackFunction *callback, void *context) {
audio_stop_dma();
NVIC_EnableIRQ(DMA1_Stream7_IRQn);
NVIC_SetPriority(DMA1_Stream7_IRQn, 5);
SPI3 ->CR2 |= SPI_CR2_TXDMAEN; // Enable I2S TX DMA request.
callback_function = callback;
callback_context = context;
buffer_number = 0;
if (callback_function)
callback_function(callback_context, buffer_number);
}
void audio_stop() {
audio_stop_dma();
SPI3 ->CR2 &= ~SPI_CR2_TXDMAEN; // Disable I2S TX DMA request.
NVIC_DisableIRQ(DMA1_Stream7_IRQn);
callback_function = (AudioCallbackFunction*)0;
}
void audio_provide_buffer(void *samples, int numsamples) {
while (!audio_provide_buffer_without_blocking(samples, numsamples))
__asm__ volatile ("wfi");
}
bool audio_provide_buffer_without_blocking(void *samples, int numsamples) {
if (next_buffer_samples)
return false;
NVIC_DisableIRQ(DMA1_Stream7_IRQn);
next_buffer_samples = samples;
next_buffer_length = numsamples;
if (!dma_running)
audio_start_dma_and_request_buffers();
NVIC_EnableIRQ(DMA1_Stream7_IRQn);
return true;
}
void audio_start_dma_and_request_buffers() {
// Configure DMA stream.
DMA1_Stream7 ->CR = (0 * DMA_SxCR_CHSEL_0 ) | // Channel 0
(1 * DMA_SxCR_PL_0 ) | // Priority 1
(1 * DMA_SxCR_PSIZE_0 ) | // PSIZE = 16 bit
(1 * DMA_SxCR_MSIZE_0 ) | // MSIZE = 16 bit
DMA_SxCR_MINC | // Increase memory address
(1 * DMA_SxCR_DIR_0 ) | // Memory to peripheral
DMA_SxCR_TCIE; // Transfer complete interrupt
DMA1_Stream7 ->NDTR = next_buffer_length;
DMA1_Stream7 ->PAR = (uint32_t) &SPI3 ->DR;
DMA1_Stream7 ->M0AR = (uint32_t) next_buffer_samples;
DMA1_Stream7 ->FCR = DMA_SxFCR_DMDIS;
DMA1_Stream7 ->CR |= DMA_SxCR_EN;
// Update state.
next_buffer_samples = (void*)0;
buffer_number ^= 1;
dma_running = true;
// Invoke callback if it exists to queue up another buffer.
if (callback_function)
callback_function(callback_context, buffer_number);
}
void audio_stop_dma() {
DMA1_Stream7 ->CR &= ~DMA_SxCR_EN; // Disable DMA stream.
while (DMA1_Stream7 ->CR & DMA_SxCR_EN )
; // Wait for DMA stream to stop.
dma_running = false;
}
void DMA1_Stream7_IRQHandler() {
DMA1 ->HIFCR |= DMA_HIFCR_CTCIF7; // Clear interrupt flag.
if (next_buffer_samples) {
audio_start_dma_and_request_buffers();
} else {
dma_running = false;
}
}
// Warning: don't call i2c_write from IRQ handler !
static void audio_write_register(uint8_t address, uint8_t value)
{
const uint8_t device = 0x4a;
const uint8_t data[2] = {address, value};
i2c_transaction_start();
i2c_write(device, data, 2);
i2c_transaction_end();
}
|