-
Notifications
You must be signed in to change notification settings - Fork 2
/
dmafft.c
128 lines (85 loc) · 2.61 KB
/
dmafft.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
#include "pciedma.h"
#include "fftcontrol.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define NSAMPLES 1024
#define PI 3.14159265
#define MASK16 0xFFFF
unsigned int* create_signal()
{
int i;
double theta, theta2, re_real, im_real;
unsigned int re_int, im_int;
unsigned int* data_samples;
void *data;
data_samples = (unsigned int*)malloc(sizeof(unsigned int)*NSAMPLES);
if(data_samples == NULL){
printf("Error: Failed to allocate data samples buffer\n");
return NULL;
}
posix_memalign( (void**)&data, 16, sizeof(unsigned int)*NSAMPLES );
for(i=0; i < NSAMPLES; i++)
{
theta = ((double)i/(double)NSAMPLES)*2.6*2.0 * PI;
re_real = cos(-theta);
im_real = sin(-theta);
theta2 = ((double)i/(double)NSAMPLES)*23.2*2.0 * PI;
re_real = re_real + (cos(-theta2) / 4.0);
im_real = im_real + (sin(-theta2) / 4.0);
re_int = (unsigned int)nearbyint(re_real * pow(2,14));
im_int = (unsigned int)nearbyint(im_real * pow(2,14));
im_int = im_int & MASK16;
im_int = im_int << 16;
((unsigned int*)data)[i] = 0;
((unsigned int*)data)[i] = re_int;
((unsigned int*)data)[i] = ((unsigned int*)data)[i] & MASK16 | (im_int);
}
return data;
}
int main()
{
unsigned int *data_samples;
int i;
pd_device_t dev;
void *memr;
printf("FFT computation demonstration\n");
if(pd_open(0,&dev) != 0){
printf("Failed to open file \n");
return -1;
}
// Create block of 1kB memory
posix_memalign( (void**)&memr, 16, sizeof(unsigned int)*NSAMPLES);
data_samples = create_signal();
// for(i=0;i<64;i++)
// printf("%d: %08x\n",i,data_samples[i]);
if(initDMA(&dev) < 0)
printf("Init DMA failed\n");
FFTreset();
//configFFT(10,0x2AA,0);
// if(setFFTfwdinv(1) < 0)
// printf("Could not set FFT direction\n");
// if(setFFTscaling(0x2AA) < 0)
// printf("Could not set FFT scaling\n");
// if(setFFTsize(10) < 0)
// printf("Could not set FFT size\n");
// printf("FFT Status: %08x\n",readFFTstatus());
if(setupSend(&dev, (void*)data_samples, sizeof(unsigned int)*NSAMPLES,3) < 0)
printf("Setup Send failed\n");
if(startSend(&dev, 0) < 0)
printf("DMA Send failed\n");
if(checkSend() < 0)
printf("Check DMA send failed\n");
freeSend(&dev);
// Receive result
setupRecv(&dev, memr, sizeof(unsigned int)*NSAMPLES);
startRecv(&dev, 1);
checkRecv();
freeRecv(&dev);
stopDMA(&dev);
for(i=0;i<64;i++)
// printf("%d: y_re = %d, y_im = %d\n",i,(short)(((int*)memr)[i] & MASK16), (short)(((int*)memr)[i] >> 16));
printf("%d: %08x\n",i,((unsigned int*)memr)[i]);
return 0;
}