Subversion Repositories f9daq

Rev

Rev 109 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
109 f9daq 1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <unistd.h>
5
#include <sys/mman.h>
6
#include <sys/ioctl.h>
7
#include <errno.h>
8
#include <signal.h>
9
#include <ctype.h>
10
#include <time.h>
11
#include <sys/time.h>
12
 
13
#include "wusbxx_dll.h" /* the header of the shared library */
14
#include "daq.h"
15
 
16
#define DEVICE_NAME "/dev/cc32_1"
17
#define PRRES(X) printf(">>> %s -> %d\n",#X,(X))
18
 
19
 
20
 
21
/* definiram lokacije enot*/
22
//#define NTDC 23         /* TDC LeCroy 2277*/
23
#define NTDC 16         /* TDC LeCroy 2277*/
24
#define END_MARKER 0xFAF5
25
int ctrlc=0;
26
int timer_out=0;
27
 
28
void SigInt (int sig)
29
{
30
    ctrlc = 1;
31
    timer_out=1;
32
 
33
}
34
 
35
struct sigaction oact;
36
void timerast (int signumber)
37
{
38
    timer_out = 1;
39
    printf("->>> TIMEOUT !!!\n");
40
}
41
 
42
void tmlnk (int tout)
43
{
44
    timer_out = 0;
45
    struct sigaction act;
46
    struct itimerval tdelay;
47
 
48
    act.sa_handler = timerast;
49
    sigemptyset (&act.sa_mask);
50
    act.sa_flags = 0;
51
 
52
    tdelay.it_value.tv_sec = tout / 100;
53
    tdelay.it_value.tv_usec = 10000 * (tout % 100);
54
    tdelay.it_interval.tv_sec = 0;
55
    tdelay.it_interval.tv_usec = 0;
56
 
57
    if (sigaction (SIGALRM, &act, &oact) < 0)
58
{
59
        perror ("sigaction(tmlnk)");
60
        exit (EXIT_FAILURE);
61
    }
62
    if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
63
    {
64
        perror ("setitimer(tmlnk)");
65
        exit (EXIT_FAILURE);
66
    }
67
}
68
 
69
void tmulk ()
70
{
71
    struct itimerval tdelay;
72
 
73
    tdelay.it_value.tv_sec = 0;
74
    tdelay.it_value.tv_usec = 0;
75
    tdelay.it_interval.tv_sec = 0;
76
    tdelay.it_interval.tv_usec = 0;
77
 
78
    if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0)
79
    {
80
        perror ("setitimer(tmulk)");
81
        exit (EXIT_FAILURE);
82
    }
83
    if (sigaction (SIGALRM, &oact, NULL) < 0)
84
    {
85
        perror ("sigaction(tmulk)");
86
        exit (EXIT_FAILURE);
87
    }
88
}
89
 
90
 
91
 
92
 
93
void CAMAC_status(void)
94
{
95
  unsigned short id,fpga,modid,inhibit,lam;
96
  unsigned short dum;
97
 
98
 
99
  long cres;
100
  int q,x;
101
 
102
  CSSA_RQX(0,0,0,&cres,&q,&x);
103
  dum = cres;
104
  id   = (dum & 0xF000) >> 12;
105
  fpga = (dum & 0x0F00) >> 8;
106
  modid =  (dum & 0x00F0) >> 4;
107
  q      =  (dum & 0x0008) >> 3;
108
  x      =  (dum & 0x0004) >> 2;
109
  inhibit=  (dum & 0x0002) >> 1;
110
  lam    =  (dum & 0x0001);
111
  printf ("CC32 CSR ID=%x FPGA=%x modID=%x Q=%d X=%d INHIBIT=%d LAM=%d \n",id,fpga,modid, q,x,inhibit,lam);
112
}
113
 
