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