qipp
2025-11-07 1db34b4e65f72d8ae8b1f0efbc7a4395f9bbcae8
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <stdio.h>
#include <stdlib.h>
//#include <def1802.h>
//#include <cdef1802.h>
#include <def21489.h>
#include <cdef21489.h>
#include <services/int/adi_int.h>
#include <string.h>
#include "sport.h"
#include "memory.h"
#include "config.h"
#include "dma.h"
 
 
struct DMA_Desc{
     u32 next;
     u32    bsize;
     u32    asize;
     u32 buffer;
};
 
//unsigned int SAMPLE_RATE = 48000;
//unsigned int SAMPLE_NUM = 64;
u16 mCodecNum = 0;
struct AudioCodec mAudioCodec[16];
static struct DMA_Desc sp_desc[32];//0a_ping,0a_pong,0b_ping,0b_pong,...
 
#if 0
//#define NUM_SAMPLES 64
 
int    sport0a_buf[2][SAMPLE_NUM*SLOTS];
int    sport0b_buf[2][SAMPLE_NUM*SLOTS];
int    sport1a_buf[2][SAMPLE_NUM*SLOTS];
int    sport1b_buf[2][SAMPLE_NUM*SLOTS];
 
int    sport2a_buf[2][SAMPLE_NUM*SLOTS];
int    sport2b_buf[2][SAMPLE_NUM*SLOTS];
int    sport3a_buf[2][SAMPLE_NUM*SLOTS];
int    sport3b_buf[2][SAMPLE_NUM*SLOTS];
 
int    sport4a_buf[2][SAMPLE_NUM*SLOTS];
int    sport4b_buf[2][SAMPLE_NUM*SLOTS];
int    sport5a_buf[2][SAMPLE_NUM*SLOTS];
int    sport5b_buf[2][SAMPLE_NUM*SLOTS];
 
int    sport6a_buf[2][SAMPLE_NUM*SLOTS];
int    sport6b_buf[2][SAMPLE_NUM*SLOTS];
int    sport7a_buf[2][SAMPLE_NUM*SLOTS];
int    sport7b_buf[2][SAMPLE_NUM*SLOTS];
 
 
//int xt[1024*1024];
 
 
//2 For ping-pong ,each TCB has four registers.
int    sport0a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport0a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport0a_buf[1]}
};
int    sport0b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport0b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport0b_buf[1]}
};
int    sport1a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport1a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport1a_buf[1]}
};
int    sport1b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport1b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport1b_buf[1]}
};
int    sport2a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport2a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport2a_buf[1]}
};
int    sport2b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport2b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport2b_buf[1]}
};
int    sport3a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport3a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport3a_buf[1]}
};
int    sport3b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport3b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport3b_buf[1]}
};
 
int    sport4a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport4a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport4a_buf[1]}
};
int    sport4b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport4b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport4b_buf[1]}
};
 
int    sport5a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport5a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport5a_buf[1]}
};
int    sport5b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport5b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport5b_buf[1]}
};
 
int    sport6a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport6a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport6a_buf[1]}
};
int    sport6b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport6b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport6b_buf[1]}
};
 
int    sport7a_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport7a_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport7a_buf[1]}
};
int    sport7b_tcb[2][4] = {
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport7b_buf[0]},
    {0, SAMPLE_NUM*SLOTS, 1, (int) sport7b_buf[1]}
};
 