114
int daq::init(){
115
 
116
  printf("DAQ init at 0x%x\n",(unsigned int *) udev);
117
  if (udev == NULL) connect();
118
  if (udev == NULL) printf("udev == NULL\n");
119
  xxusb_register_write(udev,1,0x0); // Stop DAQ mode
120
  while (xxusb_usbfifo_read(udev, (int*) stackdump,BUFF_L,100)>0);
121
 
122
  printf("DAQ init finished\n");
123
  CCCZ;
124
  CCCC;
125
 
126
  CREM_I;
127
  long cres;
128
  int q,x;
129
 
130
  // set status registerdevices_find
131
  //int tdcreg2277=0x8C00; // status register common start (green LED),Front panel timeout ,leading+trailing
132
  //  tdcreg2277= 0xC00;   //  status register common stop (red LED)                        ,leading + trailing edge recording
133
  int  tdcreg2277= 0xCC00; //  status register common start (green LED),8192 ns timeout     ,leading+trailing
134
 
135
 CSSA_WQX(NTDC,0,17,tdcreg2277,&q,&x);
136
 CSSA_RQX(NTDC,0,17,&cres,&q,&x);
137
 
138
  printf("LC2277 SetRegister 0x%04x GetRegister 0x%04x\n",tdcreg2277,(unsigned int)cres);
139
 
140
  CAMAC_status();
141
 
142
  // listmode data -----------------------------------------------
143
 
144
    long k=1;
145
 
146
 
147
  /*
148
    stackwrite[k++]=(NAF(NTDC,0,8))| 0x8000 ; // test lam
149
    stackwrite[k++]=0x80 ; // wait for lam
150
 
151
 
152
    stackwrite[k++]=(NAF(NTDC,1,27))| 0x8000;  // test buffering
153
    stackwrite[k++]=0x10 | 0x8000 ; //until Q=0
154
    stackwrite[k++]=0xF ; //max repetitions
155
 
156
    stackwrite[k++]=NAFS(0,0,16);
157
    stackwrite[k++]=0xbeef;
158
 
159
    stackwrite[k++]=(NAF(NTDC,9,27))| 0x8000;  // test buffering
160
    stackwrite[k++]=0x10 | 0x8000 ; //until Q=0
161
    stackwrite[k++]=0xFF ; //max repetitions
162
 
163
    stackwrite[k++]=NAFS(0,0,16);
164
    stackwrite[k++]=0xbeef;
165
*/
166
 
167
 
168
    stackwrite[k++]=(NAF(NTDC,0,0))| 0x8000;  // read TDC
169
    stackwrite[k++]=0x10 | 0x8000 ; //until Q=0
170
    stackwrite[k++]=0xF ; //max repetitions
171
 
172
    stackwrite[k++]=NAF(NTDC,0,9) ; // clear tdc
173
 
174
    stackwrite[k++]=NAF(NTDC,0,10) ; // clear lam
175
 
176
 
177
//    stackwrite[k++]=NAF(NTDC,0,1);  // read status register
178
    stackwrite[k++]=NAFS(0,0,16);
179
    stackwrite[k++]=END_MARKER;
180
    stackwrite[0]=k-1;
181
 
182
    for(unsigned int i=0;i<stackwrite[0];i++) printf("0x%04lx\n",stackwrite[i+1]);
183
 
184
    CAMAC_LED_settings(udev, 1,1,0,0);
185
    xxusb_stack_write(udev,0x2,(long int *)stackwrite);
186
    PRRES(xxusb_stack_read(udev,0x2,(long int *) stackdata));
187
    for(int i=0;i<13;i++) printf("0x%04lx\n",stackdata[i]);
188
 
189
    PRRES(CAMAC_register_read(udev,0,&k));
190
    char tbline[300];
191
    sprintf(tbline,"Firmware ID -> 0x%08lX",k);
192
    printf("%s\n",tbline);
193
 
194
    CAMAC_register_read(udev,1,&k);
195
    sprintf(tbline,"Global Mode -> 0x%08lX",k);
196
    printf("%s\n",tbline);
197
    k=(k&0xF000)|0x0006;
198
    //                    k=0x0002;
199
    k=7;// Start DAQ mode Buffopt=7 single event
200
    CAMAC_register_write(udev,1,k);
201
    CAMAC_register_write(udev,2,0x80); // wait 0x80 us
202
    CAMAC_register_write(udev,3,0x0);
203
    CAMAC_register_write(udev,9,0x0);
204
    CAMAC_register_write(udev,14,0x0);
205
 
206
    return 0;
207
}
208
 
209
int daq::connect(){
210
/*xxusb_device_type devices[100];
211
struct usb_device *dev;
212
dev = devices[0].usbdev;
213
udev = xxusb_device_open(dev);*/
214
    WUSBXX_load (NULL);
215
//    WUSBXX_open ((char *)"CC0126");
216
    WUSBXX_open ((char *)"CC0130");
217
    printf("daq::connect()\n");
218
    return 0;
219
}
220
 
221
int daq::disconnect(){
222
  /* zakljuci */
223
 
224
   printf("daq::disconnect()\n");
225
  return 0;
226
}
227
 
228
 
