1 | #ifndef FACT_HeadersFAD
|
---|
2 | #define FACT_HeadersFAD
|
---|
3 |
|
---|
4 | #include <ostream>
|
---|
5 |
|
---|
6 | // For debugging
|
---|
7 | #include <iostream>
|
---|
8 |
|
---|
9 | #include "ByteOrder.h"
|
---|
10 |
|
---|
11 | // ====================================================================
|
---|
12 |
|
---|
13 | namespace FAD
|
---|
14 | {
|
---|
15 | enum States
|
---|
16 | {
|
---|
17 | // State Machine states
|
---|
18 | kDisconnected = 1,
|
---|
19 | kConnecting,
|
---|
20 | kConnected
|
---|
21 | };
|
---|
22 |
|
---|
23 | enum
|
---|
24 | {
|
---|
25 | kMaxBins = 1024,
|
---|
26 | kNumTemp = 4,
|
---|
27 | kNumDac = 8,
|
---|
28 | kNumChips = 4,
|
---|
29 | kNumChannelsPerChip = 9,
|
---|
30 | kNumChannels = kNumChips*kNumChannelsPerChip,
|
---|
31 | };
|
---|
32 |
|
---|
33 | enum
|
---|
34 | {
|
---|
35 | kMaxRegAddr = 0xff, // Highest address in config-ram
|
---|
36 | kMaxRegValue = 0xffff,
|
---|
37 | kMaxDacAddr = kNumDac-1,
|
---|
38 | kMaxDacValue = 0xffff,
|
---|
39 | kMaxRoiAddr = kNumChannels-1,
|
---|
40 | kMaxRoiValue = kMaxBins,
|
---|
41 | };
|
---|
42 |
|
---|
43 | enum
|
---|
44 | {
|
---|
45 | kDelimiterStart = 0xfb01,
|
---|
46 | kDelimiterEnd = 0x04fe,
|
---|
47 | };
|
---|
48 |
|
---|
49 | // --------------------------------------------------------
|
---|
50 |
|
---|
51 | struct EventHeader
|
---|
52 | {
|
---|
53 | enum Bits
|
---|
54 | {
|
---|
55 | kDenable = 1<<11,
|
---|
56 | kDwrite = 1<<10,
|
---|
57 | kRefClkTooHigh = 1<< 9,
|
---|
58 | kRefClkTooLow = 1<< 8,
|
---|
59 | kDcmLocked = 1<< 7,
|
---|
60 | kDcmReady = 1<< 6,
|
---|
61 | kSpiSclk = 1<< 5,
|
---|
62 | };
|
---|
63 |
|
---|
64 | // Einmalig: (new header changes entry in array --> send only if array changed)
|
---|
65 | // ----------------------------------
|
---|
66 | // Event builder stores an array with all available values.
|
---|
67 | // Disconnected boards are removed (replaced by def values)
|
---|
68 | // Any received header information is immediately put in the array.
|
---|
69 | // The array is transmitted whenever it changes.
|
---|
70 | // This will usually happen only very rarely when a new connection
|
---|
71 | // is opened.
|
---|
72 | //
|
---|
73 | // Array[40] of BoardId
|
---|
74 | // Array[40] of Version
|
---|
75 | // Array[40] of DNA
|
---|
76 |
|
---|
77 | // Slow changes: (new header changes entry in array --> send only if arra changed)
|
---|
78 | // -------------------------------------------
|
---|
79 | // Event builder stores an array with all available values.
|
---|
80 | // Disconnected boards can be kept in the arrays.
|
---|
81 | // Any received header information is immediately put in the array.
|
---|
82 | // The array is transmitted whenever it changes.
|
---|
83 | //
|
---|
84 | // Connection status (disconnected, connecting, connected) / Array[40]
|
---|
85 | // Consistency of PLLLCK / Array[ 40] of PLLLCK
|
---|
86 | // Consistency of Trigger type / Array[ 40] of trigger type
|
---|
87 | // Consistency of ROI / Array[1440] of ROI
|
---|
88 | // Consistency of RefClock / Array[ 40] of ref clock
|
---|
89 | // Consistency of DAC values / Array[ 400] of DAC values
|
---|
90 | // Consistency of run number / Array[ 40] of Run numbers
|
---|
91 |
|
---|
92 | // Fast changes (new header changes value --> send only if something changed)
|
---|
93 | // -------------------
|
---|
94 | // Event builder stores an internal array of all boards and
|
---|
95 | // transmits the min/max values determined from the array
|
---|
96 | // only if they have changed. Disconnected boards are not considered.
|
---|
97 | //
|
---|
98 | // Maximum/minimum Event counter of all boards in memory + board id
|
---|
99 | // Maximum/minimum time stamp of all boards in memory + board id
|
---|
100 | // Maximum/minimum temp of all boards in memory + board id
|
---|
101 |
|
---|
102 | // Unknown:
|
---|
103 | // ------------------
|
---|
104 | // Trigger Id ?
|
---|
105 | // TriggerGeneratorPrescaler ?
|
---|
106 | // Number of Triggers to generate ?
|
---|
107 |
|
---|
108 |
|
---|
109 | // ------------------------------------------------------------
|
---|
110 |
|
---|
111 | uint16_t fStartDelimiter; // 0x04FE
|
---|
112 | uint16_t fPackageLength;
|
---|
113 | uint16_t fVersion;
|
---|
114 | uint16_t fStatus;
|
---|
115 | //
|
---|
116 | uint16_t fTriggerCrc;
|
---|
117 | uint16_t fTriggerType;
|
---|
118 | uint32_t fTriggerId;
|
---|
119 | //
|
---|
120 | uint32_t fEventCounter;
|
---|
121 | uint32_t fFreqRefClock;
|
---|
122 | //
|
---|
123 | uint16_t fBoardId;
|
---|
124 | uint16_t fAdcClockPhaseShift;
|
---|
125 | uint16_t fNumTriggersToGenerate;
|
---|
126 | uint16_t fTriggerGeneratorPrescaler;
|
---|
127 | //
|
---|
128 | uint64_t fDNA; // Xilinx DNA
|
---|
129 | //
|
---|
130 | uint32_t fTimeStamp;
|
---|
131 | uint32_t fRunNumber;
|
---|
132 | //
|
---|
133 | int16_t fTempDrs[kNumTemp]; // In units of 1/16 deg(?)
|
---|
134 | //
|
---|
135 | uint16_t fDac[kNumDac];
|
---|
136 | //
|
---|
137 |
|
---|
138 | EventHeader() { init(*this); }
|
---|
139 |
|
---|
140 | void operator=(const std::vector<uint16_t> &vec)
|
---|
141 | {
|
---|
142 | ntohcpy(vec, *this);
|
---|
143 |
|
---|
144 | Reverse(((uint16_t*)fEventCounter));
|
---|
145 | Reverse(((uint16_t*)fEventCounter)+1);
|
---|
146 |
|
---|
147 | Reverse(&fEventCounter);
|
---|
148 |
|
---|
149 | Reverse(((uint16_t*)fFreqRefClock));
|
---|
150 | Reverse(((uint16_t*)fFreqRefClock)+1);
|
---|
151 |
|
---|
152 | Reverse(&fFreqRefClock);
|
---|
153 |
|
---|
154 | Reverse(((uint16_t*)&fTimeStamp));
|
---|
155 | Reverse(((uint16_t*)&fTimeStamp)+1);
|
---|
156 |
|
---|
157 | Reverse(&fTimeStamp);
|
---|
158 |
|
---|
159 | Reverse(((uint16_t*)&fRunNumber));
|
---|
160 | Reverse(((uint16_t*)&fRunNumber)+1);
|
---|
161 |
|
---|
162 | Reverse(&fRunNumber);
|
---|
163 | Reverse(&fDNA);
|
---|
164 |
|
---|
165 | /*
|
---|
166 | // Extract temperatures (MSB indicates if temperature
|
---|
167 | // is positive or negative)
|
---|
168 | for (int i=0; i<kNumTemp; i++)
|
---|
169 | {
|
---|
170 | if (fTempDrs[i]&0x8000)
|
---|
171 | fTempDrs[i] |= 0xE000;
|
---|
172 | fTempDrs[i]>>=3;
|
---|
173 | }
|
---|
174 | */
|
---|
175 | }
|
---|
176 |
|
---|
177 | float GetTemp(int i) const
|
---|
178 | {
|
---|
179 | return (((fTempDrs[i]&0x8000)
|
---|
180 | ?
|
---|
181 | ((fTempDrs[i]&0x007fff)^0xffffffff)
|
---|
182 | : (fTempDrs[i]&0x007fff))>>3)/16.; }
|
---|
183 |
|
---|
184 | uint8_t PLLLCK() const { return fStatus>>12; }
|
---|
185 |
|
---|
186 | bool HasDenable() const { return fStatus&kDenable; }
|
---|
187 | bool HasDwrite() const { return fStatus&kDwrite; }
|
---|
188 | bool IsRefClockTooHigh() const { return fStatus&kRefClkTooHigh; }
|
---|
189 | bool IsRefClockTooLow() const { return fStatus&kRefClkTooLow; }
|
---|
190 | bool IsDcmLocked() const { return fStatus&kDcmLocked; }
|
---|
191 | bool IsDcmReady() const { return fStatus&kDcmReady; }
|
---|
192 | bool HasSpiSclk() const { return fStatus&kSpiSclk; }
|
---|
193 |
|
---|
194 | void clear() { reset(*this); }
|
---|
195 | void print(std::ostream &out) const;
|
---|
196 |
|
---|
197 | } __attribute__((__packed__));
|
---|
198 |
|
---|
199 | struct ChannelHeader
|
---|
200 | {
|
---|
201 | uint16_t fId;
|
---|
202 | uint16_t fStartCell;
|
---|
203 | uint16_t fRegionOfInterest;
|
---|
204 | uint16_t fDummy;
|
---|
205 | // uint16_t fData[];
|
---|
206 |
|
---|
207 | ChannelHeader() { init(*this); }
|
---|
208 |
|
---|
209 | void operator=(const std::vector<uint16_t> &vec)
|
---|
210 | {
|
---|
211 | ntohcpy(vec, *this);
|
---|
212 | }
|
---|
213 |
|
---|
214 | void clear() { reset(*this); }
|
---|
215 | void print(std::ostream &out) const;
|
---|
216 |
|
---|
217 | } __attribute__((__packed__));
|
---|
218 |
|
---|
219 | // Package ends with:
|
---|
220 | // 0x4242
|
---|
221 | // 0x04fe
|
---|
222 |
|
---|
223 | struct DimPassport
|
---|
224 | {
|
---|
225 | uint32_t fTimeStamp;
|
---|
226 |
|
---|
227 | uint16_t fVersion;
|
---|
228 | uint16_t fBoardId;
|
---|
229 | uint64_t fDNA; // Xilinx DNA
|
---|
230 |
|
---|
231 | DimPassport(const EventHeader &h) :
|
---|
232 | fTimeStamp(h.fTimeStamp),
|
---|
233 | fVersion(h.fVersion),
|
---|
234 | fBoardId(h.fBoardId),
|
---|
235 | fDNA(h.fDNA)
|
---|
236 | {
|
---|
237 | }
|
---|
238 |
|
---|
239 | } __attribute__((__packed__));
|
---|
240 |
|
---|
241 | struct DimSetup
|
---|
242 | {
|
---|
243 | uint32_t fTimeStamp;
|
---|
244 |
|
---|
245 | uint32_t fFreqRefClock;
|
---|
246 | uint16_t fStatus;
|
---|
247 | uint16_t fAdcClockPhaseShift;
|
---|
248 | uint16_t fNumTriggersToGenerate;
|
---|
249 | uint16_t fTriggerGeneratorPrescaler;
|
---|
250 | uint16_t fDac[kNumDac];
|
---|
251 |
|
---|
252 | DimSetup(const EventHeader &h) :
|
---|
253 | fTimeStamp(h.fTimeStamp),
|
---|
254 | fFreqRefClock(h.fFreqRefClock),
|
---|
255 | fStatus(h.fStatus),
|
---|
256 | fAdcClockPhaseShift(h.fAdcClockPhaseShift),
|
---|
257 | fNumTriggersToGenerate(h.fNumTriggersToGenerate),
|
---|
258 | fTriggerGeneratorPrescaler(h.fTriggerGeneratorPrescaler)
|
---|
259 | {
|
---|
260 | memcpy(fDac, h.fDac, sizeof(fDac));
|
---|
261 | }
|
---|
262 |
|
---|
263 | uint8_t PLLLCK() const { return fStatus>>12; }
|
---|
264 |
|
---|
265 | bool HasDenable() const { return fStatus&EventHeader::kDenable; }
|
---|
266 | bool HasDwrite() const { return fStatus&EventHeader::kDwrite; }
|
---|
267 | bool IsRefClockTooHigh() const { return fStatus&EventHeader::kRefClkTooHigh; }
|
---|
268 | bool IsRefClockTooLow() const { return fStatus&EventHeader::kRefClkTooLow; }
|
---|
269 | bool IsDcmLocked() const { return fStatus&EventHeader::kDcmLocked; }
|
---|
270 | bool IsDcmReady() const { return fStatus&EventHeader::kDcmReady; }
|
---|
271 | bool HasSpiSclk() const { return fStatus&EventHeader::kSpiSclk; }
|
---|
272 |
|
---|
273 | } __attribute__((__packed__));
|
---|
274 |
|
---|
275 | struct DimTemperatures
|
---|
276 | {
|
---|
277 | uint32_t fTimeStamp;
|
---|
278 |
|
---|
279 | float fTempDrs[kNumTemp];
|
---|
280 |
|
---|
281 | DimTemperatures(const EventHeader &h) :
|
---|
282 | fTimeStamp(h.fTimeStamp)
|
---|
283 | {
|
---|
284 | for (int i=0; i<kNumTemp; i++)
|
---|
285 | fTempDrs[i] = h.GetTemp(i);
|
---|
286 | }
|
---|
287 |
|
---|
288 | } __attribute__((__packed__));;
|
---|
289 |
|
---|
290 | struct DimEventHeader
|
---|
291 | {
|
---|
292 | uint32_t fTimeStamp;
|
---|
293 |
|
---|
294 | uint32_t fRunNumber;
|
---|
295 | uint32_t fEventCounter;
|
---|
296 | uint16_t fTriggerCrc;
|
---|
297 | uint16_t fTriggerType;
|
---|
298 | uint32_t fTriggerId;
|
---|
299 |
|
---|
300 | DimEventHeader(const EventHeader &h) :
|
---|
301 | fTimeStamp(h.fTimeStamp),
|
---|
302 | fRunNumber(h.fRunNumber),
|
---|
303 | fEventCounter(h.fEventCounter),
|
---|
304 | fTriggerCrc(h.fTriggerCrc),
|
---|
305 | fTriggerType(h.fTriggerType),
|
---|
306 | fTriggerId(h.fTriggerId)
|
---|
307 | {
|
---|
308 | }
|
---|
309 |
|
---|
310 | } __attribute__((__packed__));
|
---|
311 |
|
---|
312 | // --------------------------------------------------------------------
|
---|
313 |
|
---|
314 | inline std::ostream &operator<<(std::ostream &out, const EventHeader &h)
|
---|
315 | {
|
---|
316 | h.print(out);
|
---|
317 | return out;
|
---|
318 | }
|
---|
319 |
|
---|
320 | inline std::ostream &operator<<(std::ostream &out, const ChannelHeader &h)
|
---|
321 | {
|
---|
322 | h.print(out);
|
---|
323 | return out;
|
---|
324 | }
|
---|
325 | };
|
---|
326 |
|
---|
327 | #endif
|
---|