/
processimage.c
189 lines (148 loc) · 4.56 KB
/
processimage.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
/*
* This test application is to read/write data directly from/to the device
* from userspace.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <assert.h>
#include <stdbool.h>
void usage(void) {
printf("*argv[0] -g <GPIO_ADDRESS> -i|-o <VALUE>\n");
printf(" -g <GPIO_ADDR> GPIO physical address\n");
printf(" -i Input from GPIO\n");
printf(" -o <VALUE> Output to GPIO\n");
return;
}
typedef struct {
int cmd;
int src;
int dest;
unsigned int len;
} Conf;
FILE* openImage(char* filename, int* numbytes){
FILE* infile = fopen(filename, "rb");
if(infile==NULL){
printf("File not found %s\n",filename);
exit(1);
}
fseek(infile, 0L, SEEK_END);
*numbytes = ftell(infile);
fseek(infile, 0L, SEEK_SET);
return infile;
}
void loadImage(FILE* infile, volatile void* address, int numbytes){
int outlen = fread(address, sizeof(char), numbytes, infile);
if(outlen!=numbytes){
printf("ERROR READING\n");
}
fclose(infile);
}
bool isPowerOf2(unsigned int x) {
return x && !(x & (x - 1));
}
// we don't have a standard library... so reimplement this badly
unsigned int mylog2(unsigned int x){
printf("mylog2\n");
unsigned int res = 0;
// find highest true bit
while(x=x>>1){printf("H%d\n",x);res++;}
return res;
}
int saveImage(char* filename, volatile void* address, int numbytes){
FILE* outfile = fopen(filename, "wb");
if(outfile==NULL){
printf("could not open for writing %s\n",filename);
exit(1);
}
int outlen = fwrite(address,1,numbytes,outfile);
if(outlen!=numbytes){
printf("ERROR WRITING\n");
}
fclose(outfile);
}
int main(int argc, char *argv[]) {
unsigned gpio_addr = 0x70000000;
unsigned copy_addr = atoi(argv[1]);
if(argc!=10){
printf("ERROR< insufficient args. Should be: addr inputFilename outputFilename inputW inputH inputBitsPerPixel outputW outH outputBitsPerPixel\n");
exit(1);
}
unsigned int inputW = atoi(argv[4]);
unsigned int inputH = atoi(argv[5]);
unsigned int inputBitsPerPixel = atoi(argv[6]);
unsigned int outputW = atoi(argv[7]);
unsigned int outputH = atoi(argv[8]);
unsigned int outputBitsPerPixel = atoi(argv[9]);
if(outputBitsPerPixel%8!=0){
printf("Error, NYI - non-byte aligned output bits per pixel not supported!\n");
exit(1);
}
unsigned int outputBytesPerPixel = outputBitsPerPixel/8;
unsigned page_size = sysconf(_SC_PAGESIZE);
printf("GPIO access through /dev/mem. %d\n", page_size);
if (gpio_addr == 0) {
printf("GPIO physical address is required.\n");
usage();
return -1;
}
int fd = open ("/dev/mem", O_RDWR | O_SYNC );
if (fd < 1) {
perror(argv[0]);
return -1;
}
unsigned lenInRaw;
FILE* imfile = openImage(argv[2], &lenInRaw);
printf("file LEN %d\n",lenInRaw);
unsigned int lenIn = lenInRaw;
unsigned int lenOutRaw = outputW*outputH*outputBytesPerPixel;
unsigned int lenOut = lenOutRaw;
// HW pads to next largest axi burst size (128 bytes)
if (lenIn%(8*16)!=0){
lenIn = lenInRaw + (8*16-(lenInRaw % (8*16)));
}
if (lenOutRaw%(8*16)!=0){
lenOut = lenOutRaw + (8*16-(lenOutRaw % (8*16)));
}
// extra axi burst of metadata
lenOut = lenOut + 128;
printf("LENOUT %d, LENOUTRAW:%d\n", lenOut,lenOutRaw);
assert(lenIn % (8*16) == 0);
printf("LENIN %d\n",lenIn);
assert(lenOut % (8*16) == 0);
printf("mapping %08x\n",copy_addr);
void * ptr = mmap(NULL, lenIn+lenOut, PROT_READ|PROT_WRITE, MAP_SHARED, fd, copy_addr);
if(ptr==(void *) -1){
printf("Error mmaping\n");
exit(1);
}
loadImage( imfile, ptr, lenInRaw );
// zero out the output region
for(int i=0; i<lenOut; i++){ *(unsigned char*)(ptr+lenIn+i)=0; }
// mmap the device into memory
// This mmaps the control region (the MMIO for the control registers).
// Image data is located at addr 'copy_addr'
void * gpioptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, gpio_addr);
if(gpioptr==(void *) -1){
printf("Error mmaping gpio\n");
exit(1);
}
// this sleep is needed for the z100, but not the z20
sleep(2);
volatile Conf * conf = (Conf*) gpioptr;
conf->src = copy_addr;
conf->dest = copy_addr + lenIn;
conf->len = lenIn;
// HACK: poking cmd causes the pipeline to start. sleep for 2sec to make sure the above registers set before starting.
sleep(2);
conf->cmd = 3;
//usleep(10000);
sleep(2); // this sleep needs to be 2 for the z100, but 1 for the z20
saveImage(argv[3],ptr+lenIn,lenOutRaw);
munmap( gpioptr, page_size );
munmap( ptr, lenIn+lenOut );
return 0;
}