nidas  v1.2-1520
irigclock.h
Go to the documentation of this file.
1 /* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8; -*- */
2 /* vim: set shiftwidth=8 softtabstop=8 expandtab: */
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2007, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 /* irigclock.h
27 
28  Class for interfacing the PC104-SG time and frequency processor.
29 
30  Original Author: Mike Spowart
31 
32 */
33 
34 #ifndef IRIGCLOCK_H
35 #define IRIGCLOCK_H
36 
37 #if defined(__KERNEL__)
38 # include <linux/time.h>
39 # include "util.h"
40 #else
41 # include <sys/time.h>
42 #endif
43 
44 #include "types.h"
45 
56 };
57 
64 static inline enum irigClockRates irigClockRateToEnum(unsigned int value)
65 {
66  /* Round up to the next highest enumerated poll rate. */
67  if (value == 0) return IRIG_0_1_HZ;
68  else if (value <= 1) return IRIG_1_HZ;
69  else if (value <= 2) return IRIG_2_HZ;
70  else if (value <= 4) return IRIG_4_HZ;
71  else if (value <= 5) return IRIG_5_HZ;
72  else if (value <= 10) return IRIG_10_HZ;
73  else if (value <= 20) return IRIG_20_HZ;
74  else if (value <= 25) return IRIG_25_HZ;
75  else if (value <= 50) return IRIG_50_HZ;
76  else if (value <= 100) return IRIG_100_HZ;
77  else return IRIG_NUM_RATES; /* invalid value given */
78 }
79 
83 static inline unsigned int irigClockEnumToRate(enum irigClockRates value)
84 {
85  static unsigned int rate[] = {0, 1, 2, 4, 5, 10, 20, 25, 50, 100, 0};
86  return rate[value];
87 }
88 
89 /*
90  * fields in struct timeval on 64 bit machine are 64 bits,
91  * so declare our own 2x32 bit timeval struct, for backward
92  * compatibility of old samples and IRIG_GET/SET_CLOCK ioctls.
93  */
94 struct timeval32 {
95  int tv_sec;
96  int tv_usec;
97 };
98 
100  struct timeval32 tval;
101  unsigned char status;
102  char end[];
103 };
104 
106  struct timeval32 irigt;
107  struct timeval32 unixt;
108  unsigned char status;
109  unsigned char seqnum;
110  unsigned char syncToggles;
111  unsigned char clockAdjusts;
112  unsigned char max100HzBacklog;
113  char end[];
114 };
115 
130  int64_t irigt;
131  int64_t unixt;
132  unsigned char dummystatus;
133  unsigned char status;
134  unsigned char seqnum;
135  unsigned char syncToggles;
136  unsigned char clockAdjusts;
137  unsigned char max100HzBacklog;
138  char end[];
139 };
140 
142  dsm_sample_time_t timetag; /* timetag of sample */
143  dsm_sample_length_t length; /* number of bytes in data */
144  struct dsm_clock_data data; /* must be no padding between
145  * length and data! */
146 };
147 
149  dsm_sample_time_t timetag; /* timetag of sample */
150  dsm_sample_length_t length; /* number of bytes in data */
151  struct dsm_clock_data_2 data; /* must be no padding between
152  * length and data! */
153 };
154 
156  dsm_sample_time_t timetag; /* timetag of sample */
157  dsm_sample_length_t length; /* number of bytes in data */
158  struct dsm_clock_data_3 data; /* must be no padding between
159  * length and data! */
160 };
168 #define IRIG_MAX_DT_DIFF 20
169 #define IRIG_MIN_DT_DIFF -20
170 
172 
177  uint32_t syncToggles;
178 
184 
190 
206  unsigned char statusOR;
207 };
208 
212 #define IRIG_IOC_MAGIC 'I' /* Unique(ish) char for IRIG ioctls */
213 
214 #define IRIG_OPEN _IO(IRIG_IOC_MAGIC, 0)
215 #define IRIG_CLOSE _IO(IRIG_IOC_MAGIC, 1)
216 #define IRIG_GET_STATUS _IOR(IRIG_IOC_MAGIC, 2, struct pc104sg_status)
217 #define IRIG_GET_CLOCK _IOR(IRIG_IOC_MAGIC, 3, struct timeval32)
218 #define IRIG_SET_CLOCK _IOW(IRIG_IOC_MAGIC, 4, struct timeval32)
219 #define IRIG_OVERRIDE_CLOCK _IOW(IRIG_IOC_MAGIC, 5, struct timeval32)
220 #define IRIG_GET_CLOCK64 _IOR(IRIG_IOC_MAGIC, 6, int64_t)
221 #define IRIG_SET_CLOCK64 _IOW(IRIG_IOC_MAGIC, 7, int64_t)
222 
223 /********** Start of symbols used by kernel modules **********/
224 
225 #if defined(__KERNEL__)
226 
227 #include <linux/ioctl.h>
228 #include <linux/wait.h>
229 
230 struct irigTime {
231  int year; /* actual year, eg: 2004 */
232  int yday; /* day of year, 1-366 */
233  int hour;
234  int min;
235  int sec;
236  int msec;
237  int usec;
238  int nsec;
239 };
240 
241 
242 /*
243  * Clock ticker kept in RAM for reading (not writing!) by other kernel
244  * modules via the GET_MSEC_CLOCK and GET_TMSEC_CLOCK macros.
245  */
246 extern int TMsecClock[];
247 extern int ReadClock;
248 
259 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
260 #define GET_TMSEC_CLOCK \
261  ({\
262  int tmp = smp_load_acquire(&ReadClock);\
263  TMsecClock[tmp];\
264  })
265 #else
266 #define GET_TMSEC_CLOCK \
267  ({\
268  int tmp = READ_ONCE(ReadClock);\
269  smp_read_barrier_depends();\
270  TMsecClock[tmp];\
271  })
272 #endif
273 
274 #define GET_MSEC_CLOCK (GET_TMSEC_CLOCK/10)
275 
279 extern int get_msec_clock_resolution(void);
280 
288 extern void irig_clock_gettime(thiskernel_timespec_t* tp);
289 
290 typedef void irig_callback_func(void* privateData);
291 
295 struct irig_callback {
296  struct list_head list;
297  irig_callback_func* callback;
298  irig_callback_func* resyncCallback;
299  void* privateData;
300  enum irigClockRates rate;
301 };
302 
331 extern struct irig_callback* register_irig_callback(
332  irig_callback_func* func,
333  irig_callback_func* resync,
334  enum irigClockRates rate,
335  void* privateData,int *errp);
336 
352 extern int unregister_irig_callback(struct irig_callback*);
353 
378 extern int flush_irig_callbacks(void);
379 
384 int setRate2Output(int rate);
385 
386 #endif /* defined(__KERNEL__) */
387 
388 #endif
Definition: irigclock.h:53
Definition: irigclock.h:55
int ReadClock
Index into TMsecClock of the value to be read.
Definition: pc104sg.c:212
int setRate2Output(int rate)
Definition: pc104sg.c:1154
void irig_clock_gettime(thiskernel_timespec_t *tp)
This function is available for use by external modules.
Definition: pc104sg.c:1440
int TMsecClock[2]
A toggle buffer containing the current clock value in tenths of milliseconds since UTC midnight...
Definition: pc104sg.c:207
unsigned char status
Definition: irigclock.h:108
char end[]
Definition: irigclock.h:102
Definition: irigclock.h:105
unsigned char seqnum
Definition: irigclock.h:134
#define IRIG_MAX_DT_DIFF
Limits for how many ticks the 100Hz software clock can disagree with the hardware clock before a rese...
Definition: irigclock.h:168
Definition: irigclock.h:148
Definition: irigclock.h:54
Definition: irigclock.h:53
Definition: irigclock.h:171
Definition: irigclock.h:53
struct irig_callback * register_irig_callback(irig_callback_func *callback, irig_callback_func *resync, enum irigClockRates rate, void *privateData, int *errp)
Module function that allows other modules to register their callback function to be called at the giv...
Definition: pc104sg.c:587
unsigned char status
Definition: irigclock.h:101
static unsigned int irigClockEnumToRate(enum irigClockRates value)
Convert an enumerated value back to a rate in Hz.
Definition: irigclock.h:83
static enum irigClockRates irigClockRateToEnum(unsigned int value)
Convert a rate in Hz to an enumerated value, rounding up to the next highest supported rate...
Definition: irigclock.h:64
struct dsm_clock_data_2 data
Definition: irigclock.h:151
unsigned char max100HzBacklog
Definition: irigclock.h:137
Definition: irigclock.h:54
int64_t unixt
Definition: irigclock.h:131
unsigned char status
Definition: irigclock.h:133
dsm_sample_time_t timetag
Definition: irigclock.h:142
int dsm_sample_time_t
Depending on the module, either tenths of milliseconds, or milliseconds since 00:00 UTC today...
Definition: types.h:48
dsm_sample_time_t timetag
Definition: irigclock.h:149
int unregister_irig_callback(struct irig_callback *cb)
Modules call this function to dequeue their callbacks.
Definition: pc104sg.c:641
Definition: irigclock.h:155
char end[]
Definition: irigclock.h:113
int tv_usec
Definition: irigclock.h:96
uint32_t syncToggles
Counts of the number of times that the IRIG hardware clock has lost or regained sync.
Definition: irigclock.h:177
Definition: irigclock.h:94
Definition: irigclock.h:53
struct timeval32 irigt
Definition: irigclock.h:106
Definition: irigclock.h:54
unsigned char max100HzBacklog
Definition: irigclock.h:112
unsigned char syncToggles
Definition: irigclock.h:135
Definition: irigclock.h:141
struct dsm_clock_data_3 data
Definition: irigclock.h:158
int flush_irig_callbacks(void)
Definition: pc104sg.c:662
uint32_t slews[IRIG_MAX_DT_DIFF-IRIG_MIN_DT_DIFF+1]
Counts of the number of times that the software clock was slewed, indexed by the number of delta-Ts i...
Definition: irigclock.h:189
dsm_sample_length_t length
Definition: irigclock.h:143
unsigned char clockAdjusts
Definition: irigclock.h:136
unsigned char seqnum
Definition: irigclock.h:109
unsigned char statusOR
Value of extended status from PC104SG dual port RAM.
Definition: irigclock.h:206
dsm_sample_length_t length
Definition: irigclock.h:157
static float rate
Definition: sing.cc:53
int tv_sec
Definition: irigclock.h:95
Definition: irigclock.h:55
unsigned char clockAdjusts
Definition: irigclock.h:111
Note that a int64_t has the same size, 8 bytes, as a timeval32.
Definition: irigclock.h:129
Definition: irigclock.h:99
Definition: irigclock.h:54
struct dsm_clock_data data
Definition: irigclock.h:144
dsm_sample_time_t timetag
Definition: irigclock.h:156
#define IRIG_MIN_DT_DIFF
Definition: irigclock.h:169
unsigned int dsm_sample_length_t
length of data portion of sample.
Definition: types.h:51
dsm_sample_length_t length
Definition: irigclock.h:150
struct timeval32 tval
Definition: irigclock.h:100
unsigned char dummystatus
Definition: irigclock.h:132
Definition: irigclock.h:55
int get_msec_clock_resolution()
Definition: pc104sg.c:1455
irigClockRates
Enumeration of the callback rates supported by this module.
Definition: irigclock.h:52
unsigned char syncToggles
Definition: irigclock.h:110
struct timeval32 unixt
Definition: irigclock.h:107
int64_t irigt
Definition: irigclock.h:130
Definition: irigclock.h:53
uint32_t softwareClockResets
Total counts of the number of times that the sofware clock was reset with a step change.
Definition: irigclock.h:183
char end[]
Definition: irigclock.h:138