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