Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
326 f9daq 1
//
2
//  test.c
3
//
4
//  Description : test program for V729
5
//
6
//  Author : S. Korpar
7
//  Date   : 2005/07/15
8
// adapted to CAEN V1719 by R. Pestotnik  2012/09/07
9
 
10
#include <stdlib.h>
11
#include <stdint.h>
12
#include <stdio.h>
13
#include <string.h>
14
 
15
 
16
 
17
#ifdef _CVI_
18
#  include <windows.h>
19
#  include "CAEN_V729_CVI.h"
20
#  include "asynctmr.h"
21
#  include "H1D.h"
22
#  include "H2D.h"
23
#  include "HDraw.h"
24
typedef signed char   int8_t;
25
typedef unsigned char  uint8_t;
26
typedef short  int16_t;
27
typedef unsigned short uint16_t;
28
typedef int  int32_t;
29
typedef unsigned int  uint32_t;
30
#  include <utility.h>
31
#  define usleep(x) Delay(x*1e-3)
32
#include "sa02lib.h"
33
#define printf sa02Printf
34
# else _CVI_
35
#  include <unistd.h>
36
#  include <ctype.h>
37
#  include <vector>
38
#  include <sys/time.h>
39
#  include <sys/stat.h>
40
#  include <signal.h>
41
#  include <string>
42
#endif _CVI_
43
 
44
 
45
#include "vme.h"
46
#include "CAEN_V729_DEF.h"
47
#include "CAEN_V729.h"
48
#include "dataio.h"
49
 
50
//#include <utility.h>
51
CAEN_V729_PARAM  CAEN_V729_param;
52
 
53
#define IREG_OFFSET 0x2000
54
#define V729_WAIT 100
55
 
56
 
57
#define TRUE 1
58
#define FALSE 0
59
 
60
uint32_t V729_module=0;
61
 
62
 
63
static int          g_timerId              = 0;
64
 
65
#ifdef V729_MAIN
66
int ctrl_c=0;
67
# else V729_MAIN
68
#  ifdef V729_WINMAIN
69
int ctrl_c=0;
70
#   else V729_WINMAIN
71
extern int ctrl_c;
72
#  endif V729_WINMAIN
73
#endif V729_MAIN
74
 
75
#ifndef WIN32
76
struct sigaction oact;
77
#endif WIN32
78
//---------------------------------------------------------
79
 
80
 
81
//---------------------------------------------------------
82
DefineThreadSafeScalarVar (int, TimerOut,0);
83
 
84
 
85
void timerast (int signumber) {
86
 
87
}
88
 
89
//---------------------------------------------------------
90
 
91
int CVICALLBACK TimerOutCallback (int reserved, int theTimerId, int event,
92
                                  void *callbackData, int eventData1,
93
                                  int eventData2) {
94
  double currentTime = 0.0;
95
  double deltaTime   = 0.0;
96
  int id;
97
  /* Like a regular UI timer, the async. timer's callback is passed the    */
98
  /* total elapsed time and the time since the last tick.                  */
99
  if ((double*)eventData1)
100
    currentTime = *((double*)eventData1);
101
  if ((double*)eventData2)
102
    deltaTime   = *((double*)eventData2);
103
  if (event == EVENT_TIMER_TICK) {
104
    SetTimerOut(TRUE);
105
    id= *((int *)callbackData);
106
    fprintf(stdout,"TIMEOUT %d !!!\n", id );
107
 
108
  }
109
  /* We will get an EVENT_DISCARD when the timer is destroyed. */
110
  if (event == EVENT_DISCARD)
111
    g_timerId = 0;
112
  return 0;
113
}
114
 
115
int g_tmlnk;
116
void tmlnk (int tout) {
117
 
118
  //timer_out=0;
119
#ifdef _CVI_
120
  g_tmlnk = tout;
121
  SetTimerOut(FALSE);
122
  g_timerId = NewAsyncTimer (((double)tout) *10./ 1000.0, 1, 1, TimerOutCallback, &g_tmlnk);
123
# else _CVI_
124
 
125
  struct sigaction act;
126
  struct itimerval tdelay;
127
 
128
  act.sa_handler = timerast;
129
  sigemptyset (&act.sa_mask);
130
  act.sa_flags = 0;
131
 
132
  tdelay.it_value.tv_sec = tout / 100;
133
  tdelay.it_value.tv_usec = 10000 * (tout % 100);
134
  tdelay.it_interval.tv_sec = 0;
135
  tdelay.it_interval.tv_usec = 0;
136
 
137
  if (sigaction (SIGALRM, &act, &oact) < 0) {
138
    perror ("sigaction(tmlnk)");
139
    exit (EXIT_FAILURE);
140
  }
141
  if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0) {
142
    perror ("setitimer(tmlnk)");
143
    exit (EXIT_FAILURE);
144
  }
145
 
146
#endif _CVI_
147
}
148
 
