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

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