-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathadc792_lib_CAENComm.c
More file actions
355 lines (303 loc) · 10.6 KB
/
adc792_lib_CAENComm.c
File metadata and controls
355 lines (303 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <time.h>
#include "CAENVMElib.h"
#include "CAENComm.h"
#include "CAENVMEtypes.h"
#include "CAENVMEoslib.h"
#include "adc792_lib_CAENComm.h"
#include <iostream>
#include <vector>
using namespace std;
std::vector<unsigned long> adcaddrs;
void print_adc792_CAENCOMM_debug_word(uint32_t word)
{
short dt_type = word>>24 & 0x7;
if (dt_type==0)
{
// adc_chan = data>>17 & 0xF; //For 792N [bit 17-20]
short adc_chan = word>>16 & 0x1F; //For 792 [bit 16-20]
unsigned int adc_value = word & 0xFFF; // adc data [bit 0-11]
bool adc_overflow = (word>>12) & 0x1; // overflow bit [bit 12]
bool adc_underthreshold = (word>>13) & 0x1; // under threshold bit [bit 13]
std::cout << "raw " << word << "\tchannel " << adc_chan << "\tvalue " << adc_value << "\toverflow " << adc_overflow << "\tunderthreshold " << adc_underthreshold << std::endl;
}
}
void check_adc792_CAENCOMM_status_afterRead(int32_t BHandle)
{
uint32_t address = 0;
uint16_t data;
int status=1;
int caenst = CAENComm_Read16(BHandle,address+adc792_CAENCOMM_shift.statusreg2,&data);
status *= (1-caenst);
if(status != 1) {
printf("Could not read statusreg2\n");
}
bool full = data & adc792_CAENCOMM_bitmask.full;
bool empty = data & adc792_CAENCOMM_bitmask.empty;
if(full || !empty || status!=1 ) {
std::cout << "FULL " << full << " !EMPTY " << !empty << " STATUS " << status << std::endl;
//Try a dataReset
int dtRst=dataReset792_CAENCOMM(BHandle);
if (dtRst!=1)
{
//Now try a full software reset
softReset792_CAENCOMM(BHandle);
//Reinialize the module
init_adc792_CAENCOMM(BHandle);
}
}
}
unsigned short init_adc792_CAENCOMM(int32_t BHandle) {
int status=1;
unsigned long address;
uint16_t DataLong;
int nZS = 1; int caenst;
adcaddrs.clear();
adcaddrs.push_back(V792N_CAENCOMM_ADDRESS);
if(NUMADBOARDS >= 2) adcaddrs.push_back(V792N_CAENCOMM_ADDRESS2);
if(NUMADBOARDS >= 3) adcaddrs.push_back(V792N_CAENCOMM_ADDRESS3);
/* //Initialize all the boards */
/* if (idB>adcaddrs.size()-1) */
/* { */
/* std::cout << "ADC CARD requested " << idB << " not available" << std::endl; */
/* return 2; */
/* } */
/* QDC Reset */
address = 0x1000;
caenst = CAENComm_Read16(BHandle,address,&DataLong);
status *= (1-caenst);
printf("HELLO!!\n");
if(status != 1) {
printf("Error READING V792N_CAENCOMM firmware -> address= %ld \n",address);
return status;
}
else {
if(adc792_CAENCOMM_debug) printf("V792N_CAENCOMM firmware is version: %d \n",DataLong);
}
//Bit set register
address = 0x1006;
DataLong = 0x80; //Issue a software reset. To be cleared with bit clear register access
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
caenst = CAENComm_Read16(BHandle,address,&DataLong);
status *= (1-caenst);
if(status != 1) {
printf("ERROR: Bit Set Register read: %d\n", DataLong);
return status;
}
//Control Register: enable BLK_end
address = 0x1010;
DataLong = 0x4; //Sets bit 2 to 1 [enable blkend]
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
caenst = CAENComm_Read16(BHandle,address,&DataLong);
status *= (1-caenst);
if(status != 1) {
printf("ERROR: Bit Set Register read: %d\n", DataLong);
return status;
}
//Bit clear register
address = 0x1008;
DataLong = 0x80; //Release the software reset.
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
caenst = CAENComm_Read16(BHandle,address,&DataLong);
status *= (1-caenst);
if(status != 1) {
printf("ERROR: Bit Clear Register read: %d\n", DataLong);
return status;
}
//Bit set register 2
//Enable/disable zero suppression
if(nZS) {
address = 0x1032;
DataLong = 0x1018; //Disable Zero Suppression + disable overfl
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
if(status != 1) {
printf("ERROE: Could not disable ZS: %d\n", DataLong);
return status;
}
} else {
address = 0x1032;
DataLong = 0x1008; //Enable Zero Suppression + disable overfl
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
if(status != 1) {
printf("ERROR: Could not enable ZS: %d\n", DataLong);
return status;
}
}
//Set the thresholds.
for(int i=0; i< V792N_CAENCOMM_CHANNEL; i++) {
// address = 0x1080 +4*i; //every 4 for V792N_CAENCOMM
address = 0x1080 +2*i; //every 2 for V792
if(adc792_CAENCOMM_debug) printf("Channel %d Address : %ld\n",i,address);
DataLong = 0x0; //Threshold
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
if(adc792_CAENCOMM_debug) printf("Iped register read: %d\n", DataLong);
if(status != 1) {
printf("ERROR: Threshold register read: %d\n", DataLong);
return status;
}
}
//Set the Iped value to XX value [180, defaults; >60 for coupled channels]
address = 0x1060;
// status = vme_read_dt(address, &DataLong, AD32, D16);
caenst = CAENComm_Read16(BHandle,address,&DataLong);
status *= (1-caenst);
// if(adc792_CAENCOMM_debug) printf("Iped register read: %d\n", DataLong);
// DataLong = 0xFF; //iped value
DataLong = 0x60; //iped value
caenst = CAENComm_Write16(BHandle,address,DataLong);
status *= (1-caenst);
printf("Iped register read: %d\n", DataLong);
if(status != 1) {
printf("ERROR: Iped register read: %d\n", DataLong);
return status;
}
return status;
}
unsigned short dataReset792_CAENCOMM(int32_t BHandle)
{
unsigned short status = 1;
unsigned short caenst;
unsigned int rst=0x4;
caenst = CAENComm_Write16(BHandle,V792N_CAENCOMM_BIT_SET2,rst);
status *= (1-caenst);
caenst = CAENComm_Write16(BHandle,V792N_CAENCOMM_BIT_CLEAR2,rst);
status *= (1-caenst);
return status;
}
unsigned short softReset792_CAENCOMM(int32_t BHandle)
{
unsigned short status = 1;
unsigned short caenst;
unsigned int rst=0x80;
caenst = CAENComm_Write16(BHandle,V792N_CAENCOMM_BIT_SET1,rst);
status *= (1-caenst);
caenst = CAENComm_Write16(BHandle,V792N_CAENCOMM_BIT_CLEAR1,rst);
status *= (1-caenst);
return status;
}
vector<int> readFastNadc792_CAENCOMM(int32_t BHandle, short int& status, int nevts, vector<int> &outW)
{
/*
Implements Block Transfer Readout of V792N_CAENCOMM
returns vector with output
*/
int nbytes_tran = 0;
vector<int> outD; outD.clear();
status = 1;
int caenst;
//AS unsigned long dataV[34];
uint32_t* dataV; //each event is composed of 34x32bit words
dataV=new uint32_t[1000];
unsigned int wr;
unsigned long adc792_CAENCOMM_rdy, adc792_CAENCOMM_busy;
unsigned int ncha(32), idV; //no ZS reading all 32 channels
/* if(idB<0 || idB>NUMADBOARDS-1) { */
/* cout<<" Accssing Board number"<<idB<<" while only "<<NUMADBOARDS<<" are initialized!!! Check your configuration!"<<endl; */
/* status = 0; */
/* return outD; */
/* } */
/*
check if the fifo has something inside: use status register 1
*/
uint16_t data;
unsigned long address;
address = adc792_CAENCOMM_shift.statusreg1;
caenst = CAENComm_Read16(BHandle,address,&data);
status *= (1-caenst);
/* if(adc792_CAENCOMM_debug) printf("ST (str1) :: %i, %lx, %lx \n",status,address,data); */
adc792_CAENCOMM_rdy = data & adc792_CAENCOMM_bitmask.rdy;
adc792_CAENCOMM_busy = data & adc792_CAENCOMM_bitmask.busy;
int tmpW;
unsigned long full,empty;
/* //Check the status register to check the MEB status */
/* address = adc792_CAENCOMM_shift.statusreg2; */
/* caenst = CAENComm_Read16(BHandle,address,&data); */
/* status *= (1-caenst); */
/* full = data & adc792_CAENCOMM_bitmask.full; */
/* empty = data & adc792_CAENCOMM_bitmask.empty; */
//Event counter register
if(adc792_CAENCOMM_debug) cout<<" rdy:: "<<adc792_CAENCOMM_rdy<<" busy:: "<<adc792_CAENCOMM_busy<<endl;
if(adc792_CAENCOMM_rdy == 1 && adc792_CAENCOMM_busy == 0) {
/*
Data Ready and board not busy
*/
tmpW = 0;
full = 0; empty = 0;
//Read the Event Header
address = 0;
//Vector reset
idV = 0; while(idV<(ncha+2)*nevts) { dataV[idV] = 0; idV++; }
// wr = (ncha+2)*4*nevts;
wr = (ncha+2)*sizeof(dataV)*nevts;
printf("HELLO %d\n",wr);
caenst = CAENComm_BLTRead(BHandle,0x0,dataV,wr,&nbytes_tran);
status *= (1-caenst);
printf("%d\n",nbytes_tran);
//Vector dump into output
idV = 0; while(idV<(ncha+2)*nevts) {
outD.push_back((int)dataV[idV]);
// if (adc792_CAENCOMM_debug) print_adc792_CAENCOMM_debug_word(dataV[idV]);
idV++;
}
delete dataV;
/*
int ntry = 100, nt = 0;
while(!empty && nt<ntry) {
//Check the status register to check the MEB status
address = adcaddrs.at(idB);
caenst = CAENVME_BLTReadCycle(BHandle,address,dataV,(ncha+2)*4,
cvA24_U_DATA,cvD32,&nbytes_tran);
status *= (1-caenst);
//Vector dump into output
idV = 0; while(idV<(ncha+2)) {
outD.push_back((int)dataV[idV]);
if (adc792_CAENCOMM_debug) print_adc792_CAENCOMM_debug_word(dataV[idV]);
idV++;
}
//Check the status register to check the MEB status
address = adc792_CAENCOMM_shift.statusreg2;
caenst = CAENComm_Read16(BHandle,address,&data);
status *= (1-caenst);
full = data & adc792_CAENCOMM_bitmask.full;
empty = data & adc792_CAENCOMM_bitmask.empty;
nt++;
}
if(nt) cout<<" Warning:: needed "<<nt<<" add read to clean up MEB"<<endl;
*/
check_adc792_CAENCOMM_status_afterRead(BHandle);
}
return outD;
}
int find_adc792_CAENCOMM_eventSize(std::vector<int>& events,unsigned int evtStart)
{
short dt_type_boe = events.at(evtStart)>>24 & 0x7;
if (dt_type_boe != 2)
{
std::cout << "ADC 792:: NOT AT BEGIN OF EVENT. Data are probably corrupted" << std::endl;
return -1;
}
int channelsReadout = events.at(evtStart)>>8 & 0x3F;
short dt_type_eoe = events.at(evtStart+channelsReadout+1)>>24 & 0x7;
if (dt_type_eoe != 4)
{
std::cout << "ADC 792:: NOT AT END OF EVENT. Data are probably corrupted" << std::endl;
return -1;
}
if (adc792_CAENCOMM_debug)
{
int evtNum=events.at(evtStart+channelsReadout+1) & 0xFFFFFF;
std::cout << "ADC 792:: EVENT " << evtNum << " has " << channelsReadout << " channels readout"<< std::endl;
}
return channelsReadout+2;
}