149
//---------------------------------------------------------
150
void tmulk ( void ){
151
#ifdef _CVI_
152
  DiscardAsyncTimer( g_timerId );
153
# else _CVI_
154
  struct itimerval tdelay;
155
 
156
  tdelay.it_value.tv_sec = 0;
157
  tdelay.it_value.tv_usec = 0;
158
  tdelay.it_interval.tv_sec = 0;
159
  tdelay.it_interval.tv_usec = 0;
160
 
161
  if (setitimer (ITIMER_REAL, &tdelay, NULL) < 0) {
162
    perror ("setitimer(tmulk)");
163
    exit (EXIT_FAILURE);
164
  }
165
  if (sigaction (SIGALRM, &oact, NULL) < 0) {
166
    perror ("sigaction(tmulk)");
167
    exit (EXIT_FAILURE);
168
  }
169
#endif _CVI_
170
}
171
 
172
//---------------------------------------------------------
173
int wait_while (int num) {
174
  int i, tmp = 0;
175
 
176
  for ( i =0; i <num; i++ ) tmp = 0;
177
  return tmp;
178
}
179
 
180
//---------------------------------------------------------
181
 
182
 
183
void V729_map( uint32_t addr) {
184
  V729_module = addr;
185
  return;
186
}
187
//---------------------------------------------------------  
188
int V729_status( void ){
189
  uint32_t status;
190
  VME_A24D16_R(V729_module+V729_CSR, &status);
191
  return status;
192
}
193
//---------------------------------------------------------
194
void V729_info(FILE *kam) {
195
  uint32_t code, mid, ver;
196
 
197
 
198
  VME_A24D16_R(V729_module+0xFA,&code);
199
  VME_A24D16_R(V729_module+0xFC,&mid);
200
  VME_A24D16_R(V729_module+0xFE,&ver);
201
  printf( "code/mid/ver = 0x%04X/0x%04X/0x%04X\n", code, mid, ver);
202
  printf( "Fixed code (0xFAF5) = 0x%04X\n", code);
203
  printf( "Manufacturer/Type = 0x%02X/0x%03X\n",
204
          (mid & 0xFC00)>>10, mid & 0x3FF);
205
  printf( "Version/Serial = %d/%d\n",
206
          (ver & 0xF000)>>12, ver & 0xFFF);
207
  return;
208
}
209
 
210
 
211
//---------------------------------------------------------
212
void V729_reg( uint32_t val) {
213
  uint32_t push=0;
214
 
215
  VME_A24D16_W(V729_module+V729_FSR, val);
216
  VME_A24D16_W(V729_module+V729_UFSR, push); //push
217
  usleep(V729_WAIT);
218
 
219
}
220
 
221
 
222
//---------------------------------------------------------
223
void V729_reset(int nall,int nbefore,int obae,int obaf) {
224
  uint32_t cbl;
225
  uint32_t val=0;
226
//  cbl=4096+12-100;
227
  cbl=4096+12-nbefore;
228
 
229
  VME_A24D16_W(V729_module+V729_RR, val); // reset
230
  printf("nbefore=%d\t", nbefore);
231
  printf("nall=%d\t", nall);
232
  printf("obae=%d\t", obae);
233
  printf("obaf=%d\n", obaf);
234
 
235
// set AE
236
  V729_reg( (obae & 0xFF)<<8 );//
237
  V729_reg( (obae & 0xF00) ); //
238
 
239
  printf("CSR = 0x%x\n", V729_status() );
240
// set AF and CBL
241
  V729_reg( ((obaf & 0xFF)<<8) + (cbl & 0xFF)   );
242
  V729_reg( (obaf & 0xF00) + ((cbl & 0xF00)>>8) );
243
 
244
 
245
 
246
  VME_A24D16_W(V729_module+V729_SR,nall); //total number of samples
247
 
248
  return;
249
}
250
//---------------------------------------------------------
251
 
