source: drsdaq/VME/struck/sis1100/V2.02/test/read_write_driver_1.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: 3.5 KB
Line 
1#define _GNU_SOURCE
2#include <stdio.h>
3#include <errno.h>
4#include <string.h>
5#include <sys/types.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <sys/mman.h>
9#include <sys/ioctl.h>
10
11#include "dev/pci/sis1100_var.h"
12
13#define swap_int(x) ((((x)>>24)&0x000000ff) |\
14 (((x)>> 8)&0x0000ff00) |\
15 (((x)<< 8)&0x00ff0000) |\
16 (((x)<<24)&0xff000000))
17#define swap_short(x) ((((x)>>8)&0x000000ff) |\
18 (((x)<<8)&0x0000ff00))
19
20/****************************************************************************/
21static int read_local(int p, u_int32_t offs, u_int32_t* data)
22{
23struct sis1100_ctrl_reg reg;
24
25reg.offset=offs;
26if (ioctl(p, SIS1100_CONTROL_READ, &reg)<0)
27 {
28 fprintf(stderr, "ioctl(SIS1100_CONTROL_READ, offs=0x%x): %s\n",
29 offs, strerror(errno));
30 return -1;
31 }
32*data=reg.val;
33return 0;
34}
35/****************************************************************************/
36static int write_local(int p, u_int32_t offs, u_int32_t data)
37{
38struct sis1100_ctrl_reg reg;
39
40reg.offset=offs;
41reg.val=data;
42if (ioctl(p, SIS1100_CONTROL_WRITE, &reg)<0)
43 {
44 fprintf(stderr, "ioctl(SIS1100_CONTROL_WRITE, offs=0x%x): %s\n",
45 offs, strerror(errno));
46 return -1;
47 }
48return 0;
49}
50/****************************************************************************/
51/* !!! hier ist alles D16 */
52static int vme_read(int p, u_int32_t addr, u_int16_t* val)
53{
54u_int32_t head, be, error, _val;
55
56if (addr&2) {
57 be=0xc;
58 addr&=~2;
59} else
60 be=0x3;
61
62
63head=(be<<24)|0x010802; /* remote space 1, am, start with address */
64if (write_local(p, 0x80, head)<0) return -1; /* t_hdr */
65if (write_local(p, 0x84, 9)<0) return -1; /* t_am */
66if (write_local(p, 0x88, addr)<0) return -1; /* t_adl */
67
68do {
69 if(read_local(p, 0xac, &error)<0) return -1; /* prot_error */
70} while (error==0x005); /* deadlock */
71
72if (error) {
73 printf("vme_read 0x%08x: err=0x%x\n", addr, error);
74 return -1;
75}
76if (read_local(p, 0xa0, &_val)<0) return -1; /* tc_dal */
77*val=swap_int(_val);
78return 0;
79}
80/****************************************************************************/
81static int vme_write(int p, u_int32_t addr, u_int16_t val)
82{
83u_int32_t head, be, error, _val;
84
85_val=swap_int(val);
86if (addr&2) {
87 be=0xc;
88 addr&=~2;
89} else
90 be=0x3;
91head=(be<<24)|0x010c02; /* remote space 1, am, write, start with address */
92if (write_local(p, 0x80, head)<0) return -1; /* t_hdr */
93if (write_local(p, 0x84, 9)<0) return -1; /* t_am */
94if (write_local(p, 0x90, _val)<0) return -1; /* t_dal */
95if (write_local(p, 0x88, addr)<0) return -1; /* t_adl */
96
97do {
98 error=read_local(p, 0xac, &error); /* prot_error */
99} while (error==0x005); /* deadlock */
100
101if (error) {
102 printf("vme_write 0x%08x: err=0x%x\n", addr, error);
103 return -1;
104}
105return 0;
106}
107/****************************************************************************/
108int main(int argc, char* argv[])
109{
110int p;
111int i;
112u_int32_t base;
113u_int16_t data;
114
115if (argc<2)
116 {
117 fprintf(stderr, "usage: %s path\n", argv[0]);
118 return 1;
119 }
120if ((p=open(argv[1], O_RDWR, 0))<0) return 1;
121
122printf("search for CAEN-Modules:\n");
123for (i=0, base=0; i<10; i++, base+=0x10000) {
124 if (vme_read(p, base+0xfa, &data)==0) { /* kein Fehler */
125 if (data==0xfaf5) { /* CAEN-Module */
126 u_int16_t type, serial;
127 if (vme_read(p, base+0xfc, &type)<0) return -1;
128 if (vme_read(p, base+0xfe, &serial)<0) return -1;
129 printf("at 0x%x: type=0x%x; serial=%d\n", base, type, serial&0xfff);
130 }
131 }
132}
133
134
135close(p);
136return 0;
137}
Note: See TracBrowser for help on using the repository browser.