4 * refclock_tt560 - clock driver for the TrueTime 560 IRIG-B decoder
11 #if defined(REFCLOCK) && defined(CLOCK_TT560)
15 #include "ntp_refclock.h"
16 #include "ntp_unixtime.h"
17 #include "sys/tt560_api.h"
18 #include "ntp_stdlib.h"
24 * This driver supports the TrueTime 560 IRIG-B decoder for the PCI bus.
28 * TT560 interface definitions
30 #define DEVICE "/dev/tt560%d" /* device name and unit */
31 #define PRECISION (-20) /* precision assumed (1 us) */
32 #define REFID "IRIG" /* reference ID */
33 #define DESCRIPTION "TrueTime 560 IRIG-B PCI Decoder"
36 * Unit control structure
39 tt_mem_space_t
*tt_mem
; /* mapped address of PCI board */
40 time_freeze_reg_t tt560rawt
; /* data returned from PCI board */
43 typedef union byteswap_u
45 unsigned int long_word
;
46 unsigned char byte
[4];
52 static int tt560_start
P((int, struct peer
*));
53 static void tt560_shutdown
P((int, struct peer
*));
54 static void tt560_poll
P((int unit
, struct peer
*));
59 struct refclock refclock_tt560
= {
60 tt560_start
, /* clock_start */
61 tt560_shutdown
, /* clock_shutdown */
62 tt560_poll
, /* clock_poll */
63 noentry
, /* clock_control (not used) */
64 noentry
, /* clock_init (not used) */
65 noentry
, /* clock_buginfo (not used) */
66 NOFLAGS
/* clock_flags (not used) */
71 * tt560_start - open the TT560 device and initialize data for processing
79 register struct tt560unit
*up
;
80 struct refclockproc
*pp
;
88 (void)sprintf(device
, DEVICE
, unit
);
89 fd
= open(device
, O_RDWR
);
91 msyslog(LOG_ERR
, "tt560_start: open of %s: %m", device
);
96 * Map the device registers into user space.
98 membase
= mmap ((caddr_t
) 0, TTIME_MEMORY_SIZE
,
99 PROT_READ
| PROT_WRITE
,
100 MAP_SHARED
, fd
, (off_t
)0);
102 if (membase
== (caddr_t
) -1) {
103 msyslog(LOG_ERR
, "tt560_start: mapping of %s: %m", device
);
109 * Allocate and initialize unit structure
111 if (!(up
= (struct tt560unit
*) emalloc(sizeof(struct tt560unit
)))) {
115 memset((char *)up
, 0, sizeof(struct tt560unit
));
116 up
->tt_mem
= (tt_mem_space_t
*)membase
;
118 pp
->io
.clock_recv
= noentry
;
119 pp
->io
.srcclock
= (caddr_t
)peer
;
122 pp
->unitptr
= (caddr_t
)up
;
125 * Initialize miscellaneous peer variables
127 peer
->precision
= PRECISION
;
128 peer
->burst
= NSTAGE
;
129 pp
->clockdesc
= DESCRIPTION
;
130 memcpy((char *)&pp
->refid
, REFID
, 4);
136 * tt560_shutdown - shut down the clock
144 register struct tt560unit
*up
;
145 struct refclockproc
*pp
;
148 up
= (struct tt560unit
*)pp
->unitptr
;
149 io_closeclock(&pp
->io
);
155 * tt560_poll - called by the transmit procedure
163 register struct tt560unit
*up
;
164 struct refclockproc
*pp
;
165 time_freeze_reg_t
*tp
;
169 unsigned int *p_time_t
, *tt_mem_t
;
172 * This is the main routine. It snatches the time from the TT560
173 * board and tacks on a local timestamp.
176 up
= (struct tt560unit
*)pp
->unitptr
;
180 p_time_t
= (unsigned int *)tp
;
181 tt_mem_t
= (unsigned int *)&mp
->time_freeze_reg
;
183 *tt_mem_t
= 0; /* update the time freeze register */
184 /* and copy time stamp to memory */
185 for (i
=0; i
< TIME_FREEZE_REG_LEN
; i
++) {
186 *p_time_t
= byte_swap(*tt_mem_t
);
191 get_systime(&pp
->lastrec
);
195 * We get down to business, check the timecode format and decode
196 * its contents. If the timecode has invalid length or is not in
197 * proper format, we declare bad format and exit. Note: we
198 * can't use the sec/usec conversion produced by the driver,
199 * since the year may be suspect. All format error checking is
200 * done by the sprintf() and sscanf() routines.
202 sprintf(pp
->a_lastcode
,
203 "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x",
204 tp
->hun_day
, tp
->tens_day
, tp
->unit_day
,
205 tp
->tens_hour
, tp
->unit_hour
,
206 tp
->tens_min
, tp
->unit_min
,
207 tp
->tens_sec
, tp
->unit_sec
,
208 tp
->hun_ms
, tp
->tens_ms
, tp
->unit_ms
,
209 tp
->hun_us
, tp
->tens_us
, tp
->unit_us
,
211 pp
->lencode
= strlen(pp
->a_lastcode
);
214 printf("tt560: time %s timecode %d %s\n",
215 ulfptoa(&pp
->lastrec
, 6), pp
->lencode
,
218 if (sscanf(pp
->a_lastcode
, "%3d %2d:%2d:%2d.%6ld",
219 &pp
->day
, &pp
->hour
, &pp
->minute
, &pp
->second
, &pp
->usec
)
221 refclock_report(peer
, CEVNT_BADTIME
);
224 if ((tp
->status
& 0x6) != 0x6)
225 pp
->leap
= LEAP_NOTINSYNC
;
227 pp
->leap
= LEAP_NOWARNING
;
228 if (!refclock_process(pp
)) {
229 refclock_report(peer
, CEVNT_BADTIME
);
234 if (pp
->coderecv
== pp
->codeproc
) {
235 refclock_report(peer
, CEVNT_TIMEOUT
);
238 record_clock_stats(&peer
->srcadr
, pp
->a_lastcode
);
239 refclock_receive(peer
);
240 peer
->burst
= NSTAGE
;
243 /******************************************************************
247 * Inputs: 32 bit integer
249 * Output: byte swapped 32 bit integer.
251 * This routine is used to compensate for the byte alignment
252 * differences between big-endian and little-endian integers.
254 ******************************************************************/
256 byte_swap(unsigned int input_num
)
258 byteswap_t byte_swap
;
261 byte_swap
.long_word
= input_num
;
263 temp
= byte_swap
.byte
[3];
264 byte_swap
.byte
[3] = byte_swap
.byte
[0];
265 byte_swap
.byte
[0] = temp
;
267 temp
= byte_swap
.byte
[2];
268 byte_swap
.byte
[2] = byte_swap
.byte
[1];
269 byte_swap
.byte
[1] = temp
;
271 return (byte_swap
.long_word
);
275 int refclock_tt560_bs
;
276 #endif /* REFCLOCK */