| 1 | #ifndef FACT_HeadersFAD
|
|---|
| 2 | #define FACT_HeadersFAD
|
|---|
| 3 |
|
|---|
| 4 | #ifdef __cplusplus
|
|---|
| 5 | #include <ostream>
|
|---|
| 6 |
|
|---|
| 7 | // For debugging
|
|---|
| 8 | #include <iostream>
|
|---|
| 9 |
|
|---|
| 10 | #include "ByteOrder.h"
|
|---|
| 11 |
|
|---|
| 12 | // ====================================================================
|
|---|
| 13 |
|
|---|
| 14 | namespace FAD
|
|---|
| 15 | {
|
|---|
| 16 | #endif
|
|---|
| 17 | enum Enable
|
|---|
| 18 | {
|
|---|
| 19 | kCmdDrsEnable = 0x0600, // CMD_DENABLE/CMD_DISABLE
|
|---|
| 20 | kCmdDwrite = 0x0800, // CMD_DWRITE_RUN/CMD_DWRITE_STOP
|
|---|
| 21 | kCmdSclk = 0x1000, // CMD_SCLK_ON/OFF
|
|---|
| 22 | kCmdSrclk = 0x1500, // CMD_SRCLK_ON/OFF
|
|---|
| 23 | kCmdTriggerLine = 0x1800, // CMD_TRIGGERS_ON/CMD_TRIGGERS_OFF
|
|---|
| 24 | kCmdContTrigger = 0x1f00,
|
|---|
| 25 | kCmdRun = 0x2200, // CMD_Start/Stop
|
|---|
| 26 | kCmdBusyOff = 0x2400, //
|
|---|
| 27 | kCmdBusyOn = 0x3200, //
|
|---|
| 28 | kCmdResetEventCounter = 0x2A00, //
|
|---|
| 29 | kCmdSocket = 0x3000, // CMD_mode_command/CMD_mode_all_sockets
|
|---|
| 30 | kCmdSingleTrigger = 0xA000, // CMD_Trigger
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | enum Commands
|
|---|
| 34 | {
|
|---|
| 35 | kCmdWriteExecute = 0x0400, // Configure FAD with the current config ram
|
|---|
| 36 |
|
|---|
| 37 | kCmdWrite = 0x0500, // write to Config-RAM
|
|---|
| 38 | kCmdWriteRoi = kCmdWrite|0x00, // Baseaddress ROI-Values
|
|---|
| 39 | kCmdWriteDac = kCmdWrite|0x24, // Baseaddress DAC-Values
|
|---|
| 40 |
|
|---|
| 41 | kCmdWriteRate = kCmdWrite|0x2c, // Continous trigger rate
|
|---|
| 42 | kCmdWriteRunNumberMSW = kCmdWrite|0x2d, // Run Number most significant word
|
|---|
| 43 | kCmdWriteRunNumberLSW = kCmdWrite|0x2e, // Run Number least significant word
|
|---|
| 44 |
|
|---|
| 45 | /*
|
|---|
| 46 | kCmdRead = 0x0a00, // read from Config-RAM
|
|---|
| 47 | kCmdReadRoi = kCmdRead|0x00, // Baseaddress ROI-Values
|
|---|
| 48 | kCmdReadDac = kCmdRead|0x24, // Baseaddress DAC-Values
|
|---|
| 49 | */
|
|---|
| 50 |
|
|---|
| 51 | kCmdPhaseIncrease = 0x1200, // CMD_PS_DIRINC
|
|---|
| 52 | kCmdPhaseDecrease = 0x1300, // CMD_PS_DIRDEC
|
|---|
| 53 | kCmdPhaseApply = 0x1400, // CMD_PS_DO
|
|---|
| 54 | kCmdPhaseReset = 0x1700, // CMD_PS_RESET
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | enum States
|
|---|
| 58 | {
|
|---|
| 59 | // State Machine states
|
|---|
| 60 | kOffline = 1,
|
|---|
| 61 | kDisconnected,
|
|---|
| 62 | kConnecting,
|
|---|
| 63 | kConnected,
|
|---|
| 64 | kConfiguring1,
|
|---|
| 65 | kConfiguring2,
|
|---|
| 66 | kConfigured
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | enum
|
|---|
| 70 | {
|
|---|
| 71 | kMaxBins = 1024,
|
|---|
| 72 | kNumTemp = 4,
|
|---|
| 73 | kNumDac = 8,
|
|---|
| 74 | kNumChips = 4,
|
|---|
| 75 | kNumChannelsPerChip = 9,
|
|---|
| 76 | kNumChannels = kNumChips*kNumChannelsPerChip,
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | enum
|
|---|
| 80 | {
|
|---|
| 81 | kMaxRegAddr = 0xff, // Highest address in config-ram
|
|---|
| 82 | kMaxRegValue = 0xffff,
|
|---|
| 83 | kMaxDacAddr = kNumDac-1,
|
|---|
| 84 | kMaxDacValue = 0xffff,
|
|---|
| 85 | kMaxRoiAddr = kNumChannels-1,
|
|---|
| 86 | kMaxRoiValue = kMaxBins,
|
|---|
| 87 | kMaxRunNumber = 0xffffffff,
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | enum
|
|---|
| 91 | {
|
|---|
| 92 | kDelimiterStart = 0xfb01,
|
|---|
| 93 | kDelimiterEnd = 0x04fe,
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | // --------------------------------------------------------
|
|---|
| 97 |
|
|---|
| 98 | struct EventHeader
|
|---|
| 99 | {
|
|---|
| 100 | #ifdef __cplusplus
|
|---|
| 101 | enum Bits
|
|---|
| 102 | {
|
|---|
| 103 | kDenable = 1<<11,
|
|---|
| 104 | kDwrite = 1<<10,
|
|---|
| 105 | //kRefClkTooHigh = 1<< 9,
|
|---|
| 106 | kRefClkTooLow = 1<< 8,
|
|---|
| 107 | kDcmLocked = 1<< 7,
|
|---|
| 108 | kDcmReady = 1<< 6,
|
|---|
| 109 | kSpiSclk = 1<< 5,
|
|---|
| 110 | kBusyOff = 1<< 4, // Busy continously off
|
|---|
| 111 | kTriggerLine = 1<< 3, // Trigger line enabled
|
|---|
| 112 | kContTrigger = 1<< 2, // Cont trigger enabled
|
|---|
| 113 | kSock17 = 1<< 1, // Socket 1-7 for data transfer
|
|---|
| 114 | kBusyOn = 1<< 0, // Busy continously on
|
|---|
| 115 | };
|
|---|
| 116 | #endif
|
|---|
| 117 | // Einmalig: (new header changes entry in array --> send only if array changed)
|
|---|
| 118 | // ----------------------------------
|
|---|
| 119 | // Event builder stores an array with all available values.
|
|---|
| 120 | // Disconnected boards are removed (replaced by def values)
|
|---|
| 121 | // Any received header information is immediately put in the array.
|
|---|
| 122 | // The array is transmitted whenever it changes.
|
|---|
| 123 | // This will usually happen only very rarely when a new connection
|
|---|
| 124 | // is opened.
|
|---|
| 125 | //
|
|---|
| 126 | // Array[40] of BoardId
|
|---|
| 127 | // Array[40] of Version
|
|---|
| 128 | // Array[40] of DNA
|
|---|
| 129 |
|
|---|
| 130 | // Slow changes: (new header changes entry in array --> send only if arra changed)
|
|---|
| 131 | // -------------------------------------------
|
|---|
| 132 | // Event builder stores an array with all available values.
|
|---|
| 133 | // Disconnected boards can be kept in the arrays.
|
|---|
| 134 | // Any received header information is immediately put in the array.
|
|---|
| 135 | // The array is transmitted whenever it changes.
|
|---|
| 136 | //
|
|---|
| 137 | // Connection status (disconnected, connecting, connected) / Array[40]
|
|---|
| 138 | // Consistency of PLLLCK / Array[ 40] of PLLLCK
|
|---|
| 139 | // Consistency of Trigger type / Array[ 40] of trigger type
|
|---|
| 140 | // Consistency of ROI / Array[1440] of ROI
|
|---|
| 141 | // Consistency of RefClock / Array[ 40] of ref clock
|
|---|
| 142 | // Consistency of DAC values / Array[ 400] of DAC values
|
|---|
| 143 | // Consistency of run number / Array[ 40] of Run numbers
|
|---|
| 144 |
|
|---|
| 145 | // Fast changes (new header changes value --> send only if something changed)
|
|---|
| 146 | // -------------------
|
|---|
| 147 | // Event builder stores an internal array of all boards and
|
|---|
| 148 | // transmits the min/max values determined from the array
|
|---|
| 149 | // only if they have changed. Disconnected boards are not considered.
|
|---|
| 150 | //
|
|---|
| 151 | // Maximum/minimum Event counter of all boards in memory + board id
|
|---|
| 152 | // Maximum/minimum time stamp of all boards in memory + board id
|
|---|
| 153 | // Maximum/minimum temp of all boards in memory + board id
|
|---|
| 154 |
|
|---|
| 155 | // Unknown:
|
|---|
| 156 | // ------------------
|
|---|
| 157 | // Trigger Id ?
|
|---|
| 158 | // TriggerGeneratorPrescaler ?
|
|---|
| 159 | // Number of Triggers to generate ?
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | // ------------------------------------------------------------
|
|---|
| 163 |
|
|---|
| 164 | uint16_t fStartDelimiter; // 0x04FE
|
|---|
| 165 | uint16_t fPackageLength;
|
|---|
| 166 | uint16_t fVersion;
|
|---|
| 167 | uint16_t fStatus;
|
|---|
| 168 | //
|
|---|
| 169 | uint16_t fTriggerCrc; // Receiver timeout / CRC ; 1 byte each
|
|---|
| 170 | uint16_t fTriggerType;
|
|---|
| 171 | uint32_t fTriggerCounter;
|
|---|
| 172 | //
|
|---|
| 173 | uint32_t fEventCounter;
|
|---|
| 174 | uint32_t fFreqRefClock;
|
|---|
| 175 | //
|
|---|
| 176 | uint16_t fBoardId;
|
|---|
| 177 | uint16_t fAdcClockPhaseShift;
|
|---|
| 178 | uint16_t fNumTriggersToGenerate;
|
|---|
| 179 | uint16_t fTriggerGeneratorPrescaler;
|
|---|
| 180 | //
|
|---|
| 181 | uint64_t fDNA; // Xilinx DNA
|
|---|
| 182 | //
|
|---|
| 183 | uint32_t fTimeStamp;
|
|---|
| 184 | uint32_t fRunNumber;
|
|---|
| 185 | //
|
|---|
| 186 | int16_t fTempDrs[kNumTemp]; // In units of 1/16 deg(?)
|
|---|
| 187 | //
|
|---|
| 188 | uint16_t fDac[kNumDac];
|
|---|
| 189 | //
|
|---|
| 190 | #ifdef __cplusplus
|
|---|
| 191 | EventHeader() { init(*this); }
|
|---|
| 192 | EventHeader(const uint16_t *ptr)
|
|---|
| 193 | {
|
|---|
| 194 | *this = std::vector<uint16_t>(ptr, ptr+sizeof(EventHeader)/2);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | void operator=(const std::vector<uint16_t> &vec)
|
|---|
| 198 | {
|
|---|
| 199 | ntohcpy(vec, *this);
|
|---|
| 200 |
|
|---|
| 201 | Reverse(((uint16_t*)fEventCounter));
|
|---|
| 202 | Reverse(((uint16_t*)fEventCounter)+1);
|
|---|
| 203 |
|
|---|
| 204 | Reverse(&fEventCounter);
|
|---|
| 205 |
|
|---|
| 206 | Reverse(((uint16_t*)fTriggerCounter));
|
|---|
| 207 | Reverse(((uint16_t*)fTriggerCounter)+1);
|
|---|
| 208 |
|
|---|
| 209 | Reverse(&fTriggerCounter);
|
|---|
| 210 |
|
|---|
| 211 | Reverse(((uint16_t*)fFreqRefClock));
|
|---|
| 212 | Reverse(((uint16_t*)fFreqRefClock)+1);
|
|---|
| 213 |
|
|---|
| 214 | Reverse(&fFreqRefClock);
|
|---|
| 215 |
|
|---|
| 216 | Reverse(((uint16_t*)&fTimeStamp));
|
|---|
| 217 | Reverse(((uint16_t*)&fTimeStamp)+1);
|
|---|
| 218 |
|
|---|
| 219 | Reverse(&fTimeStamp);
|
|---|
| 220 |
|
|---|
| 221 | Reverse(((uint16_t*)&fRunNumber));
|
|---|
| 222 | Reverse(((uint16_t*)&fRunNumber)+1);
|
|---|
| 223 |
|
|---|
| 224 | Reverse(&fRunNumber);
|
|---|
| 225 | Reverse(&fDNA);
|
|---|
| 226 |
|
|---|
| 227 | /*
|
|---|
| 228 | for (int i=0; i<kNumTemp; i++)
|
|---|
| 229 | {
|
|---|
| 230 | fTempDrs[i] =
|
|---|
| 231 | (fTempDrs[i]&0x8000) ?
|
|---|
| 232 | ((fTempDrs[i]&0x007fff)^0xffffffff) :
|
|---|
| 233 | (fTempDrs[i]&0x007fff);
|
|---|
| 234 | fTempDrs[i]>>=3;
|
|---|
| 235 | }*/
|
|---|
| 236 |
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | std::vector<uint16_t> HtoN() const
|
|---|
| 240 | {
|
|---|
| 241 | EventHeader h(*this);
|
|---|
| 242 |
|
|---|
| 243 | Reverse(((uint16_t*)h.fEventCounter));
|
|---|
| 244 | Reverse(((uint16_t*)h.fEventCounter)+1);
|
|---|
| 245 |
|
|---|
| 246 | Reverse(&h.fEventCounter);
|
|---|
| 247 |
|
|---|
| 248 | Reverse(((uint16_t*)h.fFreqRefClock));
|
|---|
| 249 | Reverse(((uint16_t*)h.fFreqRefClock)+1);
|
|---|
| 250 |
|
|---|
| 251 | Reverse(&h.fFreqRefClock);
|
|---|
| 252 |
|
|---|
| 253 | Reverse(((uint16_t*)&h.fTimeStamp));
|
|---|
| 254 | Reverse(((uint16_t*)&h.fTimeStamp)+1);
|
|---|
| 255 |
|
|---|
| 256 | Reverse(&h.fTimeStamp);
|
|---|
| 257 |
|
|---|
| 258 | Reverse(((uint16_t*)&h.fRunNumber));
|
|---|
| 259 | Reverse(((uint16_t*)&h.fRunNumber)+1);
|
|---|
| 260 |
|
|---|
| 261 | Reverse(&h.fRunNumber);
|
|---|
| 262 | Reverse(&h.fDNA);
|
|---|
| 263 |
|
|---|
| 264 | /*
|
|---|
| 265 | for (int i=0; i<kNumTemp; i++)
|
|---|
| 266 | {
|
|---|
| 267 | h.fTempDrs[i] <<= 3;
|
|---|
| 268 | h.fTempDrs[i] = ((h.fTempDrs[i]&0x800000) ?
|
|---|
| 269 | (((h.fTempDrs[i]^0xffffffff)&0x007fff)|0x8000) :
|
|---|
| 270 | (h.fTempDrs[i]&0x007fff));
|
|---|
| 271 | }*/
|
|---|
| 272 |
|
|---|
| 273 | return htoncpy(h);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | bool operator==(const EventHeader &h) const
|
|---|
| 277 | {
|
|---|
| 278 | return
|
|---|
| 279 | fStatus == h.fStatus &&
|
|---|
| 280 | fRunNumber == h.fRunNumber &&
|
|---|
| 281 | fEventCounter == h.fEventCounter &&
|
|---|
| 282 | fAdcClockPhaseShift == h.fAdcClockPhaseShift &&
|
|---|
| 283 | fTriggerGeneratorPrescaler == h.fTriggerGeneratorPrescaler &&
|
|---|
| 284 | memcmp(fDac, h.fDac, sizeof(fDac))==0;
|
|---|
| 285 | }
|
|---|
| 286 | bool operator!=(const EventHeader &h) const { return !operator==(h); }
|
|---|
| 287 |
|
|---|
| 288 | float GetTemp(int i) const { return fTempDrs[i]/16.; }
|
|---|
| 289 |
|
|---|
| 290 | /*
|
|---|
| 291 | float GetTemp(int i) const
|
|---|
| 292 | {
|
|---|
| 293 | return (((fTempDrs[i]&0x8000) ?
|
|---|
| 294 | ((fTempDrs[i]&0x007fff)^0xffffffff)
|
|---|
| 295 | : (fTempDrs[i]&0x007fff))>>3)/16.; }*/
|
|---|
| 296 |
|
|---|
| 297 | uint8_t PLLLCK() const { return fStatus>>12; }
|
|---|
| 298 |
|
|---|
| 299 | bool HasDenable() const { return fStatus&kDenable; }
|
|---|
| 300 | bool HasDwrite() const { return fStatus&kDwrite; }
|
|---|
| 301 | // bool IsRefClockTooHigh() const { return fStatus&kRefClkTooHigh; }
|
|---|
| 302 | bool IsRefClockTooLow() const { return fStatus&kRefClkTooLow; }
|
|---|
| 303 | bool IsDcmLocked() const { return fStatus&kDcmLocked; }
|
|---|
| 304 | bool IsDcmReady() const { return fStatus&kDcmReady; }
|
|---|
| 305 | bool HasSpiSclk() const { return fStatus&kSpiSclk; }
|
|---|
| 306 | bool HasBusyOn() const { return fStatus&kBusyOn; }
|
|---|
| 307 | bool HasBusyOff() const { return fStatus&kBusyOff; }
|
|---|
| 308 | bool HasTriggerEnabled() const { return fStatus&kTriggerLine; }
|
|---|
| 309 | bool HasContTriggerEnabled() const { return fStatus&kContTrigger; }
|
|---|
| 310 | bool IsInSock17Mode() const { return fStatus&kSock17; }
|
|---|
| 311 |
|
|---|
| 312 | uint16_t Crate() const { return fBoardId>>8; }
|
|---|
| 313 | uint16_t Board() const { return fBoardId&0xff; }
|
|---|
| 314 |
|
|---|
| 315 | uint16_t Id() const { return Crate()*10+Board(); }
|
|---|
| 316 |
|
|---|
| 317 | void Enable(Bits pos, bool enable=true)
|
|---|
| 318 | {
|
|---|
| 319 | if (enable)
|
|---|
| 320 | fStatus |= pos;
|
|---|
| 321 | else
|
|---|
| 322 | fStatus &= ~pos;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | void clear() { reset(*this); }
|
|---|
| 326 | void print(std::ostream &out) const;
|
|---|
| 327 | #endif
|
|---|
| 328 |
|
|---|
| 329 | } __attribute__((__packed__));
|
|---|
| 330 |
|
|---|
| 331 | struct ChannelHeader
|
|---|
| 332 | {
|
|---|
| 333 | uint16_t fId;
|
|---|
| 334 | uint16_t fStartCell;
|
|---|
| 335 | uint16_t fRegionOfInterest;
|
|---|
| 336 | uint16_t fDummy;
|
|---|
| 337 | // uint16_t fData[];
|
|---|
| 338 |
|
|---|
| 339 | #ifdef __cplusplus
|
|---|
| 340 | ChannelHeader() { init(*this); }
|
|---|
| 341 |
|
|---|
| 342 | void operator=(const std::vector<uint16_t> &vec)
|
|---|
| 343 | {
|
|---|
| 344 | ntohcpy(vec, *this);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | std::vector<uint16_t> HtoN() const
|
|---|
| 348 | {
|
|---|
| 349 | ChannelHeader h(*this);
|
|---|
| 350 | return htoncpy(h);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | void clear() { reset(*this); }
|
|---|
| 354 | void print(std::ostream &out) const;
|
|---|
| 355 |
|
|---|
| 356 | uint16_t Chip() const { return fId>>4; }
|
|---|
| 357 | uint16_t Channel() const { return fId&0xf; }
|
|---|
| 358 | #endif
|
|---|
| 359 | } __attribute__((__packed__));
|
|---|
| 360 |
|
|---|
| 361 | // Package ends with:
|
|---|
| 362 | // 0x4242
|
|---|
| 363 | // 0x04fe
|
|---|
| 364 |
|
|---|
| 365 | struct Configuration
|
|---|
| 366 | {
|
|---|
| 367 | bool fDwrite;
|
|---|
| 368 | bool fDenable;
|
|---|
| 369 | bool fContinousTrigger;
|
|---|
| 370 | uint16_t fTriggerRate;
|
|---|
| 371 | uint16_t fRoi[FAD::kNumChannelsPerChip];
|
|---|
| 372 | uint16_t fDac[FAD::kNumDac];
|
|---|
| 373 |
|
|---|
| 374 | #ifdef __cplusplus
|
|---|
| 375 | Configuration() { init(*this); }
|
|---|
| 376 | #endif
|
|---|
| 377 | };
|
|---|
| 378 |
|
|---|
| 379 | // --------------------------------------------------------------------
|
|---|
| 380 | #ifdef __cplusplus
|
|---|
| 381 | inline std::ostream &operator<<(std::ostream &out, const EventHeader &h)
|
|---|
| 382 | {
|
|---|
| 383 | h.print(out);
|
|---|
| 384 | return out;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | inline std::ostream &operator<<(std::ostream &out, const ChannelHeader &h)
|
|---|
| 388 | {
|
|---|
| 389 | h.print(out);
|
|---|
| 390 | return out;
|
|---|
| 391 | }
|
|---|
| 392 | #endif
|
|---|
| 393 |
|
|---|
| 394 | #ifdef __cplusplus
|
|---|
| 395 | };
|
|---|
| 396 | #endif
|
|---|
| 397 |
|
|---|
| 398 | #endif
|
|---|