2 * PTP 1588 clock support - User space test program
4 * Copyright (C) 2010 OMICRON electronics GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
30 #include <sys/ioctl.h>
34 #include <sys/timex.h>
35 #include <sys/types.h>
39 #include <linux/ptp_clock.h>
41 #define DEVICE "/dev/ptp0"
44 #define ADJ_SETOFFSET 0x0100
48 #define CLOCK_INVALID -1
51 /* clock_adjtime is not available in GLIBC < 2.14 */
52 #if !__GLIBC_PREREQ(2, 14)
53 #include <sys/syscall.h>
54 static int clock_adjtime(clockid_t id
, struct timex
*tx
)
56 return syscall(__NR_clock_adjtime
, id
, tx
);
60 static clockid_t
get_clockid(int fd
)
63 #define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
65 return FD_TO_CLOCKID(fd
);
68 static void handle_alarm(int s
)
70 printf("received signal %d\n", s
);
73 static int install_handler(int signum
, void (*handler
)(int))
75 struct sigaction action
;
78 /* Unblock the signal. */
80 sigaddset(&mask
, signum
);
81 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
83 /* Install the signal handler. */
84 action
.sa_handler
= handler
;
86 sigemptyset(&action
.sa_mask
);
87 sigaction(signum
, &action
, NULL
);
92 static long ppb_to_scaled_ppm(int ppb
)
95 * The 'freq' field in the 'struct timex' is in parts per
96 * million, but with a 16 bit binary fractional field.
97 * Instead of calculating either one of
99 * scaled_ppm = (ppb / 1000) << 16 [1]
100 * scaled_ppm = (ppb << 16) / 1000 [2]
102 * we simply use double precision math, in order to avoid the
103 * truncation in [1] and the possible overflow in [2].
105 return (long) (ppb
* 65.536);
108 static int64_t pctns(struct ptp_clock_time
*t
)
110 return t
->sec
* 1000000000LL + t
->nsec
;
113 static void usage(char *progname
)
116 "usage: %s [options]\n"
117 " -a val request a one-shot alarm after 'val' seconds\n"
118 " -A val request a periodic alarm every 'val' seconds\n"
119 " -c query the ptp clock's capabilities\n"
120 " -d name device to open\n"
121 " -e val read 'val' external time stamp events\n"
122 " -f val adjust the ptp clock frequency by 'val' ppb\n"
123 " -g get the ptp clock time\n"
124 " -h prints this message\n"
125 " -i val index for event/trigger\n"
126 " -k val measure the time offset between system and phc clock\n"
127 " for 'val' times (Maximum 25)\n"
128 " -l list the current pin configuration\n"
129 " -L pin,val configure pin index 'pin' with function 'val'\n"
130 " the channel index is taken from the '-i' option\n"
131 " 'val' specifies the auxiliary function:\n"
133 " 1 - external time stamp\n"
134 " 2 - periodic output\n"
135 " -p val enable output with a period of 'val' nanoseconds\n"
136 " -P val enable or disable (val=1|0) the system clock PPS\n"
137 " -s set the ptp clock time from the system time\n"
138 " -S set the system time from the ptp clock time\n"
139 " -t val shift the ptp clock time by 'val' seconds\n"
140 " -T val set the ptp clock time to 'val' seconds\n",
144 int main(int argc
, char *argv
[])
146 struct ptp_clock_caps caps
;
147 struct ptp_extts_event event
;
148 struct ptp_extts_request extts_request
;
149 struct ptp_perout_request perout_request
;
150 struct ptp_pin_desc desc
;
154 static timer_t timerid
;
155 struct itimerspec timeout
;
156 struct sigevent sigevent
;
158 struct ptp_clock_time
*pct
;
159 struct ptp_sys_offset
*sysoff
;
165 char *device
= DEVICE
;
167 int adjfreq
= 0x7fffffff;
169 int capabilities
= 0;
179 int pin_index
= -1, pin_func
;
185 int64_t interval
, offset
;
187 progname
= strrchr(argv
[0], '/');
188 progname
= progname
? 1+progname
: argv
[0];
189 while (EOF
!= (c
= getopt(argc
, argv
, "a:A:cd:e:f:ghi:k:lL:p:P:sSt:T:v"))) {
192 oneshot
= atoi(optarg
);
195 periodic
= atoi(optarg
);
204 extts
= atoi(optarg
);
207 adjfreq
= atoi(optarg
);
213 index
= atoi(optarg
);
217 n_samples
= atoi(optarg
);
223 cnt
= sscanf(optarg
, "%d,%d", &pin_index
, &pin_func
);
230 perout
= atoi(optarg
);
242 adjtime
= atoi(optarg
);
246 seconds
= atoi(optarg
);
258 fd
= open(device
, O_RDWR
);
260 fprintf(stderr
, "opening %s: %s\n", device
, strerror(errno
));
264 clkid
= get_clockid(fd
);
265 if (CLOCK_INVALID
== clkid
) {
266 fprintf(stderr
, "failed to read clock id\n");
271 if (ioctl(fd
, PTP_CLOCK_GETCAPS
, &caps
)) {
272 perror("PTP_CLOCK_GETCAPS");
274 printf("capabilities:\n"
275 " %d maximum frequency adjustment (ppb)\n"
276 " %d programmable alarms\n"
277 " %d external time stamp channels\n"
278 " %d programmable periodic signals\n"
279 " %d pulse per second\n"
280 " %d programmable pins\n"
281 " %d cross timestamping\n",
288 caps
.cross_timestamping
);
292 if (0x7fffffff != adjfreq
) {
293 memset(&tx
, 0, sizeof(tx
));
294 tx
.modes
= ADJ_FREQUENCY
;
295 tx
.freq
= ppb_to_scaled_ppm(adjfreq
);
296 if (clock_adjtime(clkid
, &tx
)) {
297 perror("clock_adjtime");
299 puts("frequency adjustment okay");
304 memset(&tx
, 0, sizeof(tx
));
305 tx
.modes
= ADJ_SETOFFSET
;
306 tx
.time
.tv_sec
= adjtime
;
308 if (clock_adjtime(clkid
, &tx
) < 0) {
309 perror("clock_adjtime");
311 puts("time shift okay");
316 if (clock_gettime(clkid
, &ts
)) {
317 perror("clock_gettime");
319 printf("clock time: %ld.%09ld or %s",
320 ts
.tv_sec
, ts
.tv_nsec
, ctime(&ts
.tv_sec
));
325 clock_gettime(CLOCK_REALTIME
, &ts
);
326 if (clock_settime(clkid
, &ts
)) {
327 perror("clock_settime");
329 puts("set time okay");
334 clock_gettime(clkid
, &ts
);
335 if (clock_settime(CLOCK_REALTIME
, &ts
)) {
336 perror("clock_settime");
338 puts("set time okay");
345 if (clock_settime(clkid
, &ts
)) {
346 perror("clock_settime");
348 puts("set time okay");
353 memset(&extts_request
, 0, sizeof(extts_request
));
354 extts_request
.index
= index
;
355 extts_request
.flags
= PTP_ENABLE_FEATURE
;
356 if (ioctl(fd
, PTP_EXTTS_REQUEST
, &extts_request
)) {
357 perror("PTP_EXTTS_REQUEST");
360 puts("external time stamp request okay");
362 for (; extts
; extts
--) {
363 cnt
= read(fd
, &event
, sizeof(event
));
364 if (cnt
!= sizeof(event
)) {
368 printf("event index %u at %lld.%09u\n", event
.index
,
369 event
.t
.sec
, event
.t
.nsec
);
372 /* Disable the feature again. */
373 extts_request
.flags
= 0;
374 if (ioctl(fd
, PTP_EXTTS_REQUEST
, &extts_request
)) {
375 perror("PTP_EXTTS_REQUEST");
381 if (ioctl(fd
, PTP_CLOCK_GETCAPS
, &caps
)) {
382 perror("PTP_CLOCK_GETCAPS");
384 n_pins
= caps
.n_pins
;
386 for (i
= 0; i
< n_pins
; i
++) {
388 if (ioctl(fd
, PTP_PIN_GETFUNC
, &desc
)) {
389 perror("PTP_PIN_GETFUNC");
392 printf("name %s index %u func %u chan %u\n",
393 desc
.name
, desc
.index
, desc
.func
, desc
.chan
);
398 install_handler(SIGALRM
, handle_alarm
);
399 /* Create a timer. */
400 sigevent
.sigev_notify
= SIGEV_SIGNAL
;
401 sigevent
.sigev_signo
= SIGALRM
;
402 if (timer_create(clkid
, &sigevent
, &timerid
)) {
403 perror("timer_create");
406 /* Start the timer. */
407 memset(&timeout
, 0, sizeof(timeout
));
408 timeout
.it_value
.tv_sec
= oneshot
;
409 if (timer_settime(timerid
, 0, &timeout
, NULL
)) {
410 perror("timer_settime");
414 timer_delete(timerid
);
418 install_handler(SIGALRM
, handle_alarm
);
419 /* Create a timer. */
420 sigevent
.sigev_notify
= SIGEV_SIGNAL
;
421 sigevent
.sigev_signo
= SIGALRM
;
422 if (timer_create(clkid
, &sigevent
, &timerid
)) {
423 perror("timer_create");
426 /* Start the timer. */
427 memset(&timeout
, 0, sizeof(timeout
));
428 timeout
.it_interval
.tv_sec
= periodic
;
429 timeout
.it_value
.tv_sec
= periodic
;
430 if (timer_settime(timerid
, 0, &timeout
, NULL
)) {
431 perror("timer_settime");
437 timer_delete(timerid
);
441 if (clock_gettime(clkid
, &ts
)) {
442 perror("clock_gettime");
445 memset(&perout_request
, 0, sizeof(perout_request
));
446 perout_request
.index
= index
;
447 perout_request
.start
.sec
= ts
.tv_sec
+ 2;
448 perout_request
.start
.nsec
= 0;
449 perout_request
.period
.sec
= 0;
450 perout_request
.period
.nsec
= perout
;
451 if (ioctl(fd
, PTP_PEROUT_REQUEST
, &perout_request
)) {
452 perror("PTP_PEROUT_REQUEST");
454 puts("periodic output request okay");
458 if (pin_index
>= 0) {
459 memset(&desc
, 0, sizeof(desc
));
460 desc
.index
= pin_index
;
461 desc
.func
= pin_func
;
463 if (ioctl(fd
, PTP_PIN_SETFUNC
, &desc
)) {
464 perror("PTP_PIN_SETFUNC");
466 puts("set pin function okay");
471 int enable
= pps
? 1 : 0;
472 if (ioctl(fd
, PTP_ENABLE_PPS
, enable
)) {
473 perror("PTP_ENABLE_PPS");
475 puts("pps for system time request okay");
480 if (n_samples
<= 0 || n_samples
> 25) {
481 puts("n_samples should be between 1 and 25");
486 sysoff
= calloc(1, sizeof(*sysoff
));
491 sysoff
->n_samples
= n_samples
;
493 if (ioctl(fd
, PTP_SYS_OFFSET
, sysoff
))
494 perror("PTP_SYS_OFFSET");
496 puts("system and phc clock time offset request okay");
498 pct
= &sysoff
->ts
[0];
499 for (i
= 0; i
< sysoff
->n_samples
; i
++) {
501 tp
= pctns(pct
+2*i
+1);
502 t2
= pctns(pct
+2*i
+2);
504 offset
= (t2
+ t1
) / 2 - tp
;
506 printf("system time: %lld.%u\n",
507 (pct
+2*i
)->sec
, (pct
+2*i
)->nsec
);
508 printf("phc time: %lld.%u\n",
509 (pct
+2*i
+1)->sec
, (pct
+2*i
+1)->nsec
);
510 printf("system time: %lld.%u\n",
511 (pct
+2*i
+2)->sec
, (pct
+2*i
+2)->nsec
);
512 printf("system/phc clock time offset is %" PRId64
" ns\n"
513 "system clock time delay is %" PRId64
" ns\n",