testptp.c 12 KB

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