AlcapDAQ  1
ReadoutTest_Digitizer.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "CAENDigitizer.h"
3 
4 #include "keyb.h"
5 
6 #define CAEN_USE_DIGITIZERS
7 #define IGNORE_DPP_DEPRECATED
8 
9 #define MAXNB 1 /* Number of connected boards */
10 
11 int checkCommand() {
12  int c = 0;
13  if(!kbhit())
14  return 0;
15 
16  c = getch();
17  switch (c) {
18  case 's':
19  return 9;
20  break;
21  case 'k':
22  return 1;
23  break;
24  case 'q':
25  return 2;
26  break;
27  }
28  return 0;
29 }
30 
31 int main(int argc, char* argv[])
32 {
33  /* The following variable is the type returned from most of CAENDigitizer
34  library functions and is used to check if there was an error in function
35  execution. For example:
36  ret = CAEN_DGTZ_some_function(some_args);
37  if(ret) printf("Some error"); */
39 
40  /* The following variable will be used to get an handler for the digitizer. The
41  handler will be used for most of CAENDigitizer functions to identify the board */
42  int handle[MAXNB];
43 
44  CAEN_DGTZ_BoardInfo_t BoardInfo;
45  CAEN_DGTZ_EventInfo_t eventInfo;
46  CAEN_DGTZ_UINT16_EVENT_t *Evt = NULL;
47  char *buffer = NULL;
48 
49  int i,b;
50  int c = 0, count[MAXNB];
51  char * evtptr = NULL;
52  uint32_t size,bsize;
53  uint32_t numEvents;
54  i = sizeof(CAEN_DGTZ_TriggerMode_t);
55 
56  for(b=0; b<MAXNB; b++){
57  /* IMPORTANT: The following function identifies the different boards with a system which may change
58  for different connection methods (USB, Conet, ecc). Refer to CAENDigitizer user manual for more info.
59  brief:
60  CAEN_DGTZ_OpenDigitizer(<LikType>, <LinkNum>, <ConetNode>, <VMEBaseAddress>, <*handler>);
61  Some examples below */
62 
63  /* The following is for b boards connected via b USB direct links
64  in this case you must set <LikType> = CAEN_DGTZ_USB and <ConetNode> = <VMEBaseAddress> = 0 */
65  //ret = CAEN_DGTZ_OpenDigitizer(CAEN_DGTZ_USB, b, 0, 0, &handle[b]);
66 
67  /* The following is for b boards connected via 1 opticalLink in dasy chain
68  in this case you must set <LikType> = CAEN_DGTZ_PCI_OpticalLink and <LinkNum> = <VMEBaseAddress> = 0 */
69  //ret = CAEN_DGTZ_OpenDigitizer(Params[b].LinkType, 0, b, Params[b].VMEBaseAddress, &handle[b]);
70 
71  /* The following is for b boards connected to A2818 (or A3818) via opticalLink (or USB with A1718)
72  in this case the boards are accessed throught VME bus, and you must specify the VME address of each board:
73  <LikType> = CAEN_DGTZ_PCI_OpticalLink (CAEN_DGTZ_PCIE_OpticalLink for A3818 or CAEN_DGTZ_USB for A1718)
74  <LinkNum> must be the bridge identifier
75  <ConetNode> must be the port identifier in case of A2818 or A3818 (or 0 in case of A1718)
76  <VMEBaseAddress>[0] = <0xXXXXXXXX> (address of first board)
77  <VMEBaseAddress>[1] = <0xYYYYYYYY> (address of second board)
78  ...
79  <VMEBaseAddress>[b-1] = <0xZZZZZZZZ> (address of last board)
80  See the manual for details */
81  ret = CAEN_DGTZ_OpenDigitizer(CAEN_DGTZ_USB,0,0,0x32100000,&handle[b]);
82  if(ret != CAEN_DGTZ_Success) {
83  printf("Can't open digitizer\n");
84  goto QuitProgram;
85  }
86  /* Once we have the handler to the digitizer, we use it to call the other functions */
87  ret = CAEN_DGTZ_GetInfo(handle[b], &BoardInfo);
88  printf("\nConnected to CAEN Digitizer Model %s, recognized as board %d\n", BoardInfo.ModelName, b);
89  printf("\tROC FPGA Release is %s\n", BoardInfo.ROC_FirmwareRel);
90  printf("\tAMC FPGA Release is %s\n", BoardInfo.AMC_FirmwareRel);
91 
92  ret = CAEN_DGTZ_Reset(handle[b]); /* Reset Digitizer */
93  ret = CAEN_DGTZ_GetInfo(handle[b], &BoardInfo); /* Get Board Info */
94  ret = CAEN_DGTZ_SetRecordLength(handle[b],4096); /* Set the lenght of each waveform (in samples) */
95  ret = CAEN_DGTZ_SetChannelEnableMask(handle[b],1); /* Enable channel 0 */
96  ret = CAEN_DGTZ_SetChannelTriggerThreshold(handle[b],0,32768); /* Set selfTrigger threshold */
97  ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[b],CAEN_DGTZ_TRGMODE_ACQ_ONLY,1); /* Set trigger on channel 0 to be ACQ_ONLY */
98  ret = CAEN_DGTZ_SetSWTriggerMode(handle[b],CAEN_DGTZ_TRGMODE_ACQ_ONLY); /* Set the behaviour when a SW tirgger arrives */
99  ret = CAEN_DGTZ_SetMaxNumEventsBLT(handle[b],3); /* Set the max number of events to transfer in a sigle readout */
100  ret = CAEN_DGTZ_SetAcquisitionMode(handle[b],CAEN_DGTZ_SW_CONTROLLED); /* Set the acquisition mode */
101  if(ret != CAEN_DGTZ_Success) {
102  printf("Errors during Digitizer Configuration.\n");
103  goto QuitProgram;
104  }
105  }
106  printf("\n\nPress 's' to start the acquisition\n");
107  printf("Press 'k' to stop the acquisition\n");
108  printf("Press 'q' to quit the application\n\n");
109  while (1) {
110  c = checkCommand();
111  if (c == 9) break;
112  if (c == 2) return;
113  Sleep(100);
114  }
115  /* Malloc Readout Buffer.
116  NOTE1: The mallocs must be done AFTER digitizer's configuration!
117  NOTE2: In this example we use the same buffer, for every board. We
118  Use the first board to allocate the buffer, so if the configuration
119  is different for different boards (or you use different board models), may be
120  that the size to allocate must be different for each one. */
121  ret = CAEN_DGTZ_MallocReadoutBuffer(handle[0],&buffer,&size);
122 
123 
124  for(b=0; b<MAXNB; b++)
125  /* Start Acquisition
126  NB: the acquisition for each board starts when the following line is executed
127  so in general the acquisition does NOT starts syncronously for different boards */
128  ret = CAEN_DGTZ_SWStartAcquisition(handle[b]);
129 
130  // Start acquisition loop
131  while(1) {
132  for(b=0; b<MAXNB; b++) {
133  ret = CAEN_DGTZ_SendSWtrigger(handle[b]); /* Send a SW Trigger */
134 
135  ret = CAEN_DGTZ_ReadData(handle[b],CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT,buffer,&bsize); /* Read the buffer from the digitizer */
136 
137  /* The buffer red from the digitizer is used in the other functions to get the event data
138  The following function returns the number of events in the buffer */
139  ret = CAEN_DGTZ_GetNumEvents(handle[b],buffer,bsize,&numEvents);
140 
141  printf(".");
142  count[b] +=numEvents;
143  for (i=0;i<numEvents;i++) {
144  /* Get the Infos and pointer to the event */
145  ret = CAEN_DGTZ_GetEventInfo(handle[b],buffer,bsize,i,&eventInfo,&evtptr);
146 
147  /* Decode the event to get the data */
148  ret = CAEN_DGTZ_DecodeEvent(handle[b],evtptr,&Evt);
149  //*************************************
150  // Event Elaboration
151  //*************************************
152  ret = CAEN_DGTZ_FreeEvent(handle[b],&Evt);
153  }
154  c = checkCommand();
155  if (c == 1) goto Continue;
156  if (c == 2) goto Continue;
157  } // end of loop on boards
158  } // end of readout loop
159 
160 Continue:
161  for(b=0; b<MAXNB; b++)
162  printf("\nBoard %d: Retrieved %d Events\n",b, count[b]);
163  goto QuitProgram;
164 
165 /* Quit program routine */
166 QuitProgram:
167  // Free the buffers and close the digitizers
168  ret = CAEN_DGTZ_FreeReadoutBuffer(&buffer);
169  for(b=0; b<MAXNB; b++)
170  ret = CAEN_DGTZ_CloseDigitizer(handle[b]);
171  printf("Press 'Enter' key to exit\n");
172  c = getchar();
173  return 0;
174 }
175