229
int daq::event(unsigned int *data, int maxn){
230
 
231
  //int events=0;
232
 
233
  short ret;
234
 
235
 
236
 
237
  //while ((events==0)&&(!fStop)){
238
  int tout=100; /* 1/100 of a second */
239
  tmlnk (tout);
240
 
241
  ret=xxusb_usbfifo_read(udev,(int *) data,BUFF_L,500);
242
  tmulk();
243
 
244
 
245
  // printf("ret=%d,events=0x%08x\n",ret,data[0]);
246
  //}
247
 
248
 
249
  return ret*2;
250
}
251
 
252
 
253
 
254
daq::daq(){
255
// intercept routine
256
  if (signal (SIGINT, SigInt) == SIG_ERR) perror ("sigignore");
257
 
258
fStop=0;
259
connect();
260
init();
261
 xxusb_register_write(udev,1,0x1); // Start DAQ mode
262
}
263
 
264
daq::~daq(){
265
 xxusb_register_write(udev,1,0x0); // Stop DAQ mode
266
  while (xxusb_usbfifo_read(udev,(int *)stackdump,BUFF_L,30)>0);
267
 
268
disconnect();
269
}
270
 
271
#ifdef MAIN
272
 
273
 
274
 
275
int main (int argc, char **argv){
276
 
277
  int debug=0;
278
  int neve=1000000;
279
  char fname[0xFF];
280
  sprintf(fname, "test.dat");
281
  if (argc==1) {
282
    printf("Uporaba: %s stevilo_dogodkov ime_datoteke\n",argv[0]);
283
    printf("Meritev prekini s Ctrl-C, ce je nabranih dogodkov ze dovolj\n");
284
    exit(0);
285
  }
286
  if (argc>1) neve  = atoi(argv[1]);
287
  if (argc>2) sprintf(fname,"%s", argv[2]);
288
  if (argc>3) debug = atoi(argv[3]);
289
 
290
 
291
 
292
  // odpremo datoteko za pisanje
293
  FILE *fp=fopen(fname,"w");
294
 
295
  int hdr[4]={1};
296
 
297
  #define BSIZE 10000
298
  unsigned int data[BSIZE];
299
  daq *d= new daq();
300
 
301
  for (int ieve=0;ieve!=neve && !ctrlc ;ieve++){
302
    //int tout=100; /* 1/100 of a second */
303
    //tmlnk (tout);
304
    int nb=d->event(data,BSIZE);
305
    //tmulk();
306
    // zapis v datoteko
110 f9daq 307
    if (nb<0){
308
      ieve--;
309
      continue;
310
    }
109 f9daq 311
    hdr[1]=nb+4*sizeof(int);
312
    hdr[2]=time(NULL);
313
    hdr[3]=ieve;
314
 
315
    fwrite(hdr,   sizeof(int),4 , fp);
316
    fwrite(data,   sizeof(int),nb, fp);
317
//*****************
318
    int evsize=0;
319
    int events=0;
320
    int ib=1,count=0;
321
    events = data[0];
322
    evsize = data[ib++]&0xffff;
323
    if (ieve %100 == 0) printf("nb=%d Event:%d events=%d EvSize:%d\n",nb, ieve,  events, evsize);
110 f9daq 324
    if (evsize<2) {
325
      ieve--;
326
      continue;
327
    }
109 f9daq 328
    for (int i=0;i<evsize;i++)  {
329
       //if (debug) printf("%d\t%08x\n", ib, data[ib]);
330
       if (data[ib]== END_MARKER) break;
110 f9daq 331
       if (ib%2==0) {
109 f9daq 332
          unsigned short word1 =data[ib  ]&0xFFFF;
333
          unsigned short word2 =data[ib+1]&0xFFFF;
334
          unsigned short tdc =  word1;
335
          unsigned short ch  =  (word2 >> 1 ) &0x1F;
336
          unsigned short edge =  word2   & 0x1;
337
          unsigned short q    = (word2 >> 8)  &0x1;
338
          unsigned short x    = (word2 >> 9)  &0x1;
339
          if (debug) printf("%d. [ch=%2d] edge=%d data=%d q=%d x=%d\n",count,ch,edge,tdc, q, x);
340
          count++;
341
       }
342
       ib++;
343
    }
344
    if (data[evsize+1]!=END_MARKER) printf("Error! END_MARKER not found\n");
345
//*****************
346
 
347
 
348
  }
349
  fclose(fp);
350
  printf("Podatki so v datoteki %s\n", fname);
351
 
352
 
353
  delete d;
354
 
355
  return 0;
356
}
357
#endif