source: fact/FADctrl/FADBoard.cc@ 10174

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