2 * refclock_trak - clock driver for the TRAK 8810 GPS Station Clock
4 * Tomoaki TSURUOKA <tsuruoka@nc.fukuoka-u.ac.jp>
5 * original version Dec, 1993
6 * revised version Sep, 1994 for ntp3.4e or later
13 #if defined(REFCLOCK) && defined(CLOCK_TRAK) && defined(PPS)
17 #include "ntp_refclock.h"
18 #include "ntp_stdlib.h"
19 #include "ntp_unixtime.h"
24 #ifdef HAVE_SYS_TERMIOS_H
25 # include <sys/termios.h>
27 #ifdef HAVE_SYS_PPSCLOCK_H
28 # include <sys/ppsclock.h>
32 * This driver supports the TRAK 8810/8820 GPS Station Clock. The claimed
33 * accuracy at the 1-pps output is 200-300 ns relative to the broadcast
34 * signal; however, in most cases the actual accuracy is limited by the
35 * precision of the timecode and the latencies of the serial interface
36 * and operating system.
38 * For best accuracy, this radio requires the LDISC_ACTS line
39 * discipline, which captures a timestamp at the '*' on-time character
40 * of the timecode. Using this discipline the jitter is in the order of
41 * 1 ms and systematic error about 0.5 ms. If unavailable, the buffer
42 * timestamp is used, which is captured at the \r ending the timecode
43 * message. This introduces a systematic error of 23 character times, or
44 * about 24 ms at 9600 bps, together with a jitter well over 8 ms on Sun
47 * Using the memus, the radio should be set for 9600 bps, one stop bit
48 * and no parity. It should be set to operate in computer (no echo)
49 * mode. The timecode format includes neither the year nor leap-second
50 * warning. No provisions are included in this preliminary version of
51 * the driver to read and record detailed internal radio status.
53 * In operation, this driver sends a RQTS\r request to the radio at
54 * initialization in order to put it in continuous time output mode. The
55 * radio then sends the following message once each second:
57 * *RQTS U,ddd:hh:mm:ss.0,q<cr><lf>
59 * on-time = '*'
\v * ddd = day of year
60 * hh:mm:ss = hours, minutes, seconds
61 * q = quality indicator (phase error), 0-6:
69 * The alarm condition is indicated by '0' at Q, which means the radio
70 * has a phase error than 20 usec relative to the broadcast time. The
71 * absence of year, DST and leap-second warning in this format is also
74 * The continuous time mode is disabled using the RQTX<cr> request,
75 * following which the radio sends a RQTX DONE<cr><lf> response. In the
76 * normal mode, other control and status requests are effective,
77 * including the leap-second status request RQLS<cr>. The radio responds
78 * wtih RQLS yy,mm,dd<cr><lf>, where yy,mm,dd are the year, month and
79 * day. Presumably, this gives the epoch of the next leap second,
80 * RQLS 00,00,00 if none is specified in the GPS message. Specified in
81 * this form, the information is generally useless and is ignored by
86 * There are no special fudge factors other than the generic.
90 * Interface definitions
92 #define DEVICE "/dev/trak%d" /* device name and unit */
93 #define SPEED232 B9600 /* uart speed (9600 baud) */
94 #define PRECISION (-20) /* precision assumed (about 1 us) */
95 #define REFID "GPS\0" /* reference ID */
96 #define DESCRIPTION "TRACK 8810/8820 Station Clock" /* WRU */
98 #define LENTRAK 24 /* timecode length */
99 #define C_CTO "RQTS\r" /* start continuous time output */
102 * Unit control structure
105 int polled; /* poll message flag */
106 l_fp tstamp; /* timestamp of last poll */
110 * Function prototypes
112 static int trak_start P((int, struct peer *));
113 static void trak_shutdown P((int, struct peer *));
114 static void trak_receive P((struct recvbuf *));
115 static void trak_poll P((int, struct peer *));
120 struct refclock refclock_trak = {
121 trak_start, /* start up driver */
122 trak_shutdown, /* shut down driver */
123 trak_poll, /* transmit poll message */
124 noentry, /* not used (old trak_control) */
125 noentry, /* initialize driver (not used) */
126 noentry, /* not used (old trak_buginfo) */
127 NOFLAGS /* not used */
132 * trak_start - open the devices and initialize data for processing
140 register struct trakunit *up;
141 struct refclockproc *pp;
146 * Open serial port. The LDISC_ACTS line discipline inserts a
147 * timestamp following the "*" on-time character of the
150 (void)sprintf(device, DEVICE, unit);
153 !(fd = refclock_open(device, SPEED232, LDISC_CLK))
155 !(fd = refclock_open(device, SPEED232, 0))
161 * Allocate and initialize unit structure
163 if (!(up = (struct trakunit *)
164 emalloc(sizeof(struct trakunit)))) {
168 memset((char *)up, 0, sizeof(struct trakunit));
170 pp->io.clock_recv = trak_receive;
171 pp->io.srcclock = (caddr_t)peer;
174 if (!io_addclock(&pp->io)) {
179 pp->unitptr = (caddr_t)up;
182 * Initialize miscellaneous variables
184 peer->precision = PRECISION;
185 pp->clockdesc = DESCRIPTION;
186 memcpy((char *)&pp->refid, REFID, 4);
190 * Start continuous time output. If something breaks, fold the
193 if (write(pp->io.fd, C_CTO, sizeof(C_CTO)) != sizeof(C_CTO)) {
194 refclock_report(peer, CEVNT_FAULT);
204 * trak_shutdown - shut down the clock
212 register struct trakunit *up;
213 struct refclockproc *pp;
216 up = (struct trakunit *)pp->unitptr;
217 io_closeclock(&pp->io);
223 * trak_receive - receive data from the serial interface
227 struct recvbuf *rbufp
230 register struct trakunit *up;
231 struct refclockproc *pp;
237 struct ppsclockev ppsev;
242 #ifdef HAVE_TIOCGPPSEV
243 request = TIOCGPPSEV;
248 * Initialize pointers and read the timecode and timestamp. We
249 * then chuck out everything, including runts, except one
250 * message each poll interval.
252 peer = (struct peer *)rbufp->recv_srcclock;
254 up = (struct trakunit *)pp->unitptr;
255 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
259 * We get a buffer and timestamp following the '*' on-time
260 * character. If a valid timestamp, we use that in place of the
261 * buffer timestamp and edit out the timestamp for prettyprint
264 dpt = pp->a_lastcode;
265 dpend = dpt + pp->lencode;
266 if (*dpt == '*' && buftvtots(dpt + 1, &trtmp)) {
267 if (trtmp.l_i == pp->lastrec.l_i || trtmp.l_i ==
268 pp->lastrec.l_i + 1) {
271 while (dpt < dpend) {
277 if (up->polled == 0) return;
280 get_systime(&up->tstamp);
282 record_clock_stats(&peer->srcadr, pp->a_lastcode);
285 printf("trak: timecode %d %s\n", pp->lencode,
290 * We get down to business, check the timecode format and decode
291 * its contents. If the timecode has invalid length or is not in
292 * proper format, we declare bad format and exit.
294 if (pp->lencode < LENTRAK) {
295 refclock_report(peer, CEVNT_BADREPLY);
300 * Timecode format: "*RQTS U,ddd:hh:mm:ss.0,q"
302 if (sscanf(pp->a_lastcode, "*RQTS U,%3d:%2d:%2d:%2d.0,%c",
303 &pp->day, &pp->hour, &pp->minute, &pp->second, &qchar) != 5) {
304 refclock_report(peer, CEVNT_BADREPLY);
309 * Decode quality and leap characters. If unsynchronized, set
310 * the leap bits accordingly and exit.
313 pp->leap = LEAP_NOTINSYNC;
317 if(ioctl(fdpps,request,(caddr_t) &ppsev) >=0) {
318 ppsev.tv.tv_sec += (u_int32) JAN_1970;
319 TVTOTS(&ppsev.tv,&up->tstamp);
322 /* record the last ppsclock event time stamp */
323 pp->lastrec = up->tstamp;
324 if (!refclock_process(pp)) {
325 refclock_report(peer, CEVNT_BADTIME);
328 pp->lastref = pp->lastrec;
329 refclock_receive(peer);
334 * trak_poll - called by the transmit procedure
342 register struct trakunit *up;
343 struct refclockproc *pp;
346 * We don't really do anything here, except arm the receiving
347 * side to capture a sample and check for timeouts.
350 up = (struct trakunit *)pp->unitptr;
352 refclock_report(peer, CEVNT_TIMEOUT);
358 int refclock_trak_bs;
359 #endif /* REFCLOCK */