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

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