1 | /********************************************************************\
|
---|
2 |
|
---|
3 | Class interfacing to FAD board
|
---|
4 |
|
---|
5 | \********************************************************************/
|
---|
6 |
|
---|
7 | #include "FADBoard.h"
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | //
|
---|
11 | // Constructor
|
---|
12 | //
|
---|
13 | FADBoard::FADBoard(string Server, unsigned short ServerPort, class FAD *Parent, unsigned int Num) {
|
---|
14 |
|
---|
15 | int Ret;
|
---|
16 |
|
---|
17 | // Initialization
|
---|
18 | m = Parent;
|
---|
19 | Active = false;
|
---|
20 | Continue = true;
|
---|
21 | CommOK = false;
|
---|
22 | ACalib.Time = -1;
|
---|
23 | Status.Update.tv_sec = -1;
|
---|
24 | Port = ServerPort;
|
---|
25 | Status.Frequency = 0;
|
---|
26 | Status.Rate = 0;
|
---|
27 | Status.BoardID = 0;
|
---|
28 |
|
---|
29 | Name = new char [Server.size()+1]; // Name in permanent memory for DIM service
|
---|
30 | strcpy(Name, Server.c_str());
|
---|
31 |
|
---|
32 | // Initialise mutex for synchronization
|
---|
33 | pthread_mutexattr_t Attr;
|
---|
34 |
|
---|
35 | if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
|
---|
36 | m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
|
---|
37 | }
|
---|
38 | if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
|
---|
39 | m->Message(m->FATAL, "pthread_mutex_init() failed (%s)", strerror(Ret));
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Initialise condition variable for synchronization
|
---|
43 | if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
|
---|
44 | m->Message(m->FATAL, "pthread_cond_init() failed (%s)", strerror(Ret));
|
---|
45 | }
|
---|
46 |
|
---|
47 | // Construct DIM service name prefix
|
---|
48 | stringstream ID;
|
---|
49 | ID << SERVER_NAME"/Board" << setfill('0') << setw(2) << Num << "/";
|
---|
50 |
|
---|
51 | DIM_Name = new DimService((ID.str()+"Server").c_str(), Name);
|
---|
52 | DIM_Status = new DimService((ID.str()+"Status").c_str(), (char *) "");
|
---|
53 | DIM_ID = new DimService((ID.str()+"BoardID").c_str(), (char *) "S", NULL, 0);
|
---|
54 | DIM_Rate = new DimService((ID.str()+"RateHz").c_str(), Status.Rate);
|
---|
55 | DIM_Frequency = new DimService((ID.str()+"Frequency").c_str(), Status.Frequency);
|
---|
56 | DIM_Temp = new DimService((ID.str()+"Temperature").c_str(), (char *) "F", NULL, 0);
|
---|
57 | DIM_DAC = new DimService((ID.str()+"DAC").c_str(), (char *) "S", NULL, 0);
|
---|
58 | DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0);
|
---|
59 |
|
---|
60 | // Create thread that connects and receives data
|
---|
61 | SetStatus("Trying to connect...");
|
---|
62 |
|
---|
63 | if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) {
|
---|
64 | m->Message(m->FATAL, "pthread_create() failed in FADBoard() (%s)", strerror(Ret));
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Start thread to connect to other sockets
|
---|
68 | DimThread::start();
|
---|
69 | }
|
---|
70 |
|
---|
71 | //
|
---|
72 | // Destructor
|
---|
73 | //
|
---|
74 | FADBoard::~FADBoard() {
|
---|
75 |
|
---|
76 | int Ret;
|
---|
77 |
|
---|
78 | // Cancel thread (if it did not quit already) and wait for it to quit
|
---|
79 | if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) {
|
---|
80 | m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
|
---|
81 | }
|
---|
82 | if ((Ret = pthread_join(Thread, NULL)) != 0) {
|
---|
83 | m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
|
---|
84 | }
|
---|
85 |
|
---|
86 | // Delete condition variable
|
---|
87 | if ((Ret = pthread_cond_destroy(&CondVar)) != 0) {
|
---|
88 | m->Message(m->ERROR, "pthread_cond_destroy() failed for %s in ~FADBoard (%s)", Name, strerror(Ret));
|
---|
89 | }
|
---|
90 |
|
---|
91 | // Delete mutex
|
---|
92 | if ((Ret = pthread_mutex_destroy(&Mutex)) != 0) {
|
---|
93 | m->Message(m->ERROR, "pthread_mutex_destroy() failed for %s in ~FADBoard (%s)", Name, strerror(Ret));
|
---|
94 | }
|
---|
95 |
|
---|
96 | delete DIM_Name;
|
---|
97 | delete DIM_Status;
|
---|
98 | delete DIM_ID;
|
---|
99 | delete DIM_Rate;
|
---|
100 | delete DIM_Frequency;
|
---|
101 | delete DIM_Temp;
|
---|
102 | delete DIM_DAC;
|
---|
103 | delete DIM_ROI;
|
---|
104 | delete[] Name;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | //
|
---|
109 | // Send data to board
|
---|
110 | //
|
---|
111 | void FADBoard::Send(const void *Data, size_t Bytes) {
|
---|
112 |
|
---|
113 | // Do not send if not active or communication problem
|
---|
114 | if (!Active || !CommOK) return;
|
---|
115 |
|
---|
116 | // Write data
|
---|
117 | ssize_t Result = write(Socket, Data, Bytes);
|
---|
118 |
|
---|
119 | // Check result
|
---|
120 | if (Result == -1) m->PrintMessage("Error: Could not write to socket (%s)\n", strerror(errno));
|
---|
121 | else if ((size_t) Result < Bytes) m->PrintMessage("Error: Could only write %d bytes out of %d to socket\n", Result, Bytes);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void FADBoard::Send(unsigned short Data) {
|
---|
125 |
|
---|
126 | unsigned short Buffer = htons(Data);
|
---|
127 |
|
---|
128 | Send(&Buffer, sizeof(unsigned short));
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | //
|
---|
133 | // Get board status (mutex protected to avoid concurrent access in ReadLoop)
|
---|
134 | //
|
---|
135 | struct FADBoard::BoardStatus FADBoard::GetStatus() {
|
---|
136 |
|
---|
137 | int Ret;
|
---|
138 | struct BoardStatus S;
|
---|
139 |
|
---|
140 | // Lock
|
---|
141 | if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
|
---|
142 | m->Message(m->FATAL, "pthread_mutex_lock() failed in ReadLoop() (%s)", strerror(Ret));
|
---|
143 | }
|
---|
144 |
|
---|
145 | S = Status;
|
---|
146 |
|
---|
147 | // Unlock
|
---|
148 | if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
|
---|
149 | m->Message(m->FATAL, "pthread_mutex_unlock() failed in Unlock() (%s)", strerror(Ret));
|
---|
150 | }
|
---|
151 |
|
---|
152 | return S;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | //
|
---|
157 | // Perform amplitude calibration in steps
|
---|
158 | //
|
---|
159 | // The steps are intended to assure that up to date data is available
|
---|
160 | void FADBoard::AmplitudeCalibration() {
|
---|
161 |
|
---|
162 | vector<unsigned short> ROICmd;
|
---|
163 | unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
|
---|
164 | string Message = string("ACALIBDONE")+Name+"\n";
|
---|
165 |
|
---|
166 | switch (State) {
|
---|
167 | // ====== Part A: Check if amplitude calibration should start and initialise =====
|
---|
168 | case standbye:
|
---|
169 | if (m->Mode != m->acalib) break;
|
---|
170 |
|
---|
171 | // Invalidate current calibration
|
---|
172 | ACalib.Time = -1;
|
---|
173 | Count = 0;
|
---|
174 |
|
---|
175 | // Save initial board status, set all ROIs to 1024 and set DAC values
|
---|
176 | InitialStatus = GetStatus();
|
---|
177 |
|
---|
178 | for (unsigned int i=0; i<NChips*NChannels; i++) {
|
---|
179 | ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
|
---|
180 | ROICmd.push_back(htons(NBins));
|
---|
181 | }
|
---|
182 | Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
|
---|
183 |
|
---|
184 | DACCmd[1] = htons(0);
|
---|
185 | DACCmd[3] = htons(0);
|
---|
186 | DACCmd[5] = htons(0);
|
---|
187 | Send(DACCmd, sizeof(DACCmd));
|
---|
188 |
|
---|
189 | // Clear sum vector and set state to accumulate
|
---|
190 | memset(Sum, 0, sizeof(Sum));
|
---|
191 | State = baseline;
|
---|
192 | break;
|
---|
193 |
|
---|
194 | // ====== Part B: Baseline calibration =====
|
---|
195 | case baseline:
|
---|
196 | // Check for stopping
|
---|
197 | if (m->Mode != m->acalib) {
|
---|
198 | State = cleanup;
|
---|
199 | break;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // Average
|
---|
203 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
---|
204 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
---|
205 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
---|
206 | Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i];
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | Count++;
|
---|
211 |
|
---|
212 | // Determine baseline if integration finished
|
---|
213 | if (Count < m->NumEventsRequested) break;
|
---|
214 |
|
---|
215 | for (unsigned int i=0; i<NChips; i++) {
|
---|
216 | for (unsigned int j=0; j<NChannels; j++) {
|
---|
217 | for (unsigned int k=0; k<NBins; k++) {
|
---|
218 | ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | // Set new DAC values and start accumulation
|
---|
224 | DACCmd[1] = htons(50000);
|
---|
225 | DACCmd[3] = htons(50000);
|
---|
226 | DACCmd[5] = htons(50000);
|
---|
227 | Send(DACCmd, sizeof(DACCmd));
|
---|
228 |
|
---|
229 | // Clear sum vector and set state to accumulate
|
---|
230 | memset(Sum, 0, sizeof(Sum));
|
---|
231 | Count = 0;
|
---|
232 | State = gain;
|
---|
233 | break;
|
---|
234 |
|
---|
235 | // ====== Part C: Gain calibration =====
|
---|
236 | case gain:
|
---|
237 | // Check for stopping
|
---|
238 | if (m->Mode != m->acalib) {
|
---|
239 | State = cleanup;
|
---|
240 | break;
|
---|
241 | }
|
---|
242 |
|
---|
243 | // Average
|
---|
244 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
---|
245 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
---|
246 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
---|
247 | Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i];
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | Count++;
|
---|
252 |
|
---|
253 | // Determine gain if integration finished
|
---|
254 | if (Count < m->NumEventsRequested) break;
|
---|
255 |
|
---|
256 | for (unsigned int i=0; i<NChips; i++) {
|
---|
257 | for (unsigned int j=0; j<NChannels; j++) {
|
---|
258 | for (unsigned int k=0; k<NBins; k++) {
|
---|
259 | ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | // Set new DAC values and start accumulation
|
---|
265 | DACCmd[1] = htons(0);
|
---|
266 | DACCmd[3] = htons(0);
|
---|
267 | DACCmd[5] = htons(0);
|
---|
268 | Send(DACCmd, sizeof(DACCmd));
|
---|
269 |
|
---|
270 | // Clear sum vector and set state to accumulate
|
---|
271 | memset(Sum, 0, sizeof(Sum));
|
---|
272 | Count = 0;
|
---|
273 | State = secondary;
|
---|
274 | break;
|
---|
275 |
|
---|
276 | // ====== Part D: Secondary calibration =====
|
---|
277 | case secondary:
|
---|
278 | // Check for stopping
|
---|
279 | if (m->Mode != m->acalib) {
|
---|
280 | State = cleanup;
|
---|
281 | break;
|
---|
282 | }
|
---|
283 |
|
---|
284 | // Average
|
---|
285 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
---|
286 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
---|
287 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
---|
288 | Sum[Chip][Chan][i] = Data[Chip][Chan][i] - ACalib.Baseline[Chip][Chan][(i-Status.TriggerCell[Chip]) % NBins];
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | Count++;
|
---|
293 |
|
---|
294 | // Determine secondary baseline if integration finished
|
---|
295 | if (Count < m->NumEventsRequested) break;
|
---|
296 |
|
---|
297 | for (unsigned int i=0; i<NChips; i++) {
|
---|
298 | for (unsigned int j=0; j<NChannels; j++) {
|
---|
299 | for (unsigned int k=0; k<NBins; k++) {
|
---|
300 | ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | // Store calibration time and temperature
|
---|
306 | ACalib.DNA = Status.DNA;
|
---|
307 | ACalib.Frequency = Status.Frequency;
|
---|
308 | ACalib.Time = time(NULL);
|
---|
309 | ACalib.Temp = 0;
|
---|
310 | for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
|
---|
311 |
|
---|
312 | // Inform event thread that calibration is finished for this board
|
---|
313 | if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
|
---|
314 | m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
|
---|
315 | }
|
---|
316 |
|
---|
317 | State = cleanup;
|
---|
318 | break;
|
---|
319 |
|
---|
320 | // ====== Part E: Write back original ROI and DAC settings =====
|
---|
321 | case cleanup:
|
---|
322 | // ROI values
|
---|
323 | ROICmd.clear();
|
---|
324 | for (unsigned int i=0; i<NChips*NChannels; i++) {
|
---|
325 | ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
|
---|
326 | ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
|
---|
327 | }
|
---|
328 | Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
|
---|
329 |
|
---|
330 | // DAC values
|
---|
331 | DACCmd[1] = htons(InitialStatus.DAC[1]);
|
---|
332 | DACCmd[3] = htons(InitialStatus.DAC[2]);
|
---|
333 | DACCmd[5] = htons(InitialStatus.DAC[3]);
|
---|
334 | Send(DACCmd, sizeof(DACCmd));
|
---|
335 |
|
---|
336 | State = wait;
|
---|
337 | break;
|
---|
338 |
|
---|
339 | // ====== Wait for Mode not being idle =====
|
---|
340 | case wait:
|
---|
341 | if (m->Mode == m->idle) State = standbye;
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | //
|
---|
347 | // Connect to board and read data
|
---|
348 | //
|
---|
349 | void FADBoard::ReadLoop() {
|
---|
350 |
|
---|
351 | char Buffer[READ_BUFFER_SIZE];
|
---|
352 | unsigned int Pos = 0, Count = 0;
|
---|
353 | const PEVNT_HEADER *Header = (PEVNT_HEADER *) Buffer;
|
---|
354 | ssize_t Result;
|
---|
355 | struct sockaddr_in SocketAddress;
|
---|
356 | struct BoardStatus PrevStatus;
|
---|
357 | int Ret;
|
---|
358 |
|
---|
359 | // Resolve hostname
|
---|
360 | struct hostent *Host = gethostbyname(Name);
|
---|
361 | if (Host == 0) {
|
---|
362 | SetStatus("Could not resolve host name '%s'", Name);
|
---|
363 | return;
|
---|
364 | }
|
---|
365 |
|
---|
366 | SocketAddress.sin_family = PF_INET;
|
---|
367 | SocketAddress.sin_port = htons(Port);
|
---|
368 | SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
|
---|
369 |
|
---|
370 | // Open socket descriptor
|
---|
371 | if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
|
---|
372 | m->Message(m->ERROR, "Could not open socket for %s (%s)\n", Name, strerror(errno));
|
---|
373 | return;
|
---|
374 | }
|
---|
375 |
|
---|
376 | // Connect to server
|
---|
377 | if (connect(Socket, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
|
---|
378 | SetStatus("Could not connect to port %hu (%s)", Port, strerror(errno));
|
---|
379 | }
|
---|
380 | else {
|
---|
381 | CommOK = true;
|
---|
382 | Active = true;
|
---|
383 | SetStatus("Connected");
|
---|
384 | }
|
---|
385 |
|
---|
386 | memset(&PrevStatus, 0, sizeof(PrevStatus));
|
---|
387 |
|
---|
388 | // Leave loop if program termination requested or board communication not OK
|
---|
389 | while (!m->ExitRequest && CommOK) {
|
---|
390 | // Read data from socket
|
---|
391 | Result = read(Socket, Buffer + Pos, sizeof(Buffer)-Pos);
|
---|
392 |
|
---|
393 | // Check result of read
|
---|
394 | if (Result == -1) {
|
---|
395 | m->Message(m->ERROR, "Could not read from socket for %s, exiting read loop (%s)\n", Name, strerror(errno));
|
---|
396 | CommOK = false;
|
---|
397 | break;
|
---|
398 | }
|
---|
399 | else if (Result == 0) {
|
---|
400 | SetStatus("Server not existing anymore, exiting read loop");
|
---|
401 | CommOK = false;
|
---|
402 | break;
|
---|
403 | }
|
---|
404 |
|
---|
405 | // If not active, discard incoming data
|
---|
406 | if (!Active) continue;
|
---|
407 |
|
---|
408 | // Advance write pointer
|
---|
409 | Pos += Result;
|
---|
410 |
|
---|
411 | // Check if internal buffer full
|
---|
412 | if (Pos == sizeof(Buffer)) {
|
---|
413 | SetStatus("Internal buffer full, deleting all data in buffer");
|
---|
414 | Pos = 0;
|
---|
415 | continue;
|
---|
416 | }
|
---|
417 |
|
---|
418 | // Check if buffer starts with start_package_flag, remove data if not
|
---|
419 | unsigned int Temp = 0;
|
---|
420 | while (ntohs(*((unsigned short *) (Buffer+Temp))) != 0xfb01 && Temp<Pos) Temp++;
|
---|
421 | if (Temp != 0) {
|
---|
422 | memmove(Buffer, Buffer+Temp, Pos-Temp);
|
---|
423 | Pos -= Temp;
|
---|
424 | SetStatus("Removed %d bytes because of start_package_flag not found", Temp);
|
---|
425 | continue;
|
---|
426 | }
|
---|
427 |
|
---|
428 | // Wait until the buffer contains at least enough bytes to potentially hold a PEVNT_HEADER
|
---|
429 | if (Pos < sizeof(PEVNT_HEADER)) continue;
|
---|
430 |
|
---|
431 | unsigned int Length = ntohs(Header->package_length)*2*sizeof(char);
|
---|
432 | if (Pos < Length) continue;
|
---|
433 |
|
---|
434 | // Extract data if event end package flag correct
|
---|
435 | if (ntohs(*(unsigned short *) (Buffer+Length-sizeof(unsigned short))) == 0x04FE) {
|
---|
436 |
|
---|
437 | // Prepare pointers to channel data (channels stored in order 0,9,18,27 - 1,10,19,28 - ... - 8,17,26,35)
|
---|
438 | PCHANNEL *Channel[NChips*NChannels], *Pnt=(PCHANNEL *) (Header+1);
|
---|
439 | for(unsigned int i=0; i<NChips*NChannels; i++) {
|
---|
440 | Channel[i] = Pnt;
|
---|
441 | Pnt = (PCHANNEL *) ((short *) (Channel[i] + 1) + ntohs(Channel[i]->roi));
|
---|
442 | }
|
---|
443 |
|
---|
444 | // Wait until event thread processed the previous data and lock to avoid concurrent access in GetStatus()
|
---|
445 | Lock();
|
---|
446 | while (!Continue) {
|
---|
447 | if ((Ret = pthread_cond_wait(&CondVar, &Mutex)) != 0) {
|
---|
448 | m->Message(m->ERROR, "pthread_cond_wait() failed (%s)", strerror(Ret));
|
---|
449 | }
|
---|
450 | }
|
---|
451 | gettimeofday(&Status.Update, NULL);
|
---|
452 |
|
---|
453 | // Extract board and trigger information
|
---|
454 | Status.BoardID = ntohs(Header->board_id);
|
---|
455 | Status.FirmwareRevision = ntohs(Header->version_no);
|
---|
456 | Status.BoardTime = ntohl(Header->time);
|
---|
457 | Status.EventCounter = ntohl(Header->fad_evt_counter);
|
---|
458 | Status.TriggerID = ntohl(Header->trigger_id);
|
---|
459 | Status.Runnumber = ntohl(Header->runnumber);
|
---|
460 | Status.TriggerType = ntohs(Header->trigger_type);
|
---|
461 | Status.TriggerCRC = ntohs(Header->trigger_crc);
|
---|
462 | Status.DNA = Header->DNA;
|
---|
463 |
|
---|
464 | // Extract frequency related information
|
---|
465 | Status.Frequency = ntohl(Header->REFCLK_frequency)/1.0e3*2.048;
|
---|
466 | Status.PhaseShift = Header->adc_clock_phase_shift;
|
---|
467 | for (unsigned int i=0; i<NChips; i++) {
|
---|
468 | if ((ntohs(Header->PLLLCK)>>12 & (1<<i)) != 0) Status.Lock[i] = true;
|
---|
469 | else Status.Lock[i] = false;
|
---|
470 | }
|
---|
471 |
|
---|
472 | // Extract Firmware status info
|
---|
473 | Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
|
---|
474 | Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
|
---|
475 | Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
|
---|
476 | Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
|
---|
477 | Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
|
---|
478 |
|
---|
479 | // Extract temperatures (MSB indicates if temperature is positive or negative)
|
---|
480 | for (unsigned int i=0; i<NTemp; i++) {
|
---|
481 | if ((ntohs(Header->drs_temperature[i]) & 0x8000) == 0) Status.Temp[i] = float(ntohs(Header->drs_temperature[i]) >> 3)/16;
|
---|
482 | else Status.Temp[i] = float(0xE000 | (ntohs(Header->drs_temperature[i])) >> 3)/16;
|
---|
483 | }
|
---|
484 |
|
---|
485 | // Extract DAC channels
|
---|
486 | for (unsigned int i=0; i<NDAC; i++) Status.DAC[i] = ntohs(Header->dac[i]);
|
---|
487 |
|
---|
488 | short Buf;
|
---|
489 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
---|
490 | // Extract trigger cells
|
---|
491 | Status.TriggerCell[Chip] = (int) ntohs(Channel[Chip]->start_cell);
|
---|
492 |
|
---|
493 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
---|
494 | // Extract ROI
|
---|
495 | Status.ROI[Chip][Chan] = ntohs(Channel[Chip+NChips*Chan]->roi);
|
---|
496 |
|
---|
497 | // Extract ADC data (stored in 12 bit signed twis complement with out-of-range-bit and leading zeroes)
|
---|
498 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
---|
499 | Buf = (Channel[Chip+NChips*Chan]->adc_data[i]);
|
---|
500 | (Buf <<= 4) >>= 4; //delete the sign-bit by shifting left and shift back
|
---|
501 | Data[Chip][Chan][i] = Buf;
|
---|
502 | }
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | // Prepare predicate for condition variable
|
---|
507 | Continue = false;
|
---|
508 | Count++;
|
---|
509 | Unlock();
|
---|
510 |
|
---|
511 | // Amplitude calibration (will check if Mode is acalib)
|
---|
512 | AmplitudeCalibration();
|
---|
513 |
|
---|
514 | // Update DIM services if necessary
|
---|
515 | if (Status.Update.tv_sec - PrevStatus.Update.tv_sec > m->EventUpdateDelay) {
|
---|
516 |
|
---|
517 | // Determine event rate
|
---|
518 | Status.Rate =
|
---|
519 | Count / (double(Status.Update.tv_sec-PrevStatus.Update.tv_sec) + (Status.Update.tv_usec-PrevStatus.Update.tv_usec)/1000000.0);
|
---|
520 | Count = 0;
|
---|
521 |
|
---|
522 | if (PrevStatus.Frequency != Status.Frequency) DIM_Frequency->updateService();
|
---|
523 |
|
---|
524 | if (PrevStatus.Rate != Status.Rate) DIM_Rate->updateService();
|
---|
525 |
|
---|
526 | if (memcmp(PrevStatus.Temp, Status.Temp, sizeof(Status.Temp)) != 0) {
|
---|
527 | DIM_Temp->updateService(Status.Temp, sizeof(Status.Temp));
|
---|
528 | }
|
---|
529 | if (memcmp(PrevStatus.DAC, Status.DAC, sizeof(Status.DAC)) != 0) {
|
---|
530 | DIM_DAC->updateService(Status.DAC, sizeof(Status.DAC));
|
---|
531 | }
|
---|
532 | if (memcmp(PrevStatus.ROI, Status.ROI, sizeof(Status.ROI)) != 0) {
|
---|
533 | DIM_ROI->updateService(Status.ROI, sizeof(Status.ROI));
|
---|
534 | }
|
---|
535 | if (PrevStatus.BoardID != Status.BoardID) {
|
---|
536 | DIM_ID->updateService(&Status.BoardID, sizeof(Status.BoardID));
|
---|
537 | }
|
---|
538 |
|
---|
539 | PrevStatus = Status;
|
---|
540 | }
|
---|
541 |
|
---|
542 | // Inform event thread of new data
|
---|
543 | string Message = string("EVENT")+Name+"\n";
|
---|
544 | if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
|
---|
545 | m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard (%s)", strerror(errno));
|
---|
546 | break;
|
---|
547 | }
|
---|
548 | }
|
---|
549 | else SetStatus("End package flag incorrect, removing corrupt event");
|
---|
550 |
|
---|
551 | // Remove event data from internal buffer
|
---|
552 | memmove(Buffer, Buffer+Length, Pos-Length);
|
---|
553 | Pos = Pos-Length;
|
---|
554 | } // while()
|
---|
555 |
|
---|
556 | // Set inactive and close socket descriptor
|
---|
557 | Active = false;
|
---|
558 |
|
---|
559 | if (close(Socket) == -1) {
|
---|
560 | m->Message(m->ERROR, "Could not close socket descriptor for board %s (%s)", Name, strerror(errno));
|
---|
561 | }
|
---|
562 |
|
---|
563 | }
|
---|
564 |
|
---|
565 | //
|
---|
566 | // Launch read thread inside class
|
---|
567 | //
|
---|
568 | void FADBoard::LaunchThread(class FADBoard *m) {
|
---|
569 |
|
---|
570 | m->ReadLoop();
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 | //
|
---|
575 | // Set status message
|
---|
576 | //
|
---|
577 | void FADBoard::SetStatus(const char *Format, ...) {
|
---|
578 |
|
---|
579 | int Ret;
|
---|
580 |
|
---|
581 | // Assemble message
|
---|
582 | va_list ArgumentPointer;
|
---|
583 | va_start(ArgumentPointer, Format);
|
---|
584 | Lock();
|
---|
585 | Ret = vsnprintf(Status.Message, sizeof(Status.Message), Format, ArgumentPointer);
|
---|
586 | Unlock();
|
---|
587 | va_end(ArgumentPointer);
|
---|
588 |
|
---|
589 | if (Ret == -1) m->Message(m->FATAL, "snprintf() in FADBoard::SetStatus() failed (%s)", strerror(errno));
|
---|
590 |
|
---|
591 | // Update status service
|
---|
592 | DIM_Status->updateService(Status.Message);
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | //
|
---|
597 | // Lock and unlock mutex
|
---|
598 | //
|
---|
599 | void FADBoard::Lock() {
|
---|
600 |
|
---|
601 | int Ret;
|
---|
602 |
|
---|
603 | if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
|
---|
604 | m->Message(m->FATAL, "pthread_mutex_lock() failed in class FADBoard (%s)", strerror(Ret));
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | void FADBoard::Unlock() {
|
---|
609 |
|
---|
610 | int Ret;
|
---|
611 |
|
---|
612 | if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
|
---|
613 | m->Message(m->FATAL, "pthread_mutex_unlock() failed in class FADBoard (%s)", strerror(Ret));
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 |
|
---|
618 | //
|
---|
619 | // Open other sockets
|
---|
620 | //
|
---|
621 | // Error reporting is limited as this function is expected to be removed when firmware allows single socket
|
---|
622 | //
|
---|
623 | void FADBoard::threadHandler() {
|
---|
624 |
|
---|
625 | int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
|
---|
626 | int Socket[sizeof(List)/sizeof(int)], MaxSocketNum, Ret;
|
---|
627 | fd_set DescriptorList;
|
---|
628 | char Buffer[1000000];
|
---|
629 |
|
---|
630 | // Resolve hostname
|
---|
631 | struct hostent *Host = gethostbyname(Name);
|
---|
632 | if (Host == 0) return;
|
---|
633 |
|
---|
634 | // Connect to server
|
---|
635 | struct sockaddr_in SocketAddress;
|
---|
636 | SocketAddress.sin_family = PF_INET;
|
---|
637 | SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
|
---|
638 |
|
---|
639 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
|
---|
640 | // Open socket descriptor
|
---|
641 | if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
|
---|
642 | m->Message(m->ERROR, "OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
|
---|
643 | return;
|
---|
644 | }
|
---|
645 | MaxSocketNum = *max_element(Socket, Socket+sizeof(List)/sizeof(int));
|
---|
646 |
|
---|
647 | // Connect to server
|
---|
648 | SocketAddress.sin_port = htons((unsigned short) List[i]);
|
---|
649 | if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) return;
|
---|
650 | }
|
---|
651 |
|
---|
652 | while(true) {
|
---|
653 | // Wait for data from sockets
|
---|
654 | FD_ZERO(&DescriptorList);
|
---|
655 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
|
---|
656 | if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
|
---|
657 | m->Message(m->ERROR, "OtherSockets: Error with select() (%s)\n", strerror(errno));
|
---|
658 | break;
|
---|
659 | }
|
---|
660 |
|
---|
661 | // Data from socket
|
---|
662 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
|
---|
663 | Ret = read(Socket[i], Buffer, sizeof(Buffer));
|
---|
664 | if (Ret == -1) m->Message(m->ERROR, "OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | // Close all sockets
|
---|
669 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
|
---|
670 | if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
|
---|
671 | m->Message(m->ERROR, "OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));
|
---|
672 | }
|
---|
673 | }
|
---|
674 | }
|
---|