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