2 * refclock_tt560 - clock driver for the TrueTime 560 IRIG-B decoder
9 #if defined(REFCLOCK) && defined(CLOCK_TT560)
13 #include "ntp_refclock.h"
14 #include "ntp_unixtime.h"
15 #include "sys/tt560_api.h"
16 #include "ntp_stdlib.h"
22 * This driver supports the TrueTime 560 IRIG-B decoder for the PCI bus.
26 * TT560 interface definitions
28 #define DEVICE "/dev/tt560%d" /* device name and unit */
29 #define PRECISION (-20) /* precision assumed (1 us) */
30 #define REFID "IRIG" /* reference ID */
31 #define DESCRIPTION "TrueTime 560 IRIG-B PCI Decoder"
34 * Unit control structure
37 tt_mem_space_t *tt_mem; /* mapped address of PCI board */
38 time_freeze_reg_t tt560rawt; /* data returned from PCI board */
41 typedef union byteswap_u
43 unsigned int long_word;
44 unsigned char byte[4];
50 static int tt560_start P((int, struct peer *));
51 static void tt560_shutdown P((int, struct peer *));
52 static void tt560_poll P((int unit, struct peer *));
57 struct refclock refclock_tt560 = {
58 tt560_start, /* clock_start */
59 tt560_shutdown, /* clock_shutdown */
60 tt560_poll, /* clock_poll */
61 noentry, /* clock_control (not used) */
62 noentry, /* clock_init (not used) */
63 noentry, /* clock_buginfo (not used) */
64 NOFLAGS /* clock_flags (not used) */
69 * tt560_start - open the TT560 device and initialize data for processing
77 register struct tt560unit *up;
78 struct refclockproc *pp;
86 (void)sprintf(device, DEVICE, unit);
87 fd = open(device, O_RDWR);
89 msyslog(LOG_ERR, "tt560_start: open of %s: %m", device);
94 * Map the device registers into user space.
96 membase = mmap ((caddr_t) 0, TTIME_MEMORY_SIZE,
97 PROT_READ | PROT_WRITE,
98 MAP_SHARED, fd, (off_t)0);
100 if (membase == (caddr_t) -1) {
101 msyslog(LOG_ERR, "tt560_start: mapping of %s: %m", device);
107 * Allocate and initialize unit structure
109 if (!(up = (struct tt560unit *) emalloc(sizeof(struct tt560unit)))) {
113 memset((char *)up, 0, sizeof(struct tt560unit));
114 up->tt_mem = (tt_mem_space_t *)membase;
116 pp->io.clock_recv = noentry;
117 pp->io.srcclock = (caddr_t)peer;
120 pp->unitptr = (caddr_t)up;
123 * Initialize miscellaneous peer variables
125 peer->precision = PRECISION;
126 peer->burst = NSTAGE;
127 pp->clockdesc = DESCRIPTION;
128 memcpy((char *)&pp->refid, REFID, 4);
134 * tt560_shutdown - shut down the clock
142 register struct tt560unit *up;
143 struct refclockproc *pp;
146 up = (struct tt560unit *)pp->unitptr;
147 io_closeclock(&pp->io);
153 * tt560_poll - called by the transmit procedure
161 register struct tt560unit *up;
162 struct refclockproc *pp;
163 time_freeze_reg_t *tp;
167 unsigned int *p_time_t, *tt_mem_t;
170 * This is the main routine. It snatches the time from the TT560
171 * board and tacks on a local timestamp.
174 up = (struct tt560unit *)pp->unitptr;
178 p_time_t = (unsigned int *)tp;
179 tt_mem_t = (unsigned int *)&mp->time_freeze_reg;
181 *tt_mem_t = 0; /* update the time freeze register */
182 /* and copy time stamp to memory */
183 for (i=0; i < TIME_FREEZE_REG_LEN; i++) {
184 *p_time_t = byte_swap(*tt_mem_t);
189 get_systime(&pp->lastrec);
193 * We get down to business, check the timecode format and decode
194 * its contents. If the timecode has invalid length or is not in
195 * proper format, we declare bad format and exit. Note: we
196 * can't use the sec/usec conversion produced by the driver,
197 * since the year may be suspect. All format error checking is
198 * done by the sprintf() and sscanf() routines.
200 sprintf(pp->a_lastcode,
201 "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x",
202 tp->hun_day, tp->tens_day, tp->unit_day,
203 tp->tens_hour, tp->unit_hour,
204 tp->tens_min, tp->unit_min,
205 tp->tens_sec, tp->unit_sec,
206 tp->hun_ms, tp->tens_ms, tp->unit_ms,
207 tp->hun_us, tp->tens_us, tp->unit_us,
209 pp->lencode = strlen(pp->a_lastcode);
212 printf("tt560: time %s timecode %d %s\n",
213 ulfptoa(&pp->lastrec, 6), pp->lencode,
216 if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld",
217 &pp->day, &pp->hour, &pp->minute, &pp->second, &pp->usec)
219 refclock_report(peer, CEVNT_BADTIME);
222 if ((tp->status & 0x6) != 0x6)
223 pp->leap = LEAP_NOTINSYNC;
225 pp->leap = LEAP_NOWARNING;
226 if (!refclock_process(pp)) {
227 refclock_report(peer, CEVNT_BADTIME);
232 if (pp->coderecv == pp->codeproc) {
233 refclock_report(peer, CEVNT_TIMEOUT);
236 record_clock_stats(&peer->srcadr, pp->a_lastcode);
237 refclock_receive(peer);
238 peer->burst = NSTAGE;
241 /******************************************************************
245 * Inputs: 32 bit integer
247 * Output: byte swapped 32 bit integer.
249 * This routine is used to compensate for the byte alignment
250 * differences between big-endian and little-endian integers.
252 ******************************************************************/
254 byte_swap(unsigned int input_num)
256 byteswap_t byte_swap;
259 byte_swap.long_word = input_num;
261 temp = byte_swap.byte[3];
262 byte_swap.byte[3] = byte_swap.byte[0];
263 byte_swap.byte[0] = temp;
265 temp = byte_swap.byte[2];
266 byte_swap.byte[2] = byte_swap.byte[1];
267 byte_swap.byte[1] = temp;
269 return (byte_swap.long_word);
273 int refclock_tt560_bs;
274 #endif /* REFCLOCK */