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
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
#include <def21489.h>
#include <Cdef21489.h>
#include <math.h>
#include <filter.h>
#include <drv/fft_drv.h>
#include "fft.h"
 
 
#define pi 3.1415926
/* Declaring the external buffers needed for FFT Accelerator*/
//extern float FFT_IP_buff[]; //ÊäÈ뻺³å
//extern float FFT_OP_buff[]; //Êä³ö»º³å
 
static float FFT_VCF_buff[2*V];
static float FFT_HCF_buff[2*H];
static float FFT_IM_OP_buff[N*2];
static float FFT_SP_CF_buff[N*4];
 
/*CPIFFT,IBFFT,ILFFT,ICFFT,IMFFT,IIFFt*/
/*
CPIFFT: Í¨µÀÖ¸Õë¼Ä´æÆ÷,±£´æÏÂÒ»¸ö´«Êä¿ØÖÆ¿éµÄ¿ªÊ¼Î»Öá£
IBFFT: »ùÖ·¼Ä´æÆ÷£¬Ñ­»·»º³å¿ªÊ¼µØÖ·
ILFFT: ³¤¶È¼Ä´æÆ÷£¬Ñ­»·»º³å³¤¶È
ICFFT: ¼ÆÊý¼Ä´æÆ÷£¬±êʾ»¹Ê£Óà¶àÉÙ×ÖÐèÒª´«Êä
IMFFT: ¿ÉÐ޸ļĴæÆ÷£¬DMA¶ÁдºóIIFFT±ä»¯µÄÓзûºÅÔöÁ¿
IIFFT: Ë÷Òý¼Ä´æÆ÷£¬Ö¸ÏòÏÂÒ»¸öÄÚ²¿ÄÚ´æ¶ÁдλÖá£
*/
 
 
/*Coefficient TCB for loading V cofficients */
 
 
static int FFT_VCF_TCB[6]={0,(int)FFT_VCF_buff,2*V,2*V,1,(int)FFT_VCF_buff};// Vertical coeff TCB
static int FFT_VIP_TCB[6]={0,(int)0,2*N-1,2*N-1,2*H,(int)0};// input TCB modify is 2H
    
static int FFT_VLASTIP_TCB[6]={0,(int)0,2*N,1,1,(int)0+2*N-1};// input TCB chain
    
static int FFT_IMOP_TCB1[6]={0,(int)FFT_IM_OP_buff,2*N,2*N,1,(int)FFT_IM_OP_buff};// Vertical FFT receive buffer.
            
