testptp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <inttypes.h>
  25. #include <math.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/mman.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <sys/timex.h>
  35. #include <sys/types.h>
  36. #include <time.h>
  37. #include <unistd.h>
  38. #include <linux/ptp_clock.h>
  39. #define DEVICE "/dev/ptp0"
  40. #ifndef ADJ_SETOFFSET
  41. #define ADJ_SETOFFSET 0x0100
  42. #endif
  43. #ifndef CLOCK_INVALID
  44. #define CLOCK_INVALID -1
  45. #endif
  46. /* clock_adjtime is not available in GLIBC < 2.14 */
  47. #if !__GLIBC_PREREQ(2, 14)
  48. #include <sys/syscall.h>
  49. static int clock_adjtime(clockid_t id, struct timex *tx)
  50. {
  51. return syscall(__NR_clock_adjtime, id, tx);
  52. }
  53. #endif
  54. static clockid_t get_clockid(int fd)
  55. {
  56. #define CLOCKFD 3
  57. return (((unsigned int) ~fd) << 3) | CLOCKFD;
  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. unsigned int i;
  143. int c, cnt, fd;
  144. char *device = DEVICE;
  145. clockid_t clkid;
  146. int adjfreq = 0x7fffffff;
  147. int adjtime = 0;
  148. int capabilities = 0;
  149. int extts = 0;
  150. int gettime = 0;
  151. int index = 0;
  152. int list_pins = 0;
  153. int oneshot = 0;
  154. int pct_offset = 0;
  155. int n_samples = 0;
  156. int periodic = 0;
  157. int perout = -1;
  158. int pin_index = -1, pin_func;
  159. int pps = -1;
  160. int seconds = 0;
  161. int settime = 0;
  162. int64_t t1, t2, tp;
  163. int64_t interval, offset;
  164. progname = strrchr(argv[0], '/');
  165. progname = progname ? 1+progname : argv[0];
  166. while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghi:k:lL:p:P:sSt:T:v"))) {
  167. switch (c) {
  168. case 'a':
  169. oneshot = atoi(optarg);
  170. break;
  171. case 'A':
  172. periodic = atoi(optarg);
  173. break;
  174. case 'c':
  175. capabilities = 1;
  176. break;
  177. case 'd':
  178. device = optarg;
  179. break;
  180. case 'e':
  181. extts = atoi(optarg);
  182. break;
  183. case 'f':
  184. adjfreq = atoi(optarg);
  185. break;
  186. case 'g':
  187. gettime = 1;
  188. break;
  189. case 'i':
  190. index = atoi(optarg);
  191. break;
  192. case 'k':
  193. pct_offset = 1;
  194. n_samples = atoi(optarg);
  195. break;
  196. case 'l':
  197. list_pins = 1;
  198. break;
  199. case 'L':
  200. cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
  201. if (cnt != 2) {
  202. usage(progname);
  203. return -1;
  204. }
  205. break;
  206. case 'p':
  207. perout = atoi(optarg);
  208. break;
  209. case 'P':
  210. pps = atoi(optarg);
  211. break;
  212. case 's':
  213. settime = 1;
  214. break;
  215. case 'S':
  216. settime = 2;
  217. break;
  218. case 't':
  219. adjtime = atoi(optarg);
  220. break;
  221. case 'T':
  222. settime = 3;
  223. seconds = atoi(optarg);
  224. break;
  225. case 'h':
  226. usage(progname);
  227. return 0;
  228. case '?':
  229. default:
  230. usage(progname);
  231. return -1;
  232. }
  233. }
  234. fd = open(device, O_RDWR);
  235. if (fd < 0) {
  236. fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
  237. return -1;
  238. }
  239. clkid = get_clockid(fd);
  240. if (CLOCK_INVALID == clkid) {
  241. fprintf(stderr, "failed to read clock id\n");
  242. return -1;
  243. }
  244. if (capabilities) {
  245. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  246. perror("PTP_CLOCK_GETCAPS");
  247. } else {
  248. printf("capabilities:\n"
  249. " %d maximum frequency adjustment (ppb)\n"
  250. " %d programmable alarms\n"
  251. " %d external time stamp channels\n"
  252. " %d programmable periodic signals\n"
  253. " %d pulse per second\n"
  254. " %d programmable pins\n"
  255. " %d cross timestamping\n",
  256. caps.max_adj,
  257. caps.n_alarm,
  258. caps.n_ext_ts,
  259. caps.n_per_out,
  260. caps.pps,
  261. caps.n_pins,
  262. caps.cross_timestamping);
  263. }
  264. }
  265. if (0x7fffffff != adjfreq) {
  266. memset(&tx, 0, sizeof(tx));
  267. tx.modes = ADJ_FREQUENCY;
  268. tx.freq = ppb_to_scaled_ppm(adjfreq);
  269. if (clock_adjtime(clkid, &tx)) {
  270. perror("clock_adjtime");
  271. } else {
  272. puts("frequency adjustment okay");
  273. }
  274. }
  275. if (adjtime) {
  276. memset(&tx, 0, sizeof(tx));
  277. tx.modes = ADJ_SETOFFSET;
  278. tx.time.tv_sec = adjtime;
  279. tx.time.tv_usec = 0;
  280. if (clock_adjtime(clkid, &tx) < 0) {
  281. perror("clock_adjtime");
  282. } else {
  283. puts("time shift okay");
  284. }
  285. }
  286. if (gettime) {
  287. if (clock_gettime(clkid, &ts)) {
  288. perror("clock_gettime");
  289. } else {
  290. printf("clock time: %ld.%09ld or %s",
  291. ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
  292. }
  293. }
  294. if (settime == 1) {
  295. clock_gettime(CLOCK_REALTIME, &ts);
  296. if (clock_settime(clkid, &ts)) {
  297. perror("clock_settime");
  298. } else {
  299. puts("set time okay");
  300. }
  301. }
  302. if (settime == 2) {
  303. clock_gettime(clkid, &ts);
  304. if (clock_settime(CLOCK_REALTIME, &ts)) {
  305. perror("clock_settime");
  306. } else {
  307. puts("set time okay");
  308. }
  309. }
  310. if (settime == 3) {
  311. ts.tv_sec = seconds;
  312. ts.tv_nsec = 0;
  313. if (clock_settime(clkid, &ts)) {
  314. perror("clock_settime");
  315. } else {
  316. puts("set time okay");
  317. }
  318. }
  319. if (extts) {
  320. memset(&extts_request, 0, sizeof(extts_request));
  321. extts_request.index = index;
  322. extts_request.flags = PTP_ENABLE_FEATURE;
  323. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  324. perror("PTP_EXTTS_REQUEST");
  325. extts = 0;
  326. } else {
  327. puts("external time stamp request okay");
  328. }
  329. for (; extts; extts--) {
  330. cnt = read(fd, &event, sizeof(event));
  331. if (cnt != sizeof(event)) {
  332. perror("read");
  333. break;
  334. }
  335. printf("event index %u at %lld.%09u\n", event.index,
  336. event.t.sec, event.t.nsec);
  337. fflush(stdout);
  338. }
  339. /* Disable the feature again. */
  340. extts_request.flags = 0;
  341. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  342. perror("PTP_EXTTS_REQUEST");
  343. }
  344. }
  345. if (list_pins) {
  346. int n_pins = 0;
  347. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  348. perror("PTP_CLOCK_GETCAPS");
  349. } else {
  350. n_pins = caps.n_pins;
  351. }
  352. for (i = 0; i < n_pins; i++) {
  353. desc.index = i;
  354. if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
  355. perror("PTP_PIN_GETFUNC");
  356. break;
  357. }
  358. printf("name %s index %u func %u chan %u\n",
  359. desc.name, desc.index, desc.func, desc.chan);
  360. }
  361. }
  362. if (oneshot) {
  363. install_handler(SIGALRM, handle_alarm);
  364. /* Create a timer. */
  365. sigevent.sigev_notify = SIGEV_SIGNAL;
  366. sigevent.sigev_signo = SIGALRM;
  367. if (timer_create(clkid, &sigevent, &timerid)) {
  368. perror("timer_create");
  369. return -1;
  370. }
  371. /* Start the timer. */
  372. memset(&timeout, 0, sizeof(timeout));
  373. timeout.it_value.tv_sec = oneshot;
  374. if (timer_settime(timerid, 0, &timeout, NULL)) {
  375. perror("timer_settime");
  376. return -1;
  377. }
  378. pause();
  379. timer_delete(timerid);
  380. }
  381. if (periodic) {
  382. install_handler(SIGALRM, handle_alarm);
  383. /* Create a timer. */
  384. sigevent.sigev_notify = SIGEV_SIGNAL;
  385. sigevent.sigev_signo = SIGALRM;
  386. if (timer_create(clkid, &sigevent, &timerid)) {
  387. perror("timer_create");
  388. return -1;
  389. }
  390. /* Start the timer. */
  391. memset(&timeout, 0, sizeof(timeout));
  392. timeout.it_interval.tv_sec = periodic;
  393. timeout.it_value.tv_sec = periodic;
  394. if (timer_settime(timerid, 0, &timeout, NULL)) {
  395. perror("timer_settime");
  396. return -1;
  397. }
  398. while (1) {
  399. pause();
  400. }
  401. timer_delete(timerid);
  402. }
  403. if (perout >= 0) {
  404. if (clock_gettime(clkid, &ts)) {
  405. perror("clock_gettime");
  406. return -1;
  407. }
  408. memset(&perout_request, 0, sizeof(perout_request));
  409. perout_request.index = index;
  410. perout_request.start.sec = ts.tv_sec + 2;
  411. perout_request.start.nsec = 0;
  412. perout_request.period.sec = 0;
  413. perout_request.period.nsec = perout;
  414. if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
  415. perror("PTP_PEROUT_REQUEST");
  416. } else {
  417. puts("periodic output request okay");
  418. }
  419. }
  420. if (pin_index >= 0) {
  421. memset(&desc, 0, sizeof(desc));
  422. desc.index = pin_index;
  423. desc.func = pin_func;
  424. desc.chan = index;
  425. if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
  426. perror("PTP_PIN_SETFUNC");
  427. } else {
  428. puts("set pin function okay");
  429. }
  430. }
  431. if (pps != -1) {
  432. int enable = pps ? 1 : 0;
  433. if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
  434. perror("PTP_ENABLE_PPS");
  435. } else {
  436. puts("pps for system time request okay");
  437. }
  438. }
  439. if (pct_offset) {
  440. if (n_samples <= 0 || n_samples > 25) {
  441. puts("n_samples should be between 1 and 25");
  442. usage(progname);
  443. return -1;
  444. }
  445. sysoff = calloc(1, sizeof(*sysoff));
  446. if (!sysoff) {
  447. perror("calloc");
  448. return -1;
  449. }
  450. sysoff->n_samples = n_samples;
  451. if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
  452. perror("PTP_SYS_OFFSET");
  453. else
  454. puts("system and phc clock time offset request okay");
  455. pct = &sysoff->ts[0];
  456. for (i = 0; i < sysoff->n_samples; i++) {
  457. t1 = pctns(pct+2*i);
  458. tp = pctns(pct+2*i+1);
  459. t2 = pctns(pct+2*i+2);
  460. interval = t2 - t1;
  461. offset = (t2 + t1) / 2 - tp;
  462. printf("system time: %lld.%u\n",
  463. (pct+2*i)->sec, (pct+2*i)->nsec);
  464. printf("phc time: %lld.%u\n",
  465. (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
  466. printf("system time: %lld.%u\n",
  467. (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
  468. printf("system/phc clock time offset is %" PRId64 " ns\n"
  469. "system clock time delay is %" PRId64 " ns\n",
  470. offset, interval);
  471. }
  472. free(sysoff);
  473. }
  474. close(fd);
  475. return 0;
  476. }