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

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