RTEMS 6.1-rc5
Loading...
Searching...
No Matches
timepps.h
Go to the documentation of this file.
1
10/*-
11 * ----------------------------------------------------------------------------
12 * "THE BEER-WARE LICENSE" (Revision 42):
13 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
14 * can do whatever you want with this stuff. If we meet some day, and you think
15 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
16 * ----------------------------------------------------------------------------
17 *
18 * Copyright (c) 2011 The FreeBSD Foundation
19 * All rights reserved.
20 *
21 * Portions of this software were developed by Julien Ridoux at the University
22 * of Melbourne under sponsorship from the FreeBSD Foundation.
23 *
24 * The is a FreeBSD version of the RFC 2783 API for Pulse Per Second
25 * timing interfaces.
26 */
27
28#ifndef _SYS_TIMEPPS_H_
29#define _SYS_TIMEPPS_H_
30
31#include <sys/_ffcounter.h>
32#include <sys/ioccom.h>
33#include <sys/time.h>
34#ifdef __rtems__
35#include <rtems/score/atomic.h>
36#define PPS_SYNC
37#define hardpps _Timecounter_Discipline
38#ifdef __cplusplus
39extern "C" {
40#endif /* __cplusplus */
41#endif /* __rtems__ */
42
43#define PPS_API_VERS_1 1
44
45typedef int pps_handle_t;
46
47#ifndef __rtems__
48typedef unsigned pps_seq_t;
49#else /* __rtems__ */
50typedef Atomic_Uint pps_seq_t;
51#endif /* __rtems__ */
52
53typedef struct ntp_fp {
54 unsigned int integral;
55 unsigned int fractional;
56} ntp_fp_t;
57
58typedef union pps_timeu {
59 struct timespec tspec;
60 ntp_fp_t ntpfp;
61 unsigned long longpad[3];
63
64typedef struct {
65 pps_seq_t assert_sequence; /* assert event seq # */
66 pps_seq_t clear_sequence; /* clear event seq # */
67 pps_timeu_t assert_tu;
68 pps_timeu_t clear_tu;
69 int current_mode; /* current mode bits */
71
72typedef struct {
73 pps_seq_t assert_sequence; /* assert event seq # */
74 pps_seq_t clear_sequence; /* clear event seq # */
75 pps_timeu_t assert_tu;
76 pps_timeu_t clear_tu;
77 ffcounter assert_ffcount; /* ffcounter on assert event */
78 ffcounter clear_ffcount; /* ffcounter on clear event */
79 int current_mode; /* current mode bits */
81
82#define assert_timestamp assert_tu.tspec
83#define clear_timestamp clear_tu.tspec
84
85#define assert_timestamp_ntpfp assert_tu.ntpfp
86#define clear_timestamp_ntpfp clear_tu.ntpfp
87
88typedef struct {
89 int api_version; /* API version # */
90 int mode; /* mode bits */
91 pps_timeu_t assert_off_tu;
92 pps_timeu_t clear_off_tu;
94
95#define assert_offset assert_off_tu.tspec
96#define clear_offset clear_off_tu.tspec
97
98#define assert_offset_ntpfp assert_off_tu.ntpfp
99#define clear_offset_ntpfp clear_off_tu.ntpfp
100
101
102#define PPS_CAPTUREASSERT 0x01
103#define PPS_CAPTURECLEAR 0x02
104#define PPS_CAPTUREBOTH 0x03
105
106#define PPS_OFFSETASSERT 0x10
107#define PPS_OFFSETCLEAR 0x20
108
109#define PPS_ECHOASSERT 0x40
110#define PPS_ECHOCLEAR 0x80
111
112#define PPS_CANWAIT 0x100
113#define PPS_CANPOLL 0x200
114
115#define PPS_TSFMT_TSPEC 0x1000
116#define PPS_TSFMT_NTPFP 0x2000
117
118#define PPS_TSCLK_FBCK 0x10000
119#define PPS_TSCLK_FFWD 0x20000
120#define PPS_TSCLK_MASK 0x30000
121
122#define PPS_KC_HARDPPS 0
123#define PPS_KC_HARDPPS_PLL 1
124#define PPS_KC_HARDPPS_FLL 2
125
127 int tsformat;
128 pps_info_t pps_info_buf;
129 struct timespec timeout;
130};
131
133 int tsformat;
134 pps_info_ffc_t pps_info_buf_ffc;
135 struct timespec timeout;
136};
137
139 int kernel_consumer;
140 int edge;
141 int tsformat;
142};
143
144#define PPS_IOC_CREATE _IO('1', 1)
145#define PPS_IOC_DESTROY _IO('1', 2)
146#define PPS_IOC_SETPARAMS _IOW('1', 3, pps_params_t)
147#define PPS_IOC_GETPARAMS _IOR('1', 4, pps_params_t)
148#define PPS_IOC_GETCAP _IOR('1', 5, int)
149#define PPS_IOC_FETCH _IOWR('1', 6, struct pps_fetch_args)
150#define PPS_IOC_KCBIND _IOW('1', 7, struct pps_kcbind_args)
151#define PPS_IOC_FETCH_FFCOUNTER _IOWR('1', 8, struct pps_fetch_ffc_args)
152
153#ifdef _KERNEL
154
155struct mtx;
156
157#define KCMODE_EDGEMASK 0x03
158#define KCMODE_ABIFLAG 0x80000000 /* Internal use: abi-aware driver. */
159
160#define PPS_ABI_VERSION 1
161
162#define PPSFLAG_MTX_SPIN 0x01 /* Driver mtx is MTX_SPIN type. */
163
164struct pps_state {
165 /* Capture information. */
166 struct timehands *capth;
167 struct fftimehands *capffth;
168 unsigned capgen;
169 unsigned capcount;
170
171 /* State information. */
172 pps_params_t ppsparam;
173 pps_info_t ppsinfo;
174 pps_info_ffc_t ppsinfo_ffc;
175 int kcmode;
176 int ppscap;
177 struct timecounter *ppstc;
178 unsigned ppscount[3];
179#ifdef __rtems__
194 int (*wait)(struct pps_state *pps, struct timespec timeout);
195
201 void (*wakeup)(struct pps_state *pps);
202#endif /* __rtems__ */
203 /*
204 * The following fields are valid if the driver calls pps_init_abi().
205 */
206 uint16_t driver_abi; /* Driver sets before pps_init_abi(). */
207 uint16_t kernel_abi; /* Kernel sets during pps_init_abi(). */
208 struct mtx *driver_mtx; /* Optional, valid if non-NULL. */
209 uint32_t flags;
210};
211
212void pps_capture(struct pps_state *pps);
213void pps_event(struct pps_state *pps, int event);
214void pps_init(struct pps_state *pps);
215void pps_init_abi(struct pps_state *pps);
216int pps_ioctl(unsigned long cmd, caddr_t data, struct pps_state *pps);
217void hardpps(struct timespec *tsp, long nsec);
218
219#else /* !_KERNEL */
220
221static __inline int
222time_pps_create(int filedes, pps_handle_t *handle)
223{
224 int error;
225
226 *handle = -1;
227 error = ioctl(filedes, PPS_IOC_CREATE, 0);
228 if (error < 0)
229 return (-1);
230 *handle = filedes;
231 return (0);
232}
233
234static __inline int
235time_pps_destroy(pps_handle_t handle)
236{
237 return (ioctl(handle, PPS_IOC_DESTROY, 0));
238}
239
240static __inline int
241time_pps_setparams(pps_handle_t handle, const pps_params_t *ppsparams)
242{
243 return (ioctl(handle, PPS_IOC_SETPARAMS, ppsparams));
244}
245
246static __inline int
247time_pps_getparams(pps_handle_t handle, pps_params_t *ppsparams)
248{
249 return (ioctl(handle, PPS_IOC_GETPARAMS, ppsparams));
250}
251
252static __inline int
253time_pps_getcap(pps_handle_t handle, int *mode)
254{
255 return (ioctl(handle, PPS_IOC_GETCAP, mode));
256}
257
258static __inline int
259time_pps_fetch(pps_handle_t handle, const int tsformat,
260 pps_info_t *ppsinfobuf, const struct timespec *timeout)
261{
262 int error;
263 struct pps_fetch_args arg;
264
265 arg.tsformat = tsformat;
266 if (timeout == NULL) {
267 arg.timeout.tv_sec = -1;
268 arg.timeout.tv_nsec = -1;
269 } else
270 arg.timeout = *timeout;
271 error = ioctl(handle, PPS_IOC_FETCH, &arg);
272 *ppsinfobuf = arg.pps_info_buf;
273 return (error);
274}
275
276static __inline int
277time_pps_fetch_ffc(pps_handle_t handle, const int tsformat,
278 pps_info_ffc_t *ppsinfobuf, const struct timespec *timeout)
279{
280 struct pps_fetch_ffc_args arg;
281 int error;
282
283 arg.tsformat = tsformat;
284 if (timeout == NULL) {
285 arg.timeout.tv_sec = -1;
286 arg.timeout.tv_nsec = -1;
287 } else {
288 arg.timeout = *timeout;
289 }
290 error = ioctl(handle, PPS_IOC_FETCH_FFCOUNTER, &arg);
291 *ppsinfobuf = arg.pps_info_buf_ffc;
292 return (error);
293}
294
295static __inline int
296time_pps_kcbind(pps_handle_t handle, const int kernel_consumer,
297 const int edge, const int tsformat)
298{
299 struct pps_kcbind_args arg;
300
301 arg.kernel_consumer = kernel_consumer;
302 arg.edge = edge;
303 arg.tsformat = tsformat;
304 return (ioctl(handle, PPS_IOC_KCBIND, &arg));
305}
306
307#endif /* KERNEL */
308
309#ifdef __rtems__
310#ifdef __cplusplus
311}
312#endif /* __cplusplus */
313#endif /* __rtems__ */
314#endif /* !_SYS_TIMEPPS_H_ */
This header file provides interfaces of the feed-forward clock counter.
This header file provides the interfaces of the Atomic Operations.
Definition: timepps.h:53
Definition: timepps.h:126
Definition: timepps.h:132
Definition: timepps.h:72
Definition: timepps.h:64
Definition: timepps.h:138
Definition: timepps.h:88
Definition: timetc.h:58
Definition: kern_tc.c:200
Definition: timepps.h:58