testptp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PTP 1588 clock support - User space test program
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. */
  7. #define _GNU_SOURCE
  8. #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <inttypes.h>
  12. #include <math.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/mman.h>
  19. #include <sys/stat.h>
  20. #include <sys/time.h>
  21. #include <sys/timex.h>
  22. #include <sys/types.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include <linux/ptp_clock.h>
  26. #define DEVICE "/dev/ptp0"
  27. #ifndef ADJ_SETOFFSET
  28. #define ADJ_SETOFFSET 0x0100
  29. #endif
  30. #ifndef CLOCK_INVALID
  31. #define CLOCK_INVALID -1
  32. #endif
  33. #define NSEC_PER_SEC 1000000000LL
  34. /* clock_adjtime is not available in GLIBC < 2.14 */
  35. #if !__GLIBC_PREREQ(2, 14)
  36. #include <sys/syscall.h>
  37. static int clock_adjtime(clockid_t id, struct timex *tx)
  38. {
  39. return syscall(__NR_clock_adjtime, id, tx);
  40. }
  41. #endif
  42. static void show_flag_test(int rq_index, unsigned int flags, int err)
  43. {
  44. printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n",
  45. rq_index ? '1' + rq_index : ' ',
  46. flags, err, strerror(errno));
  47. /* sigh, uClibc ... */
  48. errno = 0;
  49. }
  50. static void do_flag_test(int fd, unsigned int index)
  51. {
  52. struct ptp_extts_request extts_request;
  53. unsigned long request[2] = {
  54. PTP_EXTTS_REQUEST,
  55. PTP_EXTTS_REQUEST2,
  56. };
  57. unsigned int enable_flags[5] = {
  58. PTP_ENABLE_FEATURE,
  59. PTP_ENABLE_FEATURE | PTP_RISING_EDGE,
  60. PTP_ENABLE_FEATURE | PTP_FALLING_EDGE,
  61. PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE,
  62. PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1),
  63. };
  64. int err, i, j;
  65. memset(&extts_request, 0, sizeof(extts_request));
  66. extts_request.index = index;
  67. for (i = 0; i < 2; i++) {
  68. for (j = 0; j < 5; j++) {
  69. extts_request.flags = enable_flags[j];
  70. err = ioctl(fd, request[i], &extts_request);
  71. show_flag_test(i, extts_request.flags, err);
  72. extts_request.flags = 0;
  73. err = ioctl(fd, request[i], &extts_request);
  74. }
  75. }
  76. }
  77. static clockid_t get_clockid(int fd)
  78. {
  79. #define CLOCKFD 3
  80. return (((unsigned int) ~fd) << 3) | CLOCKFD;
  81. }
  82. static long ppb_to_scaled_ppm(int ppb)
  83. {
  84. /*
  85. * The 'freq' field in the 'struct timex' is in parts per
  86. * million, but with a 16 bit binary fractional field.
  87. * Instead of calculating either one of
  88. *
  89. * scaled_ppm = (ppb / 1000) << 16 [1]
  90. * scaled_ppm = (ppb << 16) / 1000 [2]
  91. *
  92. * we simply use double precision math, in order to avoid the
  93. * truncation in [1] and the possible overflow in [2].
  94. */
  95. return (long) (ppb * 65.536);
  96. }
  97. static int64_t pctns(struct ptp_clock_time *t)
  98. {
  99. return t->sec * NSEC_PER_SEC + t->nsec;
  100. }
  101. static void usage(char *progname)
  102. {
  103. fprintf(stderr,
  104. "usage: %s [options]\n"
  105. " -c query the ptp clock's capabilities\n"
  106. " -d name device to open\n"
  107. " -e val read 'val' external time stamp events\n"
  108. " -f val adjust the ptp clock frequency by 'val' ppb\n"
  109. " -F chan Enable single channel mask and keep device open for debugfs verification.\n"
  110. " -g get the ptp clock time\n"
  111. " -h prints this message\n"
  112. " -i val index for event/trigger\n"
  113. " -k val measure the time offset between system and phc clock\n"
  114. " for 'val' times (Maximum 25)\n"
  115. " -l list the current pin configuration\n"
  116. " -L pin,val configure pin index 'pin' with function 'val'\n"
  117. " the channel index is taken from the '-i' option\n"
  118. " 'val' specifies the auxiliary function:\n"
  119. " 0 - none\n"
  120. " 1 - external time stamp\n"
  121. " 2 - periodic output\n"
  122. " -n val shift the ptp clock time by 'val' nanoseconds\n"
  123. " -o val phase offset (in nanoseconds) to be provided to the PHC servo\n"
  124. " -p val enable output with a period of 'val' nanoseconds\n"
  125. " -H val set output phase to 'val' nanoseconds (requires -p)\n"
  126. " -w val set output pulse width to 'val' nanoseconds (requires -p)\n"
  127. " -P val enable or disable (val=1|0) the system clock PPS\n"
  128. " -s set the ptp clock time from the system time\n"
  129. " -S set the system time from the ptp clock time\n"
  130. " -t val shift the ptp clock time by 'val' seconds\n"
  131. " -T val set the ptp clock time to 'val' seconds\n"
  132. " -x val get an extended ptp clock time with the desired number of samples (up to %d)\n"
  133. " -X get a ptp clock cross timestamp\n"
  134. " -z test combinations of rising/falling external time stamp flags\n",
  135. progname, PTP_MAX_SAMPLES);
  136. }
  137. int main(int argc, char *argv[])
  138. {
  139. struct ptp_clock_caps caps;
  140. struct ptp_extts_event event;
  141. struct ptp_extts_request extts_request;
  142. struct ptp_perout_request perout_request;
  143. struct ptp_pin_desc desc;
  144. struct timespec ts;
  145. struct timex tx;
  146. struct ptp_clock_time *pct;
  147. struct ptp_sys_offset *sysoff;
  148. struct ptp_sys_offset_extended *soe;
  149. struct ptp_sys_offset_precise *xts;
  150. char *progname;
  151. unsigned int i;
  152. int c, cnt, fd;
  153. char *device = DEVICE;
  154. clockid_t clkid;
  155. int adjfreq = 0x7fffffff;
  156. int adjtime = 0;
  157. int adjns = 0;
  158. int adjphase = 0;
  159. int capabilities = 0;
  160. int extts = 0;
  161. int flagtest = 0;
  162. int gettime = 0;
  163. int index = 0;
  164. int list_pins = 0;
  165. int pct_offset = 0;
  166. int getextended = 0;
  167. int getcross = 0;
  168. int n_samples = 0;
  169. int pin_index = -1, pin_func;
  170. int pps = -1;
  171. int seconds = 0;
  172. int settime = 0;
  173. int channel = -1;
  174. int64_t t1, t2, tp;
  175. int64_t interval, offset;
  176. int64_t perout_phase = -1;
  177. int64_t pulsewidth = -1;
  178. int64_t perout = -1;
  179. progname = strrchr(argv[0], '/');
  180. progname = progname ? 1+progname : argv[0];
  181. while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xz"))) {
  182. switch (c) {
  183. case 'c':
  184. capabilities = 1;
  185. break;
  186. case 'd':
  187. device = optarg;
  188. break;
  189. case 'e':
  190. extts = atoi(optarg);
  191. break;
  192. case 'f':
  193. adjfreq = atoi(optarg);
  194. break;
  195. case 'F':
  196. channel = atoi(optarg);
  197. break;
  198. case 'g':
  199. gettime = 1;
  200. break;
  201. case 'H':
  202. perout_phase = atoll(optarg);
  203. break;
  204. case 'i':
  205. index = atoi(optarg);
  206. break;
  207. case 'k':
  208. pct_offset = 1;
  209. n_samples = atoi(optarg);
  210. break;
  211. case 'l':
  212. list_pins = 1;
  213. break;
  214. case 'L':
  215. cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
  216. if (cnt != 2) {
  217. usage(progname);
  218. return -1;
  219. }
  220. break;
  221. case 'n':
  222. adjns = atoi(optarg);
  223. break;
  224. case 'o':
  225. adjphase = atoi(optarg);
  226. break;
  227. case 'p':
  228. perout = atoll(optarg);
  229. break;
  230. case 'P':
  231. pps = atoi(optarg);
  232. break;
  233. case 's':
  234. settime = 1;
  235. break;
  236. case 'S':
  237. settime = 2;
  238. break;
  239. case 't':
  240. adjtime = atoi(optarg);
  241. break;
  242. case 'T':
  243. settime = 3;
  244. seconds = atoi(optarg);
  245. break;
  246. case 'w':
  247. pulsewidth = atoi(optarg);
  248. break;
  249. case 'x':
  250. getextended = atoi(optarg);
  251. if (getextended < 1 || getextended > PTP_MAX_SAMPLES) {
  252. fprintf(stderr,
  253. "number of extended timestamp samples must be between 1 and %d; was asked for %d\n",
  254. PTP_MAX_SAMPLES, getextended);
  255. return -1;
  256. }
  257. break;
  258. case 'X':
  259. getcross = 1;
  260. break;
  261. case 'z':
  262. flagtest = 1;
  263. break;
  264. case 'h':
  265. usage(progname);
  266. return 0;
  267. case '?':
  268. default:
  269. usage(progname);
  270. return -1;
  271. }
  272. }
  273. fd = open(device, O_RDWR);
  274. if (fd < 0) {
  275. fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
  276. return -1;
  277. }
  278. clkid = get_clockid(fd);
  279. if (CLOCK_INVALID == clkid) {
  280. fprintf(stderr, "failed to read clock id\n");
  281. return -1;
  282. }
  283. if (capabilities) {
  284. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  285. perror("PTP_CLOCK_GETCAPS");
  286. } else {
  287. printf("capabilities:\n"
  288. " %d maximum frequency adjustment (ppb)\n"
  289. " %d programmable alarms\n"
  290. " %d external time stamp channels\n"
  291. " %d programmable periodic signals\n"
  292. " %d pulse per second\n"
  293. " %d programmable pins\n"
  294. " %d cross timestamping\n"
  295. " %d adjust_phase\n"
  296. " %d maximum phase adjustment (ns)\n",
  297. caps.max_adj,
  298. caps.n_alarm,
  299. caps.n_ext_ts,
  300. caps.n_per_out,
  301. caps.pps,
  302. caps.n_pins,
  303. caps.cross_timestamping,
  304. caps.adjust_phase,
  305. caps.max_phase_adj);
  306. }
  307. }
  308. if (0x7fffffff != adjfreq) {
  309. memset(&tx, 0, sizeof(tx));
  310. tx.modes = ADJ_FREQUENCY;
  311. tx.freq = ppb_to_scaled_ppm(adjfreq);
  312. if (clock_adjtime(clkid, &tx)) {
  313. perror("clock_adjtime");
  314. } else {
  315. puts("frequency adjustment okay");
  316. }
  317. }
  318. if (adjtime || adjns) {
  319. memset(&tx, 0, sizeof(tx));
  320. tx.modes = ADJ_SETOFFSET | ADJ_NANO;
  321. tx.time.tv_sec = adjtime;
  322. tx.time.tv_usec = adjns;
  323. while (tx.time.tv_usec < 0) {
  324. tx.time.tv_sec -= 1;
  325. tx.time.tv_usec += NSEC_PER_SEC;
  326. }
  327. if (clock_adjtime(clkid, &tx) < 0) {
  328. perror("clock_adjtime");
  329. } else {
  330. puts("time shift okay");
  331. }
  332. }
  333. if (adjphase) {
  334. memset(&tx, 0, sizeof(tx));
  335. tx.modes = ADJ_OFFSET | ADJ_NANO;
  336. tx.offset = adjphase;
  337. if (clock_adjtime(clkid, &tx) < 0) {
  338. perror("clock_adjtime");
  339. } else {
  340. puts("phase adjustment okay");
  341. }
  342. }
  343. if (gettime) {
  344. if (clock_gettime(clkid, &ts)) {
  345. perror("clock_gettime");
  346. } else {
  347. printf("clock time: %ld.%09ld or %s",
  348. ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
  349. }
  350. }
  351. if (settime == 1) {
  352. clock_gettime(CLOCK_REALTIME, &ts);
  353. if (clock_settime(clkid, &ts)) {
  354. perror("clock_settime");
  355. } else {
  356. puts("set time okay");
  357. }
  358. }
  359. if (settime == 2) {
  360. clock_gettime(clkid, &ts);
  361. if (clock_settime(CLOCK_REALTIME, &ts)) {
  362. perror("clock_settime");
  363. } else {
  364. puts("set time okay");
  365. }
  366. }
  367. if (settime == 3) {
  368. ts.tv_sec = seconds;
  369. ts.tv_nsec = 0;
  370. if (clock_settime(clkid, &ts)) {
  371. perror("clock_settime");
  372. } else {
  373. puts("set time okay");
  374. }
  375. }
  376. if (pin_index >= 0) {
  377. memset(&desc, 0, sizeof(desc));
  378. desc.index = pin_index;
  379. desc.func = pin_func;
  380. desc.chan = index;
  381. if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
  382. perror("PTP_PIN_SETFUNC");
  383. } else {
  384. puts("set pin function okay");
  385. }
  386. }
  387. if (extts) {
  388. memset(&extts_request, 0, sizeof(extts_request));
  389. extts_request.index = index;
  390. extts_request.flags = PTP_ENABLE_FEATURE;
  391. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  392. perror("PTP_EXTTS_REQUEST");
  393. extts = 0;
  394. } else {
  395. puts("external time stamp request okay");
  396. }
  397. for (; extts; extts--) {
  398. cnt = read(fd, &event, sizeof(event));
  399. if (cnt != sizeof(event)) {
  400. perror("read");
  401. break;
  402. }
  403. printf("event index %u at %lld.%09u\n", event.index,
  404. event.t.sec, event.t.nsec);
  405. fflush(stdout);
  406. }
  407. /* Disable the feature again. */
  408. extts_request.flags = 0;
  409. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  410. perror("PTP_EXTTS_REQUEST");
  411. }
  412. }
  413. if (flagtest) {
  414. do_flag_test(fd, index);
  415. }
  416. if (list_pins) {
  417. int n_pins = 0;
  418. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  419. perror("PTP_CLOCK_GETCAPS");
  420. } else {
  421. n_pins = caps.n_pins;
  422. }
  423. for (i = 0; i < n_pins; i++) {
  424. desc.index = i;
  425. if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
  426. perror("PTP_PIN_GETFUNC");
  427. break;
  428. }
  429. printf("name %s index %u func %u chan %u\n",
  430. desc.name, desc.index, desc.func, desc.chan);
  431. }
  432. }
  433. if (pulsewidth >= 0 && perout < 0) {
  434. puts("-w can only be specified together with -p");
  435. return -1;
  436. }
  437. if (perout_phase >= 0 && perout < 0) {
  438. puts("-H can only be specified together with -p");
  439. return -1;
  440. }
  441. if (perout >= 0) {
  442. if (clock_gettime(clkid, &ts)) {
  443. perror("clock_gettime");
  444. return -1;
  445. }
  446. memset(&perout_request, 0, sizeof(perout_request));
  447. perout_request.index = index;
  448. perout_request.period.sec = perout / NSEC_PER_SEC;
  449. perout_request.period.nsec = perout % NSEC_PER_SEC;
  450. perout_request.flags = 0;
  451. if (pulsewidth >= 0) {
  452. perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
  453. perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
  454. perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
  455. }
  456. if (perout_phase >= 0) {
  457. perout_request.flags |= PTP_PEROUT_PHASE;
  458. perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
  459. perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
  460. } else {
  461. perout_request.start.sec = ts.tv_sec + 2;
  462. perout_request.start.nsec = 0;
  463. }
  464. if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
  465. perror("PTP_PEROUT_REQUEST");
  466. } else {
  467. puts("periodic output request okay");
  468. }
  469. }
  470. if (pps != -1) {
  471. int enable = pps ? 1 : 0;
  472. if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
  473. perror("PTP_ENABLE_PPS");
  474. } else {
  475. puts("pps for system time request okay");
  476. }
  477. }
  478. if (pct_offset) {
  479. if (n_samples <= 0 || n_samples > 25) {
  480. puts("n_samples should be between 1 and 25");
  481. usage(progname);
  482. return -1;
  483. }
  484. sysoff = calloc(1, sizeof(*sysoff));
  485. if (!sysoff) {
  486. perror("calloc");
  487. return -1;
  488. }
  489. sysoff->n_samples = n_samples;
  490. if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
  491. perror("PTP_SYS_OFFSET");
  492. else
  493. puts("system and phc clock time offset request okay");
  494. pct = &sysoff->ts[0];
  495. for (i = 0; i < sysoff->n_samples; i++) {
  496. t1 = pctns(pct+2*i);
  497. tp = pctns(pct+2*i+1);
  498. t2 = pctns(pct+2*i+2);
  499. interval = t2 - t1;
  500. offset = (t2 + t1) / 2 - tp;
  501. printf("system time: %lld.%09u\n",
  502. (pct+2*i)->sec, (pct+2*i)->nsec);
  503. printf("phc time: %lld.%09u\n",
  504. (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
  505. printf("system time: %lld.%09u\n",
  506. (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
  507. printf("system/phc clock time offset is %" PRId64 " ns\n"
  508. "system clock time delay is %" PRId64 " ns\n",
  509. offset, interval);
  510. }
  511. free(sysoff);
  512. }
  513. if (getextended) {
  514. soe = calloc(1, sizeof(*soe));
  515. if (!soe) {
  516. perror("calloc");
  517. return -1;
  518. }
  519. soe->n_samples = getextended;
  520. if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, soe)) {
  521. perror("PTP_SYS_OFFSET_EXTENDED");
  522. } else {
  523. printf("extended timestamp request returned %d samples\n",
  524. getextended);
  525. for (i = 0; i < getextended; i++) {
  526. printf("sample #%2d: system time before: %lld.%09u\n",
  527. i, soe->ts[i][0].sec, soe->ts[i][0].nsec);
  528. printf(" phc time: %lld.%09u\n",
  529. soe->ts[i][1].sec, soe->ts[i][1].nsec);
  530. printf(" system time after: %lld.%09u\n",
  531. soe->ts[i][2].sec, soe->ts[i][2].nsec);
  532. }
  533. }
  534. free(soe);
  535. }
  536. if (getcross) {
  537. xts = calloc(1, sizeof(*xts));
  538. if (!xts) {
  539. perror("calloc");
  540. return -1;
  541. }
  542. if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, xts)) {
  543. perror("PTP_SYS_OFFSET_PRECISE");
  544. } else {
  545. puts("system and phc crosstimestamping request okay");
  546. printf("device time: %lld.%09u\n",
  547. xts->device.sec, xts->device.nsec);
  548. printf("system time: %lld.%09u\n",
  549. xts->sys_realtime.sec, xts->sys_realtime.nsec);
  550. printf("monoraw time: %lld.%09u\n",
  551. xts->sys_monoraw.sec, xts->sys_monoraw.nsec);
  552. }
  553. free(xts);
  554. }
  555. if (channel >= 0) {
  556. if (ioctl(fd, PTP_MASK_CLEAR_ALL)) {
  557. perror("PTP_MASK_CLEAR_ALL");
  558. } else if (ioctl(fd, PTP_MASK_EN_SINGLE, (unsigned int *)&channel)) {
  559. perror("PTP_MASK_EN_SINGLE");
  560. } else {
  561. printf("Channel %d exclusively enabled. Check on debugfs.\n", channel);
  562. printf("Press any key to continue\n.");
  563. getchar();
  564. }
  565. }
  566. close(fd);
  567. return 0;
  568. }