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

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