252
void V729_set_bias(int range, int bias1, int bias2) {
253
  int i;
254
  uint32_t bias;
255
 
256
  switch (range) {
257
    case 0: //User
258
      for (i=0x18; i<0x27; i+=4) {
259
        VME_A24D16_W(V729_module+i, bias1); //bias
260
        usleep(V729_WAIT);
261
        VME_A24D16_W(V729_module+i+2, bias2); //bias
262
        usleep(V729_WAIT);
263
      }
264
      printf("Bias + %x  - %x\n", bias1, bias2);
265
      break;
266
    case 1: //Single Ended +
267
      for (i=0x18; i<0x27; i+=4) {
268
        bias = 0xCC;
269
        VME_A24D16_W(V729_module+i, bias); //bias
270
        usleep(V729_WAIT);
271
        bias =0x733;
272
        VME_A24D16_W(V729_module+i+2, bias); //bias
273
        usleep(V729_WAIT);
274
      }
275
      break;
276
    case 2: //Single Ended -
277
      for (i=0x18; i<0x27; i+=4) {
278
        bias =0x733;
279
        VME_A24D16_W(V729_module+i, bias); //bias
280
        usleep(V729_WAIT);
281
        bias= 0xD99 ;
282
        VME_A24D16_W(V729_module+i+2, bias); //bias
283
        usleep(V729_WAIT);
284
      }
285
      break;
286
    case 4: //Differential Unipolar
287
      for (i=0x18; i<0x27; i+=4) {
288
        bias = 0x400;
289
        VME_A24D16_W(V729_module+i,bias); //bias
290
        usleep(V729_WAIT);
291
        bias = 0xA66;
292
        VME_A24D16_W(V729_module+i+2, bias); //bias
293
        usleep(V729_WAIT);
294
      }
295
      break;
296
    case 3: //Single Ended Bipolar
297
    case 5: //Differential Bipolar
298
    default:
299
      for (i=0x18; i<0x27; i+=2) {
300
        bias=0x733;
301
        VME_A24D16_W(V729_module+i, bias); //bias
302
        usleep(V729_WAIT);
303
      }
304
      break;
305
  }
306
  return;
307
}
308
 
309
//---------------------------------------------------------
310
void V729_aquisition_mode(uint32_t mode) { // mode 0 aqusition mode, 0x30 buffers programming
311
#ifdef V729_DEBUG
312
  int status;
313
  VME_A24D16_R(V729_module+V729_CSR, &status);
314
  usleep(V729_WAIT);
315
  fprintf(stderr, "V729_CSR 0x0E = 0x%04X\n", status);
316
#endif V729_DEBUG
317
 
318
  VME_A24D16_W(V729_module+V729_CSR, mode); //aquisition mode
319
  usleep(V729_WAIT);
320
#ifdef V729_DEBUG
321
  fprintf(stderr, "V729_CSR 0x0E = 0x%04X\n", mode);
322
  VME_A24D16_R(V729_module+V729_CSR, &status);
323
  usleep(V729_WAIT);
324
  fprintf(stderr, "V729_CSR 0x0E = 0x%04X\n", status);
325
#endif V729_DEBUG
326
  return;
327
}
328
 
329
//---------------------------------------------------------
330
void V729_soft_stop( void ){
331
  uint32_t sw=1;
332
  VME_A24D16_W(V729_module+V729_SSR,sw);
333
//  usleep(V729_WAIT);
334
  sw =0;
335
  VME_A24D16_W(V729_module+V729_OBR1,sw);
336
//  usleep(V729_WAIT);
337
  return;
338
}
339
 
340
//---------------------------------------------------------
341
void V729_clear_buffer( void ){
342
    uint32_t data1,data2;
343
    const int tout=100; /* 1/100 of a second */
344
 
345
  int cnt=0;
346
  tmlnk (tout);
347
 
348
  do {
349
    VME_A24D32_R(V729_module+V729_OBR1, &data1);
350
 
351
    if (ctrl_c|| GetTimerOut()) break;
352
 
353
 
354
    cnt++;
355
  } while (data1 & (0x3<<29));
356
 
357
  cnt=0;
358
  do {
359
    VME_A24D32_R(V729_module+V729_OBR2, &data2);
360
 
361
    if (ctrl_c|| GetTimerOut()) break;
362
 
363
    cnt++;
364
  } while (data2 & (0x3<<29));
365
 
366
  tmulk();
367
  return;
368
}
369
 
370
void V729_get_buffer(uint32_t *data1, uint32_t *data2) {
371
  VME_A24D32_R(V729_module+V729_OBR1, data1);
372
//    usleep(V729_WAIT);
373
  VME_A24D32_R(V729_module+V729_OBR2, data2);
374
//    usleep(V729_WAIT);
375
  return;
376
}
377
 
378
 
379
 
380
//---------------------------------------------------------
381
void V729_init(uint32_t addr, int nall, int cbl, uint32_t bias1, uint32_t bias2) {
382
  V729_map(addr);
383
  V729_info(stderr);
384
  //V729_reset(nall,cbl,nall,0x1); // obaf = 1, disable start signals when there is at least one sample in the buffer
385
  V729_reset(nall,cbl,nall,4000); // obaf = 1, disable start signals when there is at least one sample in the buffer
386
  V729_set_bias(0,bias1,bias2);
387
  V729_aquisition_mode(0);
388
#ifndef WIN32
389
  CAENVME_SetFIFOMode(udev,1);
390
  sleep(1);
391
#endif WIN32
392
}
393
 
394
 
395
//---------------------------------------------------------
396
int V729_blt=0;
397
int V729_SetBltMode(int mode) {
398
  V729_blt = mode;
399
  return mode;
400
}
401
 
