2012-01-25 02:12:57 +08:00
|
|
|
/**
|
|
|
|
* @file phc2sys.c
|
|
|
|
* @brief Utility program to synchronize two clocks via a PPS.
|
|
|
|
* @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <linux/pps.h>
|
|
|
|
#include <linux/ptp_clock.h>
|
|
|
|
|
|
|
|
#include "missing.h"
|
|
|
|
|
|
|
|
#define KP 0.7
|
|
|
|
#define KI 0.3
|
|
|
|
#define NS_PER_SEC 1000000000LL
|
|
|
|
|
|
|
|
#define max_ppb 512000
|
|
|
|
#define min_ppb -512000
|
|
|
|
|
|
|
|
static clockid_t clock_open(char *device)
|
|
|
|
{
|
|
|
|
int fd = open(device, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "cannot open %s: %m\n", device);
|
|
|
|
return CLOCK_INVALID;
|
|
|
|
}
|
|
|
|
return FD_TO_CLOCKID(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_ppb(clockid_t clkid, double ppb)
|
|
|
|
{
|
|
|
|
struct timex tx;
|
|
|
|
memset(&tx, 0, sizeof(tx));
|
|
|
|
tx.modes = ADJ_FREQUENCY;
|
|
|
|
tx.freq = (long) (ppb * 65.536);
|
|
|
|
if (clock_adjtime(clkid, &tx) < 0)
|
|
|
|
fprintf(stderr, "failed to adjust the clock: %m\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_step(clockid_t clkid, int64_t ns)
|
|
|
|
{
|
|
|
|
struct timex tx;
|
|
|
|
int sign = 1;
|
|
|
|
if (ns < 0) {
|
|
|
|
sign = -1;
|
|
|
|
ns *= -1;
|
|
|
|
}
|
|
|
|
memset(&tx, 0, sizeof(tx));
|
|
|
|
tx.modes = ADJ_SETOFFSET | ADJ_NANO;
|
|
|
|
tx.time.tv_sec = sign * (ns / NS_PER_SEC);
|
|
|
|
tx.time.tv_usec = sign * (ns % NS_PER_SEC);
|
|
|
|
/*
|
|
|
|
* The value of a timeval is the sum of its fields, but the
|
|
|
|
* field tv_usec must always be non-negative.
|
|
|
|
*/
|
|
|
|
if (tx.time.tv_usec < 0) {
|
|
|
|
tx.time.tv_sec -= 1;
|
|
|
|
tx.time.tv_usec += 1000000000;
|
|
|
|
}
|
|
|
|
if (clock_adjtime(clkid, &tx) < 0)
|
|
|
|
fprintf(stderr, "failed to step clock: %m\n");
|
|
|
|
}
|
|
|
|
|
2012-08-31 01:44:38 +08:00
|
|
|
static int read_phc(clockid_t clkid, clockid_t sysclk, int rdelay, int readings, int64_t *offset, uint64_t *ts)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
2012-08-29 21:28:42 +08:00
|
|
|
struct timespec tdst1, tdst2, tsrc;
|
|
|
|
int i;
|
|
|
|
int64_t interval, best_interval = INT64_MAX;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
2012-08-29 21:28:42 +08:00
|
|
|
/* Pick the quickest clkid reading. */
|
2012-08-31 01:44:38 +08:00
|
|
|
for (i = 0; i < readings; i++) {
|
2012-08-29 21:28:42 +08:00
|
|
|
if (clock_gettime(sysclk, &tdst1) ||
|
|
|
|
clock_gettime(clkid, &tsrc) ||
|
|
|
|
clock_gettime(sysclk, &tdst2)) {
|
|
|
|
perror("clock_gettime");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
|
|
|
|
tdst2.tv_nsec - tdst1.tv_nsec;
|
|
|
|
|
|
|
|
if (best_interval > interval) {
|
|
|
|
best_interval = interval;
|
|
|
|
*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
|
|
|
|
tdst1.tv_nsec - tsrc.tv_nsec +
|
|
|
|
interval / 2 - rdelay;
|
|
|
|
*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
|
|
|
|
}
|
2012-08-30 20:45:50 +08:00
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct servo {
|
2012-08-31 01:44:37 +08:00
|
|
|
uint64_t saved_ts;
|
|
|
|
int64_t saved_offset;
|
2012-01-25 02:12:57 +08:00
|
|
|
double drift;
|
|
|
|
enum {
|
2012-08-29 21:29:43 +08:00
|
|
|
SAMPLE_0, SAMPLE_1, SAMPLE_2, SAMPLE_3, SAMPLE_N
|
2012-01-25 02:12:57 +08:00
|
|
|
} state;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct servo servo;
|
|
|
|
|
|
|
|
static void do_servo(struct servo *srv,
|
|
|
|
clockid_t src, clockid_t dst,
|
2012-08-29 21:29:43 +08:00
|
|
|
int64_t offset, uint64_t ts, double kp, double ki)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
|
|
|
double ki_term, ppb;
|
|
|
|
|
2012-08-29 21:29:43 +08:00
|
|
|
printf("s%d %lld.%09llu drift %.2f\n",
|
|
|
|
srv->state, ts / NS_PER_SEC, ts % NS_PER_SEC, srv->drift);
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
switch (srv->state) {
|
2012-08-29 21:29:43 +08:00
|
|
|
case SAMPLE_0:
|
2012-01-25 02:12:57 +08:00
|
|
|
clock_ppb(dst, 0.0);
|
2012-08-31 01:44:37 +08:00
|
|
|
srv->saved_offset = offset;
|
|
|
|
srv->saved_ts = ts;
|
2012-08-29 21:29:43 +08:00
|
|
|
srv->state = SAMPLE_1;
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2012-08-29 21:29:43 +08:00
|
|
|
case SAMPLE_1:
|
|
|
|
srv->state = SAMPLE_2;
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2012-08-29 21:29:43 +08:00
|
|
|
case SAMPLE_2:
|
|
|
|
srv->state = SAMPLE_3;
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2012-08-29 21:29:43 +08:00
|
|
|
case SAMPLE_3:
|
2012-08-31 01:44:37 +08:00
|
|
|
srv->drift = (offset - srv->saved_offset) * 1e9 /
|
|
|
|
(ts - srv->saved_ts);
|
|
|
|
clock_ppb(dst, -srv->drift);
|
2012-01-25 02:12:57 +08:00
|
|
|
clock_step(dst, -offset);
|
2012-08-29 21:29:43 +08:00
|
|
|
srv->state = SAMPLE_N;
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2012-08-29 21:29:43 +08:00
|
|
|
case SAMPLE_N:
|
2012-01-25 02:12:57 +08:00
|
|
|
ki_term = ki * offset;
|
|
|
|
ppb = kp * offset + srv->drift + ki_term;
|
|
|
|
if (ppb < min_ppb) {
|
|
|
|
ppb = min_ppb;
|
|
|
|
} else if (ppb > max_ppb) {
|
|
|
|
ppb = max_ppb;
|
|
|
|
} else {
|
|
|
|
srv->drift += ki_term;
|
|
|
|
}
|
|
|
|
clock_ppb(dst, -ppb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +08:00
|
|
|
static int read_pps(int fd, int64_t *offset, uint64_t *ts)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
|
|
|
struct pps_fdata pfd;
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
pfd.timeout.sec = 10;
|
|
|
|
pfd.timeout.nsec = 0;
|
|
|
|
pfd.timeout.flags = ~PPS_TIME_INVALID;
|
|
|
|
if (ioctl(fd, PPS_FETCH, &pfd)) {
|
|
|
|
perror("ioctl PPS_FETCH");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
*ts = pfd.info.assert_tu.sec * NS_PER_SEC;
|
|
|
|
*ts += pfd.info.assert_tu.nsec;
|
|
|
|
|
|
|
|
*offset = *ts % NS_PER_SEC;
|
|
|
|
if (*offset > NS_PER_SEC / 2)
|
|
|
|
*offset -= NS_PER_SEC;
|
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n"
|
|
|
|
"usage: %s [options]\n\n"
|
|
|
|
" -c [device] slave clock device, default CLOCK_REALTIME\n"
|
|
|
|
" -d [device] master device, source of PPS events\n"
|
|
|
|
" -h prints this message and exits\n"
|
|
|
|
" -r [val] reading the PHC device takes 'val' nanoseconds\n"
|
|
|
|
" -s [device] set the time from this PHC device\n"
|
|
|
|
" -P [val] set proportional constant to 'val'\n"
|
|
|
|
" -I [val] set integration constant to 'val'\n"
|
2012-08-31 01:44:38 +08:00
|
|
|
" -R [val] set PHC update rate to 'val' Hz\n"
|
|
|
|
" -N [val] set number of PHC readings per update\n"
|
2012-01-25 02:12:57 +08:00
|
|
|
"\n",
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
double kp = KP, ki = KI;
|
|
|
|
char *device = NULL, *progname;
|
|
|
|
clockid_t src = CLOCK_INVALID, dst = CLOCK_REALTIME;
|
2012-08-29 21:29:43 +08:00
|
|
|
uint64_t pps_ts, phc_ts;
|
|
|
|
int64_t pps_offset, phc_offset;
|
2012-08-31 01:44:38 +08:00
|
|
|
int c, fd = 0, rdelay = 0, phc_readings = 5, phc_rate = 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
/* Process the command line arguments. */
|
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
progname = progname ? 1+progname : argv[0];
|
2012-08-31 01:44:38 +08:00
|
|
|
while (EOF != (c = getopt(argc, argv, "c:d:hr:s:P:I:R:N:"))) {
|
2012-01-25 02:12:57 +08:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
dst = clock_open(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
device = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rdelay = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
src = clock_open(optarg);
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
kp = atof(optarg);
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
ki = atof(optarg);
|
|
|
|
break;
|
2012-08-31 01:44:38 +08:00
|
|
|
case 'R':
|
|
|
|
phc_rate = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
phc_readings = atoi(optarg);
|
|
|
|
break;
|
2012-01-25 02:12:57 +08:00
|
|
|
case 'h':
|
|
|
|
usage(progname);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
usage(progname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +08:00
|
|
|
if (!(device || src != CLOCK_INVALID) || dst == CLOCK_INVALID) {
|
2012-01-25 02:12:57 +08:00
|
|
|
usage(progname);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
if (device) {
|
|
|
|
fd = open(device, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "cannot open %s: %m\n", device);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
if (src != CLOCK_INVALID) {
|
|
|
|
struct timespec now;
|
|
|
|
if (clock_gettime(src, &now))
|
|
|
|
perror("clock_gettime");
|
|
|
|
if (clock_settime(dst, &now))
|
|
|
|
perror("clock_settime");
|
|
|
|
}
|
|
|
|
while (1) {
|
2012-08-29 21:29:43 +08:00
|
|
|
if (fd > 0) {
|
|
|
|
if (!read_pps(fd, &pps_offset, &pps_ts))
|
|
|
|
continue;
|
|
|
|
printf("pps %9lld ", pps_offset);
|
|
|
|
} else
|
2012-08-31 01:44:38 +08:00
|
|
|
usleep(1000000 / phc_rate);
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2012-09-19 19:34:34 +08:00
|
|
|
if (src != CLOCK_INVALID) {
|
|
|
|
if (!read_phc(src, dst, rdelay, phc_readings, &phc_offset, &phc_ts))
|
|
|
|
continue;
|
|
|
|
printf("phc %9lld ", phc_offset);
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
if (fd > 0)
|
|
|
|
do_servo(&servo, src, dst, pps_offset, pps_ts, kp, ki);
|
|
|
|
else
|
|
|
|
do_servo(&servo, src, dst, phc_offset, phc_ts, kp, ki);
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|