forked from coderdj/redax
-
Notifications
You must be signed in to change notification settings - Fork 7
/
V2718.cc
144 lines (124 loc) · 4.59 KB
/
V2718.cc
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
#include "V2718.hh"
#include "MongoLog.hh"
#include <CAENVMElib.h>
V2718::V2718(std::shared_ptr<MongoLog>& log, CrateOptions c_opts){
fLog = log;
fBoardHandle=-1;
fCopts = c_opts;
}
V2718::~V2718(){
}
int V2718::Init(int link, int crate) {
if (CAENVME_Init(cvV2718, link, crate, &fBoardHandle))
return -1;
fLog->Entry(MongoLog::Local, "V2718 init, handle %i", fBoardHandle);
return SendStopSignal(false);
}
int V2718::SendStartSignal(){
// Straight copy from: https://github.com/coderdj/kodiaq
// Line 0 : S-IN.
CAENVME_SetOutputConf(fBoardHandle, cvOutput0, cvDirect, cvActiveHigh, cvManualSW);
// Line 1 : MV S-IN Logic
CAENVME_SetOutputConf(fBoardHandle, cvOutput1, cvDirect, cvActiveHigh, cvManualSW);
// Line 2 : LED Logic
CAENVME_SetOutputConf(fBoardHandle, cvOutput2, cvDirect, cvActiveHigh, cvManualSW);
// Line 3 : LED Pulser
CAENVME_SetOutputConf(fBoardHandle, cvOutput3, cvDirect, cvActiveHigh, cvMiscSignals);
// Line 4 : NV S-IN Logic
CAENVME_SetOutputConf(fBoardHandle, cvOutput4, cvDirect, cvActiveHigh, cvManualSW);
// Set the output register
unsigned int data = 0x0;
int ret;
if ((ret = CAENVME_ReadRegister(fBoardHandle, cvOutRegSet, &data)) != cvSuccess) {
fLog->Entry(MongoLog::Local, "Could not read V2718 output? ret %i", ret);
// fail?
} else {
fLog->Entry(MongoLog::Local, "Current V2718 output status: %x", data);
if (data & cvOut0Bit) fLog->Entry(MongoLog::Local, "Orphaned S-IN?");
}
data = 0;
if(fCopts.neutron_veto) //n_veto soonTM
data+=cvOut4Bit;
if(fCopts.led_trigger)
data+=cvOut2Bit;
if(fCopts.muon_veto)
data+=cvOut1Bit;
if(fCopts.s_in)
data+=cvOut0Bit;
// S-IN and logic signals
if(CAENVME_SetOutputRegister(fBoardHandle,data)!=0){
fLog->Entry(MongoLog::Error, "Couldn't set output register to crate controller");
return -1;
}
//Configure the LED pulser
if(fCopts.pulser_freq > 0){
//CAEN supports frequencies from 38 mHz to 40 MHz, but it's not continuous
//We tell the CC about the time unit (104 ms, 410 us, 1.6 us, 25ns)
//and how many of them (1-FF) to set the period
CVTimeUnits tu = cvUnit104ms;
u_int32_t width = 0x1;
u_int32_t period = 0x0;
std::vector<CVTimeUnits> tus = {cvUnit104ms, cvUnit410us, cvUnit1600ns, cvUnit25ns};
std::vector<double> widths = {104e-3, 410e-6, 1.6e-6, 25e-9};
for (unsigned i = 0; i < widths.size(); i++) {
if (fCopts.pulser_freq < 1./widths[i]) {
period = std::clamp(int(1./(widths[i]*fCopts.pulser_freq)), 1, 0xFF);
tu = tus[i];
fLog->Entry(MongoLog::Debug, "Closest freq to %.1f Hz is %.1f",
fCopts.pulser_freq, 1./(period*widths[i]));
break;
}
if (i == 3) {
fLog->Entry(MongoLog::Error, "Given an invalid LED frequency");
return -1;
}
}
// Set pulser
int ret = CAENVME_SetPulserConf(fBoardHandle, cvPulserB, period, width, tu, 0,
cvManualSW, cvManualSW);
ret *= CAENVME_StartPulser(fBoardHandle,cvPulserB);
if(ret != cvSuccess){
fLog->Entry(MongoLog::Warning, "Failed to activate LED pulser");
return -1;
}
}
return 0;
}
int V2718::SendStopSignal(bool end){
if(fBoardHandle == -1)
return 0;
// Stop the pulser if it's running
CAENVME_StopPulser(fBoardHandle, cvPulserB);
usleep(1000);
// Line 0 : S-IN.
CAENVME_SetOutputConf(fBoardHandle, cvOutput0, cvDirect, cvActiveHigh, cvManualSW);
// Line 1 : MV S-IN Logic
CAENVME_SetOutputConf(fBoardHandle, cvOutput1, cvDirect, cvActiveHigh, cvManualSW);
// Line 2 : LED Logic
CAENVME_SetOutputConf(fBoardHandle, cvOutput2, cvDirect, cvActiveHigh, cvManualSW);
// Line 3 : LED Pulser
CAENVME_SetOutputConf(fBoardHandle, cvOutput3, cvDirect, cvActiveHigh, cvMiscSignals);
// Line 4 : NV S-IN Logic
CAENVME_SetOutputConf(fBoardHandle, cvOutput4, cvDirect, cvActiveHigh, cvManualSW);
// do we somehow have an orphaned S-IN?
unsigned int data = 0x0;
int ret;
if ((ret = CAENVME_ReadRegister(fBoardHandle, cvOutRegSet, &data)) != cvSuccess) {
fLog->Entry(MongoLog::Local, "Could not read V2718 output? ret %i", ret);
// fail?
} else {
fLog->Entry(MongoLog::Local, "Current V2718 output status: %x", data);
if (data & cvOut0Bit) fLog->Entry(MongoLog::Local, "Orphaned S-IN?");
}
// Set the output register
data = 0;
CAENVME_SetOutputRegister(fBoardHandle, data);
fLog->Entry(MongoLog::Local, "V2718 output reset");
if(end){
if(CAENVME_End(fBoardHandle)!= cvSuccess){
fLog->Entry(MongoLog::Warning, "Failed to end crate");
}
fBoardHandle=-1;
}
return 0;
}