402
int V729_event(int mask, int nall, uint32_t *data,uint32_t *data1,uint32_t *data2) {
403
  uint32_t status;
404
  const int tout=200; /* 1/100 of a second */
405
  int nb=0;
406
  int count=0;
407
  int i;
408
  tmlnk (tout);
409
 
410
 
411
  //status= 0x80 ; VME_A16D16_W(IREG_OFFSET+0x8,&status); // pulse ch 8
412
  //status = 0x1a ; VME_A16D16_W(IREG_OFFSET+0xC,&status); // clr1 enable1  .... clear busy1 flag   // KEK magnet
413
  //
414
  do {
415
 
416
    VME_A24D16_R(V729_module+V729_CSR, &status);
417
 
418
    count++;
419
    if (ctrl_c || GetTimerOut()) {
420
      tmulk ();
421
      printf("[%d] daq=%x status %x AE01=%d AE23=%d Waiting for DATA TRG bit... at line %d\n",count ,  (status>>4)&0x3,status, status&0x1,(status>>2)&0x1 , __LINE__);
422
 
423
      V729_clear_buffer();
424
 
425
      return -1;
426
    }
427
 
428
  } while ((status & 0x5) ^ 0x5);
429
  tmulk ();
430
  //count =  (nall+5);
431
  count =  (nall+1);
432
  if (V729_blt) {
433
 
434
//#ifdef CAEN_V1718
435
//  VME_A24D32_FIFOBLTR(V729_module+V729_OBR1,data1,count*sizeof(uint32_t),&nb);
436
//  VME_A24D32_FIFOBLTR(V729_module+V729_OBR2,data2,count *sizeof(uint32_t),&nb);
437
//#endif CAEN_V1718
438
    for (i=0; i<count; i++) {
439
      data[2*i]   = data1[i];
440
      data[2*i+1] = data2[i];
441
    }
442
    nb*=2;
443
  } else {
444
    int nc=0;
445
    int j=0;
446
    VME_MRRST(); // MultiReadReset
447
 
448
    do {
449
      VME_R(VME_A24,VME_D32,V729_module+V729_OBR1, &data[nc]);
450
    } while ( !((data[nc]>>31)&0x1) );
451
    nc++;
452
    do {
453
      VME_R(VME_A24,VME_D32,V729_module+V729_OBR2, &data[nc]);
454
    } while ( !((data[nc]>>31)&0x1) );
455
    nc++;
456
    for (i=0; i<nall; i++) {
457
      //if (mask & 0x3)
458
      VME_MR(VME_A24,VME_D32,V729_module+V729_OBR1, &data[nc++]);
459
      //if (mask & 0xc)
460
      VME_MR(VME_A24,VME_D32,V729_module+V729_OBR2, &data[nc++]);
461
    }
462
 
463
    j = nc;
464
    if ( VME_GetInterface()!= WIENER_VMEMM ) j = VME_MREXEC(data); // MultiReadExecute   
465
 
466
 
467
    nb=j*sizeof(uint32_t);
468
 
469
  }
470
 
471
  return nb;
472
}
473
int V729_plothandle;
474
//---------------------------------------------------------
475
int V729_plot(int first, uint16_t *buf ) {
476
#ifdef _CVI_
477
  int start=23;
478
  int end=35;
479
 
480
  //const double maxy=2048;
481
  const double maxy=512;
482
  const double maxx=512;
483
  //const double maxy=2048;
484
  //const double maxy=128;
485
  //const double maxy=512;
486
  const int nx=500;
487
 
488
  const int ny=100;
489
  const int h0=10;
490
 
491
  int i;
492
  uint32_t *hdr = (uint32_t *)buf;
493
  int nb= hdr[1]-2*sizeof(uint32_t);
494
  int nitems = nb /sizeof(uint16_t);
495
  unsigned int recid=hdr[0];
496
  uint16_t *data = &buf[4];
497
  double x,y;
498
  double baseline=0;
499
  double charge=0;
500
 
501
 
502
  while (nitems>0) {
503
    int id = data[0]-1;
504
    int len= data[1]/sizeof(uint16_t)-2;
505
 
506
    if (CAEN_V729_param.verbose)  printf("\t->> recid=%d nb=%d nitems=%d id=%d len=%d\n", recid, hdr[1],nitems,id,len);
507
    if (id>3) return 0;
508
 
509
    data+=2;
510
 
511
    if (first) {
512
      for (i=0; i<4; i++) {
513
        char hname[0xFF];
514
        char htitle[0xFF];
515
        sprintf (hname,"hwf%d",i);
516
        sprintf (htitle,"CAEN V729 ch%d",i);
517
        H2DInit(h0+i,hname, htitle ,len,-0.5,1,ny,-maxy,2*maxy/ny);
518
        H2DSetTitleX(h0+i,"timebin");
519
        H2DSetTitleY(h0+i,"pulse height");
520
 
521
        sprintf (hname,"hcharge%d",i);
522
        sprintf (htitle,"CAEN V729 ch%d",i);
523
        H1DInit(h0+i,hname, htitle ,nx,-maxx,2*maxx/nx);
524
        H1DSetTitleX(h0+i,"timebin");
525
        H1DSetTitleY(h0+i,"charge");
526
      }
527
      first=0;
528
      // h1d=new TH1F("h1d","Charge;signal charge(a.u.);N",500,-maxy,5*maxy);
529
 
530
    }
531
 
532
    baseline=0;
533
    for (i=0; i<20; i++) baseline += data[i];
534
    baseline /=20.;
535
    // baseline=2000;
536
 
537
 
538
    for (i=0; i<len; i++) {
539
      if (CAEN_V729_param.verbose >2) printf("ADC %d %d\n", i, data[i]);
540
      x=i;
541
      y=data[i];
542
      y-=baseline;
543
      if (i>=start && i<end) charge += y;
544
 
545
      H2DFill(h0+id,x,y,1);
546
    }
547
    charge /=(end-start);
548
    H1DFill(h0+id,charge,1);
549
 
550
    data+=len;
551
    nitems=nitems - len-2;
552
    //if (id==2) h1d->Fill(charge);
553
  }
554
#else 
555
#warning _CVI_ is not defined  
556
#endif _CVI_
557
 
558
  return 0;
559
}
560
 