/*
sport 0,2  TDMÊä³ö
sport 1,3  TDMÊäÈë
sport 4  i2s Êä³ö
sport 5, i2s ÊäÈë
sport 6  tdmÊä³ö
sport 7  tdmÊäÈë
*/
void InitSPORT(int mask,int sampleRate)
{
    int i;
 
    int reg;
 
    /* External clock and frame syncs generated by AD1939 */
    *pDIV0 = 0x00000000;  // Transmitter (SPORT0)
    *pDIV1 = 0x00000000;  // Receiver (SPORT1) at 12.288 MHz SCLK and 48 kHz sample rate
 
 
    sport0a_tcb[0][0]= ((int)sport0a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport0a_tcb[1][0]= ((int)sport0a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport0b_tcb[0][0]= ((int)sport0b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport0b_tcb[1][0]= ((int)sport0b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport1a_tcb[0][0]= ((int)sport1a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport1a_tcb[1][0]= ((int)sport1a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport1b_tcb[0][0]= ((int)sport1b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport1b_tcb[1][0]= ((int)sport1b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport2a_tcb[0][0]= ((int)sport2a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport2a_tcb[1][0]= ((int)sport2a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport2b_tcb[0][0]= ((int)sport2b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport2b_tcb[1][0]= ((int)sport2b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport3a_tcb[0][0]= ((int)sport3a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport3a_tcb[1][0]= ((int)sport3a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport3b_tcb[0][0]= ((int)sport3b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport3b_tcb[1][0]= ((int)sport3b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport4a_tcb[0][0]= ((int)sport4a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport4a_tcb[1][0]= ((int)sport4a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport4b_tcb[0][0]= ((int)sport4b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport4b_tcb[1][0]= ((int)sport4b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport5a_tcb[0][0]= ((int)sport5a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport5a_tcb[1][0]= ((int)sport5a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport5b_tcb[0][0]= ((int)sport5b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport5b_tcb[1][0]= ((int)sport5b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport6a_tcb[0][0]= ((int)sport6a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport6a_tcb[1][0]= ((int)sport6a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport6b_tcb[0][0]= ((int)sport6b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport6b_tcb[1][0]= ((int)sport6b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    sport7a_tcb[0][0]= ((int)sport7a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport7a_tcb[1][0]= ((int)sport7a_tcb[0] + 3)&0x7ffff | (1<<19);
    sport7b_tcb[0][0]= ((int)sport7b_tcb[1] + 3)&0x7ffff | (1<<19);
    sport7b_tcb[1][0]= ((int)sport7b_tcb[0] + 3)&0x7ffff | (1<<19);
 
    *pSPCTL0 = *pSPCTL1 = *pSPCTL2=*pSPCTL3=*pSPCTL4=*pSPCTL5=*pSPCTL6=*pSPCTL7=0;
    *pSPMCTL0 = *pSPMCTL1=*pSPMCTL2=*pSPMCTL3=*pSPMCTL4=*pSPMCTL5 =*pSPMCTL6=*pSPMCTL7=0;
    *pSPCTLN0 =*pSPCTLN1=*pSPCTLN2=*pSPCTLN3=*pSPCTLN4=*pSPCTLN5 =*pSPCTLN6 =*pSPCTLN7=0;
 
    *pCPSP0A = sport0a_tcb[0][0];
    *pCPSP0B = sport0b_tcb[0][0];
    *pCPSP1A = sport1a_tcb[0][0];
    *pCPSP1B = sport1b_tcb[0][0];
    *pCPSP2A = sport2a_tcb[0][0];
    *pCPSP2B = sport2b_tcb[0][0];
    *pCPSP3A = sport3a_tcb[0][0];
    *pCPSP3B = sport3b_tcb[0][0];
 
    *pCPSP4A = sport4a_tcb[0][0];
    *pCPSP4B = sport4b_tcb[0][0];
    *pCPSP5A = sport5a_tcb[0][0];
    *pCPSP5B = sport5b_tcb[0][0];
 
    *pCPSP6A = sport6a_tcb[0][0];
    *pCPSP6B = sport6b_tcb[0][0];
 
    *pCPSP7A = sport7a_tcb[0][0];
    *pCPSP7B = sport7b_tcb[0][0];
 
 
     reg = (SPTRAN| OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A | SPEN_B | SCHEN_B | SDEN_B); // send
     *pSPCTL0 = reg;
     *pSPCTL6 = reg;
    //*pSPCTL2 = reg;
 
     reg = (SPTRAN| OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A); //output
     *pSPCTL2 = reg;
     *pSPCTL4 = reg;
 
     reg = (OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A | SPEN_B | SCHEN_B | SDEN_B); // recv
     *pSPCTL1 = reg;
     *pSPCTL3 = reg;
     *pSPCTL7 = reg;
    //*pSPCTL3 = reg;
 
     reg = (OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A );
     *pSPCTL5 = reg;
 
     adi_int_InstallHandler(ADI_CID_SP1I, DMACallBack, 0, true);
}
#endif
 
 
void sport_config(struct SportDef *  config)
{
    u32 i;
    volatile u32 *pSPCTLn[8] = {pSPCTL0,pSPCTL1,pSPCTL2,pSPCTL3,pSPCTL4,pSPCTL5,pSPCTL6,pSPCTL7};
    volatile u32 *pSPnCS0[8] = {pSP0CS0,pSP1CS0,pSP2CS0,pSP3CS0,pSP4CS0,pSP5CS0,pSP6CS0,pSP7CS0};
    volatile u32 *pSPMCTLn[8] = {pSPMCTL0,pSPMCTL1,pSPMCTL2,pSPMCTL3,pSPMCTL4,pSPMCTL5,pSPMCTL6,pSPMCTL7};
    volatile u32 *pCPSPnAB[16] = {pCPSP0A,pCPSP0B,pCPSP1A,pCPSP1B,pCPSP2A,pCPSP2B,pCPSP3A,pCPSP3B
                                    ,pCPSP4A,pCPSP4B,pCPSP5A,pCPSP5B,pCPSP6A,pCPSP6B,pCPSP7A,pCPSP7B};
 
    struct DMA_Desc* desca_0 = &sp_desc[config->spid*4 + 0];
    struct DMA_Desc* desca_1 = &sp_desc[config->spid*4 + 1];
 
    u32 dmaBufferLen = config->slots*SAMPLE_NUM;
    u32 bufLen = dmaBufferLen;
    s32* dmaBuffer_ping, *dmaBuffer_pong;
 
    if(config->interrupt ) {
        u32 iid[] = {ADI_CID_SP0I,ADI_CID_SP1I,ADI_CID_SP2I,ADI_CID_SP3I
                ,ADI_CID_SP4I,ADI_CID_SP5I,ADI_CID_SP6I,ADI_CID_SP7I};
 
        adi_int_InstallHandler(iid[config->spid], DMACallBack, 0, true);
    }
 
    if(config->enable_sec) {
        bufLen *= 2;
    }
 
    dmaBuffer_ping = (s32*)SRAMMalloc(bufLen* 2*sizeof(s32));//for ping-pong.
    if(dmaBuffer_ping == NULL) {
        printf("dmaBuffer malloc fail.\n");
    }
    memset(dmaBuffer_ping, 0 , bufLen* 2 *sizeof(s32));
    dmaBuffer_pong = dmaBuffer_ping + dmaBufferLen;
    mAudioCodec[mCodecNum].dataPtr[0] = dmaBuffer_ping;
    mAudioCodec[mCodecNum].dataPtr[1] = dmaBuffer_pong;
    mAudioCodec[mCodecNum].slot_num = config->slots;
    mAudioCodec[mCodecNum].channel_num = config->vld_a;
    mAudioCodec[mCodecNum].rx  = config->rx;
    mCodecNum++;
 
    desca_0->next = ((u32)desca_1 + 3)&0x7ffff | (1<<19);
    desca_0->buffer = (u32)dmaBuffer_ping;
    desca_0->asize = sizeof(s32);
    desca_0->bsize = config->slots*SAMPLE_NUM;
 
    desca_1->next = ((uint32_t)desca_0 + 3)&0x7ffff | (1<<19);
    desca_1->buffer = (u32)dmaBuffer_pong;
    desca_1->asize = sizeof(s32);
    desca_1->bsize = config->slots*SAMPLE_NUM;
 
    *pCPSPnAB[config->spid*2] = desca_1->next;
 
    if(config->enable_sec) {
        struct DMA_Desc* desca_2 = &sp_desc[config->spid*4 + 2];
        struct DMA_Desc* desca_3 = &sp_desc[config->spid*4 + 3];
 
        dmaBuffer_ping = dmaBuffer_pong + dmaBufferLen;
        dmaBuffer_pong = dmaBuffer_ping + dmaBufferLen;
 
        mAudioCodec[mCodecNum].dataPtr[0] = dmaBuffer_ping;
        mAudioCodec[mCodecNum].dataPtr[1] = dmaBuffer_pong;
        mAudioCodec[mCodecNum].slot_num = config->slots;
        mAudioCodec[mCodecNum].channel_num = config->vld_b;
        mAudioCodec[mCodecNum].rx  = config->rx;
        mCodecNum++;
 
        desca_2->next = ((u32)desca_3 + 3)&0x7ffff | (1<<19);
        desca_2->buffer = (u32)dmaBuffer_ping;
        desca_2->asize = sizeof(s32);
        desca_2->bsize = config->slots*SAMPLE_NUM;
 
        desca_3->next = ((u32)desca_2 + 3)&0x7ffff | (1<<19);
        desca_3->buffer = (u32)dmaBuffer_pong;
        desca_3->asize = sizeof(s32);
        desca_3->bsize = config->slots*SAMPLE_NUM;
 
        *pCPSPnAB[config->spid*2+1] = desca_3->next;
    }
 
    if(config->opmode) { //iis
        u32 reg  = SLEN32 | OPMODE | SPEN_A | SCHEN_A |SDEN_A |(config->lfs?(LFS):0) | (config->clke?CKRE:0);
    //    *pSPCTL6 = SPTRAN | SLEN32 | OPMODE | SPEN_A | SCHEN_A |SDEN_A | SPEN_B | SCHEN_B |SDEN_B;// Êä³ö
    //    *pSPCTL7 =  SLEN32 | OPMODE | SPEN_A | SCHEN_A |SDEN_A | SPEN_B | SCHEN_B |SDEN_B; //ÊäÈë
        if(config->enable_sec) {
            reg |=  SPEN_B | SCHEN_B |SDEN_B ;
        }
 
        if(config->rx == ufalse) {
            reg |= SPTRAN;
        }
 
        *pSPCTLn[config->spid] = reg;
    }
    else { //tdm
        u32 reg  = SLEN32 |  SCHEN_A |SDEN_A  | FSR | (config->clke?CKRE:0) | (config->lfs?LFS:0);
        if(config->enable_sec) {
            reg |=  SCHEN_B |SDEN_B;
        }
 
        if(config->rx == ufalse) {
            reg |= SPTRAN;
        }
 
        *pSPCTLn[config->spid] = reg;
 
        reg = 0;
        for(i=0 ;i < config->slots ;i ++ ){
            reg |= (1<<i);
        }
        *pSPnCS0[config->spid] = reg;
 
        *pSPMCTLn[config->spid] = ((config->slots-1)<<5) | (config->mfd << 1) | MCEA| (config->enable_sec?MCEB:0);
    }
}
 
uvoid sport_enable(struct SportDef *  config)
{
    u32 reg = SDEN_A;
    volatile u32 *pSPCTLn[8] = {pSPCTL0,pSPCTL1,pSPCTL2,pSPCTL3,pSPCTL4,pSPCTL5,pSPCTL6,pSPCTL7};
 
    if(config->enable_sec) {
        reg |=  SDEN_B;
    }
    *pSPCTLn[config->spid] |= reg;
}
 
uvoid DMACallBack (u32 iid, uvoidptr arg)
{
    adi_int_ClearPending(iid);
    AudioProcCallBack();
}