My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
Tfp.h
Go to the documentation of this file.
1 #ifndef GCP_ANTENNA_CONTROL_TFP_H
2 #define GCP_ANTENNA_CONTROL_TFP_H
3 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/mman.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <errno.h>
17 
18 #include <string>
19 #include <vector>
20 
21 #include "gcp/util/common/RegDate.h"
22 #include "gcp/util/common/TimeVal.h"
23 
24 namespace gcp {
25  namespace antenna {
26  namespace control {
27 
28  class Tfp {
29  public:
30 
31  //------------------------------------------------------------
32  // Device register offsets from BAR0 starting address
33  //------------------------------------------------------------
34 
35  enum CtlRegOffset {
36 
37  // Accessing this register latches the current time and
38  // status in the TIME0-TIME1 registers
39 
40  CTL_OFF_TIMEREQ = 0x0,
41 
42  // Accessing this register latches the current time and
43  // status in the EVENT0-EVENT1 registers
44 
45  CTL_OFF_EVENTREQ = 0x4,
46 
47  // Accessing this register release the EVENTx time capture
48  // lockout function if it has been enabled, allowing the
49  // event input or periodic output to capture a new time.
50  // The event time capture lockout is enabled using bit 1 of
51  // the CONTROL register, below
52 
53  CTL_OFF_UNLOCK = 0x8,
54 
55  // Reserved
56 
57  CTL_OFF_RESERVED1 = 0xC,
58 
59  // The control register
60 
61  CTL_OFF_CONTROL = 0x10,
62 
63  // The ACK register is used to prevent DPRAM contention when
64  // the same address is accessed simultaneously from both
65  // sides
66 
67  CTL_OFF_ACK = 0x14,
68 
69  // Bits 0-4 of the MASK register correspond to interrupt
70  // sources
71 
72  CTL_OFF_MASK = 0x18,
73 
74  // Same bit definitions as the MASK register, indicating
75  // which interrupt source was activated
76 
77  CTL_OFF_INTSTAT = 0x1C,
78 
79  // These registers hold the programmed Time Coincidence
80  // Strobe time. The contents of these registers depend on
81  // the time format selected. The Strobe time is programmable
82  // from hours through microseconds in the decimal time
83  // format. When the time format is set to binary, only the
84  // 22 least significant bits of the major time are used (in
85  // addition to microseconds), this allows the user to
86  // program the Strobe to become activated up to 48 days
87  // beyond the current time.
88 
89  CTL_OFF_MINSTRB = 0x20,
90  CTL_OFF_MAJSTRB = 0x24,
91 
92  // Reserved
93 
94  CTL_OFF_RESERVED2 = 0x28,
95  CTL_OFF_RESERVED3 = 0x2C,
96 
97  // These registers hold time captured by an access of the
98  // TIMEREQ register. The contents of these registers depend
99  // on the time format selected.
100 
101  CTL_OFF_TIME0 = 0x30,
102  CTL_OFF_TIME1 = 0x34,
103 
104  // These registers hold time captured when the EVENTREQ
105  // register is accessed by an Event Input (if enabled), or a
106  // PPO is generated (if enabled). The contents of these
107  // registers depend on the time format selected
108 
109  CTL_OFF_EVENT0 = 0x38,
110  CTL_OFF_EVENT1 = 0x3C
111  };
112 
113  //------------------------------------------------------------
114  // CONTROL register bit definitions
115  //------------------------------------------------------------
116 
117  // The EVSOURCE bit selects one of two signal sources for
118  // capturing time in the EVENTx registers; either the Event
119  // Input signal from the Signal I/O connector or the
120  // Programmable Periodic Output. When the PPO is selected as
121  // the Event Source, the PPO and Event Input are internally
122  // or'd, eliminating the need for an external physical
123  // connection. The EVSOURCE bit does not affect the Event
124  // Interrupt, as the Event Input signal is the only source for
125  // the Event Interrupt. This allows the Event Input to
126  // generate a PCI interrupt (without time capture) when the
127  // board is configured to use the Programmable Periodic Output
128  // (PPO) for EVENTx time capture (see Chapter 4)
129  //
130  // The EVENTEN bit is used to enable an external signal
131  // capture of time into the EVENTx registers. This bit
132  // controls the PPO and Event Input for event time
133  // capture.Enabling the Lockout function via the LOCKEN bit
134  // allows only the first instance of the selected signal
135  // source to latch time in the EVENTx registers, locking out
136  // any subsequent events, external or PPO. Use the UNLOCK
137  // register to re-arm the circuit
138 
139  enum {
140 
141  // EVENTx capture lockout enable
142 
143  CTL_LOCKEN_BIT = 0,
144 
145  // EVENTx Time Capture Register Source Select:
146  // 0 - Event Input (Select Active edge with EVSENSE)
147  // 1 - Programmable Periodic (rising edge active only)
148 
149  CTL_EVSOURCE_BIT = 1,
150 
151  // Reserved
152 
153  CTL_RESERVED_BIT = 2,
154 
155  // Event Capture register enable:
156  // 0 - disable
157  // 1 - enable (use EVSOURCE to select event source)
158 
159  CTL_EVENTEN_BIT = 3,
160 
161  // Time Coincidence strobe output enable
162  // 0 - disable (strobe output is held low)
163  // 1 - enable
164 
165  CTL_STREN_BIT = 4,
166 
167  // Time coincidence strobe mode
168  // 0 - Use major and minor time for strobe function
169  // 1 - Use minor time only for strobe function
170 
171  CTL_STRMODE_BIT = 5,
172 
173  // Output Frequency select
174  // 00 = 10 MHz
175  // 01 = 5 MHz
176  // 1X = 1 MHz
177 
178  CTL_FREQSEL0_BIT = 6,
179  CTL_FREQSEL1_BIT = 7,
180  };
181 
182  //------------------------------------------------------------
183  // ACK register bit definitions
184  //------------------------------------------------------------
185 
186  enum {
187 
188  // Set by the TFP to acknowledge receipt of a user command
189  // from the DPRAM input area. User can clear this bit by
190  // writing to the ACK register with bit 0 set
191 
192  ACK_CMD_BIT = 0,
193 
194  // Set by the TFP to acknowledge that a GPS packet is
195  // available in the DPRAM GPS packet area. User can clear
196  // this bit by writing to the ACK register with bit 2 set
197 
198  ACK_GPS_BIT = 2,
199 
200  // User write to the ACK register with bit 7 set to tell the
201  // TFP to read a command from the DPRAM input area
202 
203  ACK_READ_BIT = 7,
204  };
205 
206  enum {
207  READ_CMD = 0x81 // ACK bit 1 + ACK bit 7
208  };
209 
210  //------------------------------------------------------------
211  // MASK register definitions
212  //------------------------------------------------------------
213 
214  enum {
215  MASK_EVENT_BIT = 0, // Event input has occurred
216  MASK_PERIODIC_BIT = 1, // Periodic output has occurred
217  MASK_TIME_BIT = 2, // Time coincidence strobe has occurred
218  MASK_1PPS_BIT = 3, // 1PPS output has occurred
219  MASK_GPS_BIT = 4 // A GPS data packet is available
220  };
221 
222  //------------------------------------------------------------
223  // DPRAM command codes
224  //------------------------------------------------------------
225 
226  enum DpramCmdId {
227  DP_CMD_TIME_MODE = 0x10,
228  DP_CMD_TIME_FORMAT = 0x11,
229  DP_CMD_SET_MAJOR = 0x12,
230  DP_CMD_SET_YEAR = 0x13,
231  DP_CMD_SET_PERIODIC_OUTPUT = 0x14,
232  DP_CMD_SET_INP_FORMAT = 0x15,
233  DP_CMD_SET_INP_MOD = 0x16,
234  DP_CMD_SET_PROP_DELAY = 0x17,
235  DP_CMD_REQ_DATA = 0x19,
236  DP_CMD_SOFT_RESET = 0x1A,
237  DP_CMD_SET_OUT_FORMAT = 0x1B,
238  DP_CMD_GEN_OFFSET = 0x1C,
239  DP_CMD_LCL_OFFSET = 0x1D,
240  DP_CMD_SET_CLK_SRC = 0x20,
241  DP_CMD_GPS_FMT = 0x33,
242  DP_CMD_SET_GPS_MODE_FLAG = 0x34,
243  DP_CMD_YEAR_AUTO_INC = 0x42,
244  DP_CMD_REQ_ASSY = 0xf4,
245  DP_CMD_REQ_MODEL = 0xf6
246  };
247 
248  enum GpsFormat {
249  FORMAT_UTC = 0x0,
250  FORMAT_GPS = 0x1,
251  };
252 
253  enum TimeFormat {
254  FORMAT_BCD = 0x00,
255  FORMAT_UNIX = 0x01
256  };
257 
258  enum TimeCodeFormat {
259  FORMAT_IRIGA = 0x41,
260  FORMAT_IRIGB = 0x42,
261  FORMAT_IEEE = 0x49,
262  FORMAT_NASA36 = 0x4E,
263  };
264 
265  enum InputTimeCodeModType {
266  INP_MOD_AMP = 'M', // Amplitude modulated signal
267  INP_MOD_PULSE = 'D', // Pulse-code modulated signal
268  };
269 
270  enum ClockSource {
271  CLK_INTERNAL = 'I', // Amplitude modulated signal
272  CLK_EXTERNAL = 'E', // Pulse-code modulated signal
273  };
274 
275  enum OutputFreq {
276  FREQ_10MHZ = 0x0,
277  FREQ_5MHZ = 0x1,
278  FREQ_1MHZ = 0x2
279  };
280 
281  enum TimeMode {
282  MODE_TIMECODE = 0x00,
283  MODE_FREERUN = 0x01,
284  MODE_1PPS = 0x02,
285  MODE_REAL_CLOCK = 0x03,
286  MODE_GPS = 0x06
287  };
288 
289  enum {
290  NONE = 0x00,
291  INT_EVENT = 0x01,
292  INT_PERIODIC = 0x02,
293  INT_TIME = 0x04,
294  INT_1PPS = 0x08,
295  INT_GPS = 0x10,
296  ALL = INT_EVENT | INT_PERIODIC | INT_TIME | INT_1PPS | INT_GPS
297  };
298 
299  // The name by which the device is accessible from user-space
300 
301  static const std::string devName_;
302 
303 
304  // The size of the CTL region of the card
305  //
306  // This appears as a 4096 byte array
307 
308  static const unsigned ctlSizeInBytes_ = 0x1000;
309 
310 
311  // Offset of the control registers from the start of the
312  // card's memory space
313 
314  static const unsigned ctlOffsetInBytes_ = 0x0;
315 
316 
317  // The size of the DPRAM region of the card
318  //
319  // This appears as a 4096 byte array
320 
321  static const unsigned dpramSizeInBytes_ = 0x1000;
322 
323  // Offset of the dpram registers from the start of the
324  // card's memory space
325 
326  static const unsigned dpramOffsetInBytes_ = 0x1000;
327 
328 
329  // Constructor.
330 
331  Tfp(bool simulate=false);
332 
333 
334  // Destructor.
335 
336  virtual ~Tfp();
337 
338  void open();
339  void close();
340  void readBcdTime(gcp::util::TimeVal& tVal);
341  void readUnixTime(gcp::util::TimeVal& tVal);
342 
343  void setYear(unsigned short year);
344  void setMajorTime(unsigned int seconds);
345  void setMajorTime(unsigned short year, unsigned short dayNo,
346  unsigned char hours, unsigned char minutes,
347  unsigned char seconds);
348 
349  void setTimeMode(TimeMode mode);
350  void setTimeFormat(TimeFormat format);
351  void setInputTimeCodeFormat(TimeCodeFormat format);
352  void setOutputTimeCodeFormat(TimeCodeFormat format);
353  void setInputTimeCodeModType(InputTimeCodeModType mod);
354  void setFrequencyOutput(bool syncToPps, double dutyCyclePerc, double freqInHz);
355  void setPeriodicOutput(bool syncToPps, unsigned short n1, unsigned short n2);
356  void setOutputClockFreq(OutputFreq freq);
357  unsigned short readYear();
358  void requestModelId();
359  void requestAssemblyPartNo();
360  void softReset();
361 
362  void setGeneratorTimeOffset(short offset, bool halfHour=false);
363  void setLocalTimeOffset(short offset, bool halfHour=false);
364  void setPropagationDelay(int delayIn100ns);
365  void setClockSource(ClockSource src);
366  void setGpsFormat(GpsFormat format);
367  void enableGpsModeFlag(bool enable);
368  void enableYearAutoIncrement(bool enable);
369 
370  void defaultSetUp();
371  void specificSetUp();
372 
373  // Utility functions for reading and writing
374 
375  static void testBits(unsigned int iVal);
376  static void printBits(unsigned char c);
377  static void printBits(unsigned short s);
378  static void printBits(unsigned int i);
379  static unsigned short constructShort(unsigned char msb, unsigned char lsb);
380  static unsigned constructInt(unsigned char msb, unsigned char smsb,
381  unsigned tmsb, unsigned char lsb);
382 
383  void enableInterrupt(unsigned mask, bool enable);
384 
385  void getDate(gcp::util::RegDate& date);
386  void getDate(gcp::util::TimeVal& timeVal);
387 
388  private:
389 
390  bool simulate_;
391 
392  struct DpramCmd {
393  DpramCmd(DpramCmdId cmd);
394  void packVal(unsigned char val);
395  void packVal(unsigned short val);
396  void packVal(unsigned int val);
397 
398  std::vector<unsigned char> data_;
399  };
400 
401  int fd_;
402 
403  // Members for DPRAM access
404 
405  void* dpramPtr_; // mmap'd pointer to DPRAM
406  unsigned dpramPtrLength_; // Length in bytes, of the
407  // memory pointed to by
408  // dpramPtr_
409  unsigned short dpramInOffset_; // offset of the input area,
410  // relative to the start of
411  // dpram
412  unsigned short dpramOutOffset_; // offset of the output
413  // area, relative to the
414  // start of dpram
415  unsigned short dpramGpsOffset_; // offset of the gps area,
416  // relative to the start of
417  // dpram
418  unsigned short dpramYearOffset_; // offset of the year area,
419  // relative to the start of
420  // dpram
421 
422  unsigned char* dpramInPtr_;
423  unsigned char* dpramOutPtr_;
424  unsigned char* dpramGpsPtr_;
425  unsigned char* dpramYearPtr_;
426 
427  // Members for Control register access
428 
429  void* ctlPtr_; // mmap'd pointer to DPRAM
430  unsigned ctlPtrLength_; // Length in bytes, of the
431  // memory pointed to by
432  // ctlPtr_
433 
434  unsigned* ctlAckPtr_;
435  unsigned* ctlCtlPtr_;
436  unsigned* ctlTime0Ptr_;
437  unsigned* ctlTime1Ptr_;
438  unsigned* ctlTimeReqPtr_;
439 
440  void initialize();
441  void resetCtlPointers();
442  void resetDpramPointers();
443 
444  void mapCtlPtr(unsigned byteOffset=0);
445  void mapDpramPtr(unsigned byteOffset=0);
446  void map();
447  void unmap();
448  unsigned char* ctlPtr();
449  unsigned char* dpramPtr();
450  void sendDpramCmd(DpramCmd& cmd, bool wait=true);
451  void waitForAck();
452  void setBit(unsigned& mask, unsigned short bit, bool enable);
453 
454  }; // End class Tfp
455 
456  } // End namespace control
457  } // End namespace antenna
458 } // End namespace gcp
459 
460 
461 
462 #endif // End #ifndef GCP_ANTENNA_CONTROL_TFP_H
void requestAssemblyPartNo()
Definition: Tfp.cc:529
static unsigned short constructShort(unsigned char msb, unsigned char lsb)
Definition: Tfp.cc:669
void close()
Definition: Tfp.cc:99
void enableInterrupt(unsigned mask, bool enable)
Definition: Tfp.cc:780
void readUnixTime(gcp::util::TimeVal &tVal)
Definition: Tfp.cc:383
void setInputTimeCodeFormat(TimeCodeFormat format)
Definition: Tfp.cc:444
void requestModelId()
Definition: Tfp.cc:552
void setInputTimeCodeModType(InputTimeCodeModType mod)
Definition: Tfp.cc:464
void softReset()
Definition: Tfp.cc:504
Definition: Tfp.h:28
unsigned short readYear()
Definition: Tfp.cc:513
void open()
Definition: Tfp.cc:82
void setMajorTime(unsigned int seconds)
Definition: Tfp.cc:474
static unsigned constructInt(unsigned char msb, unsigned char smsb, unsigned tmsb, unsigned char lsb)
Definition: Tfp.cc:684
Tfp(bool simulate=false)
Definition: Tfp.cc:24
Definition: TimeVal.h:55
void setTimeFormat(TimeFormat format)
Definition: Tfp.cc:434
Definition: RegDate.h:19
virtual ~Tfp()
Definition: Tfp.cc:37
void setOutputTimeCodeFormat(TimeCodeFormat format)
Definition: Tfp.cc:454
void readBcdTime(gcp::util::TimeVal &tVal)
Definition: Tfp.cc:354
void setYear(unsigned short year)
Definition: Tfp.cc:414
void setOutputClockFreq(OutputFreq freq)
Definition: Tfp.cc:605
void setFrequencyOutput(bool syncToPps, double dutyCyclePerc, double freqInHz)
Definition: Tfp.cc:581