.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * PTP 1588 clock support - User space test program |
---|
3 | 4 | * |
---|
4 | 5 | * 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 | 6 | */ |
---|
20 | 7 | #define _GNU_SOURCE |
---|
21 | 8 | #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */ |
---|
.. | .. |
---|
48 | 35 | #define CLOCK_INVALID -1 |
---|
49 | 36 | #endif |
---|
50 | 37 | |
---|
| 38 | +#define NSEC_PER_SEC 1000000000LL |
---|
| 39 | + |
---|
51 | 40 | /* clock_adjtime is not available in GLIBC < 2.14 */ |
---|
52 | 41 | #if !__GLIBC_PREREQ(2, 14) |
---|
53 | 42 | #include <sys/syscall.h> |
---|
.. | .. |
---|
57 | 46 | } |
---|
58 | 47 | #endif |
---|
59 | 48 | |
---|
| 49 | +static void show_flag_test(int rq_index, unsigned int flags, int err) |
---|
| 50 | +{ |
---|
| 51 | + printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n", |
---|
| 52 | + rq_index ? '1' + rq_index : ' ', |
---|
| 53 | + flags, err, strerror(errno)); |
---|
| 54 | + /* sigh, uClibc ... */ |
---|
| 55 | + errno = 0; |
---|
| 56 | +} |
---|
| 57 | + |
---|
| 58 | +static void do_flag_test(int fd, unsigned int index) |
---|
| 59 | +{ |
---|
| 60 | + struct ptp_extts_request extts_request; |
---|
| 61 | + unsigned long request[2] = { |
---|
| 62 | + PTP_EXTTS_REQUEST, |
---|
| 63 | + PTP_EXTTS_REQUEST2, |
---|
| 64 | + }; |
---|
| 65 | + unsigned int enable_flags[5] = { |
---|
| 66 | + PTP_ENABLE_FEATURE, |
---|
| 67 | + PTP_ENABLE_FEATURE | PTP_RISING_EDGE, |
---|
| 68 | + PTP_ENABLE_FEATURE | PTP_FALLING_EDGE, |
---|
| 69 | + PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE, |
---|
| 70 | + PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1), |
---|
| 71 | + }; |
---|
| 72 | + int err, i, j; |
---|
| 73 | + |
---|
| 74 | + memset(&extts_request, 0, sizeof(extts_request)); |
---|
| 75 | + extts_request.index = index; |
---|
| 76 | + |
---|
| 77 | + for (i = 0; i < 2; i++) { |
---|
| 78 | + for (j = 0; j < 5; j++) { |
---|
| 79 | + extts_request.flags = enable_flags[j]; |
---|
| 80 | + err = ioctl(fd, request[i], &extts_request); |
---|
| 81 | + show_flag_test(i, extts_request.flags, err); |
---|
| 82 | + |
---|
| 83 | + extts_request.flags = 0; |
---|
| 84 | + err = ioctl(fd, request[i], &extts_request); |
---|
| 85 | + } |
---|
| 86 | + } |
---|
| 87 | +} |
---|
| 88 | + |
---|
60 | 89 | static clockid_t get_clockid(int fd) |
---|
61 | 90 | { |
---|
62 | 91 | #define CLOCKFD 3 |
---|
63 | 92 | return (((unsigned int) ~fd) << 3) | CLOCKFD; |
---|
64 | | -} |
---|
65 | | - |
---|
66 | | -static void handle_alarm(int s) |
---|
67 | | -{ |
---|
68 | | - printf("received signal %d\n", s); |
---|
69 | | -} |
---|
70 | | - |
---|
71 | | -static int install_handler(int signum, void (*handler)(int)) |
---|
72 | | -{ |
---|
73 | | - struct sigaction action; |
---|
74 | | - sigset_t mask; |
---|
75 | | - |
---|
76 | | - /* Unblock the signal. */ |
---|
77 | | - sigemptyset(&mask); |
---|
78 | | - sigaddset(&mask, signum); |
---|
79 | | - sigprocmask(SIG_UNBLOCK, &mask, NULL); |
---|
80 | | - |
---|
81 | | - /* Install the signal handler. */ |
---|
82 | | - action.sa_handler = handler; |
---|
83 | | - action.sa_flags = 0; |
---|
84 | | - sigemptyset(&action.sa_mask); |
---|
85 | | - sigaction(signum, &action, NULL); |
---|
86 | | - |
---|
87 | | - return 0; |
---|
88 | 93 | } |
---|
89 | 94 | |
---|
90 | 95 | static long ppb_to_scaled_ppm(int ppb) |
---|
.. | .. |
---|
112 | 117 | { |
---|
113 | 118 | fprintf(stderr, |
---|
114 | 119 | "usage: %s [options]\n" |
---|
115 | | - " -a val request a one-shot alarm after 'val' seconds\n" |
---|
116 | | - " -A val request a periodic alarm every 'val' seconds\n" |
---|
117 | 120 | " -c query the ptp clock's capabilities\n" |
---|
118 | 121 | " -d name device to open\n" |
---|
119 | 122 | " -e val read 'val' external time stamp events\n" |
---|
.. | .. |
---|
131 | 134 | " 1 - external time stamp\n" |
---|
132 | 135 | " 2 - periodic output\n" |
---|
133 | 136 | " -p val enable output with a period of 'val' nanoseconds\n" |
---|
| 137 | + " -H val set output phase to 'val' nanoseconds (requires -p)\n" |
---|
| 138 | + " -w val set output pulse width to 'val' nanoseconds (requires -p)\n" |
---|
134 | 139 | " -P val enable or disable (val=1|0) the system clock PPS\n" |
---|
135 | 140 | " -s set the ptp clock time from the system time\n" |
---|
136 | 141 | " -S set the system time from the ptp clock time\n" |
---|
137 | 142 | " -t val shift the ptp clock time by 'val' seconds\n" |
---|
138 | | - " -T val set the ptp clock time to 'val' seconds\n", |
---|
| 143 | + " -T val set the ptp clock time to 'val' seconds\n" |
---|
| 144 | + " -z test combinations of rising/falling external time stamp flags\n", |
---|
139 | 145 | progname); |
---|
140 | 146 | } |
---|
141 | 147 | |
---|
.. | .. |
---|
148 | 154 | struct ptp_pin_desc desc; |
---|
149 | 155 | struct timespec ts; |
---|
150 | 156 | struct timex tx; |
---|
151 | | - |
---|
152 | | - static timer_t timerid; |
---|
153 | | - struct itimerspec timeout; |
---|
154 | | - struct sigevent sigevent; |
---|
155 | | - |
---|
156 | 157 | struct ptp_clock_time *pct; |
---|
157 | 158 | struct ptp_sys_offset *sysoff; |
---|
158 | | - |
---|
159 | 159 | |
---|
160 | 160 | char *progname; |
---|
161 | 161 | unsigned int i; |
---|
.. | .. |
---|
167 | 167 | int adjtime = 0; |
---|
168 | 168 | int capabilities = 0; |
---|
169 | 169 | int extts = 0; |
---|
| 170 | + int flagtest = 0; |
---|
170 | 171 | int gettime = 0; |
---|
171 | 172 | int index = 0; |
---|
172 | 173 | int list_pins = 0; |
---|
173 | | - int oneshot = 0; |
---|
174 | 174 | int pct_offset = 0; |
---|
175 | 175 | int n_samples = 0; |
---|
176 | | - int periodic = 0; |
---|
177 | | - int perout = -1; |
---|
178 | 176 | int pin_index = -1, pin_func; |
---|
179 | 177 | int pps = -1; |
---|
180 | 178 | int seconds = 0; |
---|
.. | .. |
---|
182 | 180 | |
---|
183 | 181 | int64_t t1, t2, tp; |
---|
184 | 182 | int64_t interval, offset; |
---|
| 183 | + int64_t perout_phase = -1; |
---|
| 184 | + int64_t pulsewidth = -1; |
---|
| 185 | + int64_t perout = -1; |
---|
185 | 186 | |
---|
186 | 187 | progname = strrchr(argv[0], '/'); |
---|
187 | 188 | progname = progname ? 1+progname : argv[0]; |
---|
188 | | - while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghi:k:lL:p:P:sSt:T:v"))) { |
---|
| 189 | + while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:p:P:sSt:T:w:z"))) { |
---|
189 | 190 | switch (c) { |
---|
190 | | - case 'a': |
---|
191 | | - oneshot = atoi(optarg); |
---|
192 | | - break; |
---|
193 | | - case 'A': |
---|
194 | | - periodic = atoi(optarg); |
---|
195 | | - break; |
---|
196 | 191 | case 'c': |
---|
197 | 192 | capabilities = 1; |
---|
198 | 193 | break; |
---|
.. | .. |
---|
207 | 202 | break; |
---|
208 | 203 | case 'g': |
---|
209 | 204 | gettime = 1; |
---|
| 205 | + break; |
---|
| 206 | + case 'H': |
---|
| 207 | + perout_phase = atoll(optarg); |
---|
210 | 208 | break; |
---|
211 | 209 | case 'i': |
---|
212 | 210 | index = atoi(optarg); |
---|
.. | .. |
---|
226 | 224 | } |
---|
227 | 225 | break; |
---|
228 | 226 | case 'p': |
---|
229 | | - perout = atoi(optarg); |
---|
| 227 | + perout = atoll(optarg); |
---|
230 | 228 | break; |
---|
231 | 229 | case 'P': |
---|
232 | 230 | pps = atoi(optarg); |
---|
.. | .. |
---|
243 | 241 | case 'T': |
---|
244 | 242 | settime = 3; |
---|
245 | 243 | seconds = atoi(optarg); |
---|
| 244 | + break; |
---|
| 245 | + case 'w': |
---|
| 246 | + pulsewidth = atoi(optarg); |
---|
| 247 | + break; |
---|
| 248 | + case 'z': |
---|
| 249 | + flagtest = 1; |
---|
246 | 250 | break; |
---|
247 | 251 | case 'h': |
---|
248 | 252 | usage(progname); |
---|
.. | .. |
---|
277 | 281 | " %d programmable periodic signals\n" |
---|
278 | 282 | " %d pulse per second\n" |
---|
279 | 283 | " %d programmable pins\n" |
---|
280 | | - " %d cross timestamping\n", |
---|
| 284 | + " %d cross timestamping\n" |
---|
| 285 | + " %d adjust_phase\n", |
---|
281 | 286 | caps.max_adj, |
---|
282 | 287 | caps.n_alarm, |
---|
283 | 288 | caps.n_ext_ts, |
---|
284 | 289 | caps.n_per_out, |
---|
285 | 290 | caps.pps, |
---|
286 | 291 | caps.n_pins, |
---|
287 | | - caps.cross_timestamping); |
---|
| 292 | + caps.cross_timestamping, |
---|
| 293 | + caps.adjust_phase); |
---|
288 | 294 | } |
---|
289 | 295 | } |
---|
290 | 296 | |
---|
.. | .. |
---|
375 | 381 | } |
---|
376 | 382 | } |
---|
377 | 383 | |
---|
| 384 | + if (flagtest) { |
---|
| 385 | + do_flag_test(fd, index); |
---|
| 386 | + } |
---|
| 387 | + |
---|
378 | 388 | if (list_pins) { |
---|
379 | 389 | int n_pins = 0; |
---|
380 | 390 | if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { |
---|
.. | .. |
---|
393 | 403 | } |
---|
394 | 404 | } |
---|
395 | 405 | |
---|
396 | | - if (oneshot) { |
---|
397 | | - install_handler(SIGALRM, handle_alarm); |
---|
398 | | - /* Create a timer. */ |
---|
399 | | - sigevent.sigev_notify = SIGEV_SIGNAL; |
---|
400 | | - sigevent.sigev_signo = SIGALRM; |
---|
401 | | - if (timer_create(clkid, &sigevent, &timerid)) { |
---|
402 | | - perror("timer_create"); |
---|
403 | | - return -1; |
---|
404 | | - } |
---|
405 | | - /* Start the timer. */ |
---|
406 | | - memset(&timeout, 0, sizeof(timeout)); |
---|
407 | | - timeout.it_value.tv_sec = oneshot; |
---|
408 | | - if (timer_settime(timerid, 0, &timeout, NULL)) { |
---|
409 | | - perror("timer_settime"); |
---|
410 | | - return -1; |
---|
411 | | - } |
---|
412 | | - pause(); |
---|
413 | | - timer_delete(timerid); |
---|
| 406 | + if (pulsewidth >= 0 && perout < 0) { |
---|
| 407 | + puts("-w can only be specified together with -p"); |
---|
| 408 | + return -1; |
---|
414 | 409 | } |
---|
415 | 410 | |
---|
416 | | - if (periodic) { |
---|
417 | | - install_handler(SIGALRM, handle_alarm); |
---|
418 | | - /* Create a timer. */ |
---|
419 | | - sigevent.sigev_notify = SIGEV_SIGNAL; |
---|
420 | | - sigevent.sigev_signo = SIGALRM; |
---|
421 | | - if (timer_create(clkid, &sigevent, &timerid)) { |
---|
422 | | - perror("timer_create"); |
---|
423 | | - return -1; |
---|
424 | | - } |
---|
425 | | - /* Start the timer. */ |
---|
426 | | - memset(&timeout, 0, sizeof(timeout)); |
---|
427 | | - timeout.it_interval.tv_sec = periodic; |
---|
428 | | - timeout.it_value.tv_sec = periodic; |
---|
429 | | - if (timer_settime(timerid, 0, &timeout, NULL)) { |
---|
430 | | - perror("timer_settime"); |
---|
431 | | - return -1; |
---|
432 | | - } |
---|
433 | | - while (1) { |
---|
434 | | - pause(); |
---|
435 | | - } |
---|
436 | | - timer_delete(timerid); |
---|
| 411 | + if (perout_phase >= 0 && perout < 0) { |
---|
| 412 | + puts("-H can only be specified together with -p"); |
---|
| 413 | + return -1; |
---|
437 | 414 | } |
---|
438 | 415 | |
---|
439 | 416 | if (perout >= 0) { |
---|
.. | .. |
---|
443 | 420 | } |
---|
444 | 421 | memset(&perout_request, 0, sizeof(perout_request)); |
---|
445 | 422 | perout_request.index = index; |
---|
446 | | - perout_request.start.sec = ts.tv_sec + 2; |
---|
447 | | - perout_request.start.nsec = 0; |
---|
448 | | - perout_request.period.sec = 0; |
---|
449 | | - perout_request.period.nsec = perout; |
---|
450 | | - if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) { |
---|
| 423 | + perout_request.period.sec = perout / NSEC_PER_SEC; |
---|
| 424 | + perout_request.period.nsec = perout % NSEC_PER_SEC; |
---|
| 425 | + perout_request.flags = 0; |
---|
| 426 | + if (pulsewidth >= 0) { |
---|
| 427 | + perout_request.flags |= PTP_PEROUT_DUTY_CYCLE; |
---|
| 428 | + perout_request.on.sec = pulsewidth / NSEC_PER_SEC; |
---|
| 429 | + perout_request.on.nsec = pulsewidth % NSEC_PER_SEC; |
---|
| 430 | + } |
---|
| 431 | + if (perout_phase >= 0) { |
---|
| 432 | + perout_request.flags |= PTP_PEROUT_PHASE; |
---|
| 433 | + perout_request.phase.sec = perout_phase / NSEC_PER_SEC; |
---|
| 434 | + perout_request.phase.nsec = perout_phase % NSEC_PER_SEC; |
---|
| 435 | + } else { |
---|
| 436 | + perout_request.start.sec = ts.tv_sec + 2; |
---|
| 437 | + perout_request.start.nsec = 0; |
---|
| 438 | + } |
---|
| 439 | + |
---|
| 440 | + if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) { |
---|
451 | 441 | perror("PTP_PEROUT_REQUEST"); |
---|
452 | 442 | } else { |
---|
453 | 443 | puts("periodic output request okay"); |
---|