-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodec_CS43L22.c
executable file
·280 lines (222 loc) · 6.68 KB
/
codec_CS43L22.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
/*
* codec_CS43L22.c
*
* CS43L22 Drivers for the F4D and ChibiOS
*
* Copyright (C) 2012-14 Kumar Abhishek
*
* 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 "codec.h"
#include "gfx.h"
#include "chprintf.h"
const stm32_dma_stream_t* i2sdma;
static uint32_t i2stxdmamode=0;
extern Thread* playerThread;
static const I2CConfig i2cfg = {
OPMODE_I2C,
400000,
FAST_DUTY_CYCLE_16_9,
};
#define I2S3_TX_DMA_CHANNEL \
STM32_DMA_GETCHANNEL(STM32_SPI_SPI3_TX_DMA_STREAM, \
STM32_SPI3_TX_DMA_CHN)
static uint8_t txbuf[2]={0}, rxbuf[2]={0};
extern void dma_i2s_interrupt(void *, uint32_t);
void codec_hw_init(void)
{
// Fortunately, all pins have already been initialized in board.h
palSetPadMode(GPIOD, GPIOD_RESET, PAL_MODE_OUTPUT_PUSHPULL);
// Start the i2c driver
//i2cStart(&CODEC_I2C, &i2cfg);
// Reset the codec
codec_hw_reset();
// Write init sequence
// Keep codec powered down initially
codec_pwrCtl(0);
codec_muteCtl(0);
// Auto Detect Clock, MCLK/2
codec_writeReg(0x05, 0x81);
// Slave Mode, I2S Data Format
codec_writeReg(0x06, 0x04);
codec_pwrCtl(1);
codec_volCtl(220);
// Adjust PCM Volume
codec_writeReg(0x1A, 0x08);
codec_writeReg(0x1B, 0x08);
// Disable the analog soft ramp
codec_writeReg(0x0A, 0x00);
// Disable the digital soft ramp
codec_writeReg(0x0E, 0x04);
// Disable the limiter attack level
codec_writeReg(0x27, 0x00);
codec_writeReg(0x1C, 0x80);
}
void codec_hw_reset(void)
{
palClearPad(GPIOD, GPIOD_RESET);
halPolledDelay(MS2RTT(10));
palSetPad(GPIOD, GPIOD_RESET);
}
//static void dma_i2s_interrupt(void* dat, uint32_t flags)
//{
//// dmaStreamDisable(i2sdma);
////
//// chSysLockFromIsr();
//// chEvtSignal(playerThread, 1);
//// chSysUnlockFromIsr();
//}
extern GHandle gConsole;
static void codec_dma_init(void)
{
i2sdma=STM32_DMA_STREAM(STM32_SPI_SPI3_TX_DMA_STREAM);
i2stxdmamode = STM32_DMA_CR_CHSEL(I2S3_TX_DMA_CHANNEL) |
STM32_DMA_CR_PL(STM32_SPI_SPI3_DMA_PRIORITY) |
STM32_DMA_CR_DIR_M2P |
STM32_DMA_CR_TCIE |
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
bool_t b = dmaStreamAllocate(i2sdma,
STM32_SPI_SPI3_IRQ_PRIORITY,
(stm32_dmaisr_t)dma_i2s_interrupt,
(void *)&SPID3);
if (!b);
// gwinPrintf(gConsole, "DMA Allocated Successfully to I2S3\r\n");
dmaStreamSetPeripheral(i2sdma, &(SPI3->DR));
}
void codec_i2s_init(uint16_t sampleRate, uint8_t nBits)
{
uint16_t prescale;
uint32_t pllfreq=STM32_PLLI2SVCO / STM32_PLLI2SR_VALUE;
if (nBits!=16)
return;
// SPI3 in I2S Mode, Master
CODEC_I2S_ENABLE;
CODEC_I2S->I2SCFGR=SPI_I2SCFGR_I2SMOD | SPI_I2SCFGR_I2SCFG_1;
// Master clock mode Fs * 256
prescale=(pllfreq*10)/(256*sampleRate) + 5;
prescale/=10;
chprintf((BaseChannel*)&SD2, "Prescale value:%d\r\n", prescale);
if (prescale > 0xFF || prescale < 2) prescale=2;
if (prescale & 0x01)
CODEC_I2S->I2SPR=SPI_I2SPR_MCKOE | SPI_I2SPR_ODD | (prescale>>1);
else
CODEC_I2S->I2SPR=SPI_I2SPR_MCKOE | (prescale>>1);
codec_dma_init();
// Enable I2S DMA Request
CODEC_I2S->CR2 = SPI_CR2_TXDMAEN;
// Now Enable I2S
CODEC_I2S->I2SCFGR|=SPI_I2SCFGR_I2SE;
}
void codec_writeReg(uint8_t addr, uint8_t data)
{
txbuf[0]=addr;
txbuf[1]=data;
i2cMasterTransmitTimeout(&I2CD1, CS43L22_ADDR, txbuf, 2, NULL, 0, MS2ST(4));
}
uint8_t codec_readReg(uint8_t addr)
{
txbuf[0]=addr;
i2cMasterTransmitTimeout(&I2CD1, CS43L22_ADDR, txbuf, 1, rxbuf, 2, MS2ST(4));
return rxbuf[0];
}
void codec_pwrCtl(uint8_t pwr)
{
if (pwr)
codec_writeReg(0x02, 0x9E);
else
codec_writeReg(0x02, 0x01);
}
void codec_muteCtl(uint8_t mute)
{
if (mute)
codec_writeReg(0x04, 0xFF);
else
codec_writeReg(0x04, 0xAF);
}
void codec_volCtl(uint8_t vol)
{
if (vol > 0xE6)
{
codec_writeReg(0x20, vol-0xE7);
codec_writeReg(0x21, vol-0xE7);
}
else
{
codec_writeReg(0x20, vol+0x19);
codec_writeReg(0x21, vol+0x19);
}
}
void codec_selectAudioSource(uint8_t src)
{
switch (src) {
case CODEC_AUDIOSRC_DIGITAL:
// Disable all pass through channels
codec_writeReg(0x0E, 0x04);
break;
case CODEC_AUDIOSRC_MIC:
// Select AIN4A/B for pass through
codec_writeReg(0x08, 0x08);
codec_writeReg(0x09, 0x08);
codec_writeReg(0x0E, 0xC4);
break;
case CODEC_AUDIOSRC_FMRADIO:
// Select AIN2A/B for pass through
codec_writeReg(0x08, 0x02);
codec_writeReg(0x09, 0x02);
codec_writeReg(0x0E, 0xC4);
break;
}
// codec_writeReg(0x14, 0x20);
// codec_writeReg(0x15, 0x20);
}
// Send data to the codec via I2S
void codec_audio_send(void* txbuf, size_t n)
{
dmaStreamSetMemory0(i2sdma, txbuf);
dmaStreamSetTransactionSize(i2sdma, n);
dmaStreamSetMode(i2sdma, i2stxdmamode | STM32_DMA_CR_MINC);
dmaStreamClearInterrupt(i2sdma);
dmaStreamEnable(i2sdma);
}
void codec_audio_circular(void *txbuf, size_t n) {
dmaStreamSetMemory0(i2sdma, txbuf);
dmaStreamSetTransactionSize(i2sdma, n);
dmaStreamSetMode(i2sdma, i2stxdmamode | STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_HTIE);
dmaStreamClearInterrupt(i2sdma);
dmaStreamEnable(i2sdma);
}
void codec_pauseResumePlayback(uint8_t pause)
{
if (pause) {
codec_muteCtl(1);
codec_pwrCtl(0);
CODEC_I2S->CR2=0;
} else {
codec_pwrCtl(1);
CODEC_I2S->CR2=SPI_CR2_TXDMAEN;
codec_muteCtl(0);
}
}
void codec_sendBeep(void)
{
codec_writeReg(0x1E, 0x00);
codec_writeReg(0x1E, 0x40);
}