phc2sys: use the system offset method if available

We use the PTP_SYS_OFFSET ioctl method in preference to doing
clock_gettime from user space, the assumption being that the kernel space
measurement will always be more accurate.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2012-11-26 13:25:35 +01:00
parent 36a5224b5a
commit 4ef70c3d2e
2 changed files with 28 additions and 2 deletions

View File

@ -31,7 +31,7 @@ PRG = ptp4l pmc phc2sys hwstamp_ctl
OBJ = bmc.o clock.o config.o fsm.o ptp4l.o mave.o msg.o phc.o pi.o port.o \
print.o raw.o servo.o sk.o tlv.o tmtab.o transport.o udp.o udp6.o uds.o util.o
OBJECTS = $(OBJ) pmc.o phc2sys.o hwstamp_ctl.o
OBJECTS = $(OBJ) pmc.o phc2sys.o hwstamp_ctl.o sysoff.o
SRC = $(OBJECTS:.o=.c)
DEPEND = $(OBJECTS:.o=.d)
srcdir := $(dir $(lastword $(MAKEFILE_LIST)))
@ -48,7 +48,7 @@ ptp4l: $(OBJ)
pmc: pmc.o msg.o print.o raw.o sk.o tlv.o transport.o udp.o udp6.o uds.o util.o
phc2sys: phc2sys.o sk.o print.o
phc2sys: phc2sys.o sk.o sysoff.o print.o
hwstamp_ctl: hwstamp_ctl.o

View File

@ -34,6 +34,7 @@
#include "missing.h"
#include "sk.h"
#include "sysoff.h"
#define KP 0.7
#define KI 0.3
@ -228,6 +229,26 @@ static int do_pps_loop(char *pps_device, double kp, double ki, clockid_t dst)
return 0;
}
static int do_sysoff_loop(clockid_t src, clockid_t dst,
int rate, int n_readings, int sync_offset,
double kp, double ki)
{
uint64_t ts;
int64_t offset;
int err = 0, fd = CLOCKID_TO_FD(src);
while (1) {
usleep(1000000 / rate);
if (sysoff_measure(fd, n_readings, &offset, &ts)) {
err = -1;
break;
}
offset -= sync_offset * NS_PER_SEC;
do_servo(&servo, dst, offset, ts, kp, ki);
show_servo(stdout, "sys", offset, ts);
}
return err;
}
static void usage(char *progname)
{
fprintf(stderr,
@ -323,6 +344,11 @@ int main(int argc, char *argv[])
if (device)
return do_pps_loop(device, kp, ki, dst);
if (dst == CLOCK_REALTIME &&
SYSOFF_SUPPORTED == sysoff_probe(CLOCKID_TO_FD(src), phc_readings))
return do_sysoff_loop(src, dst, phc_rate,
phc_readings, sync_offset, kp, ki);
while (1) {
usleep(1000000 / phc_rate);
if (!read_phc(src, dst, phc_readings, &phc_offset, &phc_ts)) {