vmod/vmodttl: fixed bug related to luns not ordered and/or not starting from zero.
[ht-drivers.git] / icv196 / test / icv196vmeTest.c
blob5e417111dfc5b9d66f0c4bbcde7a3fff76424902
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/ioctl.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <time.h>
10 #include <sys/file.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <skeluser_ioctl.h>
14 #include <icv196vmelib.h>
15 #include <general_both.h>
16 #include <gp.h>
18 /* #include "icv196vmeP.h"*/
19 /* #include "/u/dscps/rtfclty/gpsynchrolib.h" */
20 //#define addr = 0x00500000
21 //#define Evtsrce_icv196 2
23 #define ICV_nln 16
24 #define ICV_nboards 8 /* MAX allowed modules */
26 extern int errno;
27 static int service_fd = 0;
28 static int synchro_fd = 0;
30 #ifdef __Lynx__
31 extern int snprintf(char *str, size_t size, const char *format, ...);
32 #endif
34 static int module = 0;
35 static int old_module = 0;
37 static char choice[10];
39 static void transmit_data(int fd, struct gpfd *gpfd);
41 void get_input(char *ch, int *inp)
43 char *p;
45 printf(" +--------------------------------------------------------------+\n");
46 printf(" | 0: exit |\n");
47 printf(" | 1: test |\n");
48 printf(" | 2: change the ICV board being accessed (current board: %d) |\n", module);
49 printf(" | 3: read interrupt enable mask |\n");
50 printf(" | 4: read module info (VME address, interr. vect. etc.) |\n");
51 printf(" | 5: read handle info (lines connected to by a user) |\n");
52 printf(" | 6: read group |\n");
53 printf(" | 7: write group |\n");
54 printf(" | 8: set I/O direction (does not concern interrupts) |\n");
55 printf(" | 9: read I/O direction (does not concern interrupts) |\n");
56 printf(" | 10: read all interrupt counters |\n");
57 printf(" | 11: read all reenable flags |\n");
58 printf(" | 12/13: set/clear reenable flag |\n");
59 printf(" | 14/15: enable/disable line |\n");
60 printf(" | 16: read an event (enable interrupt line first) |\n");
61 printf(" | 17/18: configure blocking (default)/nonblocking read |\n");
62 printf(" | |\n");
63 printf(" | Nb: functions 12-18 automatically connect to interrupt-line |\n");
64 printf(" +--------------------------------------------------------------+\n\n");
65 printf("--> ");
66 scanf("%s", ch);
67 getchar(); /* eat up \n */
69 for (p = ch; *p != '\0'; p++)
70 if (!isdigit((int)*p)) {
71 *inp = -1;
72 return;
75 *inp = atoi(ch);
76 if ((*inp < 0) || (*inp > 16))
77 printf("input not correct\n");
80 int main(int argc, char *argv[], char *envp[])
82 int retval, i, j, dir, stat, grp_nr, grp_mask = 1;
83 char buff[50] = { 0 } , line[10];
84 int status[icv_ModuleNb][icv_LineNb] = { { 0 } };
85 int input, count, size;
86 char board[2];
87 long *evt;
88 struct icv196T_Service arg;
89 struct icv196T_UserLine arg2;
90 struct icv196T_ModuleInfo arg3[icv_ModuleNb];
91 struct icv196T_HandleInfo arg4;
92 struct icv196T_ModuleInfo *Info_P;
93 struct icv196T_HandleLines *Info_H;
95 service_fd = icv196_get_handle();
96 if (service_fd < 0) {
97 fprintf(stderr, "Can't get library handle...\n");
98 exit(EXIT_FAILURE);
101 synchro_fd = service_fd; /* TODO */
103 do {
104 get_input(choice, &input);
106 switch (input) {
107 case 0:
108 break;
109 case 1:
111 struct gpfd *gpfd = open_gpf(NULL);
112 if (!gpfd) {
113 printf("\n\n<enter> to continue"); getchar();
114 break;
117 if (!load_gpf(gpfd)) {
118 printf("No data loaded from gnuplot file\n");
119 printf("\n\n<enter> to continue"); getchar();
120 break;
123 if (!do_conv_gp_file(gpfd, 1))
124 printf("Failed to create converted file\n");
125 else
126 printf("Converted file ready\n");
128 transmit_data(service_fd, gpfd);
129 printf("\n\n<enter> to continue"); getchar();
130 break;
132 case 2:
133 old_module = module;
134 printf("number of module to be accessed ?\n" );
135 scanf("%s", board);
136 module = atoi(board);
137 if ((module < 0) || (module >= ICV_nboards)) {
138 printf("board number out of range\n");
139 module = old_module;
140 break;
142 break;
143 case 3:
144 arg.module = (unsigned char) module;
145 if ((retval = ioctl(service_fd, ICVVME_intenmask, &arg)) < 0) {
146 perror("could not do ioctl\n");
147 break;
149 stat = (unsigned short)arg.data[0];
150 printf("STATUS OF INTERRUPT LINES ON BOARD NR. %d: ( 0 = disabled, 1 = enabled )\n", module);
151 for (i = 0; i <= 15; i++) {
152 printf("line %2d: %2d ", i, stat & grp_mask);
153 stat >>= 1;
154 if (((i+1) % 4) == 0 && i != 0)
155 printf("\n");
157 printf("\n");
158 break;
159 case 4:
160 Info_P = arg3;
161 if ((retval = ioctl(service_fd, ICVVME_getmoduleinfo, Info_P)) < 0) {
162 perror("could not do ioctl\n");
163 break;
166 for (i = 0; i < icv_ModuleNb; i++, Info_P++) {
167 printf("MODULE INFO FOR MODULE NR. %d:\n", i);
168 if (!Info_P->ModuleFlag) {
169 printf("module not installed\n\n");
170 continue;
173 printf("module base address: 0x%lx\n", Info_P->ModuleInfo.base);
174 printf("module address space size in bytes: %ld\n",
175 Info_P->ModuleInfo.size);
176 printf("module interrupt vectors:\n");
178 for (j = 0; j < icv_LineNb; j++) {
179 printf("line %2d:%d ", j, Info_P->ModuleInfo.vector);
181 if (((j+1) % 6) == 0 && j != 0)
182 printf("\n");
184 printf("\n");
186 printf("module interrupt levels:\n");
188 for (j = 0; j < icv_LineNb; j++) {
189 printf("line %2d:%d ", j, Info_P->ModuleInfo.level);
190 if (((j+1) % 6) == 0 && j != 0)
191 printf("\n");
193 printf("\n");
194 printf("\n");
196 break;
197 case 5:
198 Info_H = (struct icv196T_HandleLines *)&(arg4.handle[0]);
199 for (i = 0; i < SkelDrvrCLIENT_CONTEXTS; i++, Info_H++) {
200 Info_H -> pid = 0;
201 for (j = 0; j < ICV_LogLineNb; j++) {
202 Info_H -> lines[j].group = 0xff;
203 Info_H -> lines[j].index = 0xff;
207 if ((retval = ioctl(service_fd, ICVVME_gethandleinfo, &arg4)) < 0) {
208 perror("could not do ioctl\n");
209 break;
212 Info_H = (struct icv196T_HandleLines *)&(arg4.handle[0]);
213 Info_H++;
214 for (i = 1; i < SkelDrvrCLIENT_CONTEXTS; i++, Info_H++) {
215 printf("HANDLE INFO FOR HANDLE NR. %d: ", i);
216 if (!Info_H->pid) {
217 printf("handle not used\n\n");
218 continue;
220 printf("used by process pid: %d\n", Info_H -> pid);
221 printf("lines connected :");
223 for (j = 0; Info_H -> lines[j].group != 0xff; j++) {
224 printf("[modul %2d, line %2d] ", Info_H -> lines[j].group,
225 Info_H -> lines[j].index);
226 if (((j+1) % 3) == 0 && j != 0)
227 printf("\n");
229 printf("\n");
230 printf("\n");
232 break;
233 case 6: /* read from the group */
235 short *val = (short*)buff;
236 memset(buff, 0, sizeof(buff));
237 printf("Group index to read [0 - 11] --> ");
238 scanf("%d", &grp_nr); getchar();
240 if (grp_nr & 1) {
241 printf("Data size in bytes to read [1 - 2] --> ");
242 scanf("%d", &size); getchar();
243 } else
244 size = 1;
246 retval = icv196_read_channel(service_fd, module, grp_nr, size, buff);
247 if (retval < 0)
248 printf("Failed\n");
249 else
250 printf("0x%hx\n", *val);
252 printf("\n\n<enter> to continue"); getchar();
253 break;
255 case 7: /* write into the group */
256 memset(buff, 0, sizeof(buff));
257 printf("Group index to write [0 - 11] --> ");
258 scanf("%d", &grp_nr); getchar();
260 if (grp_nr&1) {
261 printf("Data size (in bytes) to write [1 - 2] --> ");
262 scanf("%d", &size); getchar();
263 } else
264 size = 1;
266 if (size == 1)
267 printf("Data to write in hex (byte) --> ");
268 else
269 printf("Data to write in hex (word) --> ");
271 scanf("%hx", (short*)buff); getchar();
273 if (icv196_write_channel(service_fd, module, grp_nr, size, buff))
274 printf("Failed\n");
275 else
276 printf("Done\n");
277 printf("\n\n<enter> to continue"); getchar();
279 break;
280 case 8:
281 printf("Group index to be set [0 - 11] -> ");
282 scanf("%d", &grp_nr); getchar();
284 printf("Direction [0 -- input, 1 -- output] -> ");
285 scanf("%d", &dir); getchar();
287 if (icv196_init_channel(service_fd, module, grp_nr, 1, dir))
288 printf("Failed\n");
289 else
290 printf("Done\n");
291 printf("\n\n<enter> to continue"); getchar();
292 break;
293 case 9:
294 memset(&arg, 0, sizeof(arg));
295 arg.module = module;
297 if (ioctl(service_fd, ICVVME_readio, &arg) < 0) {
298 perror("could not do ioctl\n");
299 break;
301 dir = arg.data[0];
302 printf("I/O groups direction on module#%d"
303 " (0:input, 1:output)\n", module);
304 for (i = 0; i <= 11; i++) {
305 printf("group[%2d] %2d\n", i, dir & 1);
306 dir >>= 1;
308 printf("\n\n<enter> to continue"); getchar();
309 break;
310 case 10:
311 arg.module = (unsigned char) module;
312 if ((retval = ioctl(service_fd, ICVVME_intcount, &arg)) < 0) {
313 printf("retval = %d \n ", retval);
314 perror("could not do ioctl\n");
315 break;
317 printf("INTERRUPT COUNTER VALUES ON BOARD NR. %d:\n", module);
318 for (i = 0; i < ICV_nln; i++) {
319 printf("line %2d: %3ld ", i, arg.data[i]);
320 if (((i+1) % 4) == 0 && i != 0)
321 printf("\n");
323 printf("\n");
324 break;
325 case 11:
326 arg.module = (unsigned char) module;
327 if ((retval = ioctl(service_fd, ICVVME_reenflags, &arg)) < 0) {
328 perror("could not do ioctl\n");
329 break;
331 printf("REENABLE INTERRUPT FLAGS ON BOARD NR. %d: ( 1 = reenable on, 2 = reenable off )\n", module);
332 for (i = 0; i < ICV_nln; i++) {
333 printf("line %2d: %2ld", i, arg.data[i]);
334 if (((i+1) % 4) == 0 && i != 0)
335 printf("\n");
337 printf("\n");
338 break;
339 case 12:
340 printf("line index to access\n");
341 printf("on module#%d\n", module);
342 printf("[0 -- 15] --> ");
343 scanf("%s", line); getchar();
345 i = atoi(line);
346 if (!WITHIN_RANGE(0, i, icv_LineNb-1)) {
347 printf("line number out of range\n");
348 break;
350 if (!status[module][i]) {
351 icv196_connect(synchro_fd, module, i, 0);
352 status[module][i] = synchro_fd;
354 synchro_fd = status[module][i];
355 arg2.group = module;
356 arg2.index = i;
357 if ((retval = ioctl(synchro_fd, ICVVME_setreenable , &arg2)) < 0) {
358 perror("could not do ioctl\n");
359 break;
361 break;
362 case 13:
363 printf("number of line to access \n" );
364 printf("on module nr. %d ? \n", module );
365 scanf("%s", line);
367 i = atoi(line);
368 if ((i < 0) || (i >= icv_LineNb)) {
369 printf("line number out of range\n");
370 break;
372 if (status[module][i] == 0) {
373 icv196_connect(synchro_fd, module, i, 0);
374 status[module][i] = synchro_fd;
376 synchro_fd = status[module][i];
377 arg2.group = module;
378 arg2.index = i;
379 if ((retval = ioctl(synchro_fd, ICVVME_clearreenable , &arg2)) < 0) {
380 perror("could not do ioctl\n");
381 break;
383 break;
384 case 14:
385 printf("Line index to access on module#%d\n", module);
386 printf("[0 -- 15] --> ");
387 scanf("%s", line); getchar();
389 i = atoi(line);
390 if (!WITHIN_RANGE(0, i, icv_LineNb-1)) {
391 printf("idx out of range\n");
392 break;
395 #if 0
396 if (status[module][i] == 0) {
397 icv196_connect(synchro_fd, module, i, 0);
398 status[module][i] = synchro_fd;
400 synchro_fd = status[module][i];
401 #endif
403 arg2.group = module;
404 arg2.index = i;
405 if (ioctl(synchro_fd, ICVVME_enable , &arg2) < 0)
406 perror("ICVVME_enable ioctl failed");
407 break;
408 case 15:
409 printf("number of line to access \n" );
410 printf("on module nr. %d ? \n", module );
411 scanf("%s", line);
413 i = atoi(line);
414 if ((i < 0) || (i >= icv_LineNb)) {
415 printf("line number out of range\n");
416 break;
418 if (status[module][i] == 0) {
419 icv196_connect(synchro_fd, module, i, 0);
420 status[module][i] = synchro_fd;
422 synchro_fd = status[module][i];
423 arg2.group = module;
424 arg2.index = i;
425 if ((retval = ioctl(synchro_fd, ICVVME_disable , &arg2)) < 0) {
426 perror("could not do ioctl\n");
427 break;
429 break;
430 case 16:
431 if (!synchro_fd) {
432 printf("no line to read from\n");
433 break;
435 memset(buff, 0, sizeof(buff));
436 count = 32;
437 if ((retval = read(synchro_fd, buff, count)) < 0) {
438 perror("could not read\n");
439 break;
441 printf("EVENTS READ:\n");
442 evt = (long *)buff;
443 if (*evt == 0) {
444 printf("no events read\n");
445 printf("\n");
446 break;
448 for (i = 0; i < 8; i++, evt++) {
449 if (*evt != 0) {
450 printf("evt. nr. %2d: 0x%lx ", i, *evt);
451 if (((i+1) % 2) == 0 && i != 0)
452 printf("\n");
455 printf("\n");
456 break;
457 case 17:
458 printf("number of line to access \n" );
459 printf("on module nr. %d ? \n", module );
460 scanf("%s", line);
462 i = atoi(line);
463 if ((i < 0) || (i >= icv_LineNb)) {
464 printf("line number out of range\n");
465 break;
467 if (status[module][i] == 0) {
468 icv196_connect(synchro_fd, module, i, 0);
469 status[module][i] = synchro_fd;
471 synchro_fd = status[module][i];
472 arg2.group = module;
473 arg2.index = i;
474 if ((retval = ioctl(synchro_fd, ICVVME_wait, &arg2)) < 0) {
475 perror("could not do ioctl\n");
476 break;
478 break;
479 case 18:
480 printf("number of line to access \n" );
481 printf("on module nr. %d ? \n", module );
482 scanf("%s", line);
484 i = atoi(line);
485 if ((i < 0) || (i >= icv_LineNb)) {
486 printf("line number out of range\n");
487 break;
489 if (status[module][i] == 0) {
490 icv196_connect(synchro_fd, module, i, 0);
491 status[module][i] = synchro_fd;
493 synchro_fd = status[module][i];
494 arg2.group = module;
495 arg2.index = i;
496 if ((retval = ioctl(synchro_fd, ICVVME_nowait, &arg2)) < 0) {
497 perror("could not do ioctl\n");
498 break;
500 break;
501 default:
502 printf("input not correct\n");
503 break;
505 } while (input != 0);
507 for (i = 0; i < icv_ModuleNb; i++)
508 for (j = 0; j < icv_LineNb; j++)
509 if (status[i][j] != 0) icv196_disconnect(synchro_fd, i, j);
511 if (icv196_put_handle(service_fd))
512 exit(EXIT_FAILURE);
514 exit(EXIT_SUCCESS);
517 static int init_group(int fd, int *grp, int dir)
519 int mt[6][2] = {
520 [0] {
521 [0] = 0,
522 [1] = 1
525 [1] {
526 [0] = 2,
527 [1] = 3
530 [2] {
531 [0] = 4,
532 [1] = 5
535 [3] {
536 [0] = 6,
537 [1] = 7
540 [4] {
541 [0] = 8,
542 [1] = 9
545 [5] {
546 [0] = 10,
547 [1] = 11
551 #if 0
552 printf("Will initialize 2 subgroups of group %d: %d && %d\n",
553 *grp, mt[*grp-1][0], mt[*grp-1][1]);
554 #endif
556 if (icv196_init_channel(fd, module, mt[*grp-1][1], 2, dir)) {
557 printf("Failed to init group %d\n", *grp);
558 return 0;
561 *grp = mt[*grp-1][1];
563 return 1;
567 * @brief Test to transmit 2byte long data
569 * @param fd --
570 * @param gpfd --
572 * <long-description>
575 static void transmit_data(int fd, struct gpfd *gpfd)
578 char buff[50] = { 0 }, *ptr;
579 short *bptr = (short*) buff;
580 int wr_grp, rd_grp, rval[2], i;
581 short wv; /* saved written value */
583 get_write:
584 printf("Group index to write into [1 -- 6] (0 -- abort) --> ");
585 scanf("%d", &wr_grp); getchar();
587 if (!wr_grp)
588 return;
590 if (!(WITHIN_RANGE(1, wr_grp, 6))) {
591 printf("Wrong write group\n");
592 goto get_write;
595 switch (wr_grp) {
596 case 1:
597 rval[0] = 3;
598 rval[1] = 5;
599 break;
600 case 2:
601 rval[0] = 4;
602 rval[1] = 6;
603 break;
604 case 3:
606 rval[0] = 1;
607 rval[1] = 5;
608 break;
609 case 4:
610 rval[0] = 2;
611 rval[1] = 6;
612 break;
613 case 5:
614 rval[0] = 1;
615 rval[1] = 3;
616 break;
617 case 6:
618 rval[0] = 2;
619 rval[1] = 4;
620 break;
623 get_read:
624 printf("Group index to read from [%d, %d] (0 -- abort) --> ",
625 rval[0], rval[1]);
626 scanf("%d", &rd_grp); getchar();
628 if (!rd_grp)
629 return;
631 if (rd_grp != rval[0] && rd_grp != rval[1]) {
632 printf("Wrong read group\n");
633 goto get_read;
636 /* set direction */
637 if (!init_group(fd, &wr_grp, 1) ||
638 !init_group(fd, &rd_grp, 0))
639 return;
641 //printf("Actual Groups: write %d read %d\n", wr_grp, rd_grp);
643 /* set new file name for read data */
644 ptr = rindex(gpfd->gpfn, '.');
645 snprintf(ptr, 4, ".rd");
647 for (i = 0; i < gpfd->elam; i++) {
648 wv = *bptr = gpfd->gpv[i].conv;
650 if (icv196_write_channel(fd, module, wr_grp, 2, buff)) {
651 printf("icv196_write_channel() failed\n");
652 continue;
655 usleep(500); /* delay for HW to do the job */
657 if (icv196_read_channel(fd, module, rd_grp, 2, buff) < 0) {
658 printf("icv196_read_channel() failed\n");
659 continue;
661 usleep(500); /* delay for HW to do the job */
663 /* safe received values and put them in the file */
664 gpfd->gpv[i].conv = *bptr;
665 //printf("Write 0x%hx Read back 0x%hx\n", wv, *bptr);
668 do_conv_gp_file(gpfd, 0);
669 printf("Received data saved in %s file\n", gpfd->gpfn);