2 * refclock_true - clock driver for the Kinemetrics Truetime receivers
3 * Receiver Version 3.0C - tested plain, with CLKLDISC
4 * Developement work being done:
5 * - Properly handle varying satellite positions (more acurately)
6 * - Integrate GPSTM and/or OMEGA and/or TRAK and/or ??? drivers
13 #if defined(REFCLOCK) && defined(CLOCK_TRUETIME)
17 #include "ntp_refclock.h"
18 #include "ntp_unixtime.h"
19 #include "ntp_stdlib.h"
24 /* This should be an atom clock but those are very hard to build.
26 * The PCL720 from P C Labs has an Intel 8253 lookalike, as well as a bunch
27 * of TTL input and output pins, all brought out to the back panel. If you
28 * wire a PPS signal (such as the TTL PPS coming out of a GOES or other
29 * Kinemetrics/Truetime clock) to the 8253's GATE0, and then also wire the
30 * 8253's OUT0 to the PCL720's INPUT3.BIT0, then we can read CTR0 to get the
31 * number of uSecs since the last PPS upward swing, mediated by reading OUT0
32 * to find out if the counter has wrapped around (this happens if more than
33 * 65535us (65ms) elapses between the PPS event and our being called.)
38 # include <machine/inline.h>
39 # include <sys/pcl720.h>
40 # include <sys/i8253.h>
41 # define PCL720_IOB 0x2a0 /* XXX */
42 # define PCL720_CTR 0 /* XXX */
46 * Support for Kinemetrics Truetime Receivers
49 * XL-DC (a 151-602-210, reported by the driver as a GPS/TM-TMD)
50 * GPS-800 TCU (an 805-957 with the RS232 Talker/Listener module)
51 * OM-DC: getting stale ("OMEGA")
53 * Most of this code is originally from refclock_wwvb.c with thanks.
54 * It has been so mangled that wwvb is not a recognizable ancestor.
56 * Timcode format: ADDD:HH:MM:SSQCL
57 * A - control A (this is stripped before we see it)
58 * Q - Quality indication (see below)
62 * Quality codes indicate possible error of
63 * 468-DC GOES Receiver:
64 * GPS-TM/TMD Receiver: (default quality codes for XL-DC)
65 * ? +/- 1 milliseconds # +/- 100 microseconds
66 * * +/- 10 microseconds . +/- 1 microsecond
67 * space less than 1 microsecond
68 * OM-DC OMEGA Receiver: (default quality codes for OMEGA)
69 * WARNING OMEGA navigation system is no longer existent
71 * ? >+/- 500 milliseconds # >+/- 50 milliseconds
72 * * >+/- 5 milliseconds . >+/- 1 millisecond
73 * A-H less than 1 millisecond. Character indicates which station
74 * is being received as follows:
75 * A = Norway, B = Liberia, C = Hawaii, D = North Dakota,
76 * E = La Reunion, F = Argentina, G = Australia, H = Japan.
78 * The carriage return start bit begins on 0 seconds and extends to 1 bit time.
80 * Notes on 468-DC and OMEGA receiver:
82 * Send the clock a 'R' or 'C' and once per second a timestamp will
83 * appear. Send a 'P' to get the satellite position once (GOES only.)
85 * Notes on the 468-DC receiver:
87 * Since the old east/west satellite locations are only historical, you can't
88 * set your clock propagation delay settings correctly and still use
89 * automatic mode. The manual says to use a compromise when setting the
90 * switches. This results in significant errors. The solution; use fudge
91 * time1 and time2 to incorporate corrections. If your clock is set for
92 * 50 and it should be 58 for using the west and 46 for using the east,
95 * fudge 127.127.5.0 time1 +0.008 time2 -0.004
97 * This corrects the 4 milliseconds advance and 8 milliseconds retard
98 * needed. The software will ask the clock which satellite it sees.
100 * Ntp.conf parameters:
101 * time1 - offset applied to samples when reading WEST satellite (default = 0)
102 * time2 - offset applied to samples when reading EAST satellite (default = 0)
103 * val1 - stratum to assign to this clock (default = 0)
104 * val2 - refid assigned to this clock (default = "TRUE", see below)
105 * flag1 - will silence the clock side of ntpd, just reading the clock
106 * without trying to write to it. (default = 0)
107 * flag2 - generate a debug file /tmp/true%d.
108 * flag3 - enable ppsclock streams module
109 * flag4 - use the PCL-720 (BSD/OS only)
116 #define DEVICE "/dev/true%d"
117 #define SPEED232 B9600 /* 9600 baud */
120 * Radio interface parameters
122 #define PRECISION (-10) /* precision assumed (about 1 ms) */
123 #define REFID "TRUE" /* reference id */
124 #define DESCRIPTION "Kinemetrics/TrueTime Receiver"
127 * Tags which station (satellite) we see
129 #define GOES_WEST 0 /* Default to WEST satellite and apply time1 */
130 #define GOES_EAST 1 /* until you discover otherwise */
133 * used by the state machine
135 enum true_event {e_Init, e_Huh, e_F18, e_F50, e_F51, e_Satellite,
136 e_Poll, e_Location, e_TS, e_Max};
137 const char *events[] = {"Init", "Huh", "F18", "F50", "F51", "Satellite",
138 "Poll", "Location", "TS"};
139 #define eventStr(x) (((int)x<(int)e_Max) ? events[(int)x] : "?")
141 enum true_state {s_Base, s_InqTM, s_InqTCU, s_InqOmega, s_InqGOES,
142 s_Init, s_F18, s_F50, s_Start, s_Auto, s_Max};
143 const char *states[] = {"Base", "InqTM", "InqTCU", "InqOmega", "InqGOES",
144 "Init", "F18", "F50", "Start", "Auto"};
145 #define stateStr(x) (((int)x<(int)s_Max) ? states[(int)x] : "?")
147 enum true_type {t_unknown, t_goes, t_tm, t_tcu, t_omega, t_Max};
148 const char *types[] = {"unknown", "goes", "tm", "tcu", "omega"};
149 #define typeStr(x) (((int)x<(int)t_Max) ? types[(int)x] : "?")
152 * unit control structure
155 unsigned int pollcnt; /* poll message counter */
156 unsigned int station; /* which station we are on */
157 unsigned int polled; /* Hand in a time sample? */
158 enum true_state state; /* state machine */
159 enum true_type type; /* what kind of clock is it? */
160 int unit; /* save an extra copy of this */
161 FILE *debug; /* debug logging file */
163 int pcl720init; /* init flag for PCL 720 */
168 * Function prototypes
170 static int true_start P((int, struct peer *));
171 static void true_shutdown P((int, struct peer *));
172 static void true_receive P((struct recvbuf *));
173 static void true_poll P((int, struct peer *));
174 static void true_send P((struct peer *, const char *));
175 static void true_doevent P((struct peer *, enum true_event));
178 static u_long true_sample720 P((void));
184 struct refclock refclock_true = {
185 true_start, /* start up driver */
186 true_shutdown, /* shut down driver */
187 true_poll, /* transmit poll message */
188 noentry, /* not used (old true_control) */
189 noentry, /* initialize driver (not used) */
190 noentry, /* not used (old true_buginfo) */
191 NOFLAGS /* not used */
195 #if !defined(__STDC__)
196 # define true_debug (void)
199 true_debug(struct peer *peer, const char *fmt, ...)
202 int want_debugging, now_debugging;
203 struct refclockproc *pp;
204 struct true_unit *up;
208 up = (struct true_unit *)pp->unitptr;
210 want_debugging = (pp->sloppyclockflag & CLK_FLAG2) != 0;
211 now_debugging = (up->debug != NULL);
212 if (want_debugging != now_debugging)
214 if (want_debugging) {
218 snprintf(filename, sizeof(filename), "/tmp/true%d.debug", up->unit);
219 fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
220 if (fd >= 0 && (up->debug = fdopen(fd, "r+"))) {
222 static char buf[BUFSIZ];
223 setvbuf(up->debug, buf, _IOLBF, BUFSIZ);
225 setlinebuf(up->debug);
235 fprintf(up->debug, "true%d: ", up->unit);
236 vfprintf(up->debug, fmt, ap);
242 * true_start - open the devices and initialize data for processing
250 register struct true_unit *up;
251 struct refclockproc *pp;
258 (void)snprintf(device, sizeof(device), DEVICE, unit);
259 if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
263 * Allocate and initialize unit structure
265 if (!(up = (struct true_unit *)
266 emalloc(sizeof(struct true_unit)))) {
270 memset((char *)up, 0, sizeof(struct true_unit));
272 pp->io.clock_recv = true_receive;
273 pp->io.srcclock = (caddr_t)peer;
276 if (!io_addclock(&pp->io)) {
281 pp->unitptr = (caddr_t)up;
284 * Initialize miscellaneous variables
286 peer->precision = PRECISION;
287 pp->clockdesc = DESCRIPTION;
288 memcpy((char *)&pp->refid, REFID, 4);
290 up->type = t_unknown;
292 true_doevent(peer, e_Init);
297 * true_shutdown - shut down the clock
305 register struct true_unit *up;
306 struct refclockproc *pp;
309 up = (struct true_unit *)pp->unitptr;
310 io_closeclock(&pp->io);
316 * true_receive - receive data from the serial interface on a clock
320 struct recvbuf *rbufp
323 register struct true_unit *up;
324 struct refclockproc *pp;
329 int lat, lon, off; /* GOES Satellite position */
330 /* Use these variable to hold data until we decide its worth keeping */
331 char rd_lastcode[BMAX];
336 * Get the clock this applies to and pointers to the data.
338 peer = (struct peer *)rbufp->recv_srcclock;
340 up = (struct true_unit *)pp->unitptr;
343 * Read clock output. Automatically handles STREAMS, CLKLDISC.
345 rd_lencode = refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
346 rd_lastcode[rd_lencode] = '\0';
349 * There is a case where <cr><lf> generates 2 timestamps.
353 pp->lencode = rd_lencode;
354 strcpy(pp->a_lastcode, rd_lastcode);
355 pp->lastrec = rd_tmp;
356 true_debug(peer, "receive(%s) [%d]\n", pp->a_lastcode, pp->lencode);
359 record_clock_stats(&peer->srcadr, pp->a_lastcode);
362 * We get down to business, check the timecode format and decode
363 * its contents. This code decodes a multitude of different
364 * clock messages. Timecodes are processed if needed. All replies
365 * will be run through the state machine to tweak driver options
366 * and program the clock.
370 * Clock misunderstood our last command?
372 if (pp->a_lastcode[0] == '?' ||
373 strcmp(pp->a_lastcode, "ERROR 05 NO SUCH FUNCTION") == 0) {
374 true_doevent(peer, e_Huh);
379 * Timecode: "nnnnn+nnn-nnn"
380 * (from GOES clock when asked about satellite position)
382 if ((pp->a_lastcode[5] == '+' || pp->a_lastcode[5] == '-') &&
383 (pp->a_lastcode[9] == '+' || pp->a_lastcode[9] == '-') &&
384 sscanf(pp->a_lastcode, "%5d%*c%3d%*c%3d", &lon, &lat, &off) == 3
386 const char *label = "Botch!";
389 * This is less than perfect. Call the (satellite)
390 * either EAST or WEST and adjust slop accodingly
391 * Perfectionists would recalculate the exact delay
392 * and adjust accordingly...
394 if (lon > 7000 && lon < 14000) {
396 new_station = GOES_EAST;
399 new_station = GOES_WEST;
403 if (new_station != up->station) {
406 dtemp = pp->fudgetime1;
407 pp->fudgetime1 = pp->fudgetime2;
408 pp->fudgetime2 = dtemp;
409 up->station = new_station;
413 /*refclock_report(peer, CEVNT_BADREPLY);*/
416 true_debug(peer, "GOES: station %s\n", label);
417 true_doevent(peer, e_Satellite);
423 * (from TM/TMD clock when it wants to tell us what it's up to.)
425 if (sscanf(pp->a_lastcode, "F%2d", &i) == 1 && i > 0 && i < 80) {
428 true_doevent(peer, e_F50);
431 true_doevent(peer, e_F51);
434 true_debug(peer, "got F%02d - ignoring\n", i);
441 * Timecode: " TRUETIME Mk III" or " TRUETIME XL"
442 * (from a TM/TMD/XL clock during initialization.)
444 if (strcmp(pp->a_lastcode, " TRUETIME Mk III") == 0 ||
445 strncmp(pp->a_lastcode, " TRUETIME XL", 12) == 0) {
446 true_doevent(peer, e_F18);
447 NLOG(NLOG_CLOCKSTATUS) {
448 msyslog(LOG_INFO, "TM/TMD/XL: %s", pp->a_lastcode);
454 * Timecode: "N03726428W12209421+000033"
456 * 0123456789012345678901234
457 * (from a TCU during initialization)
459 if ((pp->a_lastcode[0] == 'N' || pp->a_lastcode[0] == 'S') &&
460 (pp->a_lastcode[9] == 'W' || pp->a_lastcode[9] == 'E') &&
461 pp->a_lastcode[18] == '+') {
462 true_doevent(peer, e_Location);
463 NLOG(NLOG_CLOCKSTATUS) {
464 msyslog(LOG_INFO, "TCU-800: %s", pp->a_lastcode);
469 * Timecode: "ddd:hh:mm:ssQ"
470 * (from all clocks supported by this driver.)
472 if (pp->a_lastcode[3] == ':' &&
473 pp->a_lastcode[6] == ':' &&
474 pp->a_lastcode[9] == ':' &&
475 sscanf(pp->a_lastcode, "%3d:%2d:%2d:%2d%c",
476 &pp->day, &pp->hour, &pp->minute,
477 &pp->second, &synced) == 5) {
480 * Adjust the synchronize indicator according to timecode
481 * say were OK, and then say not if we really are not OK
483 if (synced == '>' || synced == '#' || synced == '?')
484 pp->leap = LEAP_NOTINSYNC;
486 pp->leap = LEAP_NOWARNING;
488 true_doevent(peer, e_TS);
491 /* If it's taken more than 65ms to get here, we'll lose. */
492 if ((pp->sloppyclockflag & CLK_FLAG4) && up->pcl720init) {
497 * find out what time it really is. Include
498 * the count from the PCL720
500 if (!clocktime(pp->day, pp->hour, pp->minute,
501 pp->second, GMT, pp->lastrec.l_ui,
502 &pp->yearstart, &off.l_ui)) {
503 refclock_report(peer, CEVNT_BADTIME);
509 pp->usec = true_sample720();
511 TVUTOTSF(pp->usec, off.l_uf);
515 * Stomp all over the timestamp that was pulled out
516 * of the input stream. It's irrelevant since we've
517 * adjusted the input time to reflect now (via pp->usec)
518 * rather than when the data was collected.
520 get_systime(&pp->lastrec);
523 * Create a true offset for feeding to pps_sample()
525 L_SUB(&off, &pp->lastrec);
527 pps_sample(peer, &off);
529 true_debug(peer, "true_sample720: %luus\n", pp->usec);
534 * The clock will blurt a timecode every second but we only
535 * want one when polled. If we havn't been polled, bail out.
540 true_doevent(peer, e_Poll);
541 if (!refclock_process(pp)) {
542 refclock_report(peer, CEVNT_BADTIME);
546 * If clock is good we send a NOMINAL message so that
547 * any previous BAD messages are nullified
549 pp->lastref = pp->lastrec;
550 refclock_receive(peer);
551 refclock_report(peer, CEVNT_NOMINAL);
554 * We have succedded in answering the poll.
555 * Turn off the flag and return
563 * No match to known timecodes, report failure and return
565 refclock_report(peer, CEVNT_BADREPLY);
571 * true_send - time to send the clock a signal to cough up a time sample
579 struct refclockproc *pp;
582 if (!(pp->sloppyclockflag & CLK_FLAG1)) {
583 register int len = strlen(cmd);
585 true_debug(peer, "Send '%s'\n", cmd);
586 if (write(pp->io.fd, cmd, (unsigned)len) != len)
587 refclock_report(peer, CEVNT_FAULT);
595 * state machine for initializing and controlling a clock
600 enum true_event event
603 struct true_unit *up;
604 struct refclockproc *pp;
607 up = (struct true_unit *)pp->unitptr;
609 NLOG(NLOG_CLOCKSTATUS) {
610 msyslog(LOG_INFO, "TRUE: clock %s, state %s, event %s",
616 true_debug(peer, "clock %s, state %s, event %s\n",
617 typeStr(up->type), stateStr(up->state), eventStr(event));
621 case e_Init: /* FALLTHROUGH */
624 * Switch back to on-second time codes and return.
626 true_send(peer, "C");
631 * After each poll, check the station (satellite).
633 true_send(peer, "P");
634 /* No state change needed. */
643 true_send(peer, "C");
647 if (up->state != s_Start && up->state != s_Auto) {
648 true_send(peer, "\03\r");
660 true_send(peer, "F18\r");
664 true_send(peer, "F50\r");
668 true_send(peer, "F51\r");
672 true_send(peer, "F08\r");
676 if (up->state != s_Start && up->state != s_Auto) {
677 true_send(peer, "\03\r");
689 true_send(peer, "MD3\r"); /* GPS Synch'd Gen. */
690 true_send(peer, "TSU\r"); /* UTC, not GPS. */
691 true_send(peer, "AU\r"); /* Auto Timestamps. */
695 if (up->state != s_Start && up->state != s_Auto) {
696 true_send(peer, "\03\r");
710 true_send(peer, "P\r");
711 up->state = s_InqGOES;
717 true_doevent(peer, e_Init);
719 case e_Init: /*FALLTHROUGH*/
720 case e_Huh: /*FALLTHROUGH*/
722 up->state = s_InqOmega;
723 true_send(peer, "C\r");
733 up->state = s_Auto; /* Inq side-effect. */
735 case e_Init: /*FALLTHROUGH*/
738 true_send(peer, "F18\r");
748 true_doevent(peer, e_Init);
750 case e_Init: /*FALLTHROUGH*/
752 true_send(peer, "PO\r");
753 up->state = s_InqTCU;
763 true_doevent(peer, e_Init);
765 case e_Init: /*FALLTHROUGH*/
775 * An expedient hack to prevent lint complaints,
776 * these don't actually need to be used here...
784 msyslog(LOG_INFO, "TRUE: state %s is unexpected!", stateStr(up->state));
793 if ((pp->sloppyclockflag & CLK_FLAG4) && !up->pcl720init) {
794 /* Make counter trigger on gate0, count down from 65535. */
795 pcl720_load(PCL720_IOB, PCL720_CTR, i8253_oneshot, 65535);
797 * (These constants are OK since
798 * they represent hardware maximums.)
800 NLOG(NLOG_CLOCKINFO) {
801 msyslog(LOG_NOTICE, "PCL-720 initialized");
811 * true_poll - called by the transmit procedure
819 struct true_unit *up;
820 struct refclockproc *pp;
823 * You don't need to poll this clock. It puts out timecodes
824 * once per second. If asked for a timestamp, take note.
825 * The next time a timecode comes in, it will be fed back.
828 up = (struct true_unit *)pp->unitptr;
832 true_doevent(peer, e_Init);
833 refclock_report(peer, CEVNT_TIMEOUT);
837 * polled every 64 seconds. Ask true_receive to hand in a
846 * true_sample720 - sample the PCL-720
853 /* We wire the PCL-720's 8253.OUT0 to bit 0 of connector 3.
854 * If it is not being held low now, we did not get called
857 if (inb(pcl720_data_16_23(PCL720_IOB)) & 0x01) {
858 NLOG(NLOG_CLOCKINFO) {
859 msyslog(LOG_NOTICE, "PCL-720 out of synch");
863 f = (65536 - pcl720_read(PCL720_IOB, PCL720_CTR));
865 msyslog(LOG_DEBUG, "PCL-720: %luus", f);
872 int refclock_true_bs;
873 #endif /* REFCLOCK */