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

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