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