2 * refclock_true - clock driver for the Kinemetrics/TrueTime receivers
3 * Receiver Version 3.0C - tested plain, with CLKLDISC
4 * Development work being done:
5 * - Support TL-3 WWV TOD receiver
12 #if defined(REFCLOCK) && defined(CLOCK_TRUETIME)
19 #include "ntp_refclock.h"
20 #include "ntp_unixtime.h"
21 #include "ntp_stdlib.h"
24 extern int async_write(int, const void *, unsigned int);
26 #define write(fd, data, octets) async_write(fd, data, octets)
29 /* This should be an atom clock but those are very hard to build.
31 * The PCL720 from P C Labs has an Intel 8253 lookalike, as well as a bunch
32 * of TTL input and output pins, all brought out to the back panel. If you
33 * wire a PPS signal (such as the TTL PPS coming out of a GOES or other
34 * Kinemetrics/Truetime clock) to the 8253's GATE0, and then also wire the
35 * 8253's OUT0 to the PCL720's INPUT3.BIT0, then we can read CTR0 to get the
36 * number of uSecs since the last PPS upward swing, mediated by reading OUT0
37 * to find out if the counter has wrapped around (this happens if more than
38 * 65535us (65ms) elapses between the PPS event and our being called.)
43 # include <machine/inline.h>
44 # include <sys/pcl720.h>
45 # include <sys/i8253.h>
46 # define PCL720_IOB 0x2a0 /* XXX */
47 # define PCL720_CTR 0 /* XXX */
51 * Support for Kinemetrics Truetime Receivers
52 * GOES: (468-DC, usable with GPS->GOES converting antenna)
54 * XL-DC: (a 151-602-210, reported by the driver as a GPS/TM-TMD)
55 * GPS-800 TCU: (an 805-957 with the RS232 Talker/Listener module)
56 * TL-3: 3 channel WWV/H receiver w/ IRIG and RS-232 outputs
57 * OM-DC: getting stale ("OMEGA")
59 * Most of this code is originally from refclock_wwvb.c with thanks.
60 * It has been so mangled that wwvb is not a recognizable ancestor.
62 * Timcode format: ADDD:HH:MM:SSQCL
63 * A - control A (this is stripped before we see it)
64 * Q - Quality indication (see below)
68 * Quality codes indicate possible error of
69 * 468-DC GOES Receiver:
70 * GPS-TM/TMD Receiver: (default quality codes for XL-DC)
71 * ? +/- 1 milliseconds # +/- 100 microseconds
72 * * +/- 10 microseconds . +/- 1 microsecond
73 * space less than 1 microsecond
74 * TL-3 Receiver: (default quality codes for TL-3)
75 * ? unknown quality (receiver is unlocked)
76 * space +/- 5 milliseconds
77 * OM-DC OMEGA Receiver: (default quality codes for OMEGA)
78 * WARNING OMEGA navigation system is no longer existent
80 * ? >+/- 500 milliseconds # >+/- 50 milliseconds
81 * * >+/- 5 milliseconds . >+/- 1 millisecond
82 * A-H less than 1 millisecond. Character indicates which station
83 * is being received as follows:
84 * A = Norway, B = Liberia, C = Hawaii, D = North Dakota,
85 * E = La Reunion, F = Argentina, G = Australia, H = Japan.
87 * The carriage return start bit begins on 0 seconds and extends to 1 bit time.
89 * Notes on 468-DC and OMEGA receiver:
91 * Send the clock a 'R' or 'C' and once per second a timestamp will
92 * appear. Send a 'P' to get the satellite position once (GOES only.)
94 * Notes on the 468-DC receiver:
96 * Since the old east/west satellite locations are only historical, you can't
97 * set your clock propagation delay settings correctly and still use
98 * automatic mode. The manual says to use a compromise when setting the
99 * switches. This results in significant errors. The solution; use fudge
100 * time1 and time2 to incorporate corrections. If your clock is set for
101 * 50 and it should be 58 for using the west and 46 for using the east,
104 * fudge 127.127.5.0 time1 +0.008 time2 -0.004
106 * This corrects the 4 milliseconds advance and 8 milliseconds retard
107 * needed. The software will ask the clock which satellite it sees.
109 * Notes on the TrueTime TimeLink TL-3 WWV TOD receiver:
111 * This clock may be polled, or send one timecode per second.
112 * That mode may be toggled via the front panel ("C" mode), or controlled
113 * from the RS-232 port. Send the receiver "ST1" to turn it on, and
114 * "ST0" to turn it off. Send "QV" to get the firmware revision (useful
115 * for identifying this model.)
117 * Note that it can take several polling cycles, especially if the receiver
118 * was in the continuous timecode mode. (It can be slow to leave that mode.)
120 * ntp.conf parameters:
121 * time1 - offset applied to samples when reading WEST satellite (default = 0)
122 * time2 - offset applied to samples when reading EAST satellite (default = 0)
123 * stratum - stratum to assign to this clock (default = 0)
124 * refid - refid assigned to this clock (default = "TRUE", see below)
125 * flag1 - will silence the clock side of ntpd, just reading the clock
126 * without trying to write to it. (default = 0)
127 * flag2 - generate a debug file /tmp/true%d.
128 * flag3 - enable ppsclock streams module
129 * flag4 - use the PCL-720 (BSD/OS only)
136 #define DEVICE "/dev/true%d"
137 #define SPEED232 B9600 /* 9600 baud */
140 * Radio interface parameters
142 #define PRECISION (-10) /* precision assumed (about 1 ms) */
143 #define REFID "TRUE" /* reference id */
144 #define DESCRIPTION "Kinemetrics/TrueTime Receiver"
147 * Tags which station (satellite) we see
149 #define GOES_WEST 0 /* Default to WEST satellite and apply time1 */
150 #define GOES_EAST 1 /* until you discover otherwise */
153 * used by the state machine
155 enum true_event {e_Init, e_Huh, e_F18, e_F50, e_F51, e_Satellite,
156 e_TL3, e_Poll, e_Location, e_TS, e_Max};
157 const char *events[] = {"Init", "Huh", "F18", "F50", "F51", "Satellite",
158 "TL3", "Poll", "Location", "TS"};
159 #define eventStr(x) (((int)x<(int)e_Max) ? events[(int)x] : "?")
161 enum true_state {s_Base, s_InqTM, s_InqTCU, s_InqOmega, s_InqGOES,
162 s_InqTL3, s_Init, s_F18, s_F50, s_Start, s_Auto, s_Max};
163 const char *states[] = {"Base", "InqTM", "InqTCU", "InqOmega", "InqGOES",
164 "InqTL3", "Init", "F18", "F50", "Start", "Auto"};
165 #define stateStr(x) (((int)x<(int)s_Max) ? states[(int)x] : "?")
167 enum true_type {t_unknown, t_goes, t_tm, t_tcu, t_omega, t_tl3, t_Max};
168 const char *types[] = {"unknown", "goes", "tm", "tcu", "omega", "tl3"};
169 #define typeStr(x) (((int)x<(int)t_Max) ? types[(int)x] : "?")
172 * unit control structure
175 unsigned int pollcnt; /* poll message counter */
176 unsigned int station; /* which station we are on */
177 unsigned int polled; /* Hand in a time sample? */
178 enum true_state state; /* state machine */
179 enum true_type type; /* what kind of clock is it? */
180 int unit; /* save an extra copy of this */
181 FILE *debug; /* debug logging file */
183 int pcl720init; /* init flag for PCL 720 */
188 * Function prototypes
190 static int true_start (int, struct peer *);
191 static void true_shutdown (int, struct peer *);
192 static void true_receive (struct recvbuf *);
193 static void true_poll (int, struct peer *);
194 static void true_send (struct peer *, const char *);
195 static void true_doevent (struct peer *, enum true_event);
198 static u_long true_sample720 (void);
204 struct refclock refclock_true = {
205 true_start, /* start up driver */
206 true_shutdown, /* shut down driver */
207 true_poll, /* transmit poll message */
208 noentry, /* not used (old true_control) */
209 noentry, /* initialize driver (not used) */
210 noentry, /* not used (old true_buginfo) */
211 NOFLAGS /* not used */
215 #if !defined(__STDC__)
216 # define true_debug (void)
220 true_debug(struct peer *peer, const char *fmt, ...)
223 int want_debugging, now_debugging;
224 struct refclockproc *pp;
225 struct true_unit *up;
231 want_debugging = (pp->sloppyclockflag & CLK_FLAG2) != 0;
232 now_debugging = (up->debug != NULL);
233 if (want_debugging != now_debugging)
235 if (want_debugging) {
239 snprintf(filename, sizeof(filename),
240 "/tmp/true%d.debug", up->unit);
241 fd = open(filename, O_CREAT | O_WRONLY | O_EXCL,
243 if (fd >= 0 && (up->debug = fdopen(fd, "w"))) {
245 static char buf[BUFSIZ];
247 setvbuf(up->debug, buf, _IOLBF, BUFSIZ);
249 setlinebuf(up->debug);
259 fprintf(up->debug, "true%d: ", up->unit);
260 vfprintf(up->debug, fmt, ap);
267 * true_start - open the devices and initialize data for processing
275 register struct true_unit *up;
276 struct refclockproc *pp;
283 snprintf(device, sizeof(device), DEVICE, unit);
284 fd = refclock_open(device, SPEED232, LDISC_CLK);
289 * Allocate and initialize unit structure
291 up = emalloc_zero(sizeof(*up));
293 pp->io.clock_recv = true_receive;
294 pp->io.srcclock = peer;
297 if (!io_addclock(&pp->io)) {
306 * Initialize miscellaneous variables
308 peer->precision = PRECISION;
309 pp->clockdesc = DESCRIPTION;
310 memcpy(&pp->refid, REFID, 4);
312 up->type = t_unknown;
316 * Send a CTRL-C character at the start,
317 * just in case the clock is already
320 true_send(peer, "\03\r");
322 true_doevent(peer, e_Init);
329 * true_shutdown - shut down the clock
337 register struct true_unit *up;
338 struct refclockproc *pp;
343 io_closeclock(&pp->io);
350 * true_receive - receive data from the serial interface on a clock
354 struct recvbuf *rbufp
357 register struct true_unit *up;
358 struct refclockproc *pp;
363 int lat, lon, off; /* GOES Satellite position */
364 /* These variables hold data until we decide to keep it */
365 char rd_lastcode[BMAX];
370 * Get the clock this applies to and pointers to the data.
372 peer = rbufp->recv_peer;
377 * Read clock output. Automatically handles STREAMS, CLKLDISC.
379 rd_lencode = refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
380 rd_lastcode[rd_lencode] = '\0';
383 * There is a case where <cr><lf> generates 2 timestamps.
387 pp->lencode = rd_lencode;
388 strlcpy(pp->a_lastcode, rd_lastcode, sizeof(pp->a_lastcode));
389 pp->lastrec = rd_tmp;
390 true_debug(peer, "receive(%s) [%d]\n", pp->a_lastcode,
394 record_clock_stats(&peer->srcadr, pp->a_lastcode);
397 * We get down to business, check the timecode format and decode
398 * its contents. This code decodes a multitude of different
399 * clock messages. Timecodes are processed if needed. All replies
400 * will be run through the state machine to tweak driver options
401 * and program the clock.
405 * Clock misunderstood our last command?
407 if (pp->a_lastcode[0] == '?' ||
408 strcmp(pp->a_lastcode, "ERROR 05 NO SUCH FUNCTION") == 0) {
409 true_doevent(peer, e_Huh);
414 * Timecode: "nnnnn+nnn-nnn"
415 * (from GOES clock when asked about satellite position)
417 if ((pp->a_lastcode[5] == '+' || pp->a_lastcode[5] == '-') &&
418 (pp->a_lastcode[9] == '+' || pp->a_lastcode[9] == '-') &&
419 sscanf(pp->a_lastcode, "%5d%*c%3d%*c%3d", &lon, &lat, &off) == 3
421 const char *label = "Botch!";
424 * This is less than perfect. Call the (satellite)
425 * either EAST or WEST and adjust slop accodingly
426 * Perfectionists would recalculate the exact delay
427 * and adjust accordingly...
429 if (lon > 7000 && lon < 14000) {
431 new_station = GOES_EAST;
434 new_station = GOES_WEST;
438 if (new_station != up->station) {
441 dtemp = pp->fudgetime1;
442 pp->fudgetime1 = pp->fudgetime2;
443 pp->fudgetime2 = dtemp;
444 up->station = new_station;
448 /*refclock_report(peer, CEVNT_BADREPLY);*/
451 true_debug(peer, "GOES: station %s\n", label);
452 true_doevent(peer, e_Satellite);
458 * (from TM/TMD clock when it wants to tell us what it's up to.)
460 if (sscanf(pp->a_lastcode, "F%2d", &i) == 1 && i > 0 && i < 80) {
463 true_doevent(peer, e_F50);
466 true_doevent(peer, e_F51);
469 true_debug(peer, "got F%02d - ignoring\n", i);
476 * Timecode: "VER xx.xx"
477 * (from a TL3 when sent "QV", so id's it during initialization.)
479 if (pp->a_lastcode[0] == 'V' && pp->a_lastcode[1] == 'E' &&
480 pp->a_lastcode[2] == 'R' && pp->a_lastcode[6] == '.') {
481 true_doevent(peer, e_TL3);
482 NLOG(NLOG_CLOCKSTATUS) {
483 msyslog(LOG_INFO, "TL3: %s", pp->a_lastcode);
489 * Timecode: " TRUETIME Mk III" or " TRUETIME XL"
490 * (from a TM/TMD/XL clock during initialization.)
492 if (strncmp(pp->a_lastcode, " TRUETIME Mk III ", 17) == 0 ||
493 strncmp(pp->a_lastcode, " TRUETIME XL", 12) == 0) {
494 true_doevent(peer, e_F18);
495 NLOG(NLOG_CLOCKSTATUS) {
496 msyslog(LOG_INFO, "TM/TMD/XL: %s", pp->a_lastcode);
502 * Timecode: "N03726428W12209421+000033"
504 * index 0123456789012345678901234
505 * (from a TCU during initialization)
507 if ((pp->a_lastcode[0] == 'N' || pp->a_lastcode[0] == 'S') &&
508 (pp->a_lastcode[9] == 'W' || pp->a_lastcode[9] == 'E') &&
509 pp->a_lastcode[18] == '+') {
510 true_doevent(peer, e_Location);
511 NLOG(NLOG_CLOCKSTATUS) {
512 msyslog(LOG_INFO, "TCU-800: %s", pp->a_lastcode);
517 * Timecode: "ddd:hh:mm:ssQ"
519 * index 0123456789012345678901234
520 * (from all clocks supported by this driver.)
522 if (pp->a_lastcode[3] == ':' &&
523 pp->a_lastcode[6] == ':' &&
524 pp->a_lastcode[9] == ':' &&
525 sscanf(pp->a_lastcode, "%3d:%2d:%2d:%2d%c",
526 &pp->day, &pp->hour, &pp->minute,
527 &pp->second, &synced) == 5) {
530 * Adjust the synchronize indicator according to timecode
531 * say were OK, and then say not if we really are not OK
533 if (synced == '>' || synced == '#' || synced == '?'
535 pp->leap = LEAP_NOTINSYNC;
537 pp->leap = LEAP_NOWARNING;
539 true_doevent(peer, e_TS);
542 /* If it's taken more than 65ms to get here, we'll lose. */
543 if ((pp->sloppyclockflag & CLK_FLAG4) && up->pcl720init) {
548 * find out what time it really is. Include
549 * the count from the PCL720
551 if (!clocktime(pp->day, pp->hour, pp->minute,
552 pp->second, GMT, pp->lastrec.l_ui,
553 &pp->yearstart, &off.l_ui)) {
554 refclock_report(peer, CEVNT_BADTIME);
560 pp->usec = true_sample720();
562 TVUTOTSF(pp->usec, off.l_uf);
566 * Stomp all over the timestamp that was pulled out
567 * of the input stream. It's irrelevant since we've
568 * adjusted the input time to reflect now (via pp->usec)
569 * rather than when the data was collected.
571 get_systime(&pp->lastrec);
574 * Create a true offset for feeding to pps_sample()
576 L_SUB(&off, &pp->lastrec);
578 pps_sample(peer, &off);
580 true_debug(peer, "true_sample720: %luus\n", pp->usec);
585 * The clock will blurt a timecode every second but we only
586 * want one when polled. If we havn't been polled, bail out.
591 /* We only call doevent if additional things need be done
592 * at poll interval. Currently, its only for GOES. We also
593 * call it for clock unknown so that it gets logged.
595 if (up->type == t_goes || up->type == t_unknown)
596 true_doevent(peer, e_Poll);
598 if (!refclock_process(pp)) {
599 refclock_report(peer, CEVNT_BADTIME);
603 * If clock is good we send a NOMINAL message so that
604 * any previous BAD messages are nullified
606 pp->lastref = pp->lastrec;
607 refclock_receive(peer);
608 refclock_report(peer, CEVNT_NOMINAL);
611 * We have succedded in answering the poll.
612 * Turn off the flag and return
620 * No match to known timecodes, report failure and return
622 refclock_report(peer, CEVNT_BADREPLY);
628 * true_send - time to send the clock a signal to cough up a time sample
636 struct refclockproc *pp;
639 if (!(pp->sloppyclockflag & CLK_FLAG1)) {
640 int len = strlen(cmd);
642 true_debug(peer, "Send '%s'\n", cmd);
643 if (write(pp->io.fd, cmd, (unsigned)len) != len)
644 refclock_report(peer, CEVNT_FAULT);
652 * state machine for initializing and controlling a clock
657 enum true_event event
660 struct true_unit *up;
661 struct refclockproc *pp;
666 NLOG(NLOG_CLOCKSTATUS) {
667 msyslog(LOG_INFO, "TRUE: clock %s, state %s, event %s",
673 true_debug(peer, "clock %s, state %s, event %s\n",
674 typeStr(up->type), stateStr(up->state), eventStr(event));
678 case e_Init: /* FALLTHROUGH */
681 * Switch back to on-second time codes and return.
683 true_send(peer, "C");
688 * After each poll, check the station (satellite).
690 true_send(peer, "P");
691 /* No state change needed. */
700 true_send(peer, "C");
704 if (up->state != s_Start && up->state != s_Auto) {
705 true_send(peer, "\03\r");
717 true_send(peer, "F18\r");
721 true_send(peer, "F50\r");
723 * Timecode: " TRUETIME Mk III" or " TRUETIME XL"
724 * (from a TM/TMD/XL clock during initialization.)
726 if ( strcmp(pp->a_lastcode, " TRUETIME Mk III") == 0 ||
727 strncmp(pp->a_lastcode, " TRUETIME XL", 12) == 0) {
728 true_doevent(peer, e_F18);
729 NLOG(NLOG_CLOCKSTATUS) {
730 msyslog(LOG_INFO, "TM/TMD/XL: %s",
738 true_send(peer, "F51\r");
742 true_send(peer, "F08\r");
746 if (up->state != s_Start && up->state != s_Auto) {
747 true_send(peer, "\03\r");
759 true_send(peer, "MD3\r"); /* GPS Synch'd Gen. */
760 true_send(peer, "TSU\r"); /* UTC, not GPS. */
761 true_send(peer, "AU\r"); /* Auto Timestamps. */
765 if (up->state != s_Start && up->state != s_Auto) {
766 true_send(peer, "\03\r");
778 true_send(peer, "ST1"); /* Turn on continuous stream */
794 true_send(peer, "P\r");
795 up->state = s_InqGOES;
801 true_doevent(peer, e_Init);
803 case e_Init: /*FALLTHROUGH*/
806 true_send(peer, "ST0"); /* turn off TL3 auto */
807 sleep(1); /* wait for it */
808 up->state = s_InqTL3;
809 true_send(peer, "QV"); /* see if its a TL3 */
819 up->state = s_Auto; /* Inq side-effect. */
820 true_send(peer, "ST1"); /* Turn on 1/sec data */
822 case e_Init: /*FALLTHROUGH*/
824 up->state = s_InqOmega;
825 true_send(peer, "C\r");
828 up->type = t_tl3; /* Already sending data */
833 "TRUE: TL3 init fellthrough! (%d)", event);
841 up->state = s_Auto; /* Inq side-effect. */
843 case e_Init: /*FALLTHROUGH*/
846 true_send(peer, "F18\r");
856 true_doevent(peer, e_Init);
858 case e_Init: /*FALLTHROUGH*/
860 true_send(peer, "PO\r");
861 up->state = s_InqTCU;
865 "TRUE: TM/TMD init fellthrough!");
873 true_doevent(peer, e_Init);
875 case e_Init: /*FALLTHROUGH*/
882 "TRUE: TCU init fellthrough!");
887 * An expedient hack to prevent lint complaints,
888 * these don't actually need to be used here...
896 msyslog(LOG_INFO, "TRUE: state %s is unexpected!",
897 stateStr(up->state));
901 msyslog(LOG_INFO, "TRUE: cannot identify refclock!");
907 if ((pp->sloppyclockflag & CLK_FLAG4) && !up->pcl720init) {
908 /* Make counter trigger on gate0, count down from 65535. */
909 pcl720_load(PCL720_IOB, PCL720_CTR, i8253_oneshot, 65535);
911 * (These constants are OK since
912 * they represent hardware maximums.)
914 NLOG(NLOG_CLOCKINFO) {
915 msyslog(LOG_NOTICE, "PCL-720 initialized");
925 * true_poll - called by the transmit procedure
933 struct true_unit *up;
934 struct refclockproc *pp;
937 * You don't need to poll this clock. It puts out timecodes
938 * once per second. If asked for a timestamp, take note.
939 * The next time a timecode comes in, it will be fed back.
943 if (up->pollcnt > 0) {
946 true_doevent(peer, e_Init);
947 refclock_report(peer, CEVNT_TIMEOUT);
951 * polled every 64 seconds. Ask true_receive to hand in a
960 * true_sample720 - sample the PCL-720
967 /* We wire the PCL-720's 8253.OUT0 to bit 0 of connector 3.
968 * If it is not being held low now, we did not get called
971 if (inb(pcl720_data_16_23(PCL720_IOB)) & 0x01) {
972 NLOG(NLOG_CLOCKINFO) {
973 msyslog(LOG_NOTICE, "PCL-720 out of synch");
977 f = (65536 - pcl720_read(PCL720_IOB, PCL720_CTR));
979 msyslog(LOG_DEBUG, "PCL-720: %luus", f);
986 int refclock_true_bs;
987 #endif /* REFCLOCK */