AlcapDAQ  1
v1724_module.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include <unistd.h>
5 #include <sys/io.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <string.h>
11 
12 #include "midas.h"
13 
14 #include "crate.h"
15 #include "diag.h"
16 
17 // CAEN includes
18 #include "CAENDigitizer.h"
19 
20 #define NBOARDS 1 /* Number of connected boards */
21 static int handle[NBOARDS];
22 //static uint32_t V1724_BASE[NBOARDS] = {0x32100000};
23 static char *caen_data_buffer[NBOARDS]; // data buffers used by CAEN
24 static uint32_t caen_data_buffer_size[NBOARDS];
25 static char *data_buffer[NBOARDS]; // data buffers used by MIDAS
26 static uint32_t data_buffer_size[NBOARDS];
27 static uint32_t data_size[NBOARDS];
28 
29 extern HNDLE hDB;
30 
31 static INT v1724_init();
32 static void v1724_exit();
33 static INT v1724_pre_bor();
34 static INT v1724_eor();
35 static INT v1724_poll_live();
36 static INT v1724_read(char *pevent); // MIDAS readout routine
37 static void v1724_readout(); // read data from digitizer buffers
38 
39 bool block_sig;
40 typedef struct timespec timer_start;
41 
43  v1724_init, // init
44  v1724_exit, // exit
45  v1724_pre_bor, // pre_bor
46  NULL, // bor
47  v1724_eor, // eor
48  v1724_poll_live, // poll_live
49  NULL, // poll_dead
50  NULL, // start_block
51  NULL, // stop_block
52  v1724_read // read
53 };
54 
55 // ======================================================================
56 // ODB structures
57 // ======================================================================
58 
59 // board settings
60 typedef struct s_v1724_odb
61 {
63  BOOL enabled;
68  BYTE trigger_edge;
70  char ROC_FirmwareRel[128];
71  char AMC_FirmwareRel[128];
72  struct {
73  BOOL enabled;
77  } ch[8];
78 
80 
82 
83 #define S_V1724_ODB_STR "\
84 [.]\n\
85 VME base = DWORD : 0x32100000\n\
86 enabled = BOOL : y\n\
87 waveform length = DWORD : 32\n\
88 acquisition mode = BYTE : 1\n\
89 software trigger mode = BYTE : 0\n\
90 hardware trigger mode = BYTE : 0\n\
91 trigger edge (0-ris 1-fal) = BYTE : 1\n\
92 down sampling factor = INT : 1\n\
93 ROC firmware = STRING : [128] -\n\
94 AMC firmware = STRING : [128] -\n\
95 \n\
96 [Ch0]\n\
97 enabled = BOOL : y\n\
98 DC offset = DWORD : 0\n\
99 self-trigger mode = BYTE : 0\n\
100 trigger threshhold = DWORD : 0\n\
101 \n\
102 [Ch1]\n\
103 enabled = BOOL : y\n\
104 DC offset = DWORD : 0\n\
105 self-trigger mode = BYTE : 1\n\
106 trigger threshhold = DWORD : 0\n\
107 \n\
108 [Ch2]\n\
109 enabled = BOOL : y\n\
110 DC offset = DWORD : 0\n\
111 self-trigger mode = BYTE : 1\n\
112 trigger threshhold = DWORD : 0\n\
113 \n\
114 [Ch3]\n\
115 enabled = BOOL : y\n\
116 DC offset = DWORD : 0\n\
117 self-trigger mode = BYTE : 1\n\
118 trigger threshhold = DWORD : 0\n\
119 \n\
120 [Ch4]\n\
121 enabled = BOOL : y\n\
122 DC offset = DWORD : 0\n\
123 self-trigger mode = BYTE : 1\n\
124 trigger threshhold = DWORD : 0\n\
125 \n\
126 [Ch5]\n\
127 enabled = BOOL : y\n\
128 DC offset = DWORD : 0\n\
129 self-trigger mode = BYTE : 1\n\
130 trigger threshhold = DWORD : 0\n\
131 \n\
132 [Ch6]\n\
133 enabled = BOOL : y\n\
134 DC offset = DWORD : 0\n\
135 self-trigger mode = BYTE : 1\n\
136 trigger threshhold = DWORD : 0\n\
137 \n\
138 [Ch7]\n\
139 enabled = BOOL : y\n\
140 DC offset = DWORD : 0\n\
141 self-trigger mode = BYTE : 1\n\
142 trigger threshhold = DWORD : 0\n\
143 "
144 
146 {
147 
148  /* ODB */
149  char str[1024];
150  HNDLE hKey;
151  for (int iboard=0; iboard<NBOARDS; iboard++)
152  {
153  sprintf(str,"/Equipment/Crate %i/Settings/CAEN%i",crate_number,iboard);
154  int status = db_check_record(hDB,0,str,S_V1724_ODB_STR,FALSE);
155  if(status==DB_NO_KEY || status==DB_STRUCT_MISMATCH)
156  {
157  status = db_create_record(hDB,0,str,S_V1724_ODB_STR);
158  printf("Creating key with status: %d \n",status);
159  }
160  else if (status==DB_INVALID_HANDLE)
161  {
162  printf("Error: Cannot connect to database to setup CAEN V1724");
163  return FE_ERR_HW;
164  }
165 
166 
167  status = db_find_key(hDB,0,str,&hKey);
168  status = db_open_record(hDB, hKey, &(S_V1724_ODB[iboard]), sizeof(S_V1724_ODB[0]), MODE_READ, NULL, NULL);
169  }
170 
171  printf("Opening CAEN VME interface ...");
172  fflush(stdout);
173 
175  CAEN_DGTZ_BoardInfo_t BoardInfo[NBOARDS];
176  uint32_t data;
177 
178  for (int iboard=0; iboard<NBOARDS; iboard++)
179  {
180 
181  if ( !S_V1724_ODB[iboard].enabled ) continue;
182 
183 #if 0
184  ret = CAEN_DGTZ_OpenDigitizer(CAEN_DGTZ_USB,0,0,S_V1724_ODB[iboard].vme_base,&handle[iboard]);
185 #else
186  ret = CAEN_DGTZ_OpenDigitizer(CAEN_DGTZ_PCIE_OpticalLink,0,0,S_V1724_ODB[iboard].vme_base,&handle[iboard]);
187 #endif
188  if(ret != CAEN_DGTZ_Success)
189  {
190  cm_msg(MERROR,"v1724_init","Can't open digitizer at address 0x%08x\n",S_V1724_ODB[iboard].vme_base);
191  return FE_ERR_HW;
192  }
193 
194  ret = CAEN_DGTZ_GetInfo(handle[iboard], &(BoardInfo[iboard]));
195  if ( ret != CAEN_DGTZ_Success )
196  {
197  cm_msg(MERROR,"v1724_init","Cannot get board info. Error 0x%08x\n",ret);
198  return FE_ERR_HW;
199  }
200  printf("\nConnected to CAEN Digitizer Model %s, recognized as board %d\n", BoardInfo[iboard].ModelName, iboard);
201  printf("\tROC FPGA Release is %s\n", BoardInfo[iboard].ROC_FirmwareRel);
202  printf("\tAMC FPGA Release is %s\n", BoardInfo[iboard].AMC_FirmwareRel);
203 
204  ret = CAEN_DGTZ_Reset(handle[iboard]); /* Reset Digitizer */
205  if ( ret != CAEN_DGTZ_Success )
206  {
207  cm_msg(MERROR,"v1724_init","Cannot reset the board. Error 0x%08x\n",ret);
208  return FE_ERR_HW;
209  }
210 
211  ret = CAEN_DGTZ_GetInfo(handle[iboard], &(BoardInfo[iboard]));
212  if ( ret != CAEN_DGTZ_Success )
213  {
214  cm_msg(MERROR,"v1724_init","Cannot get board info. Error 0x%08x\n",ret);
215  return FE_ERR_HW;
216  }
217 
218 
219  // =====================================================================================
220  // Setup 1024 data buffers (max.)
221  // =====================================================================================
222  ret = CAEN_DGTZ_WriteRegister(handle[iboard], 0x800C, 0xA);
223  if ( ret != CAEN_DGTZ_Success )
224  {
225  cm_msg(MERROR,"v1724_init","Cannot configure data buffers. Error 0x%08x\n",ret);
226  return FE_ERR_HW;
227  }
228 
229 
230  // =====================================================================================
231  // Configure max. number of buffers for BLT readout
232  // =====================================================================================
233  ret = CAEN_DGTZ_SetMaxNumEventsBLT(handle[iboard],1024);
234  if ( ret != CAEN_DGTZ_Success )
235  {
236  cm_msg(MERROR,"v1724_init","Cannot configure data buffers for BLT readout. Error 0x%08x\n",ret);
237  return FE_ERR_HW;
238  }
239 
240  // =====================================================================================
241  // Allocate memory buffer to hold data received from digitizer
242  // =====================================================================================
243  caen_data_buffer[iboard] = NULL;
244  ret = CAEN_DGTZ_MallocReadoutBuffer(handle[iboard], &(caen_data_buffer[iboard]), &(caen_data_buffer_size[iboard]));
245  if ( ret != CAEN_DGTZ_Success )
246  {
247  cm_msg(MERROR,"v1724_init","Cannot MallocReadoutBuffer. Error 0x%08x\n",ret);
248  return FE_ERR_HW;
249  }
250  printf("Readout buffer size: %i\n",caen_data_buffer_size[iboard]);
251 
252 
253  // =====================================================================================
254  // Allocate memory buffer to hold all data from digitizer
255  // =====================================================================================
256  data_buffer_size[iboard] = 0x2000000; // 32 MB
257  data_buffer[iboard] = (char*)malloc(data_buffer_size[iboard]);
258  if ( !data_buffer[iboard] )
259  {
260  cm_msg(MERROR,"v1724_init","Cannot allocate memory for data buffers.\n");
261  return FE_ERR_HW;
262  }
263 
264 #if 0
265  // PW
266  uint32_t mydata;
267  ret = CAEN_DGTZ_ReadRegister(handle[iboard], 0x8100, &mydata);
268  if ( ret != CAEN_DGTZ_Success )
269  {
270  cm_msg(MERROR,"v1724_init","Cannot read register 0x8100. Error 0x%08x\n",ret);
271  return FE_ERR_HW;
272  }
273  printf("Acquisition control register is: %d\n", mydata);
274  mydata |= (1<<4);
275  ret = CAEN_DGTZ_WriteRegister(handle[iboard], 0x8100, mydata);
276  if ( ret != CAEN_DGTZ_Success )
277  {
278  cm_msg(MERROR,"v1724_init","Cannot write register 0x8100. Error 0x%08x\n",ret);
279  return FE_ERR_HW;
280  }
281  ret = CAEN_DGTZ_ReadRegister(handle[iboard], 0x8100, &mydata);
282  if ( ret != CAEN_DGTZ_Success )
283  {
284  cm_msg(MERROR,"v1724_init","Cannot read register 0x8100. Error 0x%08x\n",ret);
285  return FE_ERR_HW;
286  }
287  printf("2nd time Acquisition control register is: %d\n", mydata);
288 
290  if ( ret != CAEN_DGTZ_Success )
291  {
292  cm_msg(MERROR,"v1724_init","Cannot read register 0x%04x. Error 0x%08x\n",CAEN_DGTZ_DOWNSAMPLE_FACT_ADD, ret);
293  return FE_ERR_HW;
294  }
295  printf("The down sampling factor currently is: 0x%08x\n", mydata);
296  mydata = 10;
298  if ( ret != CAEN_DGTZ_Success )
299  {
300  cm_msg(MERROR,"v1724_init","Cannot write register 0x8128. Error 0x%08x\n",ret);
301  return FE_ERR_HW;
302  }
304  if ( ret != CAEN_DGTZ_Success )
305  {
306  cm_msg(MERROR,"v1724_init","2: Cannot read register 0x%04x. Error 0x%08x\n", CAEN_DGTZ_DOWNSAMPLE_FACT_ADD,ret);
307  return FE_ERR_HW;
308  }
309  printf("The down sampling factor currently is: 0x%08x\n", mydata);
310  // end PW
311 #endif
312 
313  }
314 
315  printf(" [done]\n");
316 
317  return SUCCESS;
318 }
319 
321 {
322 
324  for (int iboard=0; iboard<NBOARDS; iboard++)
325  {
326 
327  if ( !S_V1724_ODB[iboard].enabled ) continue;
328 
329  // =====================================================================================
330  // Free memory allocated for data buffer
331  // =====================================================================================
333  if ( ret != CAEN_DGTZ_Success )
334  {
335  cm_msg(MERROR,"v1724_exit","Cannot FreeReadoutBuffer. Error 0x%08x\n",ret);
336  }
337 
338  // =====================================================================================
339  // Close digitizer
340  // =====================================================================================
341  ret = CAEN_DGTZ_CloseDigitizer(handle[iboard]);
342  if ( ret != CAEN_DGTZ_Success )
343  {
344  cm_msg(MERROR,"v1724_exit","Cannot CloseDigitizer. Error 0x%08x\n",ret);
345  }
346 
347  // =====================================================================================
348  // Free memory buffers
349  // =====================================================================================
350  free( data_buffer[iboard] );
351  }
352 }
353 
355 {
356 
358  for (int iboard=0; iboard<NBOARDS; iboard++)
359  {
360 
361  if ( !S_V1724_ODB[iboard].enabled ) continue;
362 
363  // ==========================================================================================
364  // Reset digitizer
365  // ==========================================================================================
366  ret = CAEN_DGTZ_Reset(handle[iboard]); /* Reset Digitizer */
367  if ( ret != CAEN_DGTZ_Success )
368  {
369  cm_msg(MERROR,"v1724_init","Cannot reset the board. Error 0x%08x\n",ret);
370  return FE_ERR_HW;
371  }
372 
373  // =====================================================================================
374  // Enable channels
375  // =====================================================================================
376  uint32_t channel_mask = 0x0; // disable all channels
377  for (uint32_t ichan=0; ichan<8; ichan++)
378  {
379  if ( S_V1724_ODB[iboard].ch[ichan].enabled )
380  channel_mask |= (1 << ichan); // enable channel ichannel
381  }
382  printf("channel enable mask: 0x%08x\n",channel_mask);
383  ret = CAEN_DGTZ_SetChannelEnableMask(handle[iboard],channel_mask); // enable all channels
384  if ( ret != CAEN_DGTZ_Success )
385  {
386  cm_msg(MERROR,"v1724_init","Cannot Enable channels. Error 0x%08x\n",ret);
387  return FE_ERR_HW;
388  }
389 
390  // =====================================================================================
391  // Acquisition control
392  // =====================================================================================
393  //ret = CAEN_DGTZ_SetAcquisitionMode(handle[iboard],CAEN_DGTZ_S_IN_CONTROLLED);
394  ret = CAEN_DGTZ_SetAcquisitionMode(handle[iboard], (CAEN_DGTZ_AcqMode_t)S_V1724_ODB[iboard].acquisition_mode);
395  if ( ret != CAEN_DGTZ_Success )
396  {
397  cm_msg(MERROR,"v1724_init","Cannot configure Acquisition Control. Error 0x%08x\n",ret);
398  return FE_ERR_HW;
399  }
400 
401 
402 
403  // =====================================================================================
404  // Record length
405  // =====================================================================================
406  ret = CAEN_DGTZ_SetRecordLength(handle[iboard], S_V1724_ODB[iboard].wf_length);
407  //ret = CAEN_DGTZ_SetRecordLength(handle[iboard], 4096);
408  if ( ret != CAEN_DGTZ_Success )
409  {
410  cm_msg(MERROR,"v1724_init","Cannot SetRecordLength. Error 0x%08x\n",ret);
411  return FE_ERR_HW;
412  }
413 
414  // =====================================================================================
415  // Self-trigger mode
416  // =====================================================================================
417 
418  // CAEN_DGTZ_TRGMODE_DISABLED
419  uint32_t self_trigger_mask = 0x0; // disable all channels
420  for (uint32_t ichan=0; ichan<8; ichan++)
421  {
422  if ( S_V1724_ODB[iboard].ch[ichan].self_trigger_mode == CAEN_DGTZ_TRGMODE_DISABLED )
423  self_trigger_mask |= (1 << ichan); // enable channel ichannel
424  }
425  printf("self-trigger DISABLED mask: 0x%08x\n",channel_mask);
426  ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[iboard], CAEN_DGTZ_TRGMODE_ACQ_ONLY, self_trigger_mask);
427  if ( ret != CAEN_DGTZ_Success )
428  {
429  cm_msg(MERROR,"v1724_init","Cannot SetCahnngelSelfTrigger. Error 0x%08x\n",ret);
430  return FE_ERR_HW;
431  }
432 
433  // CAEN_DGTZ_TRGMODE_ACQ_ONLY
434  self_trigger_mask = 0x0; // disable all channels
435  for (uint32_t ichan=0; ichan<8; ichan++)
436  {
437  if ( S_V1724_ODB[iboard].ch[ichan].self_trigger_mode == CAEN_DGTZ_TRGMODE_ACQ_ONLY )
438  self_trigger_mask |= (1 << ichan); // enable channel ichannel
439  }
440  printf("self-trigger ACQ_ONLY mask: 0x%08x\n",channel_mask);
441  ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[iboard], CAEN_DGTZ_TRGMODE_ACQ_ONLY, self_trigger_mask);
442  if ( ret != CAEN_DGTZ_Success )
443  {
444  cm_msg(MERROR,"v1724_init","Cannot SetCahnngelSelfTrigger. Error 0x%08x\n",ret);
445  return FE_ERR_HW;
446  }
447 
448 
449  // CAEN_DGTZ_TRGMODE_EXTOUT_ONLY
450  self_trigger_mask = 0x0; // disable all channels
451  for (uint32_t ichan=0; ichan<8; ichan++)
452  {
453  if ( S_V1724_ODB[iboard].ch[ichan].self_trigger_mode == CAEN_DGTZ_TRGMODE_EXTOUT_ONLY )
454  self_trigger_mask |= (1 << ichan); // enable channel ichannel
455  }
456  printf("self-trigger EXTOUT_ONLY mask: 0x%08x\n",channel_mask);
457  ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[iboard], CAEN_DGTZ_TRGMODE_EXTOUT_ONLY, self_trigger_mask);
458  if ( ret != CAEN_DGTZ_Success )
459  {
460  cm_msg(MERROR,"v1724_init","Cannot SetCahnngelSelfTrigger. Error 0x%08x\n",ret);
461  return FE_ERR_HW;
462  }
463 
464 
465  // CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT
466  self_trigger_mask = 0x0; // disable all channels
467  for (uint32_t ichan=0; ichan<8; ichan++)
468  {
469  if ( S_V1724_ODB[iboard].ch[ichan].self_trigger_mode == CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT )
470  self_trigger_mask |= (1 << ichan); // enable channel ichannel
471  }
472  printf("self-trigger ACQ_AND_EXTOUT mask: 0x%08x\n",channel_mask);
473  ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[iboard], CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT, self_trigger_mask);
474  if ( ret != CAEN_DGTZ_Success )
475  {
476  cm_msg(MERROR,"v1724_init","Cannot SetCahnngelSelfTrigger. Error 0x%08x\n",ret);
477  return FE_ERR_HW;
478  }
479 
480 
481  // =====================================================================================
482  // Trigger polarity
483  // =====================================================================================
484  int trigger_edge = S_V1724_ODB[iboard].trigger_edge;
485  if ( trigger_edge == 0 )
487  else
489  if ( ret != CAEN_DGTZ_Success )
490  {
491  cm_msg(MERROR,"v1724_init","Cannot SetTriggerPolarity. Error 0x%08x\n",ret);
492  return FE_ERR_HW;
493  }
494 
495  // =====================================================================================
496  // Trigger threshold
497  // =====================================================================================
498  for (uint32_t ichan=0; ichan<8; ichan++)
499  {
500  ret = CAEN_DGTZ_SetChannelTriggerThreshold(handle[iboard], ichan, S_V1724_ODB[iboard].ch[ichan].trigger_threshold);
501  if ( ret != CAEN_DGTZ_Success )
502  {
503  cm_msg(MERROR,"v1724_init","Cannot SetChannelTriggerThreshold. Error 0x%08x\n",ret);
504  return FE_ERR_HW;
505  }
506  }
507 
508 #if 0
509  // =====================================================================================
510  // Number of samples over threshold for trigger generation
511  // =====================================================================================
512  for (uint32_t ichan=0; ichan<8; ichan++)
513  {
514  uint32_t addr = V1724_BASE[iboard]+0x1084 + 0x100*ichan;
515  CAEN_DGTZ_WriteRegister(handle[iboard], addr, 1);
516  if ( ret != CAEN_DGTZ_Success )
517  {
518  cm_msg(MERROR,"v1724_init","Cannot Set the number of samples over threshold for trigger. Error 0x%08x\n",ret);
519  return FE_ERR_HW;
520  }
521  }
522 #endif
523 
524 #if 1
525  // =====================================================================================
526  // Set post-trigger size
527  // =====================================================================================
528  ret = CAEN_DGTZ_SetPostTriggerSize(handle[iboard],80);
529  if ( ret != CAEN_DGTZ_Success )
530  {
531  cm_msg(MERROR,"v1724_init","Cannot SetPostTriggerSize. Error 0x%08x\n",ret);
532  return FE_ERR_HW;
533  }
534 #endif
535 
536  // =====================================================================================
537  // DC offset
538  // =====================================================================================
539  for (uint32_t ichan=0; ichan<8; ichan++)
540  {
541  ret = CAEN_DGTZ_SetChannelDCOffset(handle[iboard], ichan, S_V1724_ODB[iboard].ch[ichan].offset);
542  if ( ret != CAEN_DGTZ_Success )
543  {
544  cm_msg(MERROR,"v1724_init","Cannot SetChannelDCOffset. Error 0x%08x\n",ret);
545  return FE_ERR_HW;
546  }
547  }
548 
549 #if 0
550  // =====================================================================================
551  // Pulse polarity
552  // =====================================================================================
553  for (uint32_t ichan=0; ichan<8; ichan++)
554  {
555  //ret = CAEN_DGTZ_SetChannelPulsePolarity(handle[iboard], ichan, CAEN_DGTZ_PulsePolarityPositive);
556  //ret = CAEN_DGTZ_SetChannelPulsePolarity(handle[iboard], ichan, CAEN_DGTZ_PulsePolarityNegative);
557  ret = CAEN_DGTZ_SetChannelPulsePolarity(handle[iboard], ichan, (CAEN_DGTZ_PulsePolarity_t)S_V1724_ODB[iboard].ch[ichan].polarity);
558  if ( ret != CAEN_DGTZ_Success )
559  {
560  cm_msg(MERROR,"v1724_init","Cannot SetChannelPulsePolarity. Error 0x%08x\n",ret);
561  return FE_ERR_HW;
562  }
563  }
564 #endif
565 
566 #if 0
567  // =====================================================================================
568  // Set Zero suppresion parameters
569  // =====================================================================================
570  for (uint32_t ichan=0; ichan<8; ichan++)
571  {
572  ret = CAEN_DGTZ_SetChannelZSParams(handle[iboard], ichan, CAEN_DGTZ_ZS_FINE, 20, 4);
573  if ( ret != CAEN_DGTZ_Success )
574  {
575  cm_msg(MERROR,"v1724_init","Cannot SetChannelZSParams. Error 0x%08x\n",ret);
576  return FE_ERR_HW;
577  }
578  }
579 
580 
581  // =====================================================================================
582  // Zero suppresion mode
583  // =====================================================================================
585  if ( ret != CAEN_DGTZ_Success )
586  {
587  cm_msg(MERROR,"v1724_init","Cannot SetZeroSuppressionMode. Error 0x%08x\n",ret);
588  return FE_ERR_HW;
589  }
590 #endif
591 
592  // =====================================================================================
593  // Clear digitizer data buffers
594  // =====================================================================================
595  ret = CAEN_DGTZ_ClearData( handle[iboard] );
596  if ( ret != CAEN_DGTZ_Success )
597  {
598  cm_msg(MERROR,"v1724_init","Cannot clear CAEN data buffers. Error 0x%08x\n",ret);
599  return FE_ERR_HW;
600  }
601 
602  // =====================================================================================
603  // Enable acquisition
604  // =====================================================================================
605  ret = CAEN_DGTZ_SWStartAcquisition(handle[iboard]);
606  if ( ret != CAEN_DGTZ_Success )
607  {
608  cm_msg(MERROR,"v1724_init","Cannot CAEN_DGTZ_SWStartAcquisition. Error 0x%08x\n",ret);
609  return FE_ERR_HW;
610  }
611 
612  data_size[iboard] = 0;
613 
614 
615  // ======================================================================================
616  // Channel configuration through register 0x8000=CAEN_DGTZ_BROAD_CH_CTRL_ADD
617  // ======================================================================================
618  uint32_t data;
620  if ( ret != CAEN_DGTZ_Success )
621  {
622  cm_msg(MERROR,"v1724_init","Cannot read from register 0x8000. Error 0x%08x\n",ret);
623  return FE_ERR_HW;
624  }
625 
626  // enable trigger overlap
627  data |= (1<<1);
628  printf("channel configuration register 0x8000: %0x08x\n",data);
630  if ( ret != CAEN_DGTZ_Success )
631  {
632  cm_msg(MERROR,"v1724_init","Cannot write to register 0x8000. Error 0x%08x\n",ret);
633  return FE_ERR_HW;
634  }
635 
636  }
637 
638  return SUCCESS;
639 }
640 
642 {
643 
645  for (int iboard=0; iboard<NBOARDS; iboard++)
646  {
647 
648  if ( !S_V1724_ODB[iboard].enabled ) continue;
649 
650  // =====================================================================================
651  // Disable acquisition
652  // =====================================================================================
653  ret = CAEN_DGTZ_SWStopAcquisition(handle[iboard]);
654  if ( ret != CAEN_DGTZ_Success )
655  {
656  cm_msg(MERROR,"v1724_init","Cannot CAEN_DGTZ_SWStopAcquisition. Error 0x%08x\n",ret);
657  }
658  }
659 
660  return SUCCESS;
661 }
662 
663 INT v1724_read(char *pevent)
664 {
665  printf("Read out data from digitizer(s)\n");
666 
667  // =====================================================================================
668  // Read out remaining data from the digitizer
669  // =====================================================================================
670  v1724_readout();
671 
672  // =====================================================================================
673  // Fill MIDAS event
674  // =====================================================================================
675  bk_init32(pevent);
676  char bk_name[80];
677  char *pdata;
678  for (int iboard=0; iboard<NBOARDS; iboard++)
679  {
680 
681  if ( !S_V1724_ODB[iboard].enabled ) continue;
682 
683  sprintf(bk_name, "CDG%i", iboard);
684  bk_create(pevent, bk_name, TID_BYTE, &pdata);
685  if ( data_size[iboard] > MAX_EVENT_SIZE/2 )
686  {
687  cm_msg(MERROR,"v1724_read","Event size is too large. Truncating data...\n");
688  data_size[iboard] = MAX_EVENT_SIZE/2;
689  }
690  memcpy(pdata, data_buffer[iboard], data_size[iboard]);
691  pdata += data_size[iboard];
692  bk_close(pevent, pdata);
693  //printf("board %i data size: %i\n",iboard,data_size[iboard]);
694  // reset data couner for the next event
695  data_size[iboard] = 0;
696  }
697 
698  return SUCCESS;
699 }
700 
702 {
703 
704  CAEN_DGTZ_ErrorCode ret;
705  for (int iboard=0; iboard<NBOARDS; iboard++)
706  {
707  if ( !S_V1724_ODB[iboard].enabled ) continue;
708 
709  uint32_t rdata;
710  ret = CAEN_DGTZ_ReadRegister(handle[iboard], 0x812C, &rdata);
711  //printf("Event Stored: 0x%08x ret = %i\n",rdata, ret);
712  if ( ret != CAEN_DGTZ_Success )
713  {
714  cm_msg(MERROR,"v1724_readout","Cannot read register 0x812C. Error 0x%08x\n",ret);
715  }
716 
717  //ret = CAEN_DGTZ_ReadRegister(handle[iboard], 0x814C, &rdata);
718  //printf("Event size: 0x%08x ret = %i\n",rdata, ret);
719 
720  if ( rdata > 0 )
721  {
722  // =====================================================================================
723  // Read data
724  // =====================================================================================
725  uint32_t caen_data_size;
726  ret = CAEN_DGTZ_ReadData(handle[iboard], CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, caen_data_buffer[iboard], &caen_data_size);
727  if ( ret != CAEN_DGTZ_Success )
728  {
729  cm_msg(MERROR,"v1724_readout","Cannot DGTZ_ReadData. Error 0x%08x\n",ret);
730  }
731  //printf("data size: %i\n", caen_data_size);
732 
733 #if 0
734  uint32_t numEvents;
735  CAEN_DGTZ_GetNumEvents(handle[iboard],caen_data_buffer[iboard],caen_data_size,&numEvents);
736  printf("numEvents: %i\n", numEvents);
737 #endif
738 
739  if ( data_size[iboard] + caen_data_size < data_buffer_size[iboard] )
740  {
741  memcpy( (data_buffer[iboard] + data_size[iboard]), caen_data_buffer[iboard], caen_data_size);
742  data_size[iboard] += caen_data_size;
743  }
744  }
745  }
746 
747 }
748 
750 {
751 
752  v1724_readout();
753 
754  return SUCCESS;
755 }