qipp
2025-11-07 744722688187eb4991c72464e2bb5e06b244fee7
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
#include "global.h"
 
    int    sport1a_buf[2][NUM_SAMPLES*SLOTS];        
    int    sport1b_buf[2][NUM_SAMPLES*SLOTS];
 
    
    int    sport0a_buf[2][NUM_SAMPLES*SLOTS];
    int    sport0b_buf[2][NUM_SAMPLES*SLOTS];
        
    int    sport2a_buf[2][NUM_SAMPLES*2];    
    int    sport4a_buf[2][NUM_SAMPLES*2];    
    int    sport5a_buf[2][NUM_SAMPLES*2];
 
 
//int xt[1024*1024];
 
 
//2 For ping-pong ,each TCB has four registers.
    int    sport1a_tcb[2][4] = {
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport1a_buf[0]},
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport1a_buf[1]}
    };
    int    sport1b_tcb[2][4] = {
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport1b_buf[0]},
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport1b_buf[1]}
    };
    
    ////TDM
    int    sport0a_tcb[2][4] = {
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport0a_buf[0]},
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport0a_buf[1]}
    };
    int    sport0b_tcb[2][4] = {
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport0b_buf[0]},
        {0, NUM_SAMPLES*SLOTS, 1, (int) sport0b_buf[1]}
    };
    //usb
    int    sport2a_tcb[2][4] = {
        {0, NUM_SAMPLES*2, 1, (int) sport2a_buf[0]},
        {0, NUM_SAMPLES*2, 1, (int) sport2a_buf[1]}
    };
    int    sport4a_tcb[2][4] = {
        {0, NUM_SAMPLES*2, 1, (int) sport4a_buf[0]},
        {0, NUM_SAMPLES*2, 1, (int) sport4a_buf[1]}
    };
    int    sport5a_tcb[2][4] = {
        {0, NUM_SAMPLES*2, 1, (int) sport5a_buf[0]},
        {0, NUM_SAMPLES*2, 1, (int) sport5a_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 fs_div = MCLK/RATE_SAMPLES;
    int sclk_div = MCLK/(RATE_SAMPLES*32*SLOTS);
    int sclk_div_48k = MCLK/(RATE_SAMPLES*32*2);
    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);
 
    sport4a_tcb[0][0]= ((int)sport4a_tcb[1] + 3)&0x7ffff | (1<<19);
    sport4a_tcb[1][0]= ((int)sport4a_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);
    
 
    *pSPCTL0 = *pSPCTL1 = *pSPCTL2=*pSPCTL4=*pSPCTL5=0;
    *pSPMCTL0 = *pSPMCTL1=*pSPMCTL2=*pSPMCTL4=*pSPMCTL5=0; 
    *pSPCTLN0 =*pSPCTLN1=*pSPCTLN2=*pSPCTLN4=*pSPCTLN5=0;
    
    *pCPSP0A = sport0a_tcb[0][0];
    *pCPSP0B = sport0b_tcb[0][0];
    
    *pCPSP1A = sport1a_tcb[0][0];
    *pCPSP1B = sport1b_tcb[0][0];            
    
    //usb
    *pCPSP2A = sport2a_tcb[0][0];
    *pCPSP4A = sport4a_tcb[0][0];
    *pCPSP5A = sport5a_tcb[0][0];
    
    
    //*pSPCTL2 = (OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A |LFS |CKRE); 
    *pSPCTL5 = (OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A |LFS |CKRE);
    *pSPCTL4 = (SPTRAN| OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A |LFS |CKRE);//output
    
    //iis
    //*pSPCTL1 = (SPTRAN| OPMODE | SLEN32 | SPEN_A | SCHEN_A | SDEN_A); //usb send
    reg = ( SLEN32 | SCHEN_A | SDEN_A | SCHEN_B | SDEN_B |FSR |CKRE); 
    *pSPCTL1 = reg; 
 
    //tdm
    reg = (SPTRAN| SLEN32 | SCHEN_A | SDEN_A | SCHEN_B | SDEN_B |FSR |CKRE );
    *pSPCTL0 = reg;
    
    
    
    *pSP0CS0 = *pSP1CS0 = 0x000000ff;
    //*pMT0CCS0 = 0; 
    *pSPMCTL0 = *pSPMCTL1 = (NCH7 | MFD1 |MCEA |MCEB);
    
    //tdm
    if(1)
    {    
        int n,r0 ;
       
        *pPCG_CTLA1 =0 ; *pPCG_CTLA0 =0;
           for (n=0; n<16; n++)asm("NOP;");
       
           
          r0 = sclk_div | CLKASOURCE | FSASOURCE;
          *pPCG_CTLA1 = r0 ;
     
          r0 =  fs_div | ENFSA | ENCLKA ;
          *pPCG_CTLA0 = r0;
          
          
          //bypass mode PCGB , get invert sclk
          *pPCG_CTLB1 =0 ; *pPCG_CTLB0 =0;
           for (n=0; n<16; n++)asm("NOP;");
       
           *pPCG_PW1 = 2| INVFSB ;
           
          r0 = sclk_div | CLKBSOURCE | FSBSOURCE;
          *pPCG_CTLB1 = r0 ;
     
          r0 =   ENFSB | ENCLKB ;
          *pPCG_CTLB0 = r0;
    }
    
    //iis
    if(1)
    {    
        int n,r0 ;
       
        *pPCG_CTLC1 =0 ; *pPCG_CTLC0 =0;
           for (n=0; n<16; n++)asm("NOP;");
       
          //*pPCG_PW1 =1;
          //*pPCG_PW2 = STROBEA| INVFSA;
           
          r0 =  sclk_div_48k| CLKCSOURCE | FSCSOURCE | ((sclk_div_48k/2)<<20);
          *pPCG_CTLC1 = r0 ;
     
          r0 =  fs_div | ENFSC | ENCLKC ;
          *pPCG_CTLC0 = r0;
          
    }
    
    
}