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

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