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

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