static int FFT_PWCF_TCB[N/128][6]={{0,(int)FFT_SP_CF_buff,4*N,512,1,(int)FFT_SP_CF_buff+0}};
static int FFT_IMIP_TCB[N/128][6]={{0,(int)FFT_IM_OP_buff,2*N,256,1,(int)FFT_IM_OP_buff+0}};
static int FFT_IMOP_TCB[N/128][6]={{0,(int)FFT_IM_OP_buff,2*N,256,1,(int)FFT_IM_OP_buff+0}};
 
    
static int FFT_HCF_TCB[6]={0,(int)FFT_HCF_buff,2*H,2*H,1,(int)FFT_HCF_buff}; //  Horizontal coeff TCB
static int FFT_HIP_TCB[6]={0,(int)FFT_IM_OP_buff,2*N-1,2*N-1,2*V,(int)FFT_IM_OP_buff};// Special product transmit buffer. modify is 2V
static int FFT_HLASTIP_TCB[6]={0,(int)FFT_IM_OP_buff,2*N,1,1,(int)FFT_IM_OP_buff+2*N-1};// Special product transmit buffer.
static int FFT_OP_TCB[6]={0,(int)0,2*N-1,2*N-1,2*V,(int)0};//modify is 2V
static int FFT_LASTOP_TCB[6]={0,(int)0,2*N,1,1,(int)0+2*N-1};
 
    
//sp_cf³¤¶ÈΪV*H*4
void fft_coeff_calculation(void)
{
    int i,j,k;
    float temp[128];
    float w;
    //e^-2pikm 
    for(i=0,k=0;i<H;i++){
        for(j=0;j<V;j++){
            w=-2*pi/N*i*j;
            FFT_SP_CF_buff[k++]=cosf(w);
            FFT_SP_CF_buff[k++]=sinf(w);
            FFT_SP_CF_buff[k++]=-sinf(w);
            FFT_SP_CF_buff[k++]=cosf(w);
        }
    }
    
    twidfftf(temp,temp+V/2,V);        
    //½»´íÅÅÁÐϵÊý,´ò°üģʽ
    for(i=0,j=V/2,k=0;i<V/2;i++){
        FFT_VCF_buff[k++]=temp[i];
        FFT_VCF_buff[k++]=temp[j+i];
        FFT_VCF_buff[k++]=-temp[j+i];
        FFT_VCF_buff[k++]=temp[i];
    }
    
    twidfftf(temp,temp+H/2,H);
    for(i=0,j=H/2,k=0;i<H/2;i++){
        FFT_HCF_buff[k++]=temp[i];
        FFT_HCF_buff[k++]=temp[j+i];
        FFT_HCF_buff[k++]=-temp[j+i];
        FFT_HCF_buff[k++]=temp[i];
    }    
}
 
    
void fft_config(bool repeat,int _sig)
{
    int temp,i;
    int index = _sig*5/30;
    int offset = (_sig-index*6)*5 ;
        
    if(_sig>=0){
    //Mapping the FFT DMA interrupt        
        temp=* (pPICR0+index);
        temp&=~(0x1f<<offset);
        temp|= (0x1b<<offset);
        *(pPICR0+index)=temp;
    }
 
    
    *pPMCTL1&=~(BIT_17|BIT_18);
    *pPMCTL1|=FFTACCSEL;
    //PMCTL1 effect latency
    asm("nop;nop;nop;nop;");
 
    //Clearing the FFTCTL registers
    *pFFTCTL2=0;
    *pFFTCTL1=FFT_RST;
    asm("nop;nop;nop;nop;");
    
 
    for(i=1;i<N/128;i++){
        FFT_PWCF_TCB[i][1]=FFT_PWCF_TCB[0][1];
        FFT_PWCF_TCB[i][2]=FFT_PWCF_TCB[0][2];
        FFT_PWCF_TCB[i][3]=FFT_PWCF_TCB[0][3];
        FFT_PWCF_TCB[i][4]=FFT_PWCF_TCB[0][4];
        FFT_PWCF_TCB[i][5] = (int)FFT_SP_CF_buff+i*512;
        
        FFT_IMIP_TCB[i][1]=FFT_IMIP_TCB[0][1];
        FFT_IMIP_TCB[i][2]=FFT_IMIP_TCB[0][2];
        FFT_IMIP_TCB[i][3]=FFT_IMIP_TCB[0][3];
        FFT_IMIP_TCB[i][4]=FFT_IMIP_TCB[0][4];
        FFT_IMIP_TCB[i][5] = (int)FFT_IM_OP_buff+i*256;
        
        FFT_IMOP_TCB[i][1]=FFT_IMOP_TCB[0][1];
        FFT_IMOP_TCB[i][2]=FFT_IMOP_TCB[0][2];
        FFT_IMOP_TCB[i][3]=FFT_IMOP_TCB[0][3];
        FFT_IMOP_TCB[i][4]=FFT_IMOP_TCB[0][4];
        FFT_IMOP_TCB[i][5] = (int)FFT_IM_OP_buff+i*256;
    }
    
        
    
    FFT_VCF_TCB[0]=(int)FFT_VIP_TCB+5-0x80000;
    FFT_VIP_TCB[0]=(int)FFT_VLASTIP_TCB+5-0x80000;
    temp=(int)FFT_PWCF_TCB[0];
    FFT_VLASTIP_TCB[0]=0x100000|temp+5-0x80000;
    FFT_IMOP_TCB1[0]=(int)FFT_IMOP_TCB[0]+5-0x80000;
    
    
    for(i=0;i<N/128-1;i++){
        FFT_PWCF_TCB[i][0]=(int)FFT_IMIP_TCB[i]+5-0x80000;
        temp=(int)FFT_PWCF_TCB[i+1];
        FFT_IMIP_TCB[i][0]=0x100000|temp+5-0x80000;
        FFT_IMOP_TCB[i][0]=(int)FFT_IMOP_TCB[i+1]+5-0x80000;
    }
    
    FFT_PWCF_TCB[i][0]=(int)FFT_IMIP_TCB[i]+5-0x80000;
    temp=(int)FFT_HCF_TCB;
    FFT_IMIP_TCB[i][0]=0x100000|temp+5-0x80000;
    FFT_IMOP_TCB[i][0]=(int)FFT_OP_TCB+5-0x80000;
    
    
 
    FFT_HCF_TCB[0]=(int)FFT_HIP_TCB+5-0x80000;
    FFT_HIP_TCB[0]=(int)FFT_HLASTIP_TCB+5-0x80000;
    FFT_OP_TCB[0]=(int)FFT_LASTOP_TCB+5-0x80000;
    
    
    //FFT_LASTOP_TCB[0] = (int)FFT_VCF_TCB+5-0x80000;
    
//    temp=(int)FFT_VCF_TCB;
    //*pCPIFFT=0x100000|temp+5-0x80000;
//    *pCPOFFT=(int)FFT_IMOP_TCB1+5-0x80000;
    
    //FFT_LASTOP_TCB[0]=0x100000|temp+5-0x80000;
 
    *pFFTCTL2 = FFT_CPACKIN|FFT_CPACKOUT;
    
    temp = log(V/8)/log(2);
    *pFFTCTL2|= (1<<(temp+6));
    temp = log(V)/log(2);
    *pFFTCTL2|= (temp<<3);
    temp = log(H/8)/log(2);
    *pFFTCTL2|= (1<<(temp+15));
    temp = log(H)/log(2);
    *pFFTCTL2|= (temp<<12);
    temp = log(N/256)/log(2);
    *pFFTCTL2|= 1<<(temp+21);
    
    if(repeat){
        *pFFTCTL2 |= FFT_RPT;
    }
 
    *pFFTCTL1=FFT_EN|FFT_DMAEN|FFT_START;
}
 
 
void fft_startup(int input_address,int out_address)
{
 
    int temp=(int)FFT_VCF_TCB;
        
    FFT_VIP_TCB[1]=FFT_VIP_TCB[5]=input_address;
    FFT_VLASTIP_TCB[1]=input_address;
    FFT_VLASTIP_TCB[5]=input_address+2*N-1;
 
    FFT_OP_TCB[1]=FFT_OP_TCB[5]=out_address;
    FFT_LASTOP_TCB[1]=out_address;
    FFT_LASTOP_TCB[5]=out_address+2*N-1;
        
 
    *pCPIFFT=0x100000|temp+5-0x80000;
    *pCPOFFT=(int)FFT_IMOP_TCB1+5-0x80000;
    
}