|
|
63b4e9 |
diff --git a/makefile b/makefile
|
|
|
63b4e9 |
index 5295b60..7763106 100644
|
|
|
63b4e9 |
--- a/makefile
|
|
|
63b4e9 |
+++ b/makefile
|
|
|
63b4e9 |
@@ -23,6 +23,7 @@ VER = -DVER=$(version)
|
|
|
63b4e9 |
CFLAGS = -Wall $(VER) $(incdefs) $(DEBUG) $(EXTRA_CFLAGS)
|
|
|
63b4e9 |
LDLIBS = -lm -lrt -pthread $(EXTRA_LDFLAGS)
|
|
|
63b4e9 |
PRG = ptp4l hwstamp_ctl nsm phc2sys phc_ctl pmc timemaster ts2phc
|
|
|
63b4e9 |
+TESTPTP = testptp.o
|
|
|
63b4e9 |
FILTERS = filter.o mave.o mmedian.o
|
|
|
63b4e9 |
SERVOS = linreg.o ntpshm.o nullf.o pi.o servo.o
|
|
|
63b4e9 |
TRANSP = raw.o transport.o udp.o udp6.o uds.o
|
|
|
63b4e9 |
@@ -35,7 +36,7 @@ OBJ = bmc.o clock.o clockadj.o clockcheck.o config.o designated_fsm.o \
|
|
|
63b4e9 |
unicast_fsm.o unicast_service.o util.o version.o
|
|
|
63b4e9 |
|
|
|
63b4e9 |
OBJECTS = $(OBJ) hwstamp_ctl.o nsm.o phc2sys.o phc_ctl.o pmc.o pmc_agent.o \
|
|
|
63b4e9 |
- pmc_common.o sysoff.o timemaster.o $(TS2PHC)
|
|
|
63b4e9 |
+ pmc_common.o sysoff.o timemaster.o $(TS2PHC) $(TESTPTP)
|
|
|
63b4e9 |
SRC = $(OBJECTS:.o=.c)
|
|
|
63b4e9 |
DEPEND = $(OBJECTS:.o=.d)
|
|
|
63b4e9 |
srcdir := $(dir $(lastword $(MAKEFILE_LIST)))
|
|
|
63b4e9 |
@@ -48,7 +49,7 @@ sbindir = $(prefix)/sbin
|
|
|
63b4e9 |
mandir = $(prefix)/man
|
|
|
63b4e9 |
man8dir = $(mandir)/man8
|
|
|
63b4e9 |
|
|
|
63b4e9 |
-all: $(PRG)
|
|
|
63b4e9 |
+all: $(PRG) testptp
|
|
|
63b4e9 |
|
|
|
63b4e9 |
ptp4l: $(OBJ)
|
|
|
63b4e9 |
|
|
|
63b4e9 |
@@ -71,6 +72,8 @@ timemaster: phc.o print.o rtnl.o sk.o timemaster.o util.o version.o
|
|
|
63b4e9 |
ts2phc: config.o clockadj.o hash.o interface.o phc.o print.o $(SERVOS) sk.o \
|
|
|
63b4e9 |
$(TS2PHC) util.o version.o
|
|
|
63b4e9 |
|
|
|
63b4e9 |
+testptp: testptp.o
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
version.o: .version version.sh $(filter-out version.d,$(DEPEND))
|
|
|
63b4e9 |
|
|
|
63b4e9 |
.version: force
|
|
|
63b4e9 |
@@ -80,9 +83,10 @@ version.o: .version version.sh $(filter-out version.d,$(DEPEND))
|
|
|
63b4e9 |
|
|
|
63b4e9 |
force:
|
|
|
63b4e9 |
|
|
|
63b4e9 |
-install: $(PRG)
|
|
|
63b4e9 |
+install: $(PRG) testptp
|
|
|
63b4e9 |
install -p -m 755 -d $(DESTDIR)$(sbindir) $(DESTDIR)$(man8dir)
|
|
|
63b4e9 |
install $(PRG) $(DESTDIR)$(sbindir)
|
|
|
63b4e9 |
+ install testptp $(DESTDIR)$(sbindir)
|
|
|
63b4e9 |
for x in $(PRG:%=%.8); do \
|
|
|
63b4e9 |
[ -f $$x ] && install -p -m 644 -t $(DESTDIR)$(man8dir) $$x ; \
|
|
|
63b4e9 |
done
|
|
|
63b4e9 |
diff --git a/testptp.c b/testptp.c
|
|
|
63b4e9 |
new file mode 100644
|
|
|
63b4e9 |
index 0000000..6ed02ca
|
|
|
63b4e9 |
--- /dev/null
|
|
|
63b4e9 |
+++ b/testptp.c
|
|
|
63b4e9 |
@@ -0,0 +1,513 @@
|
|
|
63b4e9 |
+// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
63b4e9 |
+/*
|
|
|
63b4e9 |
+ * PTP 1588 clock support - User space test program
|
|
|
63b4e9 |
+ *
|
|
|
63b4e9 |
+ * Copyright (C) 2010 OMICRON electronics GmbH
|
|
|
63b4e9 |
+ */
|
|
|
63b4e9 |
+#ifndef _GNU_SOURCE
|
|
|
63b4e9 |
+#define _GNU_SOURCE
|
|
|
63b4e9 |
+#endif
|
|
|
63b4e9 |
+#define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
|
|
|
63b4e9 |
+#include <errno.h>
|
|
|
63b4e9 |
+#include <fcntl.h>
|
|
|
63b4e9 |
+#include <inttypes.h>
|
|
|
63b4e9 |
+#include <math.h>
|
|
|
63b4e9 |
+#include <signal.h>
|
|
|
63b4e9 |
+#include <stdio.h>
|
|
|
63b4e9 |
+#include <stdlib.h>
|
|
|
63b4e9 |
+#include <string.h>
|
|
|
63b4e9 |
+#include <sys/ioctl.h>
|
|
|
63b4e9 |
+#include <sys/mman.h>
|
|
|
63b4e9 |
+#include <sys/stat.h>
|
|
|
63b4e9 |
+#include <sys/time.h>
|
|
|
63b4e9 |
+#include <sys/timex.h>
|
|
|
63b4e9 |
+#include <sys/types.h>
|
|
|
63b4e9 |
+#include <time.h>
|
|
|
63b4e9 |
+#include <unistd.h>
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+#include <linux/ptp_clock.h>
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+#define DEVICE "/dev/ptp0"
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+#ifndef ADJ_SETOFFSET
|
|
|
63b4e9 |
+#define ADJ_SETOFFSET 0x0100
|
|
|
63b4e9 |
+#endif
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+#ifndef CLOCK_INVALID
|
|
|
63b4e9 |
+#define CLOCK_INVALID -1
|
|
|
63b4e9 |
+#endif
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+#define NSEC_PER_SEC 1000000000LL
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+/* clock_adjtime is not available in GLIBC < 2.14 */
|
|
|
63b4e9 |
+#if !__GLIBC_PREREQ(2, 14)
|
|
|
63b4e9 |
+#include <sys/syscall.h>
|
|
|
63b4e9 |
+static int clock_adjtime(clockid_t id, struct timex *tx)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ return syscall(__NR_clock_adjtime, id, tx);
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+#endif
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+static void show_flag_test(int rq_index, unsigned int flags, int err)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n",
|
|
|
63b4e9 |
+ rq_index ? '1' + rq_index : ' ',
|
|
|
63b4e9 |
+ flags, err, strerror(errno));
|
|
|
63b4e9 |
+ /* sigh, uClibc ... */
|
|
|
63b4e9 |
+ errno = 0;
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+static void do_flag_test(int fd, unsigned int index)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ struct ptp_extts_request extts_request;
|
|
|
63b4e9 |
+ unsigned long request[2] = {
|
|
|
63b4e9 |
+ PTP_EXTTS_REQUEST,
|
|
|
63b4e9 |
+ PTP_EXTTS_REQUEST2,
|
|
|
63b4e9 |
+ };
|
|
|
63b4e9 |
+ unsigned int enable_flags[5] = {
|
|
|
63b4e9 |
+ PTP_ENABLE_FEATURE,
|
|
|
63b4e9 |
+ PTP_ENABLE_FEATURE | PTP_RISING_EDGE,
|
|
|
63b4e9 |
+ PTP_ENABLE_FEATURE | PTP_FALLING_EDGE,
|
|
|
63b4e9 |
+ PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE,
|
|
|
63b4e9 |
+ PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1),
|
|
|
63b4e9 |
+ };
|
|
|
63b4e9 |
+ int err, i, j;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ memset(&extts_request, 0, sizeof(extts_request));
|
|
|
63b4e9 |
+ extts_request.index = index;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ for (i = 0; i < 2; i++) {
|
|
|
63b4e9 |
+ for (j = 0; j < 5; j++) {
|
|
|
63b4e9 |
+ extts_request.flags = enable_flags[j];
|
|
|
63b4e9 |
+ err = ioctl(fd, request[i], &extts_request);
|
|
|
63b4e9 |
+ show_flag_test(i, extts_request.flags, err);
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ extts_request.flags = 0;
|
|
|
63b4e9 |
+ err = ioctl(fd, request[i], &extts_request);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+static clockid_t get_clockid(int fd)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+#define CLOCKFD 3
|
|
|
63b4e9 |
+ return (((unsigned int) ~fd) << 3) | CLOCKFD;
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+static long ppb_to_scaled_ppm(int ppb)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ /*
|
|
|
63b4e9 |
+ * The 'freq' field in the 'struct timex' is in parts per
|
|
|
63b4e9 |
+ * million, but with a 16 bit binary fractional field.
|
|
|
63b4e9 |
+ * Instead of calculating either one of
|
|
|
63b4e9 |
+ *
|
|
|
63b4e9 |
+ * scaled_ppm = (ppb / 1000) << 16 [1]
|
|
|
63b4e9 |
+ * scaled_ppm = (ppb << 16) / 1000 [2]
|
|
|
63b4e9 |
+ *
|
|
|
63b4e9 |
+ * we simply use double precision math, in order to avoid the
|
|
|
63b4e9 |
+ * truncation in [1] and the possible overflow in [2].
|
|
|
63b4e9 |
+ */
|
|
|
63b4e9 |
+ return (long) (ppb * 65.536);
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+static int64_t pctns(struct ptp_clock_time *t)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ return t->sec * 1000000000LL + t->nsec;
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+static void usage(char *progname)
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ fprintf(stderr,
|
|
|
63b4e9 |
+ "usage: %s [options]\n"
|
|
|
63b4e9 |
+ " -c query the ptp clock's capabilities\n"
|
|
|
63b4e9 |
+ " -d name device to open\n"
|
|
|
63b4e9 |
+ " -e val read 'val' external time stamp events\n"
|
|
|
63b4e9 |
+ " -f val adjust the ptp clock frequency by 'val' ppb\n"
|
|
|
63b4e9 |
+ " -g get the ptp clock time\n"
|
|
|
63b4e9 |
+ " -h prints this message\n"
|
|
|
63b4e9 |
+ " -i val index for event/trigger\n"
|
|
|
63b4e9 |
+ " -k val measure the time offset between system and phc clock\n"
|
|
|
63b4e9 |
+ " for 'val' times (Maximum 25)\n"
|
|
|
63b4e9 |
+ " -l list the current pin configuration\n"
|
|
|
63b4e9 |
+ " -L pin,val configure pin index 'pin' with function 'val'\n"
|
|
|
63b4e9 |
+ " the channel index is taken from the '-i' option\n"
|
|
|
63b4e9 |
+ " 'val' specifies the auxiliary function:\n"
|
|
|
63b4e9 |
+ " 0 - none\n"
|
|
|
63b4e9 |
+ " 1 - external time stamp\n"
|
|
|
63b4e9 |
+ " 2 - periodic output\n"
|
|
|
63b4e9 |
+ " -p val enable output with a period of 'val' nanoseconds\n"
|
|
|
63b4e9 |
+ " -H val set output phase to 'val' nanoseconds (requires -p)\n"
|
|
|
63b4e9 |
+ " -w val set output pulse width to 'val' nanoseconds (requires -p)\n"
|
|
|
63b4e9 |
+ " -P val enable or disable (val=1|0) the system clock PPS\n"
|
|
|
63b4e9 |
+ " -s set the ptp clock time from the system time\n"
|
|
|
63b4e9 |
+ " -S set the system time from the ptp clock time\n"
|
|
|
63b4e9 |
+ " -t val shift the ptp clock time by 'val' seconds\n"
|
|
|
63b4e9 |
+ " -T val set the ptp clock time to 'val' seconds\n"
|
|
|
63b4e9 |
+ " -z test combinations of rising/falling external time stamp flags\n",
|
|
|
63b4e9 |
+ progname);
|
|
|
63b4e9 |
+}
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+int main(int argc, char *argv[])
|
|
|
63b4e9 |
+{
|
|
|
63b4e9 |
+ struct ptp_clock_caps caps;
|
|
|
63b4e9 |
+ struct ptp_extts_event event;
|
|
|
63b4e9 |
+ struct ptp_extts_request extts_request;
|
|
|
63b4e9 |
+ struct ptp_perout_request perout_request;
|
|
|
63b4e9 |
+ struct ptp_pin_desc desc;
|
|
|
63b4e9 |
+ struct timespec ts;
|
|
|
63b4e9 |
+ struct timex tx;
|
|
|
63b4e9 |
+ struct ptp_clock_time *pct;
|
|
|
63b4e9 |
+ struct ptp_sys_offset *sysoff;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ char *progname;
|
|
|
63b4e9 |
+ unsigned int i;
|
|
|
63b4e9 |
+ int c, cnt, fd;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ char *device = DEVICE;
|
|
|
63b4e9 |
+ clockid_t clkid;
|
|
|
63b4e9 |
+ int adjfreq = 0x7fffffff;
|
|
|
63b4e9 |
+ int adjtime = 0;
|
|
|
63b4e9 |
+ int capabilities = 0;
|
|
|
63b4e9 |
+ int extts = 0;
|
|
|
63b4e9 |
+ int flagtest = 0;
|
|
|
63b4e9 |
+ int gettime = 0;
|
|
|
63b4e9 |
+ int index = 0;
|
|
|
63b4e9 |
+ int list_pins = 0;
|
|
|
63b4e9 |
+ int pct_offset = 0;
|
|
|
63b4e9 |
+ int n_samples = 0;
|
|
|
63b4e9 |
+ int pin_index = -1, pin_func;
|
|
|
63b4e9 |
+ int pps = -1;
|
|
|
63b4e9 |
+ int seconds = 0;
|
|
|
63b4e9 |
+ int settime = 0;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ int64_t t1, t2, tp;
|
|
|
63b4e9 |
+ int64_t interval, offset;
|
|
|
63b4e9 |
+ int64_t perout_phase = -1;
|
|
|
63b4e9 |
+ int64_t pulsewidth = -1;
|
|
|
63b4e9 |
+ int64_t perout = -1;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ progname = strrchr(argv[0], '/');
|
|
|
63b4e9 |
+ progname = progname ? 1+progname : argv[0];
|
|
|
63b4e9 |
+ while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:p:P:sSt:T:w:z"))) {
|
|
|
63b4e9 |
+ switch (c) {
|
|
|
63b4e9 |
+ case 'c':
|
|
|
63b4e9 |
+ capabilities = 1;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'd':
|
|
|
63b4e9 |
+ device = optarg;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'e':
|
|
|
63b4e9 |
+ extts = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'f':
|
|
|
63b4e9 |
+ adjfreq = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'g':
|
|
|
63b4e9 |
+ gettime = 1;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'H':
|
|
|
63b4e9 |
+ perout_phase = atoll(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'i':
|
|
|
63b4e9 |
+ index = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'k':
|
|
|
63b4e9 |
+ pct_offset = 1;
|
|
|
63b4e9 |
+ n_samples = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'l':
|
|
|
63b4e9 |
+ list_pins = 1;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'L':
|
|
|
63b4e9 |
+ cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
|
|
|
63b4e9 |
+ if (cnt != 2) {
|
|
|
63b4e9 |
+ usage(progname);
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'p':
|
|
|
63b4e9 |
+ perout = atoll(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'P':
|
|
|
63b4e9 |
+ pps = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 's':
|
|
|
63b4e9 |
+ settime = 1;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'S':
|
|
|
63b4e9 |
+ settime = 2;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 't':
|
|
|
63b4e9 |
+ adjtime = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'T':
|
|
|
63b4e9 |
+ settime = 3;
|
|
|
63b4e9 |
+ seconds = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'w':
|
|
|
63b4e9 |
+ pulsewidth = atoi(optarg);
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'z':
|
|
|
63b4e9 |
+ flagtest = 1;
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ case 'h':
|
|
|
63b4e9 |
+ usage(progname);
|
|
|
63b4e9 |
+ return 0;
|
|
|
63b4e9 |
+ case '?':
|
|
|
63b4e9 |
+ default:
|
|
|
63b4e9 |
+ usage(progname);
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ fd = open(device, O_RDWR);
|
|
|
63b4e9 |
+ if (fd < 0) {
|
|
|
63b4e9 |
+ fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ clkid = get_clockid(fd);
|
|
|
63b4e9 |
+ if (CLOCK_INVALID == clkid) {
|
|
|
63b4e9 |
+ fprintf(stderr, "failed to read clock id\n");
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (capabilities) {
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
|
|
|
63b4e9 |
+ perror("PTP_CLOCK_GETCAPS");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ printf("capabilities:\n"
|
|
|
63b4e9 |
+ " %d maximum frequency adjustment (ppb)\n"
|
|
|
63b4e9 |
+ " %d programmable alarms\n"
|
|
|
63b4e9 |
+ " %d external time stamp channels\n"
|
|
|
63b4e9 |
+ " %d programmable periodic signals\n"
|
|
|
63b4e9 |
+ " %d pulse per second\n"
|
|
|
63b4e9 |
+ " %d programmable pins\n"
|
|
|
63b4e9 |
+ " %d cross timestamping\n"
|
|
|
63b4e9 |
+ " %d adjust_phase\n",
|
|
|
63b4e9 |
+ caps.max_adj,
|
|
|
63b4e9 |
+ caps.n_alarm,
|
|
|
63b4e9 |
+ caps.n_ext_ts,
|
|
|
63b4e9 |
+ caps.n_per_out,
|
|
|
63b4e9 |
+ caps.pps,
|
|
|
63b4e9 |
+ caps.n_pins,
|
|
|
63b4e9 |
+ caps.cross_timestamping,
|
|
|
63b4e9 |
+ caps.adjust_phase);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (0x7fffffff != adjfreq) {
|
|
|
63b4e9 |
+ memset(&tx, 0, sizeof(tx));
|
|
|
63b4e9 |
+ tx.modes = ADJ_FREQUENCY;
|
|
|
63b4e9 |
+ tx.freq = ppb_to_scaled_ppm(adjfreq);
|
|
|
63b4e9 |
+ if (clock_adjtime(clkid, &tx)) {
|
|
|
63b4e9 |
+ perror("clock_adjtime");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("frequency adjustment okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (adjtime) {
|
|
|
63b4e9 |
+ memset(&tx, 0, sizeof(tx));
|
|
|
63b4e9 |
+ tx.modes = ADJ_SETOFFSET;
|
|
|
63b4e9 |
+ tx.time.tv_sec = adjtime;
|
|
|
63b4e9 |
+ tx.time.tv_usec = 0;
|
|
|
63b4e9 |
+ if (clock_adjtime(clkid, &tx) < 0) {
|
|
|
63b4e9 |
+ perror("clock_adjtime");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("time shift okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (gettime) {
|
|
|
63b4e9 |
+ if (clock_gettime(clkid, &ts)) {
|
|
|
63b4e9 |
+ perror("clock_gettime");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ printf("clock time: %ld.%09ld or %s",
|
|
|
63b4e9 |
+ ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (settime == 1) {
|
|
|
63b4e9 |
+ clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
63b4e9 |
+ if (clock_settime(clkid, &ts)) {
|
|
|
63b4e9 |
+ perror("clock_settime");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("set time okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (settime == 2) {
|
|
|
63b4e9 |
+ clock_gettime(clkid, &ts);
|
|
|
63b4e9 |
+ if (clock_settime(CLOCK_REALTIME, &ts)) {
|
|
|
63b4e9 |
+ perror("clock_settime");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("set time okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (settime == 3) {
|
|
|
63b4e9 |
+ ts.tv_sec = seconds;
|
|
|
63b4e9 |
+ ts.tv_nsec = 0;
|
|
|
63b4e9 |
+ if (clock_settime(clkid, &ts)) {
|
|
|
63b4e9 |
+ perror("clock_settime");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("set time okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (extts) {
|
|
|
63b4e9 |
+ memset(&extts_request, 0, sizeof(extts_request));
|
|
|
63b4e9 |
+ extts_request.index = index;
|
|
|
63b4e9 |
+ extts_request.flags = PTP_ENABLE_FEATURE;
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
|
|
|
63b4e9 |
+ perror("PTP_EXTTS_REQUEST");
|
|
|
63b4e9 |
+ extts = 0;
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("external time stamp request okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ for (; extts; extts--) {
|
|
|
63b4e9 |
+ cnt = read(fd, &event, sizeof(event));
|
|
|
63b4e9 |
+ if (cnt != sizeof(event)) {
|
|
|
63b4e9 |
+ perror("read");
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ printf("event index %u at %lld.%09u\n", event.index,
|
|
|
63b4e9 |
+ event.t.sec, event.t.nsec);
|
|
|
63b4e9 |
+ fflush(stdout);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ /* Disable the feature again. */
|
|
|
63b4e9 |
+ extts_request.flags = 0;
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
|
|
|
63b4e9 |
+ perror("PTP_EXTTS_REQUEST");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (flagtest) {
|
|
|
63b4e9 |
+ do_flag_test(fd, index);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (list_pins) {
|
|
|
63b4e9 |
+ int n_pins = 0;
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
|
|
|
63b4e9 |
+ perror("PTP_CLOCK_GETCAPS");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ n_pins = caps.n_pins;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ for (i = 0; i < n_pins; i++) {
|
|
|
63b4e9 |
+ desc.index = i;
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
|
|
|
63b4e9 |
+ perror("PTP_PIN_GETFUNC");
|
|
|
63b4e9 |
+ break;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ printf("name %s index %u func %u chan %u\n",
|
|
|
63b4e9 |
+ desc.name, desc.index, desc.func, desc.chan);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (pulsewidth >= 0 && perout < 0) {
|
|
|
63b4e9 |
+ puts("-w can only be specified together with -p");
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (perout_phase >= 0 && perout < 0) {
|
|
|
63b4e9 |
+ puts("-H can only be specified together with -p");
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (perout >= 0) {
|
|
|
63b4e9 |
+ if (clock_gettime(clkid, &ts)) {
|
|
|
63b4e9 |
+ perror("clock_gettime");
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ memset(&perout_request, 0, sizeof(perout_request));
|
|
|
63b4e9 |
+ perout_request.index = index;
|
|
|
63b4e9 |
+ perout_request.period.sec = perout / NSEC_PER_SEC;
|
|
|
63b4e9 |
+ perout_request.period.nsec = perout % NSEC_PER_SEC;
|
|
|
63b4e9 |
+ perout_request.flags = 0;
|
|
|
63b4e9 |
+ if (pulsewidth >= 0) {
|
|
|
63b4e9 |
+ perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
|
|
|
63b4e9 |
+ perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
|
|
|
63b4e9 |
+ perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ if (perout_phase >= 0) {
|
|
|
63b4e9 |
+ perout_request.flags |= PTP_PEROUT_PHASE;
|
|
|
63b4e9 |
+ perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
|
|
|
63b4e9 |
+ perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ perout_request.start.sec = ts.tv_sec + 2;
|
|
|
63b4e9 |
+ perout_request.start.nsec = 0;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
|
|
|
63b4e9 |
+ perror("PTP_PEROUT_REQUEST");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("periodic output request okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (pin_index >= 0) {
|
|
|
63b4e9 |
+ memset(&desc, 0, sizeof(desc));
|
|
|
63b4e9 |
+ desc.index = pin_index;
|
|
|
63b4e9 |
+ desc.func = pin_func;
|
|
|
63b4e9 |
+ desc.chan = index;
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
|
|
|
63b4e9 |
+ perror("PTP_PIN_SETFUNC");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("set pin function okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (pps != -1) {
|
|
|
63b4e9 |
+ int enable = pps ? 1 : 0;
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
|
|
|
63b4e9 |
+ perror("PTP_ENABLE_PPS");
|
|
|
63b4e9 |
+ } else {
|
|
|
63b4e9 |
+ puts("pps for system time request okay");
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (pct_offset) {
|
|
|
63b4e9 |
+ if (n_samples <= 0 || n_samples > 25) {
|
|
|
63b4e9 |
+ puts("n_samples should be between 1 and 25");
|
|
|
63b4e9 |
+ usage(progname);
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ sysoff = calloc(1, sizeof(*sysoff));
|
|
|
63b4e9 |
+ if (!sysoff) {
|
|
|
63b4e9 |
+ perror("calloc");
|
|
|
63b4e9 |
+ return -1;
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+ sysoff->n_samples = n_samples;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
|
|
|
63b4e9 |
+ perror("PTP_SYS_OFFSET");
|
|
|
63b4e9 |
+ else
|
|
|
63b4e9 |
+ puts("system and phc clock time offset request okay");
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ pct = &sysoff->ts[0];
|
|
|
63b4e9 |
+ for (i = 0; i < sysoff->n_samples; i++) {
|
|
|
63b4e9 |
+ t1 = pctns(pct+2*i);
|
|
|
63b4e9 |
+ tp = pctns(pct+2*i+1);
|
|
|
63b4e9 |
+ t2 = pctns(pct+2*i+2);
|
|
|
63b4e9 |
+ interval = t2 - t1;
|
|
|
63b4e9 |
+ offset = (t2 + t1) / 2 - tp;
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ printf("system time: %lld.%u\n",
|
|
|
63b4e9 |
+ (pct+2*i)->sec, (pct+2*i)->nsec);
|
|
|
63b4e9 |
+ printf("phc time: %lld.%u\n",
|
|
|
63b4e9 |
+ (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
|
|
|
63b4e9 |
+ printf("system time: %lld.%u\n",
|
|
|
63b4e9 |
+ (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
|
|
|
63b4e9 |
+ printf("system/phc clock time offset is %" PRId64 " ns\n"
|
|
|
63b4e9 |
+ "system clock time delay is %" PRId64 " ns\n",
|
|
|
63b4e9 |
+ offset, interval);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ free(sysoff);
|
|
|
63b4e9 |
+ }
|
|
|
63b4e9 |
+
|
|
|
63b4e9 |
+ close(fd);
|
|
|
63b4e9 |
+ return 0;
|
|
|
63b4e9 |
+}
|