-
Notifications
You must be signed in to change notification settings - Fork 1
/
daqLogic.cpp
224 lines (170 loc) · 5.75 KB
/
daqLogic.cpp
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
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdint>
#include <csignal>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sstream>
#include <iomanip>
#include "myModules.h" // VME Module Id.s
#include "myV2718.h" // CAEN V2718 VME Bridge class
#include "myV513.h" // CAEN V513 I/O REG class
#include "daqcommon.h"
#include "daqSem.h"
#include "daqShm.h"
using namespace std;
#define TRIGGER_MASK 7 // channels 0, 1, 2 -> 0 = PHYS TRIG, 1 = PED TRIG, 2 = IN SPILL
#define TRIGGER_OR_IN 7 // channel 7 -> TRIGGER OR INPUT
#define PEDESTAL_VETO 6 // channel 8 -> PEDESTAL VETO
#define DAQ_VETO 7 // channel 9 -> DAQ VETO
#define UNLOCK_PED_TRIGGER 8 // channel 10 -> UNLOCK PEDESTAL TRIGGER
#define UNLOCK_PHYS_TRIGGER 9 // channel 11 -> UNLOCK PHYSICS TRIGGER
#define SCALER_RESET 10 // channel 12 -> SCALER RESET
#define V775_35ps 35 // 35 ps as scale for V775
#define V775_100ps 100 // 100 ps as scale for V775
#define V775_140ps 140 // 140 ps as scale for V775
uint32_t hereIam(0);
inline void enableTriggers( v2718& ioreg ) { ioreg.clearOutputBit( DAQ_VETO ); }
inline void disableTriggers( v2718& ioreg ) { ioreg.setOutputBit( DAQ_VETO ); }
inline void enablePedestals( v2718& ioreg ) { ioreg.clearOutputBit( PEDESTAL_VETO ); }
inline void disablePedestals( v2718& ioreg ) { ioreg.setOutputBit( PEDESTAL_VETO ); }
inline void resetScaler( v2718& ioreg )
{
ioreg.setOutputBit( SCALER_RESET );
ioreg.clearOutputBit( SCALER_RESET );
}
inline void unlockTriggers( v2718& ioreg )
{
ioreg.setOutputBit( UNLOCK_PED_TRIGGER );
ioreg.setOutputBit( UNLOCK_PHYS_TRIGGER );
ioreg.clearOutputBit( UNLOCK_PED_TRIGGER );
ioreg.clearOutputBit( UNLOCK_PHYS_TRIGGER );
}
void initV2718( v2718& ioreg )
{
ioreg.print();
ioreg.systemReset();
x_usleep(10);
for ( int out = cvOutput0; out <= cvOutput4; out++ )
{
CVOutputSelect cvout = static_cast<CVOutputSelect>(out);
ioreg.setOutputManual (cvout);
}
for ( int inp = cvInput0; inp <= cvInput1; inp++ )
{
CVInputSelect cvinp = static_cast<CVInputSelect>(inp);
ioreg.setInput (cvinp);
}
disableTriggers( ioreg );
disablePedestals( ioreg );
}
void initV513( v513& ioreg )
{
uint16_t w;
ioreg.read16phys(0xFE, &w);
cout << hex << " V513 FE " << w << endl;
ioreg.read16phys(0xFC, &w);
cout << hex << " V513 FC " << w << endl;
ioreg.read16phys(0xFA, &w);
cout << hex << " V513 FA " << w << endl;
ioreg.write16phys(0x48, 0);
ioreg.write16phys(0x46, 0);
ioreg.write16phys(0x42, 0);
ioreg.reset();
ioreg.read16phys(0x04, &w);
cout << hex << " V513 0x4 " << w << endl;
}
volatile bool abort_run(false);
volatile bool pause_run(false);
void cntrl_c_handler ( int32_t sig )
{
time_t timestr = time(NULL);
char * stime = ctime(×tr);
stime[24] = 0;
fprintf(stderr,"\n\n%s cntrl_c_handler: sig %d hereIam %d\n\n", stime, sig, hereIam);
fprintf(stderr,"aborting run\n\n");
abort_run = true;
}
void sigusr1_handler ( int32_t sig )
{
time_t timestr = time(NULL);
char * stime = ctime(×tr);
stime[24] = 0;
fprintf(stderr,"\n\n%s sigusr1_handler: sig %d hereIam %d pause_run is %d\n\n", stime, sig, hereIam, pause_run);
pause_run = not pause_run;
pause_run ? fprintf(stderr,"pausing run\n\n") : fprintf(stderr,"resuming run\n\n");
}
uint64_t xreadtime(0);
int main()
{
signal( SIGINT, cntrl_c_handler ); // Control-C handler
signal( SIGUSR1, sigusr1_handler ); // Control-USR1 handler
v2718 v2718_0( "/V2718/cvA24_U_DATA/0" ); // VME interface
initV2718( v2718_0 );
v513 ioreg( 0xa00000, "/V2718/cvA24_U_DATA/0" ); // I/O register
initV513( ioreg );
// initialise shared segment
daqShm mShm ( 0x10, mModeMaster );
volatile uint32_t* daqReadyToRun = mShm.daqReadyToRun();
*daqReadyToRun = 0;
volatile uint32_t* askDaqToRun = mShm.askDaqToRun();
*askDaqToRun = 0;
volatile uint32_t* fromDaq = mShm.fromDaq();
*fromDaq = 0;
volatile uint32_t* toDaq = mShm.toDaq();
*toDaq = 0;
volatile uint32_t* eventNr = mShm.eventNr();
*eventNr = 0;
volatile uint32_t* spillNr = mShm.spillNr();
*spillNr = 0;
volatile uint32_t* triggerMask = mShm.triggerMask();
*triggerMask = 0;
unlockTriggers( v2718_0 );
resetScaler( v2718_0 );
daqSem mySem (0x40, sModeMaster );
while (*daqReadyToRun == 0) { x_usleep(1); }
*askDaqToRun = 1;
enableTriggers( v2718_0 );
bool isSpill(false), wasSpill(false);
volatile int sv;
do
{
uint32_t trigPat;
enableTriggers( v2718_0 );
v2718_0.readInputRegister ( &trigPat );
isSpill = (( ioreg.readInputRegister() & 0x8000) != 0 );
if ( isSpill != wasSpill )
{
if ( isSpill ) (*spillNr) ++;
cout << " isSpill: " << isSpill
<< " wasSpill " << wasSpill
<< " spillNr " << *spillNr << endl;
wasSpill = isSpill;
}
bool isTrig = (trigPat != 0);
if ( isTrig )
{
(*eventNr) ++;
(*toDaq) ++;
*triggerMask = trigPat | 4;
while (*triggerMask != 0) { sched_yield(); }
disableTriggers( v2718_0 );
unlockTriggers( v2718_0 );
x_usleep(1);
v2718_0.readInputRegister ( &trigPat );
cout << " isSpill: " << isSpill
<< " wasSpill " << wasSpill
<< " spillNr " << *spillNr
<< " trigPat " << trigPat << endl;
wasSpill = isSpill;
}
sv = mySem.semGet();
}
while ((sv == 0) && !abort_run);
disableTriggers( v2718_0 );
*askDaqToRun = 0;
while (*daqReadyToRun == 1) { x_usleep(1); }
return 0;
}