testptp.c 10 KB

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