2 * refclock_heath - clock driver for Heath GC-1000 and and GC-1000 II
8 #if defined(REFCLOCK) && defined(CLOCK_HEATH)
12 #include "ntp_refclock.h"
13 #include "ntp_stdlib.h"
18 #ifdef HAVE_SYS_IOCTL_H
19 # include <sys/ioctl.h>
20 #endif /* not HAVE_SYS_IOCTL_H */
23 * This driver supports the Heath GC-1000 Most Accurate Clock, with
24 * RS232C Output Accessory. This is a WWV/WWVH receiver somewhat less
25 * robust than other supported receivers. Its claimed accuracy is 100 ms
26 * when actually synchronized to the broadcast signal, but this doesn't
27 * happen even most of the time, due to propagation conditions, ambient
28 * noise sources, etc. When not synchronized, the accuracy is at the
29 * whim of the internal clock oscillator, which can wander into the
30 * sunset without warning. Since the indicated precision is 100 ms,
31 * expect a host synchronized only to this thing to wander to and fro,
32 * occasionally being rudely stepped when the offset exceeds the default
33 * clock_max of 128 ms.
35 * There are two GC-1000 versions supported by this driver. The original
36 * GC-1000 with RS-232 output first appeared in 1983, but dissapeared
37 * from the market a few years later. The GC-1000 II with RS-232 output
38 * first appeared circa 1990, but apparently is no longer manufactured.
39 * The two models differ considerably, both in interface and commands.
40 * The GC-1000 has a pseudo-bipolar timecode output triggered by a RTS
41 * transition. The timecode includes both the day of year and time of
42 * day. The GC-1000 II has a true bipolar output and a complement of
43 * single character commands. The timecode includes only the time of
48 * The internal DIPswitches should be set to operate in MANUAL mode. The
49 * external DIPswitches should be set to GMT and 24-hour format.
51 * In MANUAL mode the clock responds to a rising edge of the request to
52 * send (RTS) modem control line by sending the timecode. Therefore, it
53 * is necessary that the operating system implement the TIOCMBIC and
54 * TIOCMBIS ioctl system calls and TIOCM_RTS control bit. Present
55 * restrictions require the use of a POSIX-compatible programming
56 * interface, although other interfaces may work as well.
58 * A simple hardware modification to the clock can be made which
59 * prevents the clock hearing the request to send (RTS) if the HI SPEC
60 * lamp is out. Route the HISPEC signal to the tone decoder board pin
61 * 19, from the display, pin 19. Isolate pin 19 of the decoder board
62 * first, but maintain connection with pin 10. Also isolate pin 38 of
63 * the CPU on the tone board, and use half an added 7400 to gate the
64 * original signal to pin 38 with that from pin 19.
66 * The clock message consists of 23 ASCII printing characters in the
69 * hh:mm:ss.f AM dd/mm/yr<cr>
71 * hh:mm:ss.f = hours, minutes, seconds
72 * f = deciseconds ('?' when out of spec)
73 * AM/PM/bb = blank in 24-hour mode
74 * dd/mm/yr = day, month, year
76 * The alarm condition is indicated by '?', rather than a digit, at f.
77 * Note that 0?:??:??.? is displayed before synchronization is first
78 * established and hh:mm:ss.? once synchronization is established and
79 * then lost again for about a day.
83 * Commands consist of a single letter and are case sensitive. When
84 * enterred in lower case, a description of the action performed is
85 * displayed. When enterred in upper case the action is performed.
86 * Following is a summary of descriptions as displayed by the clock:
88 * The clock responds with a command The 'A' command returns an ASCII
89 * local time string: HH:MM:SS.T xx<CR>, where
94 * T = tenths-of-seconds
95 * xx = 'AM', 'PM', or ' '
96 * <CR> = carriage return
98 * The 'D' command returns 24 pairs of bytes containing the variable
99 * divisor value at the end of each of the previous 24 hours. This
100 * allows the timebase trimming process to be observed. UTC hour 00 is
101 * always returned first. The first byte of each pair is the high byte
102 * of (variable divisor * 16); the second byte is the low byte of
103 * (variable divisor * 16). For example, the byte pair 3C 10 would be
104 * returned for a divisor of 03C1 hex (961 decimal).
106 * The 'I' command returns: | TH | TL | ER | DH | DL | U1 | I1 | I2 | ,
109 * TH = minutes since timebase last trimmed (high byte)
110 * TL = minutes since timebase last trimmed (low byte)
111 * ER = last accumulated error in 1.25 ms increments
112 * DH = high byte of (current variable divisor * 16)
113 * DL = low byte of (current variable divisor * 16)
114 * U1 = UT1 offset (/.1 s): | + | 4 | 2 | 1 | 0 | 0 | 0 | 0 |
115 * I1 = information byte 1: | W | C | D | I | U | T | Z | 1 | ,
127 * I2 = information byte 2: | 8 | 8 | 4 | 2 | 1 | D | d | S | ,
130 * 8, 8, 4, 2, 1 = TIME ZONE switch settings
131 * D = DST bit (#55) in last-received frame
132 * d = DST bit (#2) in last-received frame
133 * S = clock is in simulation mode
135 * The 'P' command returns 24 bytes containing the number of frames
136 * received without error during UTC hours 00 through 23, providing an
137 * indication of hourly propagation. These bytes are updated each hour
138 * to reflect the previous 24 hour period. UTC hour 00 is always
141 * The 'T' command returns the UTC time: | HH | MM | SS | T0 | , where
142 * HH = tens-of-hours and hours (packed BCD)
143 * MM = tens-of-minutes and minutes (packed BCD)
144 * SS = tens-of-seconds and seconds (packed BCD)
145 * T = tenths-of-seconds (BCD)
149 * A fudge time1 value of .04 s appears to center the clock offset
150 * residuals. The fudge time2 parameter is the local time offset east of
151 * Greenwich, which depends on DST. Sorry about that, but the clock
152 * gives no hint on what the DIPswitches say.
156 * Interface definitions
158 #define DEVICE "/dev/heath%d" /* device name and unit */
159 #define PRECISION (-4) /* precision assumed (about 100 ms) */
160 #define REFID "WWV\0" /* reference ID */
161 #define DESCRIPTION "Heath GC-1000 Most Accurate Clock" /* WRU */
163 #define LENHEATH1 23 /* min timecode length */
164 #define LENHEATH2 13 /* min timecode length */
167 * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
170 static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
171 static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
174 * Baud rate table. The GC-1000 supports 1200, 2400 and 4800; the
175 * GC-1000 II supports only 9600.
177 static int speed[] = {B1200, B2400, B4800, B9600};
180 * Unit control structure
183 int pollcnt; /* poll message counter */
184 l_fp tstamp; /* timestamp of last poll */
188 * Function prototypes
190 static int heath_start P((int, struct peer *));
191 static void heath_shutdown P((int, struct peer *));
192 static void heath_receive P((struct recvbuf *));
193 static void heath_poll P((int, struct peer *));
198 struct refclock refclock_heath = {
199 heath_start, /* start up driver */
200 heath_shutdown, /* shut down driver */
201 heath_poll, /* transmit poll message */
202 noentry, /* not used (old heath_control) */
203 noentry, /* initialize driver */
204 noentry, /* not used (old heath_buginfo) */
205 NOFLAGS /* not used */
210 * heath_start - open the devices and initialize data for processing
218 register struct heathunit *up;
219 struct refclockproc *pp;
226 (void)sprintf(device, DEVICE, unit);
227 if (!(fd = refclock_open(device, speed[peer->ttlmax & 0x3], 0)))
231 * Allocate and initialize unit structure
233 if (!(up = (struct heathunit *)
234 emalloc(sizeof(struct heathunit)))) {
238 memset((char *)up, 0, sizeof(struct heathunit));
240 pp->io.clock_recv = heath_receive;
241 pp->io.srcclock = (caddr_t)peer;
244 if (!io_addclock(&pp->io)) {
249 pp->unitptr = (caddr_t)up;
252 * Initialize miscellaneous variables
254 peer->precision = PRECISION;
255 peer->burst = NSTAGE;
256 pp->clockdesc = DESCRIPTION;
257 memcpy((char *)&pp->refid, REFID, 4);
264 * heath_shutdown - shut down the clock
272 register struct heathunit *up;
273 struct refclockproc *pp;
276 up = (struct heathunit *)pp->unitptr;
277 io_closeclock(&pp->io);
283 * heath_receive - receive data from the serial interface
287 struct recvbuf *rbufp
290 register struct heathunit *up;
291 struct refclockproc *pp;
299 * Initialize pointers and read the timecode and timestamp
301 peer = (struct peer *)rbufp->recv_srcclock;
303 up = (struct heathunit *)pp->unitptr;
304 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
308 * We get a buffer and timestamp for each <cr>; however, we use
309 * the timestamp captured at the RTS modem control line toggle
310 * on the assumption that's what the radio bases the timecode
311 * on. Apparently, the radio takes about a second to make up its
312 * mind to send a timecode, so the receive timestamp is
315 pp->lastrec = up->tstamp;
319 printf("heath: timecode %d %s\n", pp->lencode,
324 * We get down to business, check the timecode format and decode
325 * its contents. If the timecode has invalid length or is not in
326 * proper format, we declare bad format and exit.
328 switch (pp->lencode) {
331 * GC-1000 timecode format: "hh:mm:ss.f AM mm/dd/yy"
332 * GC-1000 II timecode format: "hh:mm:ss.f "
335 if (sscanf(pp->a_lastcode,
336 "%2d:%2d:%2d.%c%5c%2d/%2d/%2d", &pp->hour,
337 &pp->minute, &pp->second, &dsec, a, &month, &day,
339 refclock_report(peer, CEVNT_BADREPLY);
345 * GC-1000 II timecode format: "hh:mm:ss.f "
348 if (sscanf(pp->a_lastcode, "%2d:%2d:%2d.%c", &pp->hour,
349 &pp->minute, &pp->second, &dsec) != 4) {
350 refclock_report(peer, CEVNT_BADREPLY);
356 refclock_report(peer, CEVNT_BADREPLY);
361 * We determine the day of the year from the DIPswitches. This
362 * should be fixed, since somebody might forget to set them.
363 * Someday this hazard will be fixed by a fiendish scheme that
364 * looks at the timecode and year the radio shows, then computes
365 * the residue of the seconds mod the seconds in a leap cycle.
366 * If in the third year of that cycle and the third and later
367 * months of that year, add one to the day. Then, correct the
368 * timecode accordingly. Icky pooh. This bit of nonsense could
369 * be avoided if the engineers had been required to write a
370 * device driver before finalizing the timecode format.
372 if (month < 1 || month > 12 || day < 1) {
373 refclock_report(peer, CEVNT_BADTIME);
377 if (day > day1tab[month - 1]) {
378 refclock_report(peer, CEVNT_BADTIME);
381 for (i = 0; i < month - 1; i++)
384 if (day > day2tab[month - 1]) {
385 refclock_report(peer, CEVNT_BADTIME);
388 for (i = 0; i < month - 1; i++)
394 * Determine synchronization and last update
396 if (!isdigit((int)dsec))
397 pp->leap = LEAP_NOTINSYNC;
399 pp->msec = (dsec - '0') * 100;
400 pp->leap = LEAP_NOWARNING;
402 if (!refclock_process(pp))
403 refclock_report(peer, CEVNT_BADTIME);
408 * heath_poll - called by the transmit procedure
416 register struct heathunit *up;
417 struct refclockproc *pp;
418 int bits = TIOCM_RTS;
421 * At each poll we check for timeout and toggle the RTS modem
422 * control line, then take a timestamp. Presumably, this is the
423 * event the radio captures to generate the timecode.
426 up = (struct heathunit *)pp->unitptr;
430 * We toggle the RTS modem control lead (GC-1000) and sent a T
431 * (GC-1000 II) to kick a timecode loose from the radio. This
432 * code works only for POSIX and SYSV interfaces. With bsd you
433 * are on your own. We take a timestamp between the up and down
434 * edges to lengthen the pulse, which should be about 50 usec on
435 * a Sun IPC. With hotshot CPUs, the pulse might get too short.
438 if (ioctl(pp->io.fd, TIOCMBIC, (char *)&bits) < 0)
439 refclock_report(peer, CEVNT_FAULT);
440 get_systime(&up->tstamp);
441 ioctl(pp->io.fd, TIOCMBIS, (char *)&bits);
442 if (write(pp->io.fd, "T", 1) != 1)
443 refclock_report(peer, CEVNT_FAULT);
446 if (pp->coderecv == pp->codeproc) {
447 refclock_report(peer, CEVNT_TIMEOUT);
450 record_clock_stats(&peer->srcadr, pp->a_lastcode);
451 refclock_receive(peer);
452 peer->burst = NSTAGE;
456 int refclock_heath_bs;
457 #endif /* REFCLOCK */