561
//---------------------------------------------------------
562
int V729_decode(uint32_t mask, int nall, uint32_t *data,  uint16_t *dadc, int debug) {
563
 
564
  int hdrlen=6;  // 4+2
565
  uint16_t *adc[4]= {0};
566
  int i,j;
567
 
568
  int nc=0;
569
  int narrays=0;
570
 
571
 
572
  int indx[4] = {0,0,0,0};
573
  int shft[4] = {0,0,0,0};
574
  int ashft[4]= {0,0,0,0};
575
  int nmask=0;
576
 
577
  for (i=0; i<4; i++) {
578
    adc[i] = &dadc[hdrlen+i*(nall+2)];
579
    // printf("---->ID %d len %d ,%d\n",dadc[hdrlen+i*(nall-2)],dadc[hdrlen+i*(nall-1)],hdrlen+i*(nall+2)) ;
580
  }
581
  if (mask & 0x3) narrays++;
582
  if (mask & 0xc) narrays++;
583
 
584
  for (i=0; i<4; i++) if (mask & (1<<i) ) {
585
      indx[nmask] = i;
586
      shft[nmask] = (i%2)*12;
587
      if (( (mask & 0x3) > 0 ) && (i>1))  ashft[nmask]=1;
588
      else ashft[nmask]=0;
589
      nmask++;
590
    }
591
  for (j=0; j<nmask; j++) {
592
    nc=0;
593
    for (i=0; i<nall+1; i++) {
594
      int ii=narrays*i+ashft[j];
595
 
596
      uint32_t data1 = data[ii];
597
 
598
      if (data1 & (1<<31)) {
599
        if (debug)
600
          fprintf(stdout,"data1: empty flags: 0x%X, time tag: 0x%06X %d\n", (data1>>29) & 0x3, data1 & 0xFFFFFF, data1 & 0xFFFFFF);
601
      } else if ((data1>>29) & 0x3) {
602
        if (nc<nall) {
603
          adc[j][nc] = (data1>>shft[j]) & 0xFFF;
604
          if (debug >2) printf("[%d/%d] ADC%d adc0=%d\n", i,nc,indx[j], adc[j][nc]);
605
          nc++;
606
        }
607
 
608
      } else {
609
        if (debug) fprintf(stdout,"Data(%4d) %d indx=%d %d nc=%d(%d) %d 0x%08X\n", i, indx[j],narrays*i+ashft[j],j,nc,nmask,narrays, data1);
610
      }
611
    }
612
  }
613
  return nc;
614
}
615
 
616
//---------------------------------------------------------
617
void V729_Usage(void) {
618
 
619
  printf ("*********************************************************************************:\n");
620
  printf ("Usage: CAEN_V729 <arguments> .........   CAEN V729A data aquisition:\n\n");
621
  printf ("Arguments: \n");
622
  printf ("-d <debuglevel>  \n");
623
  printf ("-b <bias+>  \n");
624
  printf ("-e <bias->  \n");
625
  printf ("-a <VME address>  \n");
626
  printf ("-c <n samples before stop>  \n");
627
  printf ("-n <number of events> \n");
628
  printf ("-l <nall> number of samples \n");
629
  printf ("-m <channel mask: 4bits (1 bit/ch)> \n");
630
  printf ("-o <output file name> \n");
631
 
632
  printf ("*********************************************************************************:\n");
633
}
634
//---------------------------------------------------------
635
// test program
636
 
