2 * refclock_ulink - clock driver for Ultralink WWVB receiver
9 #if defined(REFCLOCK) && defined(CLOCK_ULINK)
16 #include "ntp_refclock.h"
17 #include "ntp_stdlib.h"
19 /* This driver supports ultralink Model 320,325,330,331,332 WWVB radios
21 * this driver was based on the refclock_wwvb.c driver
22 * in the ntp distribution.
26 * fudge flag1 0 don't poll clock
27 * 1 send poll character
30 * 99/9/09 j.c.lang original edit's
31 * 99/9/11 j.c.lang changed timecode parse to
32 * match what the radio actually
34 * 99/10/11 j.c.lang added support for continous
35 * time code mode (dipsw2)
36 * 99/11/26 j.c.lang added support for 320 decoder
37 * (taken from Dave Strout's
39 * 99/11/29 j.c.lang added fudge flag 1 to control
41 * 99/12/15 j.c.lang fixed 320 quality flag
42 * 01/02/21 s.l.smith fixed 33x quality flag
43 * added more debugging stuff
44 * updated 33x time code explanation
45 * 04/01/23 frank migge added support for 325 decoder
46 * (tested with ULM325.F)
48 * Questions, bugs, ideas send to:
50 * tcnojl1@earthlink.net
53 * dstrout@linuxfoundry.com
56 * frank.migge@oracle.com
59 * on the Ultralink model 33X decoder Dip switch 2 controls
60 * polled or continous timecode
61 * set fudge flag1 if using polled (needed for model 320 and 325)
62 * dont set fudge flag1 if dip switch 2 is set on model 33x decoder
67 * Interface definitions
69 #define DEVICE "/dev/wwvb%d" /* device name and unit */
70 #define SPEED232 B9600 /* uart speed (9600 baud) */
71 #define PRECISION (-10) /* precision assumed (about 10 ms) */
72 #define REFID "WWVB" /* reference ID */
73 #define DESCRIPTION "Ultralink WWVB Receiver" /* WRU */
75 #define LEN33X 32 /* timecode length Model 33X and 325 */
76 #define LEN320 24 /* timecode length Model 320 */
78 #define SIGLCHAR33x 'S' /* signal strength identifier char 325 */
79 #define SIGLCHAR325 'R' /* signal strength identifier char 33x */
82 * unit control structure
85 u_char tcswitch; /* timecode switch */
86 l_fp laststamp; /* last receive timestamp */
92 static int ulink_start (int, struct peer *);
93 static void ulink_shutdown (int, struct peer *);
94 static void ulink_receive (struct recvbuf *);
95 static void ulink_poll (int, struct peer *);
100 struct refclock refclock_ulink = {
101 ulink_start, /* start up driver */
102 ulink_shutdown, /* shut down driver */
103 ulink_poll, /* transmit poll message */
104 noentry, /* not used */
105 noentry, /* not used */
106 noentry, /* not used */
112 * ulink_start - open the devices and initialize data for processing
120 register struct ulinkunit *up;
121 struct refclockproc *pp;
126 * Open serial port. Use CLK line discipline, if available.
128 snprintf(device, sizeof(device), DEVICE, unit);
129 fd = refclock_open(device, SPEED232, LDISC_CLK);
134 * Allocate and initialize unit structure
136 up = emalloc(sizeof(struct ulinkunit));
137 memset(up, 0, sizeof(struct ulinkunit));
139 pp->io.clock_recv = ulink_receive;
140 pp->io.srcclock = peer;
143 if (!io_addclock(&pp->io)) {
152 * Initialize miscellaneous variables
154 peer->precision = PRECISION;
155 pp->clockdesc = DESCRIPTION;
156 memcpy((char *)&pp->refid, REFID, 4);
162 * ulink_shutdown - shut down the clock
170 register struct ulinkunit *up;
171 struct refclockproc *pp;
176 io_closeclock(&pp->io);
183 * ulink_receive - receive data from the serial interface
187 struct recvbuf *rbufp
190 struct ulinkunit *up;
191 struct refclockproc *pp;
194 l_fp trtmp; /* arrival timestamp */
195 int quality = INT_MAX; /* quality indicator */
196 int temp; /* int temp */
197 char syncchar; /* synchronization indicator */
198 char leapchar; /* leap indicator */
199 char modechar; /* model 320 mode flag */
200 char siglchar; /* model difference between 33x/325 */
201 char char_quality[2]; /* temp quality flag */
204 * Initialize pointers and read the timecode and timestamp
206 peer = rbufp->recv_peer;
209 temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
212 * Note we get a buffer and timestamp for both a <cr> and <lf>,
213 * but only the <cr> timestamp is retained.
216 if (up->tcswitch == 0) {
218 up->laststamp = trtmp;
224 pp->lastrec = up->laststamp;
225 up->laststamp = trtmp;
229 printf("ulink: timecode %d %s\n", pp->lencode,
234 * We get down to business, check the timecode format and decode
235 * its contents. If the timecode has invalid length or is not in
236 * proper format, we declare bad format and exit.
238 syncchar = leapchar = modechar = siglchar = ' ';
239 switch (pp->lencode ) {
243 * First we check if the format is 33x or 325:
244 * <CR><LF>S9+D 00 YYYY+DDDUTCS HH:MM:SSL+5 (33x)
245 * <CR><LF>R5_1C00LYYYY+DDDUTCS HH:MM:SSL+5 (325)
246 * simply by comparing if the signal level is 'S' or 'R'
249 if (sscanf(pp->a_lastcode, "%c%*31c",
252 if(siglchar == SIGLCHAR325) {
255 * decode for a Model 325 decoder.
256 * Timecode format from January 23, 2004 datasheet is:
258 * <CR><LF>R5_1C00LYYYY+DDDUTCS HH:MM:SSL+5
260 * R WWVB decodersignal readability R1 - R5
261 * 5 R1 is unreadable, R5 is best
262 * space a space (0x20)
263 * 1 Data bit 0, 1, M (pos mark), or ? (unknown).
264 * C Reception from either (C)olorado or (H)awaii
265 * 00 Hours since last good WWVB frame sync. Will
267 * space Space char (0x20) or (0xa5) if locked to wwvb
268 * YYYY Current year, 2000-2099
269 * + Leap year indicator. '+' if a leap year,
270 * a space (0x20) if not.
271 * DDD Day of year, 000 - 365.
272 * UTC Timezone (always 'UTC').
273 * S Daylight savings indicator
274 * S - standard time (STD) in effect
275 * O - during STD to DST day 0000-2400
276 * D - daylight savings time (DST) in effect
277 * I - during DST to STD day 0000-2400
278 * space Space character (0x20)
280 * : This is the REAL in sync indicator (: = insync)
282 * : : = in sync ? = NOT in sync
284 * L Leap second flag. Changes from space (0x20)
285 * to 'I' or 'D' during month preceding leap
286 * second adjustment. (I)nsert or (D)elete
287 * +5 UT1 correction (sign + digit ))
290 if (sscanf(pp->a_lastcode,
291 "%*2c %*2c%2c%*c%4d%*c%3d%*4c %2d%c%2d:%2d%c%*2c",
292 char_quality, &pp->year, &pp->day,
293 &pp->hour, &syncchar, &pp->minute, &pp->second,
296 if (char_quality[0] == '0') {
298 } else if (char_quality[0] == '0') {
299 quality = (char_quality[1] & 0x0f);
304 if (leapchar == 'I' ) leapchar = '+';
305 if (leapchar == 'D' ) leapchar = '-';
310 printf("ulink: char_quality %c %c\n",
311 char_quality[0], char_quality[1]);
312 printf("ulink: quality %d\n", quality);
313 printf("ulink: syncchar %x\n", syncchar);
314 printf("ulink: leapchar %x\n", leapchar);
322 if(siglchar == SIGLCHAR33x) {
325 * We got a Model 33X decoder.
326 * Timecode format from January 29, 2001 datasheet is:
327 * <CR><LF>S9+D 00 YYYY+DDDUTCS HH:MM:SSL+5
328 * S WWVB decoder sync indicator. S for in-sync(?)
329 * or N for noisy signal.
330 * 9+ RF signal level in S-units, 0-9 followed by
331 * a space (0x20). The space turns to '+' if the
333 * D Data bit 0, 1, 2 (position mark), or
335 * space Space character (0x20)
336 * 00 Hours since last good WWVB frame sync. Will
337 * be 00-23 hrs, or '1d' to '7d'. Will be 'Lk'
338 * if currently in sync.
339 * space Space character (0x20)
340 * YYYY Current year, 1990-2089
341 * + Leap year indicator. '+' if a leap year,
342 * a space (0x20) if not.
343 * DDD Day of year, 001 - 366.
344 * UTC Timezone (always 'UTC').
345 * S Daylight savings indicator
346 * S - standard time (STD) in effect
347 * O - during STD to DST day 0000-2400
348 * D - daylight savings time (DST) in effect
349 * I - during DST to STD day 0000-2400
350 * space Space character (0x20)
352 * : This is the REAL in sync indicator (: = insync)
354 * : : = in sync ? = NOT in sync
356 * L Leap second flag. Changes from space (0x20)
357 * to '+' or '-' during month preceding leap
359 * +5 UT1 correction (sign + digit ))
362 if (sscanf(pp->a_lastcode,
363 "%*4c %2c %4d%*c%3d%*4c %2d%c%2d:%2d%c%*2c",
364 char_quality, &pp->year, &pp->day,
365 &pp->hour, &syncchar, &pp->minute, &pp->second,
368 if (char_quality[0] == 'L') {
370 } else if (char_quality[0] == '0') {
371 quality = (char_quality[1] & 0x0f);
379 printf("ulink: char_quality %c %c\n",
380 char_quality[0], char_quality[1]);
381 printf("ulink: quality %d\n", quality);
382 printf("ulink: syncchar %x\n", syncchar);
383 printf("ulink: leapchar %x\n", leapchar);
397 * The timecode format is:
399 * <cr><lf>SQRYYYYDDD+HH:MM:SS.mmLT<cr>
403 * S = 'S' -- sync'd in last hour,
404 * '0'-'9' - hours x 10 since last update,
406 * Q = Number of correlating time-frames, from 0 to 5
407 * R = 'R' -- reception in progress,
408 * 'N' -- Noisy reception,
409 * ' ' -- standby mode
410 * YYYY = year from 1990 to 2089
411 * DDD = current day from 1 to 366
412 * + = '+' if current year is a leap year, else ' '
413 * HH = UTC hour 0 to 23
414 * MM = Minutes of current hour from 0 to 59
415 * SS = Seconds of current minute from 0 to 59
416 * mm = 10's milliseconds of the current second from 00 to 99
417 * L = Leap second pending at end of month
418 * 'I' = insert, 'D'= delete
419 * T = DST <-> STD transition indicators
423 if (sscanf(pp->a_lastcode, "%c%1d%c%4d%3d%*c%2d:%2d:%2d.%2ld%c",
424 &syncchar, &quality, &modechar, &pp->year, &pp->day,
425 &pp->hour, &pp->minute, &pp->second,
426 &pp->nsec, &leapchar) == 10) {
427 pp->nsec *= 10000000; /* M320 returns 10's of msecs */
428 if (leapchar == 'I' ) leapchar = '+';
429 if (leapchar == 'D' ) leapchar = '-';
430 if (syncchar != '?' ) syncchar = ':';
436 refclock_report(peer, CEVNT_BADREPLY);
441 * Decode quality indicator
442 * For the 325 & 33x series, the lower the number the "better"
443 * the time is. I used the dispersion as the measure of time
444 * quality. The quality indicator in the 320 is the number of
445 * correlating time frames (the more the better)
449 * The spec sheet for the 325 & 33x series states the clock will
450 * maintain +/-0.002 seconds accuracy when locked to WWVB. This
451 * is indicated by 'Lk' in the quality portion of the incoming
452 * string. When not in lock, a drift of +/-0.015 seconds should
454 * With the quality indicator decoding scheme above, the 'Lk'
455 * condition will produce a quality value of 0. If the quality
456 * indicator starts with '0' then the second character is the
457 * number of hours since we were last locked. If the first
458 * character is anything other than 'L' or '0' then we have been
459 * out of lock for more than 9 hours so we assume the worst and
460 * force a quality value that selects the 'default' maximum
461 * dispersion. The dispersion values below are what came with the
462 * driver. They're not unreasonable so they've not been changed.
465 if (pp->lencode == LEN33X) {
480 pp->disp=MAXDISPERSE;
501 pp->disp=MAXDISPERSE;
508 * Decode synchronization, and leap characters. If
509 * unsynchronized, set the leap bits accordingly and exit.
510 * Otherwise, set the leap bits according to the leap character.
514 pp->leap = LEAP_NOTINSYNC;
515 else if (leapchar == '+')
516 pp->leap = LEAP_ADDSECOND;
517 else if (leapchar == '-')
518 pp->leap = LEAP_DELSECOND;
520 pp->leap = LEAP_NOWARNING;
523 * Process the new sample in the median filter and determine the
524 * timecode timestamp.
526 if (!refclock_process(pp)) {
527 refclock_report(peer, CEVNT_BADTIME);
533 * ulink_poll - called by the transmit procedure
542 struct refclockproc *pp;
547 if (pp->sloppyclockflag & CLK_FLAG1) {
548 if (write(pp->io.fd, &pollchar, 1) != 1)
549 refclock_report(peer, CEVNT_FAULT);
556 if (pp->coderecv == pp->codeproc) {
557 refclock_report(peer, CEVNT_TIMEOUT);
560 pp->lastref = pp->lastrec;
561 refclock_receive(peer);
562 record_clock_stats(&peer->srcadr, pp->a_lastcode);
567 int refclock_ulink_bs;
568 #endif /* REFCLOCK */