source: trunk/FACT++/src/HeadersFTM.h@ 10739

Last change on this file since 10739 was 10734, checked in by tbretz, 14 years ago
Added EnablePixel to StaticData.
File size: 21.0 KB
Line 
1#ifndef FACT_HeadersFTM
2#define FACT_HeadersFTM
3
4#include <ostream>
5
6// For debugging
7#include <iostream>
8
9#include "ByteOrder.h"
10
11// ====================================================================
12
13namespace FTM
14{
15 enum States
16 {
17 // State Machine states
18 kDisconnected = 1,
19 kConnected,
20 kIdle,
21 kTakingData,
22
23 // FTM internal states
24 kFtmIdle = 1, ///< Trigger output disabled, configuration possible
25 kFtmConfig = 2, ///< FTM and FTUs are being reconfigured
26 kFtmRunning = 3, ///< Trigger output enabled, configuration ignored
27 kFtmCalib = 4,
28 };
29
30 /// Command codes for FTM communication
31 enum Commands
32 {
33 // First word
34 kCmdRead = 0x0001, ///< Request data
35 kCmdWrite = 0x0002, ///< Send data
36 kCmdStartRun = 0x0004, ///< Enable the trigger output
37 kCmdStopRun = 0x0008, ///< Disable the trigger output
38 kCmdPing = 0x0010, ///< Ping all FTUs (get FTU list)
39 kCmdCrateReset = 0x0020, ///< Reboot (no power cycle) all FTUs and FADs of one crate
40 kCmdDisableReports = 0x0040, ///< Disable transmission of rate-reports (dynamic data)
41 kCmdToggleLed = 0xc000,
42
43 // second word for read and write
44 kCmdStaticData = 0x0001, ///< Specifies that static (configuration) data is read/written
45 kCmdDynamicData = 0x0002, ///< Specifies that dynamic data is read/written
46 kCmdRegister = 0x0004, ///< Specifies that a register is read/written
47
48 // second word for StartRun
49 kStartRun = 0x0001, ///< ...until kCmdStopRun
50 kTakeNevents = 0x0002, ///< ...fixed number of events
51 };
52
53
54 /// Types sent in the header of the following data
55 enum Types
56 {
57 kHeader = 0, ///< Local extension to identify a header in fCounter
58 kStaticData = 1, ///< Static (configuration) data
59 kDynamicData = 2, ///< Dynamic data (rates)
60 kFtuList = 3, ///< FTU list (answer of ping)
61 kErrorList = 4, ///< Error list (error when FTU communication failed)
62 kRegister = 5, ///< A requested register value
63 };
64
65 // --------------------------------------------------------------------
66
67 enum Delimiter
68 {
69 kDelimiterStart = 0xfb01, ///< Start delimiter send before each header
70 kDelimiterEnd = 0x04fe ///< End delimiter send after each data block
71 };
72
73 struct Header
74 {
75 uint16_t fDelimiter; ///< Start delimiter
76 uint16_t fType; ///< Type of the data to be received after the header
77 uint16_t fDataSize; ///< Size in words to be received after the header (incl end delim.)
78 uint16_t fState; ///< State of the FTM central state machine
79 uint64_t fBoardId; ///< FPGA device DNA (unique chip id)
80 uint16_t fFirmwareId; ///< Version number
81 uint32_t fTriggerCounter; ///< FTM internal counter of all trigger decision independant of trigger-line enable/disable (reset: start/stop run)
82 uint64_t fTimeStamp; ///< Internal counter (micro-seconds, reset: start/stop run)
83
84 Header() { init(*this); }
85
86 std::vector<uint16_t> HtoN() const
87 {
88 Header h(*this);
89
90 Reverse(&h.fBoardId);
91 Reverse(&h.fTriggerCounter);
92 Reverse(&h.fTimeStamp);
93
94 return htoncpy(h);
95 }
96 void operator=(const std::vector<uint16_t> &vec)
97 {
98 ntohcpy(vec, *this);
99
100 Reverse(&fBoardId);
101 Reverse(&fTriggerCounter);
102 Reverse(&fTimeStamp);
103 }
104
105 void clear() { reset(*this); }
106 void print(std::ostream &out) const;
107
108 } __attribute__((__packed__));
109
110 struct DimPassport
111 {
112 uint64_t fBoardId;
113 uint16_t fFirmwareId;
114
115 DimPassport(const Header &h) :
116 fBoardId(h.fBoardId),
117 fFirmwareId(h.fFirmwareId)
118 {
119 }
120 } __attribute__((__packed__));
121
122 struct DimTriggerCounter
123 {
124 uint64_t fTimeStamp;
125 uint32_t fTriggerCounter;
126
127 DimTriggerCounter(const Header &h) :
128 fTimeStamp(h.fTimeStamp),
129 fTriggerCounter(h.fTriggerCounter)
130 {
131 }
132 } __attribute__((__packed__));
133
134
135 struct StaticDataBoard
136 {
137 uint16_t fEnable[4]; /// enable of 4x9 pixels coded as 4x9bits
138 uint16_t fDAC[5]; /// 0-3 (A-D) Threshold of patches, 4 (H) Threshold for N out of 4 (12 bit each)
139 uint16_t fPrescaling; /// Internal readout time of FTUs for trigger counter
140
141 StaticDataBoard() { init(*this); }
142
143 void print(std::ostream &out) const;
144
145 } __attribute__((__packed__));
146
147 struct StaticData
148 {
149 enum Limits
150 {
151 kMaxMultiplicity = 40, ///< Minimum required trigger multiplicity
152 kMaxWindow = 0xf, ///< (4ns * x + 8ns) At least N (multiplicity) rising edges (trigger signal) within this window
153 kMaxDeadTime = 0xffff, ///< (4ns * x + 8ns)
154 kMaxDelayTimeMarker = 0x3ff, ///< (4ns * x + 8ns)
155 kMaxDelayTrigger = 0x3ff, ///< (4ns * x + 8ns)
156 kMaxTriggerInterval = 0x3ff, ///<
157 kMaxSequence = 0x1f,
158 kMaxDAC = 0xfff,
159 kMaxPixelIdx = 1439,
160 kMaskSettings = 0xf,
161 kMaskLEDs = 0xf,
162 };
163
164 enum GeneralSettings
165 {
166 kTrigger = 0x80, ///< Physics trigger decision (PhysicTrigger)
167 kPedestal = 0x40, ///< Pedestal trigger (artifical)
168 kLPint = 0x20, ///< Enable artificial trigger after light pulse (LP2)
169 kLPext = 0x10, ///< Enable trigger decision after light pulse (CalibrationTrigger, LP1)
170 kExt2 = 0x08, ///< External trigger signal 2
171 kExt1 = 0x04, ///< External trigger signal 1
172 kVeto = 0x02, ///< Veto trigger decision / artifical triggers
173 // -===> ClockCond
174 kTimeMarker = 0x01, ///< Select clock conditioner frequency (1) / time marker (0) as output
175 };
176
177 uint16_t fGeneralSettings; // Enable for different trigger types / select for TIM/ClockConditioner output (only 8 bit used)
178 uint16_t fStatusLEDs; // only 8 bit used
179 uint16_t fTriggerInterval; // [ms] Interval between two artificial triggers (no matter which type) minimum 1ms, 10 bit
180 uint16_t fTriggerSequence; // Ratio between trigger types send as artificial trigger (in this order) 3x5bit
181 uint64_t fDummy0;
182 uint16_t fMultiplicityPhysics; /// Required trigger multiplicity for physcis triggers (0-40)
183 uint16_t fMultiplicityCalib; /// Required trigger multiplicity calibration (LPext) triggers (0-40)
184 uint16_t fDelayTrigger; /// (4ns * x + 8ns) FTM internal programmable delay between trigger decision and output
185 uint16_t fDelayTimeMarker; /// (4ns * x + 8ns) FTM internal programmable delay between trigger descision and time marker output
186 uint16_t fDeadTime; /// (4ns * x + 8ns) FTM internal programmable dead time after trigger decision
187 uint32_t fClockConditioner[8]; // R0, R1, R8, R9, R11, R13, R14, R15
188 uint16_t fWindowPhysics; /// (4ns * x + 8ns) At least N (multiplicity) rising edges (trigger signal) within this window
189 uint16_t fWindowCalib; /// (4ns * x + 8ns) At least N (multiplicity) rising edges (trigger signal) within this window
190 uint16_t fDummy1;
191
192 StaticDataBoard fBoard[4][10]; // 4 crates * 10 boards (Crate0/FTU0 == readout time of FTUs)
193
194 uint16_t fActiveFTU[4]; // 4 crates * 10 bits (FTU enable)
195
196 StaticData() { init(*this); }
197
198 std::vector<uint16_t> HtoN() const
199 {
200 StaticData d(*this);
201 for (int i=0; i<8; i++)
202 Reverse(d.fClockConditioner+i);
203
204 return htoncpy(d);
205 }
206
207 void operator=(const std::vector<uint16_t> &vec)
208 {
209 ntohcpy(vec, *this);
210
211 for (int i=0; i<8; i++)
212 Reverse(fClockConditioner+i);
213 }
214
215 void clear() { reset(*this); }
216 void print(std::ostream &out) const;
217
218 StaticDataBoard &operator[](int i) { return fBoard[i/10][i%10]; }
219 const StaticDataBoard &operator[](int i) const { return fBoard[i/10][i%10]; }
220
221 void EnableFTU(int i) { fActiveFTU[i/10] |= (1<<(i%10)); }
222 void DisableFTU(int i) { fActiveFTU[i/10] &= ~(1<<(i%10)); }
223
224 void EnableAllFTU() { for (int i=0; i<4; i++) fActiveFTU[i] = 0x3ff; }
225 void DisableAllFTU() { for (int i=0; i<4; i++) fActiveFTU[i] = 0; }
226
227 void ToggleFTU(int i) { fActiveFTU[i/10] ^= (1<<(i%10)); }
228
229 void Enable(GeneralSettings type, bool enable)
230 {
231 if (enable)
232 fGeneralSettings |= uint16_t(type);
233 else
234 fGeneralSettings &= ~uint16_t(type); }
235
236 bool IsEnabled(GeneralSettings type) { return fGeneralSettings&uint16_t(type); }
237
238 void EnablePixel(int idx, bool enable)
239 {
240 const int board = idx%40;
241 const int patch = (idx/40)/9;
242 const int pixel = (idx/40)%9;
243
244 uint16_t &pix = fBoard[board/10][board%10].fEnable[patch];
245
246 if (enable)
247 pix |= (1<<pixel);
248 else
249 pix &= ~(1<<pixel);
250 }
251
252 uint8_t GetSequencePed() const { return (fTriggerSequence>>10)&0x1f; }
253 uint8_t GetSequenceLPint() const { return (fTriggerSequence>> 5)&0x1f; }
254 uint8_t GetSequenceLPext() const { return (fTriggerSequence) &0x1f; }
255
256 } __attribute__((__packed__));
257
258 // DimStructures must be a multiple of two... I don't know why
259 struct DimStaticData
260 {
261 uint64_t fTimeStamp;
262 //8
263 uint16_t fGeneralSettings; // only 8 bit used
264 uint16_t fStatusLEDs; // only 8 bit used
265 uint64_t fActiveFTU; // 40 bits in row
266 //20
267 uint16_t fTriggerInterval; // only 10 bit used
268 //22
269 uint16_t fTriggerSeqLPint; // only 5bits used
270 uint16_t fTriggerSeqLPext; // only 5bits used
271 uint16_t fTriggerSeqPed; // only 5bits used
272 //28
273 uint16_t fMultiplicityPhysics; // 0-40
274 uint16_t fMultiplicityCalib; // 0-40
275 //32
276 uint16_t fWindowPhysics;
277 uint16_t fWindowCalib;
278 //36
279 uint16_t fDelayTrigger;
280 uint16_t fDelayTimeMarker;
281 uint32_t fDeadTime;
282 //44
283 uint16_t fClockConditioner[8];
284 //60
285 uint16_t fEnable[90]; // 160*9bit = 180byte
286 uint16_t fThreshold[160];
287 uint16_t fMultiplicity[40]; // N out of 4
288 uint16_t fPrescaling[40];
289 // 640+60 = 700
290
291 bool HasTrigger() const { return fGeneralSettings & StaticData::kTrigger; }
292 bool HasPedestal() const { return fGeneralSettings & StaticData::kPedestal; }
293 bool HasLPext() const { return fGeneralSettings & StaticData::kLPext; }
294 bool HasLPint() const { return fGeneralSettings & StaticData::kLPint; }
295 bool HasExt2() const { return fGeneralSettings & StaticData::kExt2; }
296 bool HasExt1() const { return fGeneralSettings & StaticData::kExt1; }
297 bool HasVeto() const { return fGeneralSettings & StaticData::kVeto; }
298 bool HasTimeMarker() const { return fGeneralSettings & StaticData::kTimeMarker; }
299
300 bool IsActive(int i) const { return fActiveFTU&(uint64_t(1)<<i); }
301 bool IsEnabled(int i) const { return fEnable[i/16]&(1<<(i%16)); }
302
303 DimStaticData() { memset(this, 0, sizeof(DimStaticData)); }
304
305 DimStaticData(const Header &h, const StaticData &d) :
306 fTimeStamp(h.fTimeStamp),
307 fGeneralSettings(d.fGeneralSettings),
308 fStatusLEDs(d.fStatusLEDs),
309 fActiveFTU( uint64_t(d.fActiveFTU[0]) |
310 (uint64_t(d.fActiveFTU[1])<<10) |
311 (uint64_t(d.fActiveFTU[2])<<20) |
312 (uint64_t(d.fActiveFTU[3])<<30)),
313 fTriggerInterval(d.fTriggerInterval),
314 fTriggerSeqLPint((d.fTriggerSequence>>5)&0x1f),
315 fTriggerSeqLPext((d.fTriggerSequence)&0x1f),
316 fTriggerSeqPed((d.fTriggerSequence>>10)&0x1f),
317 fMultiplicityPhysics(d.fMultiplicityPhysics),
318 fMultiplicityCalib(d.fMultiplicityCalib),
319 fWindowPhysics(d.fWindowPhysics*4+8),
320 fWindowCalib(d.fWindowCalib*4+8),
321 fDelayTrigger(d.fDelayTrigger*4+8),
322 fDelayTimeMarker(d.fDelayTimeMarker*4+8),
323 fDeadTime(uint32_t(d.fDeadTime)*4+8)
324 {
325 memcpy(fClockConditioner, d.fClockConditioner, sizeof(uint16_t)*8);
326
327 uint16_t src[160];
328 for (int i=0; i<40; i++)
329 {
330 for (int j=0; j<4; j++)
331 {
332 src[i*4+j] = d[i].fEnable[j];
333 fThreshold[i*4+j] = d[i].fDAC[j];
334 }
335
336 fMultiplicity[i] = d[i].fDAC[4];
337 fPrescaling[i] = d[i].fPrescaling+1;
338 }
339 bitcpy(fEnable, 90, src, 160, 9);
340 }
341
342 } __attribute__((__packed__));
343
344
345 struct DynamicDataBoard
346 {
347 uint32_t fRatePatch[4]; // Patch 0,1,2,3
348 uint32_t fRateTotal; // Sum
349
350 uint16_t fOverflow; // Patches: bits 0-3, total 4
351 uint16_t fCrcError;
352
353 void print(std::ostream &out) const;
354
355 void reverse()
356 {
357 for (int i=0; i<4; i++)
358 Reverse(fRatePatch+i);
359
360 Reverse(&fRateTotal);
361 }
362
363 uint32_t &operator[](int i) { return fRatePatch[i]; }
364
365 } __attribute__((__packed__));
366
367
368 struct DynamicData
369 {
370 uint64_t fOnTimeCounter;
371 uint16_t fTempSensor[4]; // U45, U46, U48, U49
372
373 DynamicDataBoard fBoard[4][10]; // 4 crates * 10 boards
374
375 DynamicData() { init(*this); }
376
377 std::vector<uint16_t> HtoN() const
378 {
379 DynamicData d(*this);
380
381 Reverse(&d.fOnTimeCounter);
382
383 for (int c=0; c<4; c++)
384 for (int b=0; b<10; b++)
385 d.fBoard[c][b].reverse();
386
387 return htoncpy(d);
388 }
389
390 void operator=(const std::vector<uint16_t> &vec)
391 {
392 ntohcpy(vec, *this);
393
394 Reverse(&fOnTimeCounter);
395
396 for (int c=0; c<4; c++)
397 for (int b=0; b<10; b++)
398 fBoard[c][b].reverse();
399 }
400
401 void clear() { reset(*this); }
402 void print(std::ostream &out) const;
403
404 DynamicDataBoard &operator[](int i) { return fBoard[i/10][i%10]; }
405 const DynamicDataBoard &operator[](int i) const { return fBoard[i/10][i%10]; }
406
407 } __attribute__((__packed__));
408
409
410 struct DimDynamicData
411 {
412 uint64_t fTimeStamp;
413
414 uint64_t fOnTimeCounter;
415 float fTempSensor[4];
416
417 uint32_t fRatePatch[160];
418
419 uint32_t fRateBoard[40];
420 uint16_t fRateOverflow[40];
421
422 uint16_t fCrcError[40];
423
424 DimDynamicData(const Header &h, const DynamicData &d) :
425 fTimeStamp(h.fTimeStamp),
426 fOnTimeCounter(d.fOnTimeCounter)
427 {
428 for (int i=0; i<4; i++)
429 fTempSensor[i] = d.fTempSensor[i];
430
431 for (int i=0; i<40; i++)
432 {
433 fRateBoard[i] = d[i].fRateTotal;
434 fRateOverflow[i] = d[i].fOverflow;
435 fCrcError[i] = d[i].fCrcError;
436 for (int j=0; j<4; j++)
437 fRatePatch[i*4+j] = d[i].fRatePatch[j];
438 }
439 }
440
441 } __attribute__((__packed__));
442
443
444 struct FtuResponse
445 {
446 uint16_t fPingAddr; // Number of Pings and addr (pings= see error)
447 uint64_t fDNA;
448 uint16_t fErrorCounter; //
449
450 void reverse() { Reverse(&fDNA); }
451
452 void print(std::ostream &out) const;
453
454 } __attribute__((__packed__));
455
456 struct FtuList
457 {
458 uint16_t fNumBoards; /// Total number of boards responded
459 uint16_t fNumBoardsCrate[4]; /// Num of board responded in crate 0-3
460 uint16_t fActiveFTU[4]; /// List of active FTU boards in crate 0-3
461
462 FtuResponse fFTU[4][10];
463
464 FtuList() { init(*this); }
465
466 std::vector<uint16_t> HtoN() const
467 {
468 FtuList d(*this);
469
470 for (int c=0; c<4; c++)
471 for (int b=0; b<10; b++)
472 d.fFTU[c][b].reverse();
473
474 return htoncpy(d);
475 }
476
477 void operator=(const std::vector<uint16_t> &vec)
478 {
479 ntohcpy(vec, *this);
480
481 for (int c=0; c<4; c++)
482 for (int b=0; b<10; b++)
483 fFTU[c][b].reverse();
484 }
485
486 void clear() { reset(*this); }
487 void print(std::ostream &out) const;
488
489 FtuResponse &operator[](int i) { return fFTU[i/10][i%10]; }
490 const FtuResponse &operator[](int i) const { return fFTU[i/10][i%10]; }
491
492 } __attribute__((__packed__));
493
494 struct DimFtuList
495 {
496 uint64_t fTimeStamp;
497 uint64_t fActiveFTU;
498
499 uint16_t fNumBoards; /// Number of boards answered in total
500 uint8_t fNumBoardsCrate[4]; /// Number of boards answered per crate
501
502 uint64_t fDNA[40]; /// DNA of FTU board
503 uint8_t fAddr[40]; /// Address of FTU board
504 uint8_t fPing[40]; /// Number of pings until response (same as in Error)
505
506 DimFtuList(const Header &h, const FtuList &d) :
507 fTimeStamp(h.fTimeStamp),
508 fActiveFTU( uint64_t(d.fActiveFTU[0]) |
509 (uint64_t(d.fActiveFTU[1])<<10) |
510 (uint64_t(d.fActiveFTU[2])<<20) |
511 (uint64_t(d.fActiveFTU[3])<<30)),
512 fNumBoards(d.fNumBoards)
513 {
514 for (int i=0; i<4; i++)
515 fNumBoardsCrate[i] = d.fNumBoardsCrate[i];
516
517 for (int i=0; i<40; i++)
518 {
519 fDNA[i] = d[i].fDNA;
520 fAddr[i] = d[i].fPingAddr&0x3f;
521 fPing[i] = (d[i].fPingAddr>>8)&0x3;
522 }
523 }
524
525 bool IsActive(int i) const { return fActiveFTU&(uint64_t(1)<<i); }
526
527 } __attribute__((__packed__));
528
529
530 struct Error
531 {
532 uint16_t fNumCalls; // 0=error, >1 needed repetition but successfull
533
534 uint16_t fDelimiter;
535 uint16_t fDestAddress;
536 uint16_t fSrcAddress;
537 uint16_t fFirmwareId;
538 uint16_t fCommand;
539 uint16_t fData[21];
540 uint16_t fCrcErrorCounter;
541 uint16_t fCrcCheckSum;
542
543 Error() { init(*this); }
544
545 std::vector<uint16_t> HtoN() const
546 {
547 return htoncpy(*this);
548 }
549
550 void operator=(const std::vector<uint16_t> &vec) { ntohcpy(vec, *this); }
551
552 void clear() { reset(*this); }
553
554 uint16_t &operator[](int idx) { return fData[idx]; }
555 const uint16_t &operator[](int idx) const { return fData[idx]; }
556
557 void print(std::ostream &out) const;
558
559 } __attribute__((__packed__));
560
561 struct DimError
562 {
563 uint64_t fTimeStamp;
564 Error fError;
565
566 DimError(const Header &h, const Error &e) :
567 fTimeStamp(h.fTimeStamp),
568 fError(e)
569 {
570 fError.fDestAddress = (e.fDestAddress&0x3)*10 + ((e.fDestAddress>>2)&0xf);
571 fError.fSrcAddress = (e.fSrcAddress &0x3)*10 + ((e.fSrcAddress >>2)&0xf);
572 }
573
574 } __attribute__((__packed__));
575
576 /*
577 struct Command
578 {
579 uint16_t fStartDelimiter;
580 uint16_t fCommand;
581 uint16_t fParam[3];
582
583 Command() { init(*this); }
584
585 void HtoN() { hton(*this); }
586 void NtoH() { ntoh(*this); }
587
588 void operator=(const std::vector<uint16_t> &vec) { ntohcpy(vec, *this); }
589
590 void clear() { reset(*this); }
591
592
593 } __attribute__((__packed__));
594 */
595
596 // --------------------------------------------------------------------
597
598 inline std::ostream &operator<<(std::ostream &out, const FtuResponse &h)
599 {
600 h.print(out);
601 return out;
602 }
603
604 inline std::ostream &operator<<(std::ostream &out, const Header &h)
605 {
606 h.print(out);
607 return out;
608 }
609
610
611 inline std::ostream &operator<<(std::ostream &out, const FtuList &h)
612 {
613 h.print(out);
614 return out;
615 }
616
617 inline std::ostream &operator<<(std::ostream &out, const DynamicDataBoard &h)
618 {
619 h.print(out);
620 return out;
621 }
622
623 inline std::ostream &operator<<(std::ostream &out, const DynamicData &h)
624 {
625 h.print(out);
626 return out;
627 }
628
629 inline std::ostream &operator<<(std::ostream &out, const StaticDataBoard &h)
630 {
631 h.print(out);
632 return out;
633 }
634
635 inline std::ostream &operator<<(std::ostream &out, const StaticData &h)
636 {
637 h.print(out);
638 return out;
639 }
640
641 inline std::ostream &operator<<(std::ostream &out, const Error &h)
642 {
643 h.print(out);
644 return out;
645 }
646};
647
648#endif
Note: See TracBrowser for help on using the repository browser.