637
int V729_GetParams(int argc,char **argv) {
638
#ifdef _CVI_
639
  int p = CAEN_V729_param.panel;
640
  GetCtrlVal(p,P4_MASK,&CAEN_V729_param.mask);
641
  GetCtrlVal(p,P4_CBL ,&CAEN_V729_param.cbl);
642
  GetCtrlVal(p,P4_NALL,&CAEN_V729_param.nall);
643
  GetCtrlVal(p,P4_FILENAME,CAEN_V729_param.filename);
644
  GetCtrlVal(p,P4_VERBOSE,&CAEN_V729_param.verbose);
645
  GetCtrlVal(p,P4_NEVE,&CAEN_V729_param.neve);
646
  GetCtrlVal(p,P4_ADDR,&CAEN_V729_param.addr);
647
  GetCtrlVal(p,P4_BIAS1,&CAEN_V729_param.bias1);
648
  GetCtrlVal(p,P4_BIAS2,&CAEN_V729_param.bias2);
649
  GetCtrlVal(p,P4_FRACTION,&CAEN_V729_param.fraction);
650
 
651
# else _CVI_
652
  CAEN_V729_param.mask=0xF;
653
  CAEN_V729_param.cbl=13;
654
  CAEN_V729_param.nall=1330;
655
  CAEN_V729_param.filename= 0;
656
  CAEN_V729_param.verbose = 0;
657
  CAEN_V729_param.neve    = 1;
658
  CAEN_V729_param.addr = V729_OFFSET;
659
  CAEN_V729_param.bias1 = 0x500; // vecje vrednosti povzrocajo tezave in zacno kanali oscilirat ....
660
  CAEN_V729_param.bias2 = 0x500;
661
  CAEN_V729_param.append = 0;
662
  CAEN_V729_param.fp = NULL;
663
  CAEN_V729_param.fraction = 0;
664
  int c=0;
665
  while ((c = getopt (argc, argv, "b:e:a:d:c:l:n:o:m:f:r:")) != -1)
666
    switch (c) {
667
      case 'd':
668
        CAEN_V729_param.verbose=atoi(optarg);
669
        break;
670
      case 'c':
671
        CAEN_V729_param.cbl=atoi(optarg);
672
        break;
673
      case 'l':
674
        CAEN_V729_param.nall=atoi(optarg);
675
 
676
        if (CAEN_V729_param.nall%16) {
677
          printf ("buffer length should be multiple of 16 , modulo = %d\n",CAEN_V729_param.nall%16);
678
          // exit(-1);
679
        }
680
        break;
681
      case 'n':
682
        CAEN_V729_param.neve=atoi(optarg);
683
        break;
684
      case 'r':
685
        CAEN_V729_param.fraction=atof(optarg);
686
        break;
687
      case 'a':
688
        CAEN_V729_param.addr=strtoul (optarg,NULL,0);
689
        break;             // address
690
      case 'b':
691
        CAEN_V729_param.bias1=strtoul (optarg,NULL,0);
692
        break;             // bias
693
      case 'e':
694
        CAEN_V729_param.bias2=strtoul (optarg,NULL,0);
695
        break;             // bias
696
 
697
      case 'm':
698
        CAEN_V729_param.mask=strtoul (optarg,NULL,0);
699
        break;             // mask
700
      case 'o':
701
        sprintf(CAEN_V729_param.filename,"%s", optarg );
702
        CAEN_V729_param.append=0;
703
        break;       // output
704
      case 'f':
705
        sprintf(CAEN_V729_param.filename,"%s", optarg );
706
        CAEN_V729_param.append=1;
707
        break;       // output
708
    }
709
#endif _CVI_
710
  return 0;
711
}
712
 
713
int V729_LoadPanel (int Parent_Panel_Handle, char Filename[], int Panel_Resource_ID) {
714
 
715
  CAEN_V729_param.panel = LoadPanel(Parent_Panel_Handle, Filename, Panel_Resource_ID);
716
  return CAEN_V729_param.panel;
717
}
718
 
719
 
720
 
721
#ifdef _CVI_
722
int CVICALLBACK HDrawCB (int panel, int control, int event,
723
                         void *callbackData, int eventData1, int eventData2) {
724
  int ch=0,type=0;
725
  switch (event) {
726
    case EVENT_COMMIT:
727
      GetCtrlVal(CAEN_V729_param.panel,P4_ADCCH, &ch);
728
      GetCtrlVal(CAEN_V729_param.panel,P4_HDISPLAY, &type);
729
      if (type) {
730
        H2DDraw(10+ch,CAEN_V729_param.panel,P4_GRAPH, &V729_plothandle);
731
      } else {
732
        H1DDraw(10+ch,CAEN_V729_param.panel,P4_GRAPH, &V729_plothandle);
733
      }
734
      break;
735
  }
736
  return 0;
737
}
738
 
739
int CVICALLBACK V729_daq(void *functionData) {
740
  return V729_main ( 0, NULL);
741
}
742
#endif _CVI_
743
 
744
void CatchSig (int i) {
745
  ctrl_c = 1;
746
}
747
 
748
int p4h;
749
 
750
int  V729_SetFilePointer (FILE *fp) {
751
  CAEN_V729_param.fp = fp;
752
  return 0;
753
}
754
 
