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