testptp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 void usage(char *progname)
  90. {
  91. fprintf(stderr,
  92. "usage: %s [options]\n"
  93. " -a val request a one-shot alarm after 'val' seconds\n"
  94. " -A val request a periodic alarm every 'val' seconds\n"
  95. " -c query the ptp clock's capabilities\n"
  96. " -d name device to open\n"
  97. " -e val read 'val' external time stamp events\n"
  98. " -f val adjust the ptp clock frequency by 'val' ppb\n"
  99. " -g get the ptp clock time\n"
  100. " -h prints this message\n"
  101. " -p val enable output with a period of 'val' nanoseconds\n"
  102. " -P val enable or disable (val=1|0) the system clock PPS\n"
  103. " -s set the ptp clock time from the system time\n"
  104. " -S set the system time from the ptp clock time\n"
  105. " -t val shift the ptp clock time by 'val' seconds\n",
  106. progname);
  107. }
  108. int main(int argc, char *argv[])
  109. {
  110. struct ptp_clock_caps caps;
  111. struct ptp_extts_event event;
  112. struct ptp_extts_request extts_request;
  113. struct ptp_perout_request perout_request;
  114. struct timespec ts;
  115. struct timex tx;
  116. static timer_t timerid;
  117. struct itimerspec timeout;
  118. struct sigevent sigevent;
  119. char *progname;
  120. int c, cnt, fd;
  121. char *device = DEVICE;
  122. clockid_t clkid;
  123. int adjfreq = 0x7fffffff;
  124. int adjtime = 0;
  125. int capabilities = 0;
  126. int extts = 0;
  127. int gettime = 0;
  128. int oneshot = 0;
  129. int periodic = 0;
  130. int perout = -1;
  131. int pps = -1;
  132. int settime = 0;
  133. progname = strrchr(argv[0], '/');
  134. progname = progname ? 1+progname : argv[0];
  135. while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) {
  136. switch (c) {
  137. case 'a':
  138. oneshot = atoi(optarg);
  139. break;
  140. case 'A':
  141. periodic = atoi(optarg);
  142. break;
  143. case 'c':
  144. capabilities = 1;
  145. break;
  146. case 'd':
  147. device = optarg;
  148. break;
  149. case 'e':
  150. extts = atoi(optarg);
  151. break;
  152. case 'f':
  153. adjfreq = atoi(optarg);
  154. break;
  155. case 'g':
  156. gettime = 1;
  157. break;
  158. case 'p':
  159. perout = atoi(optarg);
  160. break;
  161. case 'P':
  162. pps = atoi(optarg);
  163. break;
  164. case 's':
  165. settime = 1;
  166. break;
  167. case 'S':
  168. settime = 2;
  169. break;
  170. case 't':
  171. adjtime = atoi(optarg);
  172. break;
  173. case 'h':
  174. usage(progname);
  175. return 0;
  176. case '?':
  177. default:
  178. usage(progname);
  179. return -1;
  180. }
  181. }
  182. fd = open(device, O_RDWR);
  183. if (fd < 0) {
  184. fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
  185. return -1;
  186. }
  187. clkid = get_clockid(fd);
  188. if (CLOCK_INVALID == clkid) {
  189. fprintf(stderr, "failed to read clock id\n");
  190. return -1;
  191. }
  192. if (capabilities) {
  193. if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
  194. perror("PTP_CLOCK_GETCAPS");
  195. } else {
  196. printf("capabilities:\n"
  197. " %d maximum frequency adjustment (ppb)\n"
  198. " %d programmable alarms\n"
  199. " %d external time stamp channels\n"
  200. " %d programmable periodic signals\n"
  201. " %d pulse per second\n",
  202. caps.max_adj,
  203. caps.n_alarm,
  204. caps.n_ext_ts,
  205. caps.n_per_out,
  206. caps.pps);
  207. }
  208. }
  209. if (0x7fffffff != adjfreq) {
  210. memset(&tx, 0, sizeof(tx));
  211. tx.modes = ADJ_FREQUENCY;
  212. tx.freq = ppb_to_scaled_ppm(adjfreq);
  213. if (clock_adjtime(clkid, &tx)) {
  214. perror("clock_adjtime");
  215. } else {
  216. puts("frequency adjustment okay");
  217. }
  218. }
  219. if (adjtime) {
  220. memset(&tx, 0, sizeof(tx));
  221. tx.modes = ADJ_SETOFFSET;
  222. tx.time.tv_sec = adjtime;
  223. tx.time.tv_usec = 0;
  224. if (clock_adjtime(clkid, &tx) < 0) {
  225. perror("clock_adjtime");
  226. } else {
  227. puts("time shift okay");
  228. }
  229. }
  230. if (gettime) {
  231. if (clock_gettime(clkid, &ts)) {
  232. perror("clock_gettime");
  233. } else {
  234. printf("clock time: %ld.%09ld or %s",
  235. ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
  236. }
  237. }
  238. if (settime == 1) {
  239. clock_gettime(CLOCK_REALTIME, &ts);
  240. if (clock_settime(clkid, &ts)) {
  241. perror("clock_settime");
  242. } else {
  243. puts("set time okay");
  244. }
  245. }
  246. if (settime == 2) {
  247. clock_gettime(clkid, &ts);
  248. if (clock_settime(CLOCK_REALTIME, &ts)) {
  249. perror("clock_settime");
  250. } else {
  251. puts("set time okay");
  252. }
  253. }
  254. if (extts) {
  255. memset(&extts_request, 0, sizeof(extts_request));
  256. extts_request.index = 0;
  257. extts_request.flags = PTP_ENABLE_FEATURE;
  258. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  259. perror("PTP_EXTTS_REQUEST");
  260. extts = 0;
  261. } else {
  262. puts("external time stamp request okay");
  263. }
  264. for (; extts; extts--) {
  265. cnt = read(fd, &event, sizeof(event));
  266. if (cnt != sizeof(event)) {
  267. perror("read");
  268. break;
  269. }
  270. printf("event index %u at %lld.%09u\n", event.index,
  271. event.t.sec, event.t.nsec);
  272. fflush(stdout);
  273. }
  274. /* Disable the feature again. */
  275. extts_request.flags = 0;
  276. if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
  277. perror("PTP_EXTTS_REQUEST");
  278. }
  279. }
  280. if (oneshot) {
  281. install_handler(SIGALRM, handle_alarm);
  282. /* Create a timer. */
  283. sigevent.sigev_notify = SIGEV_SIGNAL;
  284. sigevent.sigev_signo = SIGALRM;
  285. if (timer_create(clkid, &sigevent, &timerid)) {
  286. perror("timer_create");
  287. return -1;
  288. }
  289. /* Start the timer. */
  290. memset(&timeout, 0, sizeof(timeout));
  291. timeout.it_value.tv_sec = oneshot;
  292. if (timer_settime(timerid, 0, &timeout, NULL)) {
  293. perror("timer_settime");
  294. return -1;
  295. }
  296. pause();
  297. timer_delete(timerid);
  298. }
  299. if (periodic) {
  300. install_handler(SIGALRM, handle_alarm);
  301. /* Create a timer. */
  302. sigevent.sigev_notify = SIGEV_SIGNAL;
  303. sigevent.sigev_signo = SIGALRM;
  304. if (timer_create(clkid, &sigevent, &timerid)) {
  305. perror("timer_create");
  306. return -1;
  307. }
  308. /* Start the timer. */
  309. memset(&timeout, 0, sizeof(timeout));
  310. timeout.it_interval.tv_sec = periodic;
  311. timeout.it_value.tv_sec = periodic;
  312. if (timer_settime(timerid, 0, &timeout, NULL)) {
  313. perror("timer_settime");
  314. return -1;
  315. }
  316. while (1) {
  317. pause();
  318. }
  319. timer_delete(timerid);
  320. }
  321. if (perout >= 0) {
  322. if (clock_gettime(clkid, &ts)) {
  323. perror("clock_gettime");
  324. return -1;
  325. }
  326. memset(&perout_request, 0, sizeof(perout_request));
  327. perout_request.index = 0;
  328. perout_request.start.sec = ts.tv_sec + 2;
  329. perout_request.start.nsec = 0;
  330. perout_request.period.sec = 0;
  331. perout_request.period.nsec = perout;
  332. if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
  333. perror("PTP_PEROUT_REQUEST");
  334. } else {
  335. puts("periodic output request okay");
  336. }
  337. }
  338. if (pps != -1) {
  339. int enable = pps ? 1 : 0;
  340. if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
  341. perror("PTP_ENABLE_PPS");
  342. } else {
  343. puts("pps for system time request okay");
  344. }
  345. }
  346. close(fd);
  347. return 0;
  348. }