755
int  V729_main (int argc,char **argv) {
756
  int hdrlen=4; // number of u16 words
757
  FILE *fp=NULL;
758
  int output=0;
759
  int bsize;
760
  int nmask,imask;
761
  int i,ch, len,nb;
762
  int adclen;
763
  uint32_t *data , *data1 ,*data2 ;
764
  uint16_t *adc ;
765
  uint32_t *evhdr;
766
  int ncount =0;
767
  int nerrors=0;
768
  int nball  =0;
769
 
770
 
771
  // reset timers
772
  time_t t,told=0, tstart;
773
  time(&t);
774
  tstart=t;
775
  InitializeTimerOut();
776
  printf("V729_main \n");
777
  V729_GetParams(argc,argv);
778
  // open file
779
  if (CAEN_V729_param.fp !=NULL) {
780
    fp= CAEN_V729_param.fp;
781
    output=2;
782
    printf("V729_main Data file is already opened\n");
783
  } else {
784
    if (strlen(CAEN_V729_param.filename)>0) {
785
      if (CAEN_V729_param.verbose) printf("Data in the file:%s\n", CAEN_V729_param.filename);
786
      if (CAEN_V729_param.append) fp=fopen(CAEN_V729_param.filename,"a");
787
      else        fp=fopen(CAEN_V729_param.filename,"w");
788
      output=1;
789
    }
790
  }
791
 
792
 
793
  // allocate storage
794
 
795
  bsize = sizeof(uint32_t)*2*(CAEN_V729_param.nall+1);
796
  data  = (uint32_t *)malloc(bsize);
797
  data1 = (uint32_t *)malloc(bsize);
798
  data2 = (uint32_t *)malloc(bsize);
799
  nmask=0;
800
  for (i=0; i<4; i++) if (CAEN_V729_param.mask & (1<<i)) nmask++;
801
  adclen = CAEN_V729_param.nall+2;
802
  len    = nmask* adclen+hdrlen;
803
  adc = (uint16_t *) malloc(len  *sizeof(uint16_t)); // 4 channels + 4 ch header + 1 event hdr
804
 
805
  // write headers
806
  evhdr  = (uint32_t *) &adc[0];
807
  evhdr[0] = CAENV729REC_ID;
808
  evhdr[1] = len  *sizeof(uint16_t);
809
  imask=0;
810
  for (ch=0; ch<4; ch++) {
811
    if (  CAEN_V729_param.mask & (1<<ch) ) {
812
      uint16_t *chhdr= &adc[hdrlen+imask*adclen];
813
      chhdr[0] = (uint16_t) ch+1;
814
      chhdr[1] = (uint16_t) adclen * sizeof(uint16_t);
815
      //printf("========>%d %d [%d,%d,%d]hdr=%d %d\n",ch, hdrlen+imask*adclen,hdrlen,imask,adclen,chhdr[0],chhdr[1]);
816
      imask++;
817
 
818
    }
819
  }
820
 
821
  // start
822
 
823
 
824
  // open VME and initialize board
825
 
826
  V729_init(CAEN_V729_param.addr, CAEN_V729_param.nall, CAEN_V729_param.cbl, CAEN_V729_param.bias1,CAEN_V729_param.bias2);
827
 
828
  // event loop
829
  for (i=0; i<CAEN_V729_param.neve; i++) {
830
    GetCtrlVal(CAEN_V729_param.panel,P4_VERBOSE,&CAEN_V729_param.verbose);
831
    if ( CAEN_V729_param.verbose) printf("neve=%d\n",i);
832
    nb = V729_event(CAEN_V729_param.mask,CAEN_V729_param.nall,data, data1,data2);
833
 
834
    if (ctrl_c) {
835
 
836
      break;
837
    }
838
    if (nb<0) {  // timeout
839
      i--;
840
      nerrors++;
841
    } else {
842
 
843
      V729_decode(CAEN_V729_param.mask,CAEN_V729_param.nall,data,adc, CAEN_V729_param.verbose);
844
 
845
#ifdef _CVI_
846
      V729_plot(!i, adc );
847
#endif _CVI_
848
 
849
      if (output) {
850
        if (Random(0,1)<CAEN_V729_param.fraction) {
851
          nball += fwrite(adc  ,   1, sizeof(uint16_t)*len, fp);
852
        }
853
      }
854
      ncount++;
855
    }
856
    time(&t);
857
#ifdef _CVI_
858
    if (t/4!=told ) {
859
      double done=0;
860
      if (CAEN_V729_param.neve) done= (double) (i)/CAEN_V729_param.neve;
861
      HDrawCB ( 0, 0, EVENT_COMMIT ,NULL, 0,0);
862
 
863
      EstimatedFinish(CAEN_V729_param.panel, P4_PROGRESS, P4_ETA, tstart, done);
864
    }
865
#endif _CVI_
866
    if (t/4!=told ) printf("%d events in %2.2f min (%d s) TIMEOUTS=%d %s",ncount, (double)(t-tstart)/60.,(int)(t-tstart), nerrors, ctime(&t));
867
    told=t/4;
868
  }
869
#ifdef _CVI_
870
  HDrawCB ( 0, 0, EVENT_COMMIT ,NULL, 0,0);
871
#endif _CVI_
872
  // end
873
  if (output) {
874
    for (ch=0; ch<4; ch++) {
875
      if (  CAEN_V729_param.mask & (1<<ch) ) {
876
        H2DWrite2File(10+ch,fp);
877
        H1DWrite2File(10+ch,fp);
878
      }
879
    }
880
    if (output==1) fclose(fp);
881
  }
882
  CAEN_V729_param.fp = NULL;
883
  free(data);
884
  free(data2);
885
  free(data1);
886
  free(adc);
887
  time(&t);
888
  printf("%d events in %2.2f min  (%d s)  TIMEOUTS=%d  %s\n",ncount, (double)(t-tstart)/60.,(int)(t-tstart), nerrors, ctime(&t));
889
  return 0;
890
}
891
 
