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

Last change on this file since 11570 was 11527, checked in by tbretz, 13 years ago
Added operator!= to Header
File size: 12.2 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 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 bool operator!=(const EventHeader &h) const { return !operator==(h); }
282
283 float GetTemp(int i) const { return fTempDrs[i]/16.; }
284
285 /*
286 float GetTemp(int i) const
287 {
288 return (((fTempDrs[i]&0x8000) ?
289 ((fTempDrs[i]&0x007fff)^0xffffffff)
290 : (fTempDrs[i]&0x007fff))>>3)/16.; }*/
291
292 uint8_t PLLLCK() const { return fStatus>>12; }
293
294 bool HasDenable() const { return fStatus&kDenable; }
295 bool HasDwrite() const { return fStatus&kDwrite; }
296// bool IsRefClockTooHigh() const { return fStatus&kRefClkTooHigh; }
297 bool IsRefClockTooLow() const { return fStatus&kRefClkTooLow; }
298 bool IsDcmLocked() const { return fStatus&kDcmLocked; }
299 bool IsDcmReady() const { return fStatus&kDcmReady; }
300 bool HasSpiSclk() const { return fStatus&kSpiSclk; }
301 bool HasBusy() const { return fStatus&kBusy; }
302 bool HasTriggerEnabled() const { return fStatus&kTriggerLine; }
303 bool HasContTriggerEnabled() const { return fStatus&kContTrigger; }
304 bool IsInSock17Mode() const { return fStatus&kSock17; }
305
306 uint16_t Crate() const { return fBoardId>>8; }
307 uint16_t Board() const { return fBoardId&0xff; }
308
309 uint16_t Id() const { return Crate()*10+Board(); }
310
311 void Enable(Bits pos, bool enable=true)
312 {
313 if (enable)
314 fStatus |= pos;
315 else
316 fStatus &= ~pos;
317 }
318
319 void clear() { reset(*this); }
320 void print(std::ostream &out) const;
321#endif
322
323 } __attribute__((__packed__));
324
325 struct ChannelHeader
326 {
327 uint16_t fId;
328 uint16_t fStartCell;
329 uint16_t fRegionOfInterest;
330 uint16_t fDummy;
331 // uint16_t fData[];
332
333#ifdef __cplusplus
334 ChannelHeader() { init(*this); }
335
336 void operator=(const std::vector<uint16_t> &vec)
337 {
338 ntohcpy(vec, *this);
339 }
340
341 std::vector<uint16_t> HtoN() const
342 {
343 ChannelHeader h(*this);
344 return htoncpy(h);
345 }
346
347 void clear() { reset(*this); }
348 void print(std::ostream &out) const;
349
350 uint16_t Chip() const { return fId>>4; }
351 uint16_t Channel() const { return fId&0xf; }
352#endif
353 } __attribute__((__packed__));
354
355 // Package ends with:
356 // 0x4242
357 // 0x04fe
358
359 struct Configuration
360 {
361 bool fDwrite;
362 bool fDenable;
363 bool fContinousTrigger;
364 uint16_t fTriggerRate;
365 uint16_t fRoi[FAD::kNumChannelsPerChip];
366 uint16_t fDac[FAD::kNumDac];
367
368#ifdef __cplusplus
369 Configuration() { init(*this); }
370#endif
371 };
372
373 // --------------------------------------------------------------------
374#ifdef __cplusplus
375 inline std::ostream &operator<<(std::ostream &out, const EventHeader &h)
376 {
377 h.print(out);
378 return out;
379 }
380
381 inline std::ostream &operator<<(std::ostream &out, const ChannelHeader &h)
382 {
383 h.print(out);
384 return out;
385 }
386#endif
387
388#ifdef __cplusplus
389};
390#endif
391
392#endif
Note: See TracBrowser for help on using the repository browser.