source: drsdaq/VME/struck/sis1100/V2.02/test/block_rw.c@ 23

Last change on this file since 23 was 22, checked in by ogrimm, 16 years ago
First commit of drsdaq program
File size: 6.5 KB
Line 
1#define _GNU_SOURCE
2#define _LARGEFILE_SOURCE
3#define _LARGEFILE64_SOURCE
4#define _FILE_OFFSET_BITS 64
5#include <stdio.h>
6#include <errno.h>
7#include <string.h>
8#include <sys/types.h>
9#include <time.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <fcntl.h>
13#include <sys/ioctl.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
16#include <arpa/inet.h>
17#include <netdb.h>
18
19#include "dev/pci/sis1100_var.h"
20
21#define VMESTART 0x84000000
22
23#if SIS3100_Version < 200
24enum sis1100_subdev {sis1100_subdev_vme, sis1100_subdev_ram,
25 sis1100_subdev_dsp};
26#endif
27
28off_t devstart;
29int count;
30int width;
31int loops;
32int addr;
33int am;
34int use_dma;
35int read_type;
36int print;
37char* dev_path;
38int p;
39
40static void
41printusage(int argc, char* argv[])
42{
43 printf("usage: %s\n"
44 " [-c count (number of words with width w); must be>0]\n"
45 " [-w width default: 4]\n"
46 " [-l loops default: 1]\n"
47 " [-a addr default: 0]\n"
48 " [-m addr_modifier default: 0xB]\n"
49 " [-d 1|0 (use dma); default: 0]\n"
50 " [-r 1|2|3 (1=read 2=write 3=both); default: write;read;compare]\n"
51 " [-p (print values)]\n"
52 " sis1100_path\n",
53 argv[0]);
54}
55
56static int
57getoptions(int argc, char* argv[])
58{
59 extern char *optarg;
60 extern int optind;
61 extern int opterr;
62 extern int optopt;
63 int errflag, c;
64 const char* args="c:w:l:a:m:d:r:p";
65
66 count=0;
67 width=4;
68 loops=1;
69 addr=0;
70 am=0xb;
71 use_dma=0;
72 read_type=3;
73 print=0;
74
75 optarg=0; errflag=0;
76
77 while (!errflag && ((c=getopt(argc, argv, args))!=-1)) {
78 switch (c) {
79 case 'c': count=strtoul(optarg, 0, 0); break;
80 case 'w': width=atoi(optarg); break;
81 case 'l': loops=atoi(optarg); break;
82 case 'a': addr=strtoul(optarg, 0, 0); break;
83 case 'm': am=strtoul(optarg, 0, 0); break;
84 case 'd': use_dma=atoi(optarg); break;
85 case 'r': read_type=atoi(optarg); break;
86 case 'p': print=1; break;
87 default: errflag=1;
88 }
89 }
90
91 if (errflag || optind!=argc-1 || count<=0) {
92 printusage(argc, argv);
93 return -1;
94 }
95 dev_path=argv[optind];
96
97 return 0;
98}
99
100static int
101do_write(int p, char* buf, int count, int width, int addr, int loop)
102{
103 int start=random();
104 int num=count*width;
105 int res, i;
106
107 for (i=0; i<num; i++) {
108 buf[i]=start++;
109 }
110 if (!loop) printf("write %d bytes\n", num);
111 if (lseek(p, devstart+addr, SEEK_SET)==((off_t)-1)) {
112 printf("lseek to %d: %s\n", addr, strerror(errno));
113 return -1;
114 }
115 res=write(p, buf, num);
116 if (res<0) {
117 printf("write %d bytes to 0x%08x: %s\n", num, addr, strerror(errno));
118 return -1;
119 } else if (res!=num) {
120 printf("wrote only %d bytes\n", res);
121 return -1;
122 }
123 return 0;
124}
125
126static int
127do_read(int p, char* buf, int count, int width, int addr, int loop)
128{
129 int num=count*width;
130 int res;
131
132 if (!loop) printf("read %d bytes\n", num);
133 if (lseek(p, devstart+addr, SEEK_SET)==((off_t)-1)) {
134 printf("lseek to 0x%08x: %s\n", addr, strerror(errno));
135 return -1;
136 }
137 res=read(p, buf, num);
138 if (res<0) {
139 printf("read %d bytes from 0x%08x: %s\n", num, addr, strerror(errno));
140 return -1;
141 } else if (res!=num) {
142 printf("read only %d bytes\n", res);
143 return -1;
144 }
145 return 0;
146}
147
148static int
149do_compare(char* ibuf, char* obuf, int count, int width, int loop)
150{
151 int num=count*width;
152 int i;
153
154 if (bcmp(ibuf, obuf, num)) {
155 for (i=0; i<num; i++) {
156 if (ibuf[i]!=obuf[i]) {
157 printf("[%d] %x->%x\n", i, obuf[i], ibuf[i]);
158 }
159 }
160 return -1;
161 }
162 return 0;
163}
164
165int main(int argc, char* argv[])
166{
167 int l;
168 u_int32_t max=0;
169 char *ibuf, *obuf;
170 struct vmespace space;
171 enum sis1100_subdev devtype;
172
173 if (getoptions(argc, argv)) return 1;
174
175 if ((p=open(dev_path, O_RDWR, 0))<0) {
176 printf("open \"%s\": %s\n", dev_path, strerror(errno));
177 return 1;
178 }
179
180 if (ioctl(p, SIS1100_DEVTYPE, &devtype)<0) {
181 printf("ioctl(SIS1100_DEVTYPE): %s\n", strerror(errno));
182 return 1;
183 }
184 switch (devtype) {
185 case sis1100_subdev_vme: printf("using VME Device\n"); break;
186 case sis1100_subdev_ram: printf("using RAM Device\n"); break;
187 case sis1100_subdev_ctrl: printf("cannot use CONTROL Device\n"); return 1;
188 case sis1100_subdev_dsp: printf("cannot use DSP Device\n"); return 1;
189 default:
190 printf("cannot use unknown device %d\n", devtype);
191 return 1;
192 }
193 switch (devtype) {
194 case sis1100_subdev_vme:
195 max=0x04000000;
196 devstart=VMESTART;
197 break;
198 case sis1100_subdev_ram:
199 {
200 if (ioctl(p, SIS1100_MAPSIZE, &max)) {
201 printf("ioctl(MAPSIZE): %s\n", strerror(errno));
202 return 1;
203 }
204 devstart=0;
205 }
206 break;
207 }
208 printf("usable size is 0x%08x (%d MByte)\n", max, max/(1<<20));
209 if (count*width>max) {
210 printf("count is too large.\n");
211 return 1;
212 }
213 ibuf=obuf=0;
214 if (read_type&1) {
215 ibuf=calloc(count, width);
216 if (!ibuf) {
217 printf("cannot allocate memory for read buffer\n");
218 return 1;
219 }
220 }
221
222 if (read_type&2) {
223 obuf=calloc(count, width);
224 if (!obuf) {
225 printf("cannot allocate memory for write buffer\n");
226 return 1;
227 }
228 }
229
230 space.am=am;
231 space.datasize=width;
232 space.swap=1;
233 space.mapit=0;
234 space.mindmalen=!!use_dma;
235 if (ioctl(p, SETVMESPACE, &space)) {
236 printf("ioctl(SETVMESPACE): %s\n", strerror(errno));
237 return 1;
238 }
239
240 for (l=0; l<loops; l++) {
241 /*if (loops>1) printf("loop %d\n", l);*/
242 if (read_type&2) {
243 if (do_write(p, obuf, count, width, addr, l)) return 2;
244 }
245
246 if (read_type&1) {
247 if (do_read(p, ibuf, count, width, addr, l)) return 2;
248 }
249
250 if ((read_type&3)==3) {
251 if (do_compare(ibuf, obuf, count, width, l)) return 2;
252 }
253 }
254
255 if (print) {
256 int i;
257 switch (width) {
258 case 1:
259 for (i=0; i<count; i++) printf("%02x ", ((u_int8_t*)ibuf)[i]);
260 break;
261 case 2:
262 for (i=0; i<count; i++) printf("%04x ", ((u_int16_t*)ibuf)[i]);
263 break;
264 case 4:
265 for (i=0; i<count; i++) printf("%08x ", ((u_int32_t*)ibuf)[i]);
266 break;
267 }
268 printf("\n");
269 }
270
271 if (obuf) free(obuf);
272 if (ibuf) free(ibuf);
273 close(p);
274 return 0;
275}
Note: See TracBrowser for help on using the repository browser.