892
 
893
 
894
#ifdef V729_MAIN
895
int main(int argc,char **argv) {
896
  if (argc == 1) {
897
 
898
    V729_Usage();
899
    exit (-1);
900
  }
901
  // intercept routine
902
  if (signal (SIGINT, CatchSig) == SIG_ERR)  perror ("sigignore");
903
  VME_START(NULL);
904
  V729_main (argc,argv);
905
  VME_STOP();
906
  return ;
907
}
908
#endif V729_MAIN
909
 
910
#ifdef V729_WINMAIN
911
 
912
int CVICALLBACK HidePanelCB (int panel, int control, int event,
913
                             void *callbackData, int eventData1, int eventData2) {
914
  switch (event) {
915
    case EVENT_COMMIT:
916
      HidePanel (panel);
917
      break;
918
  }
919
  return 0;
920
}
921
 
922
 
923
void SetDimming(int state) {
924
  SetCtrlAttribute (p4h, P4_DAQ, ATTR_DIMMED, state);
925
  SetCtrlAttribute (p4h, P4_STOP, ATTR_DIMMED, !state);
926
}
927
 
928
void CVICALLBACK EndOfThread (int poolHandle, int functionID, unsigned int event, int value, void *callbackData) {
929
 
930
 
931
  SetDimming(0);
932
  printf("End of Thread \n");
933
  return ;
934
 
935
}
936
 
937
static int poolHandle = 0;
938
static int p1h, pID, rID, tfID;
939
#  define MAX_THREADS 10
940
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
941
                       LPSTR lpszCmdLine, int nCmdShow) {
942
  char title[0xFF]="";
943
  int i;
944
  if (InitCVIRTE (hInstance, 0, 0) == 0)
945
    return -1;  /* out of memory */
946
 
947
  SetSleepPolicy(VAL_SLEEP_MORE);
948
  CmtNewThreadPool (MAX_THREADS, &poolHandle);
949
 
950
  VME_START(WIENER_VMEMM);
951
  SetStdioPort (CVI_STDIO_WINDOW );
952
  if ((p4h = V729_LoadPanel (0, "CAEN_V729_CVI.uir", P4)) < 0)
953
    return -1;
954
 
955
  GetPanelAttribute (p4h, ATTR_TITLE, title );
956
 
957
  SetPanelAttribute (p4h, ATTR_TITLE, title );
958
 
959
 
960
  DisplayPanel (p4h);
961
 
962
  InitColors();
963
 
964
  do {
965
    ThreadFunctionPtr mythread = NULL;
966
    GetUserEvent (1, &pID, &rID);
967
 
968
 
969
    if (pID ==p4h && rID == P4_DAQ            ) mythread=V729_daq;
970
    if (mythread!=NULL) {
971
      printf("New Thread panel=%d button=%d\n", pID, rID);
972
      ctrl_c=0;
973
 
974
 
975
      CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &rID,
976
                                        DEFAULT_THREAD_PRIORITY,
977
                                        EndOfThread,
978
                                        EVENT_TP_THREAD_FUNCTION_END,
979
                                        NULL, RUN_IN_SCHEDULED_THREAD,
980
                                        &tfID);
981
      SetDimming(1);
982
    }
983
 
984
    if ( pID==p4h && rID == P4_STOP ) {
985
      printf("Stopping the  Thread %d\n", tfID);
986
      ctrl_c=1;
987
    }
988
    if ( pID==p4h && rID == P4_HIDE ) {
989
      printf("Exit\n");
990
      break;
991
    }
992
  } while (1);
993
 
994
  CmtDiscardThreadPool (poolHandle);
995
  DiscardPanel (p4h);
996
 
997
  VME_STOP();
998
  return 0;
999
}
1000
#endif V729_WINMAIN