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