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],0xFF); /* Enable channel 0 */
96  ret = CAEN_DGTZ_SetChannelTriggerThreshold(handle[b],0,512); /* Set selfTrigger threshold */
97 
98  // trigger threshold
99  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x1080, 15000);
100 
101  // trigger nsamples over threshold
102  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x1084, 2);
103 
105  ret = CAEN_DGTZ_SetChannelDCOffset(handle[b], 0, 0);
106 
107  //ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[b],CAEN_DGTZ_TRGMODE_ACQ_ONLY,1); /* Set trigger on channel 0 to be ACQ_ONLY */
108  ret = CAEN_DGTZ_SetChannelSelfTrigger(handle[b],CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT,0x1); /* Set trigger on channel 0 to be ACQ_AND_EXTOUT */
109  //ret = CAEN_DGTZ_SetSWTriggerMode(handle[b],CAEN_DGTZ_TRGMODE_ACQ_ONLY); /* Set the behaviour when a SW tirgger arrives */
110  ret = CAEN_DGTZ_SetSWTriggerMode(handle[b],CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT); /* Set the behaviour when a SW tirgger arrives */
111  ret = CAEN_DGTZ_SetMaxNumEventsBLT(handle[b],1024); /* Set the max number of events to transfer in a sigle readout */
112  ret = CAEN_DGTZ_SetAcquisitionMode(handle[b],CAEN_DGTZ_SW_CONTROLLED); /* Set the acquisition mode */
113 
114  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x800C, 0xA); /* make 1024 buffers */
115 
116  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8100, 0x8); /* count all triggers */
117 
118  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8114, 32); /* post-trigger samples */
119 
120  // front panel trigger out enable mask
121  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8110, 0xC00000FF);
122 
123  // DAC monitor mode: voltage level monitor
124  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8144, 0x4);
125 
126  // Front panel io control
127  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x811C, 0x0);
128 
130 
131  if(ret != CAEN_DGTZ_Success) {
132  printf("Errors during Digitizer Configuration.\n");
133  goto QuitProgram;
134  }
135  }
136  printf("\n\nPress 's' to start the acquisition\n");
137  printf("Press 'k' to stop the acquisition\n");
138  printf("Press 'q' to quit the application\n\n");
139  while (1) {
140  c = checkCommand();
141  if (c == 9) break;
142  if (c == 2) return;
143  Sleep(100);
144  }
145  /* Malloc Readout Buffer.
146  NOTE1: The mallocs must be done AFTER digitizer's configuration!
147  NOTE2: In this example we use the same buffer, for every board. We
148  Use the first board to allocate the buffer, so if the configuration
149  is different for different boards (or you use different board models), may be
150  that the size to allocate must be different for each one. */
151  ret = CAEN_DGTZ_MallocReadoutBuffer(handle[0],&buffer,&size);
152 
153 
154  for(b=0; b<MAXNB; b++)
155  /* Start Acquisition
156  NB: the acquisition for each board starts when the following line is executed
157  so in general the acquisition does NOT starts syncronously for different boards */
158  ret = CAEN_DGTZ_SWStartAcquisition(handle[b]);
159 
160 
161  // Start acquisition loop
162  while(1) {
163  for(b=0; b<MAXNB; b++) {
164  ret = CAEN_DGTZ_SendSWtrigger(handle[b]); /* Send a SW Trigger */
165  printf("SW trigger status: %i\n",ret);
166 
167  //ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8108, 0xFFFF); /* Send a SW trigger */
168  // printf("SW trigger status: %i\n",ret);
169 
170  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8138, 1); /* SET DAC */
171  printf("Write DAC status: %i\n",ret);
172 
173 
174  uint32_t xxx;
175  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x812C, &xxx);
176  printf("Event Stored: 0x%08x ret = %i\n",xxx, ret);
177 
178  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x814C, &xxx);
179  printf("Event size: 0x%08x ret = %i\n",xxx, ret);
180 
181  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x810C, &xxx);
182  printf("Trigger Source enable mask: 0x%08x ret = %i\n",xxx, ret);
183 
184  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x1088, &xxx);
185  printf("Channel 0 status: 0x%08x ret = %i\n",xxx, ret);
186 
187  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x1188, &xxx);
188  printf("Channel 1 status: 0x%08x ret = %i\n",xxx, ret);
189 
190  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x1094, &xxx);
191  printf("Channel 0 buffer occupancy: 0x%08x ret = %i\n",xxx, ret);
192 
193  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x8100, &xxx);
194  printf("Acquision Control: 0x%08x ret = %i\n",xxx, ret);
195 
196  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x8104, &xxx);
197  printf("Acquision Status: 0x%08x ret = %i\n",xxx, ret);
198 
199  ret = CAEN_DGTZ_ReadRegister(handle[b], 0x8114, &xxx);
200  printf("Post trigger samples: 0x%08x ret = %i\n",xxx, ret);
201 
202 
203 
204  ret = CAEN_DGTZ_ReadData(handle[b],CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT,buffer,&bsize); /* Read the buffer from the digitizer */
205 
206  /* The buffer red from the digitizer is used in the other functions to get the event data
207  The following function returns the number of events in the buffer */
208  ret = CAEN_DGTZ_GetNumEvents(handle[b],buffer,bsize,&numEvents);
209 
210  printf(".");
211  count[b] +=numEvents;
212  for (i=0;i<numEvents;i++) {
213  /* Get the Infos and pointer to the event */
214  ret = CAEN_DGTZ_GetEventInfo(handle[b],buffer,bsize,i,&eventInfo,&evtptr);
215 
216  /* Decode the event to get the data */
217  ret = CAEN_DGTZ_DecodeEvent(handle[b],evtptr,&Evt);
218  //*************************************
219  // Event Elaboration
220  //*************************************
221  ret = CAEN_DGTZ_FreeEvent(handle[b],&Evt);
222  }
223 
224  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8010, 1024); /* Free buffers */
225  printf("Free buffers status: %i\n",ret);
226 
227  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8138, 1024); /* SET DAC */
228  printf("Write DAC status: %i\n",ret);
229 
230  // front panel trigger out enable mask
231  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8110, 0xC00000FF);
232 
233  usleep(1000000);
234 
235  // front panel trigger out enable mask
236  ret = CAEN_DGTZ_WriteRegister(handle[b], 0x8110, 0x0);
237 
238  c = checkCommand();
239  if (c == 1) goto Continue;
240  if (c == 2) goto Continue;
241  } // end of loop on boards
242  } // end of readout loop
243 
244 Continue:
245  for(b=0; b<MAXNB; b++)
246  printf("\nBoard %d: Retrieved %d Events\n",b, count[b]);
247  goto QuitProgram;
248 
249 /* Quit program routine */
250 QuitProgram:
251  // Free the buffers and close the digitizers
252  ret = CAEN_DGTZ_FreeReadoutBuffer(&buffer);
253  for(b=0; b<MAXNB; b++)
254  ret = CAEN_DGTZ_CloseDigitizer(handle[b]);
255  printf("Press 'Enter' key to exit\n");
256  c = getchar();
257  return 0;
258 }
259