blob: e9eaee622032c7a0682bbccacccb6013745e11af [file] [log] [blame]
Richard Cochrand94ba802011-04-22 12:03:08 +02001/*
2 * PTP 1588 clock support - User space test program
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
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.
10 *
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.
15 *
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.
19 */
20#include <errno.h>
21#include <fcntl.h>
22#include <math.h>
23#include <signal.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <sys/stat.h>
30#include <sys/time.h>
31#include <sys/timex.h>
32#include <sys/types.h>
33#include <time.h>
34#include <unistd.h>
35
36#include <linux/ptp_clock.h>
37
38#define DEVICE "/dev/ptp0"
39
40#ifndef ADJ_SETOFFSET
41#define ADJ_SETOFFSET 0x0100
42#endif
43
44#ifndef CLOCK_INVALID
45#define CLOCK_INVALID -1
46#endif
47
48/* When glibc offers the syscall, this will go away. */
49#include <sys/syscall.h>
50static int clock_adjtime(clockid_t id, struct timex *tx)
51{
52 return syscall(__NR_clock_adjtime, id, tx);
53}
54
55static clockid_t get_clockid(int fd)
56{
57#define CLOCKFD 3
58#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
59
60 return FD_TO_CLOCKID(fd);
61}
62
63static void handle_alarm(int s)
64{
65 printf("received signal %d\n", s);
66}
67
68static int install_handler(int signum, void (*handler)(int))
69{
70 struct sigaction action;
71 sigset_t mask;
72
73 /* Unblock the signal. */
74 sigemptyset(&mask);
75 sigaddset(&mask, signum);
76 sigprocmask(SIG_UNBLOCK, &mask, NULL);
77
78 /* Install the signal handler. */
79 action.sa_handler = handler;
80 action.sa_flags = 0;
81 sigemptyset(&action.sa_mask);
82 sigaction(signum, &action, NULL);
83
84 return 0;
85}
86
87static long ppb_to_scaled_ppm(int ppb)
88{
89 /*
90 * The 'freq' field in the 'struct timex' is in parts per
91 * million, but with a 16 bit binary fractional field.
92 * Instead of calculating either one of
93 *
94 * scaled_ppm = (ppb / 1000) << 16 [1]
95 * scaled_ppm = (ppb << 16) / 1000 [2]
96 *
97 * we simply use double precision math, in order to avoid the
98 * truncation in [1] and the possible overflow in [2].
99 */
100 return (long) (ppb * 65.536);
101}
102
Dong Zhu568ebc52013-09-17 15:32:35 +0800103static int64_t pctns(struct ptp_clock_time *t)
104{
105 return t->sec * 1000000000LL + t->nsec;
106}
107
Richard Cochrand94ba802011-04-22 12:03:08 +0200108static void usage(char *progname)
109{
110 fprintf(stderr,
111 "usage: %s [options]\n"
112 " -a val request a one-shot alarm after 'val' seconds\n"
113 " -A val request a periodic alarm every 'val' seconds\n"
114 " -c query the ptp clock's capabilities\n"
115 " -d name device to open\n"
116 " -e val read 'val' external time stamp events\n"
117 " -f val adjust the ptp clock frequency by 'val' ppb\n"
118 " -g get the ptp clock time\n"
119 " -h prints this message\n"
Stefan Sørensen61950e82014-02-04 08:46:36 +0100120 " -i val index for event/trigger\n"
Dong Zhu568ebc52013-09-17 15:32:35 +0800121 " -k val measure the time offset between system and phc clock\n"
122 " for 'val' times (Maximum 25)\n"
Richard Cochran888a3682014-03-20 22:21:53 +0100123 " -l list the current pin configuration\n"
124 " -L pin,val configure pin index 'pin' with function 'val'\n"
125 " the channel index is taken from the '-i' option\n"
126 " 'val' specifies the auxiliary function:\n"
127 " 0 - none\n"
128 " 1 - external time stamp\n"
129 " 2 - periodic output\n"
Richard Cochrand94ba802011-04-22 12:03:08 +0200130 " -p val enable output with a period of 'val' nanoseconds\n"
131 " -P val enable or disable (val=1|0) the system clock PPS\n"
132 " -s set the ptp clock time from the system time\n"
133 " -S set the system time from the ptp clock time\n"
134 " -t val shift the ptp clock time by 'val' seconds\n",
135 progname);
136}
137
138int main(int argc, char *argv[])
139{
140 struct ptp_clock_caps caps;
141 struct ptp_extts_event event;
142 struct ptp_extts_request extts_request;
143 struct ptp_perout_request perout_request;
Richard Cochran888a3682014-03-20 22:21:53 +0100144 struct ptp_pin_desc desc;
Richard Cochrand94ba802011-04-22 12:03:08 +0200145 struct timespec ts;
146 struct timex tx;
147
148 static timer_t timerid;
149 struct itimerspec timeout;
150 struct sigevent sigevent;
151
Dong Zhu568ebc52013-09-17 15:32:35 +0800152 struct ptp_clock_time *pct;
153 struct ptp_sys_offset *sysoff;
154
155
Richard Cochrand94ba802011-04-22 12:03:08 +0200156 char *progname;
Dong Zhu568ebc52013-09-17 15:32:35 +0800157 int i, c, cnt, fd;
Richard Cochrand94ba802011-04-22 12:03:08 +0200158
159 char *device = DEVICE;
160 clockid_t clkid;
161 int adjfreq = 0x7fffffff;
162 int adjtime = 0;
163 int capabilities = 0;
164 int extts = 0;
165 int gettime = 0;
Stefan Sørensen61950e82014-02-04 08:46:36 +0100166 int index = 0;
Richard Cochran888a3682014-03-20 22:21:53 +0100167 int list_pins = 0;
Richard Cochrand94ba802011-04-22 12:03:08 +0200168 int oneshot = 0;
Dong Zhu568ebc52013-09-17 15:32:35 +0800169 int pct_offset = 0;
170 int n_samples = 0;
Richard Cochrand94ba802011-04-22 12:03:08 +0200171 int periodic = 0;
172 int perout = -1;
Richard Cochran888a3682014-03-20 22:21:53 +0100173 int pin_index = -1, pin_func;
Richard Cochrand94ba802011-04-22 12:03:08 +0200174 int pps = -1;
175 int settime = 0;
176
Dong Zhu568ebc52013-09-17 15:32:35 +0800177 int64_t t1, t2, tp;
178 int64_t interval, offset;
179
Richard Cochrand94ba802011-04-22 12:03:08 +0200180 progname = strrchr(argv[0], '/');
181 progname = progname ? 1+progname : argv[0];
Richard Cochran888a3682014-03-20 22:21:53 +0100182 while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghi:k:lL:p:P:sSt:v"))) {
Richard Cochrand94ba802011-04-22 12:03:08 +0200183 switch (c) {
184 case 'a':
185 oneshot = atoi(optarg);
186 break;
187 case 'A':
188 periodic = atoi(optarg);
189 break;
190 case 'c':
191 capabilities = 1;
192 break;
193 case 'd':
194 device = optarg;
195 break;
196 case 'e':
197 extts = atoi(optarg);
198 break;
199 case 'f':
200 adjfreq = atoi(optarg);
201 break;
202 case 'g':
203 gettime = 1;
204 break;
Stefan Sørensen61950e82014-02-04 08:46:36 +0100205 case 'i':
206 index = atoi(optarg);
207 break;
Dong Zhu568ebc52013-09-17 15:32:35 +0800208 case 'k':
209 pct_offset = 1;
210 n_samples = atoi(optarg);
211 break;
Richard Cochran888a3682014-03-20 22:21:53 +0100212 case 'l':
213 list_pins = 1;
214 break;
215 case 'L':
216 cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
217 if (cnt != 2) {
218 usage(progname);
219 return -1;
220 }
221 break;
Richard Cochrand94ba802011-04-22 12:03:08 +0200222 case 'p':
223 perout = atoi(optarg);
224 break;
225 case 'P':
226 pps = atoi(optarg);
227 break;
228 case 's':
229 settime = 1;
230 break;
231 case 'S':
232 settime = 2;
233 break;
234 case 't':
235 adjtime = atoi(optarg);
236 break;
237 case 'h':
238 usage(progname);
239 return 0;
240 case '?':
241 default:
242 usage(progname);
243 return -1;
244 }
245 }
246
247 fd = open(device, O_RDWR);
248 if (fd < 0) {
249 fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
250 return -1;
251 }
252
253 clkid = get_clockid(fd);
254 if (CLOCK_INVALID == clkid) {
255 fprintf(stderr, "failed to read clock id\n");
256 return -1;
257 }
258
259 if (capabilities) {
260 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
261 perror("PTP_CLOCK_GETCAPS");
262 } else {
263 printf("capabilities:\n"
264 " %d maximum frequency adjustment (ppb)\n"
265 " %d programmable alarms\n"
266 " %d external time stamp channels\n"
267 " %d programmable periodic signals\n"
Richard Cochran888a3682014-03-20 22:21:53 +0100268 " %d pulse per second\n"
269 " %d programmable pins\n",
Richard Cochrand94ba802011-04-22 12:03:08 +0200270 caps.max_adj,
271 caps.n_alarm,
272 caps.n_ext_ts,
273 caps.n_per_out,
Richard Cochran888a3682014-03-20 22:21:53 +0100274 caps.pps,
275 caps.n_pins);
Richard Cochrand94ba802011-04-22 12:03:08 +0200276 }
277 }
278
279 if (0x7fffffff != adjfreq) {
280 memset(&tx, 0, sizeof(tx));
281 tx.modes = ADJ_FREQUENCY;
282 tx.freq = ppb_to_scaled_ppm(adjfreq);
283 if (clock_adjtime(clkid, &tx)) {
284 perror("clock_adjtime");
285 } else {
286 puts("frequency adjustment okay");
287 }
288 }
289
290 if (adjtime) {
291 memset(&tx, 0, sizeof(tx));
292 tx.modes = ADJ_SETOFFSET;
293 tx.time.tv_sec = adjtime;
294 tx.time.tv_usec = 0;
295 if (clock_adjtime(clkid, &tx) < 0) {
296 perror("clock_adjtime");
297 } else {
298 puts("time shift okay");
299 }
300 }
301
302 if (gettime) {
303 if (clock_gettime(clkid, &ts)) {
304 perror("clock_gettime");
305 } else {
306 printf("clock time: %ld.%09ld or %s",
307 ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
308 }
309 }
310
311 if (settime == 1) {
312 clock_gettime(CLOCK_REALTIME, &ts);
313 if (clock_settime(clkid, &ts)) {
314 perror("clock_settime");
315 } else {
316 puts("set time okay");
317 }
318 }
319
320 if (settime == 2) {
321 clock_gettime(clkid, &ts);
322 if (clock_settime(CLOCK_REALTIME, &ts)) {
323 perror("clock_settime");
324 } else {
325 puts("set time okay");
326 }
327 }
328
329 if (extts) {
330 memset(&extts_request, 0, sizeof(extts_request));
Stefan Sørensen61950e82014-02-04 08:46:36 +0100331 extts_request.index = index;
Richard Cochrand94ba802011-04-22 12:03:08 +0200332 extts_request.flags = PTP_ENABLE_FEATURE;
333 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
334 perror("PTP_EXTTS_REQUEST");
335 extts = 0;
336 } else {
337 puts("external time stamp request okay");
338 }
339 for (; extts; extts--) {
340 cnt = read(fd, &event, sizeof(event));
341 if (cnt != sizeof(event)) {
342 perror("read");
343 break;
344 }
345 printf("event index %u at %lld.%09u\n", event.index,
346 event.t.sec, event.t.nsec);
347 fflush(stdout);
348 }
349 /* Disable the feature again. */
350 extts_request.flags = 0;
351 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
352 perror("PTP_EXTTS_REQUEST");
353 }
354 }
355
Richard Cochran888a3682014-03-20 22:21:53 +0100356 if (list_pins) {
357 int n_pins = 0;
358 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
359 perror("PTP_CLOCK_GETCAPS");
360 } else {
361 n_pins = caps.n_pins;
362 }
363 for (i = 0; i < n_pins; i++) {
364 desc.index = i;
365 if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
366 perror("PTP_PIN_GETFUNC");
367 break;
368 }
369 printf("name %s index %u func %u chan %u\n",
370 desc.name, desc.index, desc.func, desc.chan);
371 }
372 }
373
Richard Cochrand94ba802011-04-22 12:03:08 +0200374 if (oneshot) {
375 install_handler(SIGALRM, handle_alarm);
376 /* Create a timer. */
377 sigevent.sigev_notify = SIGEV_SIGNAL;
378 sigevent.sigev_signo = SIGALRM;
379 if (timer_create(clkid, &sigevent, &timerid)) {
380 perror("timer_create");
381 return -1;
382 }
383 /* Start the timer. */
384 memset(&timeout, 0, sizeof(timeout));
385 timeout.it_value.tv_sec = oneshot;
386 if (timer_settime(timerid, 0, &timeout, NULL)) {
387 perror("timer_settime");
388 return -1;
389 }
390 pause();
391 timer_delete(timerid);
392 }
393
394 if (periodic) {
395 install_handler(SIGALRM, handle_alarm);
396 /* Create a timer. */
397 sigevent.sigev_notify = SIGEV_SIGNAL;
398 sigevent.sigev_signo = SIGALRM;
399 if (timer_create(clkid, &sigevent, &timerid)) {
400 perror("timer_create");
401 return -1;
402 }
403 /* Start the timer. */
404 memset(&timeout, 0, sizeof(timeout));
405 timeout.it_interval.tv_sec = periodic;
406 timeout.it_value.tv_sec = periodic;
407 if (timer_settime(timerid, 0, &timeout, NULL)) {
408 perror("timer_settime");
409 return -1;
410 }
411 while (1) {
412 pause();
413 }
414 timer_delete(timerid);
415 }
416
417 if (perout >= 0) {
418 if (clock_gettime(clkid, &ts)) {
419 perror("clock_gettime");
420 return -1;
421 }
422 memset(&perout_request, 0, sizeof(perout_request));
Stefan Sørensen61950e82014-02-04 08:46:36 +0100423 perout_request.index = index;
Richard Cochrand94ba802011-04-22 12:03:08 +0200424 perout_request.start.sec = ts.tv_sec + 2;
425 perout_request.start.nsec = 0;
426 perout_request.period.sec = 0;
427 perout_request.period.nsec = perout;
428 if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
429 perror("PTP_PEROUT_REQUEST");
430 } else {
431 puts("periodic output request okay");
432 }
433 }
434
Richard Cochran888a3682014-03-20 22:21:53 +0100435 if (pin_index >= 0) {
436 memset(&desc, 0, sizeof(desc));
437 desc.index = pin_index;
438 desc.func = pin_func;
439 desc.chan = index;
440 if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
441 perror("PTP_PIN_SETFUNC");
442 } else {
443 puts("set pin function okay");
444 }
445 }
446
Richard Cochrand94ba802011-04-22 12:03:08 +0200447 if (pps != -1) {
448 int enable = pps ? 1 : 0;
449 if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
450 perror("PTP_ENABLE_PPS");
451 } else {
452 puts("pps for system time request okay");
453 }
454 }
455
Dong Zhu568ebc52013-09-17 15:32:35 +0800456 if (pct_offset) {
457 if (n_samples <= 0 || n_samples > 25) {
458 puts("n_samples should be between 1 and 25");
459 usage(progname);
460 return -1;
461 }
462
463 sysoff = calloc(1, sizeof(*sysoff));
464 if (!sysoff) {
465 perror("calloc");
466 return -1;
467 }
468 sysoff->n_samples = n_samples;
469
470 if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
471 perror("PTP_SYS_OFFSET");
472 else
473 puts("system and phc clock time offset request okay");
474
475 pct = &sysoff->ts[0];
476 for (i = 0; i < sysoff->n_samples; i++) {
477 t1 = pctns(pct+2*i);
478 tp = pctns(pct+2*i+1);
479 t2 = pctns(pct+2*i+2);
480 interval = t2 - t1;
481 offset = (t2 + t1) / 2 - tp;
482
483 printf("system time: %ld.%ld\n",
484 (pct+2*i)->sec, (pct+2*i)->nsec);
485 printf("phc time: %ld.%ld\n",
486 (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
487 printf("system time: %ld.%ld\n",
488 (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
489 printf("system/phc clock time offset is %ld ns\n"
490 "system clock time delay is %ld ns\n",
491 offset, interval);
492 }
493
494 free(sysoff);
495 }
496
Richard Cochrand94ba802011-04-22 12:03:08 +0200497 close(fd);
498 return 0;
499}