testptp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 * 1000000000LL + 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. " -g get the ptp clock time\n"
  110. " -h prints this message\n"
  111. " -i val index for event/trigger\n"
  112. " -k val measure the time offset between system and phc clock\n"
  113. " for 'val' times (Maximum 25)\n"
  114. " -l list the current pin configuration\n"
  115. " -L pin,val configure pin index 'pin' with function 'val'\n"
  116. " the channel index is taken from the '-i' option\n"
  117. " 'val' specifies the auxiliary function:\n"
  118. " 0 - none\n"
  119. " 1 - external time stamp\n"
  120. " 2 - periodic output\n"
  121. " -n val shift the ptp clock time by 'val' nanoseconds\n"
  122. " -p val enable output with a period of 'val' nanoseconds\n"
  123. " -H val set output phase to 'val' nanoseconds (requires -p)\n"
  124. " -w val set output pulse width to 'val' nanoseconds (requires -p)\n"
  125. " -P val enable or disable (val=1|0) the system clock PPS\n"
  126. " -s set the ptp clock time from the system time\n"
  127. " -S set the system time from the ptp clock time\n"
  128. " -t val shift the ptp clock time by 'val' seconds\n"
  129. " -T val set the ptp clock time to 'val' seconds\n"
  130. " -z test combinations of rising/falling external time stamp flags\n",
  131. progname);
  132. }
  133. int main(int argc, char *argv[])
  134. {
  135. struct ptp_clock_caps caps;
  136. struct ptp_extts_event event;
  137. struct ptp_extts_request extts_request;
  138. struct ptp_perout_request perout_request;
  139. struct ptp_pin_desc desc;
  140. struct timespec ts;
  141. struct timex tx;
  142. struct ptp_clock_time *pct;
  143. struct ptp_sys_offset *sysoff;
  144. char *progname;
  145. unsigned int i;
  146. int c, cnt, fd;
  147. char *device = DEVICE;
  148. clockid_t clkid;
  149. int adjfreq = 0x7fffffff;
  150. int adjtime = 0;
  151. int adjns = 0;
  152. int capabilities = 0;
  153. int extts = 0;
  154. int flagtest = 0;
  155. int gettime = 0;
  156. int index = 0;
  157. int list_pins = 0;
  158. int pct_offset = 0;
  159. int n_samples = 0;
  160. int pin_index = -1, pin_func;
  161. int pps = -1;
  162. int seconds = 0;
  163. int settime = 0;
  164. int64_t t1, t2, tp;
  165. int64_t interval, offset;
  166. int64_t perout_phase = -1;
  167. int64_t pulsewidth = -1;
  168. int64_t perout = -1;
  169. progname = strrchr(argv[0], '/');
  170. progname = progname ? 1+progname : argv[0];
  171. while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:n:p:P:sSt:T:w:z"))) {
  172. switch (c) {
  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 'H':
  189. perout_phase = atoll(optarg);
  190. break;
  191. case 'i':
  192. index = atoi(optarg);
  193. break;
  194. case 'k':
  195. pct_offset = 1;
  196. n_samples = atoi(optarg);
  197. break;
  198. case 'l':
  199. list_pins = 1;
  200. break;
  201. case 'L':
  202. cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
  203. if (cnt != 2) {
  204. usage(progname);
  205. return -1;
  206. }
  207. break;
  208. case 'n':
  209. adjns = atoi(optarg);
  210. break;
  211. case 'p':
  212. perout = atoll(optarg);
  213. break;
  214. case 'P':
  215. pps = atoi(optarg);
  216. break;
  217. case 's':
  218. settime = 1;
  219. break;
  220. case 'S':
  221. settime = 2;
  222. break;
  223. case 't':
  224. adjtime = atoi(optarg);
  225. break;
  226. case 'T':
  227. settime = 3;
  228. seconds = atoi(optarg);
  229. break;
  230. case 'w':
  231. pulsewidth = atoi(optarg);
  232. break;
  233. case 'z':
  234. flagtest = 1;
  235. break;
  236. case 'h':
  237. usage(progname);
  238. return 0;
  239. case '?':
  240. default:
  241. usage(progname);
  242. return -1;
  243. }
  244. }
  245. fd = open(device, O_RDWR);
  246. if (fd < 0) {
  247. fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
  248. return -1;
  249. }
  250. clkid = get_clockid(fd);
  251. if (CLOCK_INVALID == clkid) {
  252. fprintf(stderr, "failed to read clock id\n");
  253. return -1;
  254. }
  255. if (capabilities) {
  256. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  257. perror("PTP_CLOCK_GETCAPS");
  258. } else {
  259. printf("capabilities:\n"
  260. " %d maximum frequency adjustment (ppb)\n"
  261. " %d programmable alarms\n"
  262. " %d external time stamp channels\n"
  263. " %d programmable periodic signals\n"
  264. " %d pulse per second\n"
  265. " %d programmable pins\n"
  266. " %d cross timestamping\n"
  267. " %d adjust_phase\n",
  268. caps.max_adj,
  269. caps.n_alarm,
  270. caps.n_ext_ts,
  271. caps.n_per_out,
  272. caps.pps,
  273. caps.n_pins,
  274. caps.cross_timestamping,
  275. caps.adjust_phase);
  276. }
  277. }
  278. if (0x7fffffff != adjfreq) {
  279. memset(&tx, 0, sizeof(tx));
  280. tx.modes = ADJ_FREQUENCY;
  281. tx.freq = ppb_to_scaled_ppm(adjfreq);
  282. if (clock_adjtime(clkid, &tx)) {
  283. perror("clock_adjtime");
  284. } else {
  285. puts("frequency adjustment okay");
  286. }
  287. }
  288. if (adjtime || adjns) {
  289. memset(&tx, 0, sizeof(tx));
  290. tx.modes = ADJ_SETOFFSET | ADJ_NANO;
  291. tx.time.tv_sec = adjtime;
  292. tx.time.tv_usec = adjns;
  293. while (tx.time.tv_usec < 0) {
  294. tx.time.tv_sec -= 1;
  295. tx.time.tv_usec += 1000000000;
  296. }
  297. if (clock_adjtime(clkid, &tx) < 0) {
  298. perror("clock_adjtime");
  299. } else {
  300. puts("time shift okay");
  301. }
  302. }
  303. if (gettime) {
  304. if (clock_gettime(clkid, &ts)) {
  305. perror("clock_gettime");
  306. } else {
  307. printf("clock time: %ld.%09ld or %s",
  308. ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
  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. if (settime == 2) {
  320. clock_gettime(clkid, &ts);
  321. if (clock_settime(CLOCK_REALTIME, &ts)) {
  322. perror("clock_settime");
  323. } else {
  324. puts("set time okay");
  325. }
  326. }
  327. if (settime == 3) {
  328. ts.tv_sec = seconds;
  329. ts.tv_nsec = 0;
  330. if (clock_settime(clkid, &ts)) {
  331. perror("clock_settime");
  332. } else {
  333. puts("set time okay");
  334. }
  335. }
  336. if (pin_index >= 0) {
  337. memset(&desc, 0, sizeof(desc));
  338. desc.index = pin_index;
  339. desc.func = pin_func;
  340. desc.chan = index;
  341. if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
  342. perror("PTP_PIN_SETFUNC");
  343. } else {
  344. puts("set pin function okay");
  345. }
  346. }
  347. if (extts) {
  348. memset(&extts_request, 0, sizeof(extts_request));
  349. extts_request.index = index;
  350. extts_request.flags = PTP_ENABLE_FEATURE;
  351. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  352. perror("PTP_EXTTS_REQUEST");
  353. extts = 0;
  354. } else {
  355. puts("external time stamp request okay");
  356. }
  357. for (; extts; extts--) {
  358. cnt = read(fd, &event, sizeof(event));
  359. if (cnt != sizeof(event)) {
  360. perror("read");
  361. break;
  362. }
  363. printf("event index %u at %lld.%09u\n", event.index,
  364. event.t.sec, event.t.nsec);
  365. fflush(stdout);
  366. }
  367. /* Disable the feature again. */
  368. extts_request.flags = 0;
  369. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  370. perror("PTP_EXTTS_REQUEST");
  371. }
  372. }
  373. if (flagtest) {
  374. do_flag_test(fd, index);
  375. }
  376. if (list_pins) {
  377. int n_pins = 0;
  378. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  379. perror("PTP_CLOCK_GETCAPS");
  380. } else {
  381. n_pins = caps.n_pins;
  382. }
  383. for (i = 0; i < n_pins; i++) {
  384. desc.index = i;
  385. if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
  386. perror("PTP_PIN_GETFUNC");
  387. break;
  388. }
  389. printf("name %s index %u func %u chan %u\n",
  390. desc.name, desc.index, desc.func, desc.chan);
  391. }
  392. }
  393. if (pulsewidth >= 0 && perout < 0) {
  394. puts("-w can only be specified together with -p");
  395. return -1;
  396. }
  397. if (perout_phase >= 0 && perout < 0) {
  398. puts("-H can only be specified together with -p");
  399. return -1;
  400. }
  401. if (perout >= 0) {
  402. if (clock_gettime(clkid, &ts)) {
  403. perror("clock_gettime");
  404. return -1;
  405. }
  406. memset(&perout_request, 0, sizeof(perout_request));
  407. perout_request.index = index;
  408. perout_request.period.sec = perout / NSEC_PER_SEC;
  409. perout_request.period.nsec = perout % NSEC_PER_SEC;
  410. perout_request.flags = 0;
  411. if (pulsewidth >= 0) {
  412. perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
  413. perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
  414. perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
  415. }
  416. if (perout_phase >= 0) {
  417. perout_request.flags |= PTP_PEROUT_PHASE;
  418. perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
  419. perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
  420. } else {
  421. perout_request.start.sec = ts.tv_sec + 2;
  422. perout_request.start.nsec = 0;
  423. }
  424. if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
  425. perror("PTP_PEROUT_REQUEST");
  426. } else {
  427. puts("periodic output request okay");
  428. }
  429. }
  430. if (pps != -1) {
  431. int enable = pps ? 1 : 0;
  432. if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
  433. perror("PTP_ENABLE_PPS");
  434. } else {
  435. puts("pps for system time request okay");
  436. }
  437. }
  438. if (pct_offset) {
  439. if (n_samples <= 0 || n_samples > 25) {
  440. puts("n_samples should be between 1 and 25");
  441. usage(progname);
  442. return -1;
  443. }
  444. sysoff = calloc(1, sizeof(*sysoff));
  445. if (!sysoff) {
  446. perror("calloc");
  447. return -1;
  448. }
  449. sysoff->n_samples = n_samples;
  450. if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
  451. perror("PTP_SYS_OFFSET");
  452. else
  453. puts("system and phc clock time offset request okay");
  454. pct = &sysoff->ts[0];
  455. for (i = 0; i < sysoff->n_samples; i++) {
  456. t1 = pctns(pct+2*i);
  457. tp = pctns(pct+2*i+1);
  458. t2 = pctns(pct+2*i+2);
  459. interval = t2 - t1;
  460. offset = (t2 + t1) / 2 - tp;
  461. printf("system time: %lld.%09u\n",
  462. (pct+2*i)->sec, (pct+2*i)->nsec);
  463. printf("phc time: %lld.%09u\n",
  464. (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
  465. printf("system time: %lld.%09u\n",
  466. (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
  467. printf("system/phc clock time offset is %" PRId64 " ns\n"
  468. "system clock time delay is %" PRId64 " ns\n",
  469. offset, interval);
  470. }
  471. free(sysoff);
  472. }
  473. close(fd);
  474. return 0;
  475. }