Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 264 | f9daq | 1 | //#define XXDLL |
| 2 | //#include "vme.h" |
||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | typedef unsigned short ADDRESS_MODIFIER; |
||
| 7 | #define Std_NoPriv_Data (ADDRESS_MODIFIER)0x39 |
||
| 8 | |||
| 9 | #include "wusbvme_dll.h" |
||
| 10 | #include "toolbox.h" |
||
| 11 | #include <stdio.h> |
||
| 12 | #include <stdlib.h> |
||
| 13 | #include <string.h> |
||
| 14 | |||
| 15 | |||
| 16 | #include <errno.h> |
||
| 17 | #include <ctype.h> |
||
| 18 | #include <time.h> |
||
| 19 | |||
| 20 | |||
| 21 | #include <signal.h> |
||
| 22 | |||
| 23 | |||
| 24 | #include "CAENV965_DEF.h" |
||
| 25 | |||
| 26 | #include "petdemo_uir.h" |
||
| 27 | #include "daq.h" |
||
| 28 | |||
| 29 | extern int p1; |
||
| 30 | extern int ctrl_c; |
||
| 31 | |||
| 32 | |||
| 33 | #define VERSION 1.0 |
||
| 34 | #define TIMEOUT 3 |
||
| 35 | |||
| 36 | int *fThreshold = NULL; |
||
| 37 | |||
| 38 | /* VME modules */ |
||
| 39 | |||
| 40 | #define CAEN_V792 0x340000 // IJS V792 |
||
| 41 | #define CAEN_V792_1 0x530000 // FMF1 V792 |
||
| 42 | #define CAEN_V792_2 0x630000 // FMF2 V792 |
||
| 43 | #define CAEN_V965 0x350000 // IJS V965 |
||
| 44 | int addr[3]= {CAEN_V792,CAEN_V792_1,CAEN_V965 }; |
||
| 45 | const int nadc=2; |
||
| 46 | #define BUFF_L 2048 |
||
| 47 | static int stackwrite[10000]; |
||
| 48 | static int stackdata[10000],stackdump[27000]; |
||
| 49 | |||
| 50 | |||
| 51 | /************************************************/ |
||
| 52 | int weight_while(int num) { |
||
| 53 | int i, tmp; |
||
| 54 | |||
| 55 | for ( i =0; i <num; i++ ) tmp = 0; |
||
| 56 | return 0; |
||
| 57 | } |
||
| 58 | #define WWHILE weight_while(0) |
||
| 59 | |||
| 60 | #define TRUE 1 |
||
| 61 | #define FALSE 0 |
||
| 62 | |||
| 63 | int timer_out; |
||
| 64 | |||
| 65 | void timerast (int signumber) { |
||
| 66 | timer_out = TRUE; |
||
| 67 | fprintf(stderr,"TIMEOUT !!!\n"); |
||
| 68 | } |
||
| 69 | |||
| 70 | void tmlnk (int tout) { |
||
| 71 | |||
| 72 | } |
||
| 73 | |||
| 74 | void tmulk () { |
||
| 75 | |||
| 76 | } |
||
| 77 | |||
| 78 | int fexist( char *path) { |
||
| 79 | |||
| 80 | |||
| 81 | if(!path || !*path) return 0; |
||
| 82 | ssize_t fileSize; |
||
| 83 | int res = FileExists (path, &fileSize); |
||
| 84 | |||
| 85 | |||
| 86 | if (res>0) { |
||
| 87 | return 1; |
||
| 88 | } else { |
||
| 89 | return 0; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | int init() { |
||
| 97 | |||
| 98 | xxusb_register_write(WUSB_udev,1,0x0); // Stop DAQ mode |
||
| 99 | while (xxusb_usbfifo_read(WUSB_udev,(int *) stackdump,BUFF_L,100)>0); |
||
| 100 | |||
| 101 | |||
| 102 | int rate=1000; |
||
| 103 | int freq=80000000/rate-40; // 80 MHz |
||
| 104 | if (freq<72) freq=72; |
||
| 105 | // Set DGG channel A as a pulser, output on O1, |
||
| 106 | // with delay =500 x 12.5ns, |
||
| 107 | // and width = 500 x 12.5ns, |
||
| 108 | // not latching or inverting |
||
| 109 | // VME_DGG(udev,0,6,0,24000,6000,0,0); |
||
| 110 | VME_DGG(WUSB_udev,0,6,0,freq,40,0,0); |
||
| 111 | // Set DGG channel B to trigger on NIM1, output on O2, |
||
| 112 | // with delay =200 x 12.5ns, |
||
| 113 | // and width = 200 x 12.5ns, |
||
| 114 | // not latching or inverting |
||
| 115 | VME_DGG(WUSB_udev,1,1,1,0,10,0,1); |
||
| 116 | printf("CAEN V965 Pedestal set to %d\n", fPedestal); |
||
| 117 | |||
| 118 | // INIT stackdata |
||
| 119 | int fPedestal=255; |
||
| 120 | WIENER_VMUSB_StackInit(); |
||
| 121 | |||
| 122 | for (int i=0; i<nadc; i++) { |
||
| 123 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_CRN , 0x0); |
||
| 124 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_GEO , i); |
||
| 125 | WIENER_VMUSB_StackReadA24D16(addr[i] + CAENV965_GEO); |
||
| 126 | for (int j=0; j<32; j++) { |
||
| 127 | WIENER_VMUSB_StackWriteA24D16(addr[i] + CAENV965_THM + 0x02*j, fThreshold[j+i*32]); // threshold/kill for 32 channels |
||
| 128 | } |
||
| 129 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS1, 0x80 ); // soft reset |
||
| 130 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BC1, 0x80 ); // soft reset |
||
| 131 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_PED, fPedestal ); // pedestal |
||
| 132 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS2,0x5000); |
||
| 133 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS2,0x4); // clear module |
||
| 134 | WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BC2,0x4); |
||
| 135 | } |
||
| 136 | WIENER_VMUSB_StackMarker(0xFAFC); |
||
| 137 | //WIENER_VMUSB_StackPrint(); |
||
| 138 | |||
| 139 | |||
| 140 | VME_LED_settings(WUSB_udev, 0,0,0,0); // Set Yellow LED to light with with USB out buffer not empty |
||
| 141 | VME_LED_settings(WUSB_udev, 1,1,0,0); // Set Red LED to light with NIM1 |
||
| 142 | VME_LED_settings(WUSB_udev,2,0,0,0); // Set Green LED to light when stack is not empty |
||
| 143 | |||
| 144 | unsigned int vmereg; |
||
| 145 | VME_register_read(WUSB_udev,0x00,&vmereg); |
||
| 146 | printf("VMUSB Firmware ID -> 0x%08X\n",vmereg); |
||
| 147 | |||
| 148 | VME_register_read(WUSB_udev,0x04,&vmereg); |
||
| 149 | printf("VMUSB Global Mode -> 0x%08X\n",vmereg); |
||
| 150 | |||
| 151 | vmereg=(vmereg&0xF000)|0x0004; |
||
| 152 | VME_register_write(WUSB_udev,0x04,vmereg); |
||
| 153 | VME_register_write(WUSB_udev,0x08,0x00000080); |
||
| 154 | VME_register_write(WUSB_udev,0x28,0x0); |
||
| 155 | VME_register_write(WUSB_udev,0x2C,0x0); |
||
| 156 | VME_register_write(WUSB_udev,0x30,0x0); |
||
| 157 | VME_register_write(WUSB_udev,0x34,0x0); |
||
| 158 | VME_register_write(WUSB_udev,0x3C,0x000); |
||
| 159 | |||
| 160 | int nb = WIENER_VMUSB_StackGetUint32(10000,stackdata); |
||
| 161 | int ret= xxusb_stack_execute(WUSB_udev,(uint32_t *)stackdata); |
||
| 162 | printf("Init::%d ret=%d\n",nb,ret); |
||
| 163 | if (ret>0) for (int k=0; k<ret/2; k++) printf ("stackdata=0x%08X\n",stackdata[k]); |
||
| 164 | |||
| 165 | |||
| 166 | WIENER_VMUSB_StackClear(); |
||
| 167 | WIENER_VMUSB_StackMarker(0xFFAB); |
||
| 168 | for (int j=0; j<36; j++) WIENER_VMUSB_StackReadA24D32(addr[0] + CAENV965_OB); |
||
| 169 | WIENER_VMUSB_StackMarker(0xFAFB); |
||
| 170 | for (int k0=0; k0<nadc; k0++) { |
||
| 171 | WIENER_VMUSB_StackWriteA24D16(addr[k0] + CAENV965_BS2,0x4); // clear module |
||
| 172 | WIENER_VMUSB_StackWriteA24D16(addr[k0] + CAENV965_BC2,0x4); |
||
| 173 | } |
||
| 174 | |||
| 175 | int nb0= WIENER_VMUSB_StackGetUint32(10000,&stackwrite[0]); |
||
| 176 | if (nb0>768) { |
||
| 177 | fprintf(stderr,"nb0=%d > 768 error xxusb_stack_write\n", nb0); |
||
| 178 | exit(-1); |
||
| 179 | } |
||
| 180 | nb =xxusb_stack_write(WUSB_udev,0x2,(uint32_t *) stackwrite); |
||
| 181 | nb0=xxusb_stack_read(WUSB_udev,0x2,(uint32_t *) stackdata); |
||
| 182 | for (int k1=0; k1<stackwrite[0]+1; k1++) { |
||
| 183 | if (stackdata[k1]!=stackwrite[k1]) printf("%d %d init err %x %x\n",nb,nb0, |
||
| 184 | stackwrite[k1], stackdata[k1]); |
||
| 185 | } |
||
| 186 | |||
| 187 | if (fMode==2) xxusb_register_write(WUSB_udev,1,0x1); // Start DAQ mode |
||
| 188 | |||
| 189 | printf("daq::init() \n"); |
||
| 190 | return 0; |
||
| 191 | } |
||
| 192 | |||
| 193 | int vmconnect() { |
||
| 194 | WIENER_VMUSB_VME_START("VM0120"); |
||
| 195 | //VME_START(WIENER_VMUSB); |
||
| 196 | |||
| 197 | printf("daq::connect()\n"); |
||
| 198 | return 0; |
||
| 199 | } |
||
| 200 | |||
| 201 | int vmdisconnect() { |
||
| 202 | /* zakljuci */ |
||
| 203 | WIENER_VMUSB_VME_STOP(); |
||
| 204 | printf("daq::disconnect()\n"); |
||
| 205 | return 0; |
||
| 206 | } |
||
| 207 | |||
| 208 | int clear() { |
||
| 209 | return 0; |
||
| 210 | } |
||
| 211 | |||
| 212 | int module_header(int recid,uint32_t *data,int len) { |
||
| 213 | data[0] = recid; |
||
| 214 | data[1] = (len >0)? len : 0 ; |
||
| 215 | return data[1]+2; |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | int event(unsigned int *data, int maxn, int *ctr, int print) { |
||
| 220 | int tout=200; /* 1/100 of a second */ |
||
| 221 | const int lsize=sizeof(uint32_t); |
||
| 222 | |||
| 223 | |||
| 224 | ctr[0]++; |
||
| 225 | ctr[1]++; |
||
| 226 | |||
| 227 | int count=0; |
||
| 228 | switch (fMode) { |
||
| 229 | case 0: { // normal calls |
||
| 230 | unsigned short clr= 0x4; |
||
| 231 | unsigned int status=0; |
||
| 232 | uint32_t mdata; |
||
| 233 | |||
| 234 | for (int i=0; i<nadc; i++) { |
||
| 235 | // wait for trg |
||
| 236 | tmlnk (tout); |
||
| 237 | do WIENER_VMUSB_VME_A24D16_R( addr[i] + CAENV965_SR1, &status); |
||
| 238 | while ( (status&0x1)==0 && timer_out==0 && ctrl_c==0 ); |
||
| 239 | tmulk(); |
||
| 240 | // readout data |
||
| 241 | if (timer_out) return 0; |
||
| 242 | if (ctrl_c) return 0; |
||
| 243 | int len=0; |
||
| 244 | |||
| 245 | do { |
||
| 246 | WIENER_VMUSB_VME_A24D32_R(addr[i] + CAENV965_OB, &mdata); |
||
| 247 | mdata=data[count++]; |
||
| 248 | len++; |
||
| 249 | } while ( (mdata & 0x4000000)==0 && timer_out==0) ; // bit 26 EOB or not valid datum |
||
| 250 | // clear |
||
| 251 | WIENER_VMUSB_VME_A24D16_W( addr[i] + CAENV965_BS2, &clr); |
||
| 252 | WIENER_VMUSB_VME_A24D16_W( addr[i] + CAENV965_BC2, &clr); |
||
| 253 | |||
| 254 | if (count+2<maxn) { |
||
| 255 | if (print) printf("V965 %3d\n",len); |
||
| 256 | count+=module_header(0x130+i,&data[count],len); |
||
| 257 | ctr[2]++; |
||
| 258 | ctr[3]+=len; |
||
| 259 | } |
||
| 260 | |||
| 261 | timer_out=0; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | break; |
||
| 265 | case 1: { // stack execute |
||
| 266 | WIENER_VMUSB_StackGetUint32(10000,(int *)data); |
||
| 267 | int ret=xxusb_stack_execute(WUSB_udev,(uint32_t *) data); //The first element of the array is the number of bytes. |
||
| 268 | if (ret< 0 ) { |
||
| 269 | printf ("xxusb_stack_execute error err=%d\n",ret); \ |
||
| 270 | count = 0; |
||
| 271 | } else count= ret/lsize; |
||
| 272 | |||
| 273 | } |
||
| 274 | break; |
||
| 275 | case 2: { // stack load |
||
| 276 | int ret=xxusb_usbfifo_read(WUSB_udev,(int *) data,BUFF_L,100); |
||
| 277 | if (ret< 0 ) { |
||
| 278 | if (ret!=-110) { |
||
| 279 | printf ("xxusb_usbfifo_read error err=%d\n",ret); |
||
| 280 | end(); |
||
| 281 | init(); |
||
| 282 | } |
||
| 283 | count = 0; |
||
| 284 | } else { |
||
| 285 | if (debug) { |
||
| 286 | for (int i=0; i<100; i++) { |
||
| 287 | printf ("%4d fifodata=0x%08X\n",i, data[i]); |
||
| 288 | if (data[i]==0xFAFB) break; |
||
| 289 | } |
||
| 290 | /* |
||
| 291 | |||
| 292 | |||
| 293 | 1 fifodata=0x00000049 |
||
| 294 | 2 fifodata=0x0000FFAB |
||
| 295 | 3 fifodata=0x00002000 |
||
| 296 | 4 fifodata=0x00000200 |
||
| 297 | 5 fifodata=0x00004141 |
||
| 298 | 6 fifodata=0x00000000 |
||
| 299 | 7 fifodata=0x00004057 |
||
| 300 | |||
| 301 | 70 fifodata=0x00000400 |
||
| 302 | 71 fifodata=0x00000035 |
||
| 303 | 72 fifodata=0x00000600 |
||
| 304 | 73 fifodata=0x00000035 |
||
| 305 | 74 fifodata=0x0000FAFB |
||
| 306 | |||
| 307 | |||
| 308 | */ |
||
| 309 | } |
||
| 310 | static double t0=0; |
||
| 311 | static int nc =0; |
||
| 312 | nc += data[0]; |
||
| 313 | if (print) { |
||
| 314 | //printf("##------------------ret=%d data[0]=%d\n",ret,(int)data[0]); |
||
| 315 | double t1 = Timer(); |
||
| 316 | double dt = t1-t0; |
||
| 317 | t0 = t1; |
||
| 318 | float rate = (dt>0)?nc/dt:0; |
||
| 319 | |||
| 320 | //printf("--------ret=%d data[0]=%f nc=%d dt=%f\n",ret,rate,nc,dt); |
||
| 321 | PlotStripChart (p1, P1_DAQRATE, &rate, 1, 0, 0,VAL_FLOAT); |
||
| 322 | nc =0; |
||
| 323 | } |
||
| 324 | count= ret/lsize; |
||
| 325 | ctr[2]+=data[0]; |
||
| 326 | ctr[3]+=count; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | return count*lsize; |
||
| 332 | } |
||
| 333 | |||
| 334 | int end() { |
||
| 335 | |||
| 336 | xxusb_register_write(WUSB_udev,1,0x0); // Stop DAQ mode |
||
| 337 | while (xxusb_usbfifo_read(WUSB_udev,(int *) stackdata,BUFF_L,30)>0); |
||
| 338 | printf("daq::end()\n"); |
||
| 339 | return 0; |
||
| 340 | } |
||
| 341 | |||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | int vmacquire (int neve,FILE *fp, char *fpedname) { |
||
| 346 | // neve .... negative argument time ( in s )limited event loop |
||
| 347 | if (fThreshold== NULL) fThreshold = (int *) malloc (sizeof(int)*128); |
||
| 348 | // print welcome message |
||
| 349 | time_t t, told; |
||
| 350 | double tstart, tstop; |
||
| 351 | time(&t); |
||
| 352 | fThresholdEnable=0; |
||
| 353 | |||
| 354 | #define BSIZE 10000 |
||
| 355 | uint32_t data[10000]; |
||
| 356 | |||
| 357 | fMode = 2; |
||
| 358 | fPedestal=255; |
||
| 359 | for (int ki=0; ki<128; ki++) { |
||
| 360 | if (ki<72) fThreshold[ki]=0; |
||
| 361 | else fThreshold[ki]=0x1<<8; // samo 4 kanali na zadnjem modulu so enablani |
||
| 362 | } |
||
| 363 | fThresholdEnable=0; |
||
| 364 | fStop=0; |
||
| 365 | |||
| 366 | |||
| 367 | |||
| 368 | if (fpedname !=NULL) { |
||
| 369 | |||
| 370 | FILE *fped=fopen(fpedname,"r"); |
||
| 371 | |||
| 372 | int j=0; |
||
| 373 | int ndim=400; |
||
| 374 | char line[ndim]; |
||
| 375 | int val=0; |
||
| 376 | while (fgets(line,ndim,fped)!=NULL) { |
||
| 377 | sscanf(line,"%d",&val); |
||
| 378 | fThreshold[j++]=val; |
||
| 379 | } |
||
| 380 | fThresholdEnable=1; |
||
| 381 | fclose(fped); |
||
| 382 | |||
| 383 | } |
||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | init(); |
||
| 388 | |||
| 389 | |||
| 390 | int hdr[4]= {2}; // recid |
||
| 391 | int i=0; |
||
| 392 | int ntotal=0; |
||
| 393 | int counters[30]= {0,0,0,0,0, 0,0,0,0,0,0,0}; |
||
| 394 | char names[10][20]= {"TRG","CAEN V965"}; |
||
| 395 | time(&t); |
||
| 396 | tstart=Timer(); |
||
| 397 | told =t; |
||
| 398 | tstop=tstart+360000; |
||
| 399 | if (neve<-1) { |
||
| 400 | tstop=tstart-neve; |
||
| 401 | neve=-1; |
||
| 402 | } |
||
| 403 | for (i=0; i!=neve && !ctrl_c && Timer()<tstop; i++) { |
||
| 404 | time(&t); |
||
| 405 | if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(Timer()-tstart)/60., ctime(&t)); |
||
| 406 | int nb=event(data,BSIZE, counters,t!=told); |
||
| 407 | analyse(nb, data); |
||
| 408 | if (nb>0) { |
||
| 409 | // zapis v datoteko |
||
| 410 | hdr[1]=nb+4*sizeof(int); |
||
| 411 | hdr[2]=time(NULL); |
||
| 412 | hdr[3]=i; |
||
| 413 | |||
| 414 | fwrite(hdr, 1, sizeof(int)*4, fp); //gzip |
||
| 415 | ntotal += fwrite(data,1, nb, fp); |
||
| 416 | told=t; |
||
| 417 | } else i--; |
||
| 418 | } |
||
| 419 | |||
| 420 | end(); |
||
| 421 | |||
| 422 | printf("Number of Events: %d\n",i); |
||
| 423 | if (ctrl_c) printf("User Program termination CTRL-C\n"); |
||
| 424 | if (Timer()>tstop ) printf("Timeout termination tstart# t>tstop: %d# %d >%d\n",(int)t, (int)tstart, (int) tstop); |
||
| 425 | |||
| 426 | |||
| 427 | printf("%d bytes written to file\nCounts:\n", (int) (ntotal*sizeof(int))); |
||
| 428 | for (i=0; i<2; i++) printf("%s\t%d\t%d\n",names[i],counters[2*i],counters[2*i+1]) ; |
||
| 429 | |||
| 430 | |||
| 431 | return 0; |
||
| 432 | } |
||
| 433 |