source: trunk/FACT++/src/HeadersFAD.h@ 11154

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