Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
146 | f9daq | 1 | #include <stdio.h> |
2 | #include <stdlib.h> |
||
3 | #include <string.h> |
||
4 | #include <unistd.h> |
||
5 | #include <sys/mman.h> |
||
6 | #include <errno.h> |
||
7 | #include <signal.h> |
||
8 | #include <ctype.h> |
||
9 | #include <time.h> |
||
10 | #include "../include/wusbxx_dll.h" /* the header of the shared library */ |
||
11 | #include "../include/daq.h" |
||
12 | |||
13 | //#define DEBUG /* vkljuci dodatni izpis */ |
||
14 | #ifdef DEBUG |
||
15 | #define DBG(X) X |
||
16 | #define DBGFUNI(X) printf(">>> %s -> %d\n",#X,(X)) |
||
17 | #else |
||
18 | #define DBG(X) |
||
19 | #define DBGFUNI(X) X |
||
20 | #endif |
||
21 | |||
22 | /* definiram lokacije enot*/ |
||
23 | //#define NTDC 1 /* TDC */ |
||
24 | //#define NTDCCH 8 |
||
25 | //#define NADC 2 /* ADC */ |
||
26 | //#define NADCCH 8 |
||
27 | int ctrlc=0; |
||
28 | char *ccserial=(char*)"CC0126"; |
||
29 | //int devDetect; // variable to tell if we detect any devices |
||
30 | |||
31 | int daq::connect(){ |
||
32 | // odpri daq |
||
33 | xxusb_device_type devices[100]; |
||
34 | //struct usb_device *dev; |
||
35 | devDetect = xxusb_devices_find(devices); |
||
36 | // printf("Detected devices: %d\n", devDetect); |
||
37 | //dev = devices[0].usbdev; |
||
38 | //udev = xxusb_device_open(dev); |
||
39 | |||
40 | if(devDetect > 0) |
||
41 | { |
||
42 | WUSBXX_load(NULL); |
||
43 | WUSBXX_open(ccserial); |
||
44 | printf("daq::connect()\n"); |
||
45 | } |
||
46 | else |
||
47 | printf("daq::connect() - No devices were detected!\n"); |
||
48 | return 0; |
||
49 | } |
||
50 | |||
51 | int daq::init(int chan = 0){ |
||
52 | |||
53 | int i; |
||
54 | long k; |
||
55 | |||
56 | DBGFUNI(xxusb_register_write(udev,1,0x0)); // Stop DAQ mode |
||
57 | while (xxusb_usbfifo_read(udev, (int*) stackdump,BUFF_L,100)>0); |
||
58 | |||
59 | CCCZ; |
||
60 | CCCC; |
||
61 | CREM_I; |
||
62 | |||
63 | NTDC = 1; |
||
64 | NADC = 2; |
||
65 | if(chan != 0) |
||
66 | { |
||
67 | NTDCCH = chan/2; |
||
68 | NADCCH = chan/2; |
||
69 | } |
||
70 | else |
||
71 | { |
||
72 | NTDCCH = 1; |
||
73 | NADCCH = 1; |
||
74 | } |
||
75 | printf("after: NTDCCH = %d, NADCCH = %d\n", NTDCCH, NADCCH); |
||
76 | |||
77 | // create command stack for the TDC and ADC |
||
78 | k=1; |
||
79 | for(i=0;(i<NTDCCH)&&(i<NADCCH);i++) { stackwrite[k++]=NAF(NTDC,i,0); stackwrite[k++]=NAF(NADC,i,0); } |
||
80 | // for(i=0;i<NADCCH;i++) stackwrite[k++]=NAF(NADC,i,0); |
||
81 | stackwrite[k++]=NAF(NTDC,0,9); |
||
82 | stackwrite[k++]=NAF(NADC,0,9); |
||
83 | stackwrite[k++]=NAFS(0,0,16); // insert next word to data |
||
84 | stackwrite[k++]=0xfafb; // event termination word |
||
85 | stackwrite[0]=k-1; |
||
86 | // upload stack #2 |
||
87 | xxusb_stack_write(udev,0x2,(long int *)stackwrite); |
||
88 | xxusb_stack_read(udev,0x2,(long int *) stackdata); |
||
89 | DBG(for(i=0;i<k;i++) printf("0x%04x\n",stackdata[i]);) |
||
90 | |||
91 | int ret[10]; |
||
92 | |||
93 | CAMAC_LED_settings(udev, 1,1,0,0); |
||
94 | ret[0] = CAMAC_register_read(udev,0,&k); |
||
95 | printf("Firmware ID (return %d) -> 0x%08lX\n",ret[0],k); // GKM: Firmware ID (i.e. 0x72000001 = 0111 0010 0000 0000 0000 0000 0000 0001) |
||
96 | ret[1] = CAMAC_register_read(udev,1,&k); |
||
97 | printf("Global Mode (return %d) -> 0x%08lX\n",ret[1],k); |
||
98 | k=(k&0xF000)|0x0005; // set buffer length: n=0..6 -> 0x10000 >> n, n=7 -> single event |
||
99 | ret[0] = CAMAC_register_write(udev,1,k); // GKM: sets the buffer length (i.e. k=5 -> buf length=128 words) |
||
100 | ret[1] = CAMAC_register_write(udev,2,0x80); // wait 0x80 us after trigger // GKM: delay settings in microseconds |
||
101 | ret[2] = CAMAC_register_write(udev,3,0x0); // Scaler Readout Control Register // GKM: scaler readout settings - sets the frequency of readout (if 0, it is disabled) |
||
102 | ret[3] = CAMAC_register_write(udev,9,0x0); // Lam Mask Register // GKM: When 0, readout is triggered by the signal on NIM input |
||
103 | ret[4] = CAMAC_register_write(udev,14,0x0); // USB Bulk Transfer Setup Register |
||
104 | |||
105 | // CAMAC_DGG(udev,1,2,3,0,200,0,0); |
||
106 | // CAMAC_DGG(udev,0,0,0,0,100,0,0); |
||
107 | ret[5] = CAMAC_register_write(udev,5,(0x06<<16)+(0x04<<8)+0x00); // output // GKM: NIM outputs (i.e. 0x060400 = 00 0110 0000 0100 0000 0000 -> NIM O2=DGG_B, NIM O3=DGG_A) |
||
108 | ret[6] = CAMAC_register_write(udev,6,(0x01<<24)+(0x01<<16)+(0x0<<8)+0x0); // SCLR & DGG // GKM: device source selector (i.e. 0x01010000 = 00 0000 0001 0000 0001 0000 0000 0000 0000 -> DGG_A=NIM I1, DGG_B=NIM I1, SCLR=disabled) |
||
109 | ret[7] = CAMAC_register_write(udev,7,(100<<16)+0); // output // GKM: Delay and Gate Generator A registers (i.e. 0x00640000 = 0000 0000 0110 0100 0000 0000 0000 0000 -> DDG_A [gate=100, delay=0]) |
||
110 | ret[8] = CAMAC_register_write(udev,8,(10000<<16)+0); // output // GKM: Delay and Gate Generator B registers (i.e. 0x27100000 = 0010 0111 0001 0000 0000 0000 0000 0000 -> DDG_B [gate=10000, delay=0]) |
||
111 | ret[9] = CAMAC_register_write(udev,13,0); // output // GKM: Extended (course) delay (i.e. 0x00000000 = 0 -> DDG_A ext=0, DDG_B ext=0) |
||
112 | |||
113 | // for(i = 0; i < 10; i++) printf("Setting %d? -> return = %d\n",i,ret[i]); |
||
114 | |||
115 | // ret[0] = CAMAC_register_read(udev,1,&k); |
||
116 | // printf("k (return %d) -> 0x%08lX\n",ret[0],k); |
||
117 | |||
118 | printf("daq::init()\n"); |
||
119 | return 0; |
||
120 | } |
||
121 | |||
122 | int daq::start(){ |
||
123 | xxusb_register_write(udev,1,0x1); // Start DAQ mode |
||
124 | printf("daq::start()\n"); |
||
125 | return 0; |
||
126 | } |
||
127 | |||
128 | int daq::stop(){ |
||
129 | xxusb_register_write(udev,1,0x0); // Stop DAQ mode |
||
130 | while (xxusb_usbfifo_read(udev,(int *)stackdump,BUFF_L,30)>0); |
||
131 | printf("daq::stop()\n"); |
||
132 | return 0; |
||
133 | } |
||
134 | |||
135 | int daq::event(unsigned int *data, int maxn){ |
||
136 | int ib,count; |
||
137 | int events,evsize; |
||
138 | short ret; |
||
139 | |||
140 | ib=0; |
||
141 | ret=xxusb_usbfifo_read(udev,(int *) stackdata,BUFF_L,500); |
||
142 | events=stackdata[ib++]; |
||
143 | DBG(printf("ret=%d,events=0x%08x\n",ret,events);) |
||
144 | if ((ret<0)||(ret!=(((NTDCCH+NADCCH)*4+4)*events+4))) return 0; |
||
145 | |||
146 | count=0; |
||
147 | while (ib<(ret/2-1)){ |
||
148 | evsize = stackdata[ib++]&0xffff; |
||
149 | DBG(printf("Event:%d EvSize:%d\n", events, evsize);) |
||
150 | for (int i=0;i<(NTDCCH+NADCCH);i++,ib++) data[count++] =stackdata[ib++]&0xffff; |
||
151 | if (stackdata[ib++]!=0xfafb){ |
||
152 | printf("Error!\n"); |
||
153 | return 0; |
||
154 | } |
||
155 | events--; |
||
156 | if (fStop) return 0; |
||
157 | } |
||
158 | if (stackdata[ib++]!=0xffff){ |
||
159 | printf("Error!\n"); |
||
160 | return 0; |
||
161 | } |
||
162 | |||
163 | // count = 1; |
||
164 | return count; |
||
165 | } |
||
166 | |||
167 | int daq::disconnect(){ |
||
168 | // zapri daq |
||
169 | WUSBXX_close(); |
||
170 | printf("daq::disconnect()\n"); |
||
171 | return 0; |
||
172 | } |
||
173 | |||
174 | daq::daq(){ |
||
175 | fStop=0; |
||
176 | connect(); |
||
177 | // if(devDetect > 0) |
||
178 | // init(); |
||
179 | } |
||
180 | |||
181 | daq::~daq(){ |
||
182 | disconnect(); |
||
183 | } |
||
184 | |||
185 | #ifdef MAIN |
||
186 | void CatchSig (int signumber) |
||
187 | { |
||
188 | ctrlc = 1; |
||
189 | } |
||
190 | |||
191 | |||
192 | int main (int argc, char **argv){ |
||
193 | |||
194 | int neve=1000000; |
||
195 | char *fname="test.dat"; |
||
196 | if (argc==1) { |
||
197 | printf("Uporaba: %s stevilo_dogodkov ime_datoteke\n",argv[0]); |
||
198 | printf("Meritev prekini s Ctrl-C, ce je nabranih dogodkov ze dovolj\n"); |
||
199 | exit(0); |
||
200 | } |
||
201 | if (argc>1) neve = atoi(argv[1]); |
||
202 | if (argc>2) fname = argv[2]; |
||
203 | |||
204 | |||
205 | // intercept routine |
||
206 | if (signal (SIGINT, CatchSig) == SIG_ERR) perror ("sigignore"); |
||
207 | |||
208 | #define BSIZE 10000 |
||
209 | int i,ieve,nc,nb; |
||
210 | // int hdr[4]={1,(NTDCCH+4)*sizeof(int)}; // hdr[0]=1, hdr[1]=(NTDCCH+4)*4 |
||
211 | int hdr[4]={1,(NTDCCH+NADCCH+4)*sizeof(int)}; |
||
212 | unsigned short adc; |
||
213 | unsigned int data[BSIZE]; |
||
214 | daq *d= new daq(); |
||
215 | time_t time_check; |
||
216 | |||
217 | // odpremo datoteko za pisanje |
||
218 | FILE *fp=fopen(fname,"w"); |
||
219 | |||
220 | d->start(); |
||
221 | ieve=0; |
||
222 | |||
223 | while((ieve<neve)&&(!ctrlc)){ |
||
224 | nc=d->event(data,BSIZE); |
||
225 | nb=0; |
||
226 | while((nc>0)&&(ieve++<neve)&&(!ctrlc)){ |
||
227 | // zapis v datoteko |
||
228 | hdr[2]=time(NULL); |
||
229 | hdr[3]=ieve; |
||
230 | fwrite(hdr,sizeof(int),4 ,fp); |
||
231 | fwrite(&data[nb],sizeof(int),(NTDCCH+NADCCH),fp); |
||
232 | // DBG( |
||
233 | for(i=0;i<(NTDCCH+NADCCH);i++){ |
||
234 | adc=data[nb+i]&0xFFFF; |
||
235 | if(i % 2 == 0) |
||
236 | printf(/*"nev=%4d %d. TDC data=%d\n"*/"%d\t"/*,ieve,i*//*/2*/,adc); |
||
237 | // printf("nev=%4d %d. TDC data=%d\n",ieve,i/2,adc); |
||
238 | else if(i % 2 == 1) |
||
239 | printf(/*"nev=%4d %d. TDC data=%d\n"*/"%d\t"/*,ieve,i*//*/2*/,adc); |
||
240 | // printf("nev=%4d %d. ADC data=%d\n",ieve,i/2,adc); |
||
241 | } |
||
242 | printf("\n"); |
||
243 | // ) |
||
244 | nb+=(NTDCCH+NADCCH); |
||
245 | nc-=(NTDCCH+NADCCH); |
||
246 | if (!(ieve%1000)) printf("event no. -> %d\n",ieve); |
||
247 | }; |
||
248 | }; |
||
249 | |||
250 | d->stop(); |
||
251 | fclose(fp); |
||
252 | printf("Podatki so v datoteki %s\n", fname); |
||
253 | delete d; |
||
254 | |||
255 | return 0; |
||
256 | } |
||
257 | #endif |