2 * refclock_irig - audio IRIG-B/E demodulator/decoder
8 #if defined(REFCLOCK) && defined(CLOCK_IRIG)
12 #include "ntp_refclock.h"
13 #include "ntp_calendar.h"
14 #include "ntp_stdlib.h"
19 #ifdef HAVE_SYS_IOCTL_H
20 #include <sys/ioctl.h>
21 #endif /* HAVE_SYS_IOCTL_H */
26 * Audio IRIG-B/E demodulator/decoder
28 * This driver receives, demodulates and decodes IRIG-B/E signals when
29 * connected to the audio codec /dev/audio. The IRIG signal format is an
30 * amplitude-modulated carrier with pulse-width modulated data bits. For
31 * IRIG-B, the carrier frequency is 1000 Hz and bit rate 100 b/s; for
32 * IRIG-E, the carrier frequenchy is 100 Hz and bit rate 10 b/s. The
33 * driver automatically recognizes which format is in use.
35 * The program processes 8000-Hz mu-law companded samples using separate
36 * signal filters for IRIG-B and IRIG-E, a comb filter, envelope
37 * detector and automatic threshold corrector. Cycle crossings relative
38 * to the corrected slice level determine the width of each pulse and
39 * its value - zero, one or position identifier. The data encode 20 BCD
40 * digits which determine the second, minute, hour and day of the year
41 * and sometimes the year and synchronization condition. The comb filter
42 * exponentially averages the corresponding samples of successive baud
43 * intervals in order to reliably identify the reference carrier cycle.
44 * A type-II phase-lock loop (PLL) performs additional integration and
45 * interpolation to accurately determine the zero crossing of that
46 * cycle, which determines the reference timestamp. A pulse-width
47 * discriminator demodulates the data pulses, which are then encoded as
48 * the BCD digits of the timecode.
50 * The timecode and reference timestamp are updated once each second
51 * with IRIG-B (ten seconds with IRIG-E) and local clock offset samples
52 * saved for later processing. At poll intervals of 64 s, the saved
53 * samples are processed by a trimmed-mean filter and used to update the
56 * An automatic gain control feature provides protection against
57 * overdriven or underdriven input signal amplitudes. It is designed to
58 * maintain adequate demodulator signal amplitude while avoiding
59 * occasional noise spikes. In order to assure reliable capture, the
60 * decompanded input signal amplitude must be greater than 100 units and
61 * the codec sample frequency error less than 250 PPM (.025 percent).
63 * The program performs a number of error checks to protect against
64 * overdriven or underdriven input signal levels, incorrect signal
65 * format or improper hardware configuration. Specifically, if any of
66 * the following errors occur for a time measurement, the data are
69 * o The peak carrier amplitude is less than DRPOUT (100). This usually
70 * means dead IRIG signal source, broken cable or wrong input port.
72 * o The frequency error is greater than MAXFREQ +-250 PPM (.025%). This
73 * usually means broken codec hardware or wrong codec configuration.
75 * o The modulation index is less than MODMIN (0.5). This usually means
76 * overdriven IRIG signal or wrong IRIG format.
78 * o A frame synchronization error has occurred. This usually means
79 * wrong IRIG signal format or the IRIG signal source has lost
80 * synchronization (signature control).
82 * o A data decoding error has occurred. This usually means wrong IRIG
85 * o The current second of the day is not exactly one greater than the
86 * previous one. This usually means a very noisy IRIG signal or
87 * insufficient CPU resources.
89 * o An audio codec error (overrun) occurred. This usually means
90 * insufficient CPU resources, as sometimes happens with Sun SPARC
91 * IPCs when doing something useful.
93 * Note that additional checks are done elsewhere in the reference clock
98 * The timecode format used for debugging and data recording includes
99 * data helpful in diagnosing problems with the IRIG signal and codec
100 * connections. With debugging enabled (-d on the ntpd command line),
101 * the driver produces one line for each timecode in the following
104 * 00 1 98 23 19:26:52 721 143 0.694 20 0.1 66.5 3094572411.00027
106 * The most recent line is also written to the clockstats file at 64-s
109 * The first field contains the error flags in hex, where the hex bits
110 * are interpreted as below. This is followed by the IRIG status
111 * indicator, year of century, day of year and time of day. The status
112 * indicator and year are not produced by some IRIG devices. Following
113 * these fields are the signal amplitude (0-8100), codec gain (0-255),
114 * modulation index (0-1), time constant (2-20), carrier phase error
115 * (us) and carrier frequency error (PPM). The last field is the on-time
116 * timestamp in NTP format.
118 * The fraction part of the on-time timestamp is a good indicator of how
119 * well the driver is doing. Once upon a time, an UltrSPARC 30 and
120 * Solaris 2.7 kept the clock within a few tens of microseconds relative
121 * to the IRIG-B signal. Accuracy with IRIG-E was about ten times worse.
122 * Unfortunately, Sun broke the 2.7 audio driver in 2.8, which has a 10-
123 * ms sawtooth modulation. The driver attempts to remove the modulation
124 * by some clever estimation techniques which mostly work. With the
125 * "mixerctl -o" command before starting the daemon, the jitter is down
126 * to about 100 microseconds. Your experience may vary.
128 * Unlike other drivers, which can have multiple instantiations, this
129 * one supports only one. It does not seem likely that more than one
130 * audio codec would be useful in a single machine. More than one would
131 * probably chew up too much CPU time anyway.
135 * Fudge flag4 causes the dubugging output described above to be
136 * recorded in the clockstats file. Fudge flag2 selects the audio input
137 * port, where 0 is the mike port (default) and 1 is the line-in port.
138 * It does not seem useful to select the compact disc player port. Fudge
139 * flag3 enables audio monitoring of the input signal. For this purpose,
140 * the monitor gain is set to a default value. Fudgetime2 is used as a
141 * frequency vernier for broken codec sample frequency.
144 * Interface definitions
146 #define DEVICE_AUDIO "/dev/audio" /* audio device name */
147 #define PRECISION (-17) /* precision assumed (about 10 us) */
148 #define REFID "IRIG" /* reference ID */
149 #define DESCRIPTION "Generic IRIG Audio Driver" /* WRU */
150 #define AUDIO_BUFSIZ 320 /* audio buffer size (40 ms) */
151 #define SECOND 8000 /* nominal sample rate (Hz) */
152 #define BAUD 80 /* samples per baud interval */
153 #define OFFSET 128 /* companded sample offset */
154 #define SIZE 256 /* decompanding table size */
155 #define CYCLE 8 /* samples per carrier cycle */
156 #define SUBFLD 10 /* bits per subfield */
157 #define FIELD 10 /* subfields per field */
158 #define MINTC 2 /* min PLL time constant */
159 #define MAXTC 20 /* max PLL time constant max */
160 #define MAXAMP 6000. /* maximum signal level */
161 #define MAXCLP 100 /* max clips above reference per s */
162 #define DRPOUT 100. /* dropout signal level */
163 #define MODMIN 0.5 /* minimum modulation index */
164 #define MAXFREQ (250e-6 * SECOND) /* freq tolerance (.025%) */
165 #define PI 3.1415926535 /* the real thing */
167 #define WIGGLE 11 /* wiggle filter length */
168 #endif /* IRIG_SUCKS */
171 * Experimentally determined filter delays
173 #define IRIG_B .0019 /* IRIG-B filter delay */
174 #define IRIG_E .0019 /* IRIG-E filter delay */
177 * Data bit definitions
179 #define BIT0 0 /* zero */
180 #define BIT1 1 /* one */
181 #define BITP 2 /* position identifier */
184 * Error flags (up->errflg)
186 #define IRIG_ERR_AMP 0x01 /* low carrier amplitude */
187 #define IRIG_ERR_FREQ 0x02 /* frequency tolerance exceeded */
188 #define IRIG_ERR_MOD 0x04 /* low modulation index */
189 #define IRIG_ERR_SYNCH 0x08 /* frame synch error */
190 #define IRIG_ERR_DECODE 0x10 /* frame decoding error */
191 #define IRIG_ERR_CHECK 0x20 /* second numbering discrepancy */
192 #define IRIG_ERR_ERROR 0x40 /* codec error (overrun) */
193 #define IRIG_ERR_SIGERR 0x80 /* IRIG status error (Spectracom) */
196 * IRIG unit control structure
199 u_char timecode[21]; /* timecode string */
200 l_fp timestamp; /* audio sample timestamp */
201 l_fp tick; /* audio sample increment */
202 double integ[BAUD]; /* baud integrator */
203 double phase, freq; /* logical clock phase and frequency */
204 double zxing; /* phase detector integrator */
205 double yxing; /* cycle phase */
206 double exing; /* envelope phase */
207 double modndx; /* modulation index */
208 double irig_b; /* IRIG-B signal amplitude */
209 double irig_e; /* IRIG-E signal amplitude */
210 int errflg; /* error flags */
212 * Audio codec variables
214 double comp[SIZE]; /* decompanding table */
215 int port; /* codec port */
216 int gain; /* codec gain */
217 int mongain; /* codec monitor gain */
218 int clipcnt; /* sample clipped count */
219 int seccnt; /* second interval counter */
224 double hpf[5]; /* IRIG-B filter shift register */
225 double lpf[5]; /* IRIG-E filter shift register */
226 double intmin, intmax; /* integrated envelope min and max */
227 double envmax; /* peak amplitude */
228 double envmin; /* noise amplitude */
229 double maxsignal; /* integrated peak amplitude */
230 double noise; /* integrated noise amplitude */
231 double lastenv[CYCLE]; /* last cycle amplitudes */
232 double lastint[CYCLE]; /* last integrated cycle amplitudes */
233 double lastsig; /* last carrier sample */
234 double fdelay; /* filter delay */
235 int decim; /* sample decimation factor */
236 int envphase; /* envelope phase */
237 int envptr; /* envelope phase pointer */
238 int carphase; /* carrier phase */
239 int envsw; /* envelope state */
240 int envxing; /* envelope slice crossing */
241 int tc; /* time constant */
242 int tcount; /* time constant counter */
243 int badcnt; /* decimation interval counter */
248 int pulse; /* cycle counter */
249 int cycles; /* carrier cycles */
250 int dcycles; /* data cycles */
251 int xptr; /* translate table pointer */
252 int lastbit; /* last code element length */
253 int second; /* previous second */
254 int fieldcnt; /* subfield count in field */
255 int bits; /* demodulated bits */
256 int bitcnt; /* bit count in subfield */
258 l_fp wigwag; /* wiggle accumulator */
259 int wp; /* wiggle filter pointer */
260 l_fp wiggle[WIGGLE]; /* wiggle filter */
261 l_fp wigbot[WIGGLE]; /* wiggle bottom fisher*/
262 #endif /* IRIG_SUCKS */
267 * Function prototypes
269 static int irig_start P((int, struct peer *));
270 static void irig_shutdown P((int, struct peer *));
271 static void irig_receive P((struct recvbuf *));
272 static void irig_poll P((int, struct peer *));
275 * More function prototypes
277 static void irig_base P((struct peer *, double));
278 static void irig_rf P((struct peer *, double));
279 static void irig_decode P((struct peer *, int));
280 static void irig_gain P((struct peer *));
285 struct refclock refclock_irig = {
286 irig_start, /* start up driver */
287 irig_shutdown, /* shut down driver */
288 irig_poll, /* transmit poll message */
289 noentry, /* not used (old irig_control) */
290 noentry, /* initialize driver (not used) */
291 noentry, /* not used (old irig_buginfo) */
292 NOFLAGS /* not used */
298 static char hexchar[] = { /* really quick decoding table */
299 '0', '8', '4', 'c', /* 0000 0001 0010 0011 */
300 '2', 'a', '6', 'e', /* 0100 0101 0110 0111 */
301 '1', '9', '5', 'd', /* 1000 1001 1010 1011 */
302 '3', 'b', '7', 'f' /* 1100 1101 1110 1111 */
307 * irig_start - open the devices and initialize data for processing
311 int unit, /* instance number (used for PCM) */
312 struct peer *peer /* peer structure pointer */
315 struct refclockproc *pp;
321 int fd; /* file descriptor */
323 double step; /* codec adjustment */
328 fd = audio_init(DEVICE_AUDIO, AUDIO_BUFSIZ, unit);
337 * Allocate and initialize unit structure
339 if (!(up = (struct irigunit *)
340 emalloc(sizeof(struct irigunit)))) {
344 memset((char *)up, 0, sizeof(struct irigunit));
346 pp->unitptr = (caddr_t)up;
347 pp->io.clock_recv = irig_receive;
348 pp->io.srcclock = (caddr_t)peer;
351 if (!io_addclock(&pp->io)) {
358 * Initialize miscellaneous variables
360 peer->precision = PRECISION;
361 pp->clockdesc = DESCRIPTION;
362 memcpy((char *)&pp->refid, REFID, 4);
369 * The companded samples are encoded sign-magnitude. The table
370 * contains all the 256 values in the interest of speed.
372 up->comp[0] = up->comp[OFFSET] = 0.;
373 up->comp[1] = 1; up->comp[OFFSET + 1] = -1.;
374 up->comp[2] = 3; up->comp[OFFSET + 2] = -3.;
376 for (i = 3; i < OFFSET; i++) {
377 up->comp[i] = up->comp[i - 1] + step;
378 up->comp[OFFSET + i] = -up->comp[i];
382 DTOLFP(1. / SECOND, &up->tick);
388 * irig_shutdown - shut down the clock
392 int unit, /* instance number (not used) */
393 struct peer *peer /* peer structure pointer */
396 struct refclockproc *pp;
400 up = (struct irigunit *)pp->unitptr;
401 io_closeclock(&pp->io);
407 * irig_receive - receive data from the audio device
409 * This routine reads input samples and adjusts the logical clock to
410 * track the irig clock by dropping or duplicating codec samples.
414 struct recvbuf *rbufp /* receive buffer structure pointer */
418 struct refclockproc *pp;
424 double sample; /* codec sample */
425 u_char *dpt; /* buffer pointer */
426 int bufcnt; /* buffer counter */
427 l_fp ltemp; /* l_fp temp */
429 peer = (struct peer *)rbufp->recv_srcclock;
431 up = (struct irigunit *)pp->unitptr;
434 * Main loop - read until there ain't no more. Note codec
435 * samples are bit-inverted.
437 DTOLFP((double)rbufp->recv_length / SECOND, <emp);
438 L_SUB(&rbufp->recv_time, <emp);
439 up->timestamp = rbufp->recv_time;
440 dpt = rbufp->recv_buffer;
441 for (bufcnt = 0; bufcnt < rbufp->recv_length; bufcnt++) {
442 sample = up->comp[~*dpt++ & 0xff];
445 * Clip noise spikes greater than MAXAMP. If no clips,
446 * increase the gain a tad; if the clips are too high,
449 if (sample > MAXAMP) {
452 } else if (sample < -MAXAMP) {
458 * Variable frequency oscillator. The codec oscillator
459 * runs at the nominal rate of 8000 samples per second,
460 * or 125 us per sample. A frequency change of one unit
461 * results in either duplicating or deleting one sample
462 * per second, which results in a frequency change of
465 up->phase += up->freq / SECOND;
466 up->phase += pp->fudgetime2 / 1e6;
467 if (up->phase >= .5) {
469 } else if (up->phase < -.5) {
471 irig_rf(peer, sample);
472 irig_rf(peer, sample);
474 irig_rf(peer, sample);
476 L_ADD(&up->timestamp, &up->tick);
479 * Once each second, determine the IRIG format and gain.
481 up->seccnt = (up->seccnt + 1) % SECOND;
482 if (up->seccnt == 0) {
483 if (up->irig_b > up->irig_e) {
491 up->irig_b = up->irig_e = 0;
496 * Set the input port and monitor gain for the next buffer.
498 if (pp->sloppyclockflag & CLK_FLAG2)
502 if (pp->sloppyclockflag & CLK_FLAG3)
503 up->mongain = MONGAIN;
509 * irig_rf - RF processing
511 * This routine filters the RF signal using a highpass filter for IRIG-B
512 * and a lowpass filter for IRIG-E. In case of IRIG-E, the samples are
513 * decimated by a factor of ten. The lowpass filter functions also as a
514 * decimation filter in this case. Note that the codec filters function
515 * as roofing filters to attenuate both the high and low ends of the
516 * passband. IIR filter coefficients were determined using Matlab Signal
517 * Processing Toolkit.
521 struct peer *peer, /* peer structure pointer */
522 double sample /* current signal sample */
525 struct refclockproc *pp;
531 double irig_b, irig_e; /* irig filter outputs */
534 up = (struct irigunit *)pp->unitptr;
537 * IRIG-B filter. 4th-order elliptic, 800-Hz highpass, 0.3 dB
538 * passband ripple, -50 dB stopband ripple, phase delay .0022
541 irig_b = (up->hpf[4] = up->hpf[3]) * 2.322484e-01;
542 irig_b += (up->hpf[3] = up->hpf[2]) * -1.103929e+00;
543 irig_b += (up->hpf[2] = up->hpf[1]) * 2.351081e+00;
544 irig_b += (up->hpf[1] = up->hpf[0]) * -2.335036e+00;
545 up->hpf[0] = sample - irig_b;
546 irig_b = up->hpf[0] * 4.335855e-01
547 + up->hpf[1] * -1.695859e+00
548 + up->hpf[2] * 2.525004e+00
549 + up->hpf[3] * -1.695859e+00
550 + up->hpf[4] * 4.335855e-01;
551 up->irig_b += irig_b * irig_b;
554 * IRIG-E filter. 4th-order elliptic, 130-Hz lowpass, 0.3 dB
555 * passband ripple, -50 dB stopband ripple, phase delay .0219 s.
557 irig_e = (up->lpf[4] = up->lpf[3]) * 8.694604e-01;
558 irig_e += (up->lpf[3] = up->lpf[2]) * -3.589893e+00;
559 irig_e += (up->lpf[2] = up->lpf[1]) * 5.570154e+00;
560 irig_e += (up->lpf[1] = up->lpf[0]) * -3.849667e+00;
561 up->lpf[0] = sample - irig_e;
562 irig_e = up->lpf[0] * 3.215696e-03
563 + up->lpf[1] * -1.174951e-02
564 + up->lpf[2] * 1.712074e-02
565 + up->lpf[3] * -1.174951e-02
566 + up->lpf[4] * 3.215696e-03;
567 up->irig_e += irig_e * irig_e;
570 * Decimate by a factor of either 1 (IRIG-B) or 10 (IRIG-E).
572 up->badcnt = (up->badcnt + 1) % up->decim;
573 if (up->badcnt == 0) {
575 irig_base(peer, irig_b);
577 irig_base(peer, irig_e);
582 * irig_base - baseband processing
584 * This routine processes the baseband signal and demodulates the AM
585 * carrier using a synchronous detector. It then synchronizes to the
586 * data frame at the baud rate and decodes the data pulses.
590 struct peer *peer, /* peer structure pointer */
591 double sample /* current signal sample */
594 struct refclockproc *pp;
600 double xxing; /* phase detector interpolated output */
601 double lope; /* integrator output */
602 double env; /* envelope detector output */
603 double dtemp; /* double temp */
606 up = (struct irigunit *)pp->unitptr;
609 * Synchronous baud integrator. Corresponding samples of current
610 * and past baud intervals are integrated to refine the envelope
611 * amplitude and phase estimate. We keep one cycle of both the
612 * raw and integrated data for later use.
614 up->envphase = (up->envphase + 1) % BAUD;
615 up->carphase = (up->carphase + 1) % CYCLE;
616 up->integ[up->envphase] += (sample - up->integ[up->envphase]) /
618 lope = up->integ[up->envphase];
619 up->lastenv[up->carphase] = sample;
620 up->lastint[up->carphase] = lope;
623 * Phase detector. Sample amplitudes are integrated over the
624 * baud interval. Cycle phase is determined from these
625 * amplitudes using an eight-sample cyclic buffer. A phase
626 * change of 360 degrees produces an output change of one unit.
628 if (up->lastsig > 0 && lope <= 0) {
629 xxing = lope / (up->lastsig - lope);
630 up->zxing += (up->carphase - 4 + xxing) / CYCLE;
635 * Update signal/noise estimates and PLL phase/frequency.
637 if (up->envphase == 0) {
640 * Update envelope signal and noise estimates and mess
643 up->maxsignal = up->intmax;
644 up->noise = up->intmin;
645 if (up->maxsignal < DRPOUT)
646 up->errflg |= IRIG_ERR_AMP;
647 if (up->maxsignal > 0)
648 up->modndx = (up->intmax - up->intmin) /
652 if (up->modndx < MODMIN)
653 up->errflg |= IRIG_ERR_MOD;
654 up->intmin = 1e6; up->intmax = 0;
655 if (up->errflg & (IRIG_ERR_AMP | IRIG_ERR_FREQ |
656 IRIG_ERR_MOD | IRIG_ERR_SYNCH)) {
662 * Update PLL phase and frequency. The PLL time constant
663 * is set initially to stabilize the frequency within a
664 * minute or two, then increases to the maximum. The
665 * frequency is clamped so that the PLL capture range
666 * cannot be exceeded.
668 dtemp = up->zxing * up->decim / BAUD;
671 up->phase += dtemp / up->tc;
672 up->freq += dtemp / (4. * up->tc * up->tc);
673 if (up->freq > MAXFREQ) {
675 up->errflg |= IRIG_ERR_FREQ;
676 } else if (up->freq < -MAXFREQ) {
678 up->errflg |= IRIG_ERR_FREQ;
683 * Synchronous demodulator. There are eight samples in the cycle
684 * and ten cycles in the baud interval. The amplitude of each
685 * cycle is determined at the last sample in the cycle. The
686 * beginning of the data pulse is determined from the integrated
687 * samples, while the end of the pulse is determined from the
688 * raw samples. The raw data bits are demodulated relative to
689 * the slice level and left-shifted in the decoding register.
691 if (up->carphase != 7)
694 env = (up->lastenv[2] - up->lastenv[6]) / 2.;
695 lope = (up->lastint[2] - up->lastint[6]) / 2.;
696 if (lope > up->intmax)
698 if (lope < up->intmin)
702 * Pulse code demodulator and reference timestamp. The decoder
703 * looks for a sequence of ten bits; the first two bits must be
704 * one, the last two bits must be zero. Frame synch is asserted
705 * when three correct frames have been found.
707 up->pulse = (up->pulse + 1) % 10;
710 else if (up->pulse == 9)
713 if (env >= (up->envmax + up->envmin) / 2.)
716 if (lope >= (up->maxsignal + up->noise) / 2.)
718 if ((up->cycles & 0x303c0f03) == 0x300c0300) {
723 * The PLL time constant starts out small, in order to
724 * sustain a frequency tolerance of 250 PPM. It
725 * gradually increases as the loop settles down. Note
726 * that small wiggles are not believed, unless they
727 * persist for lots of samples.
730 up->errflg |= IRIG_ERR_SYNCH;
732 up->exing = -up->yxing;
733 if (fabs(up->envxing - up->envphase) <= 1) {
735 if (up->tcount > 50 * up->tc) {
740 up->envxing = up->envphase;
742 up->exing -= up->envxing - up->envphase;
746 up->envxing = up->envphase;
750 * Determine a reference timestamp, accounting for the
751 * codec delay and filter delay. Note the timestamp is
752 * for the previous frame, so we have to backtrack for
753 * this plus the delay since the last carrier positive
756 dtemp = up->decim * ((up->exing + BAUD) / SECOND + 1.) +
758 DTOLFP(dtemp, <emp);
759 pp->lastrec = up->timestamp;
760 L_SUB(&pp->lastrec, <emp);
763 * The data bits are collected in ten-bit frames. The
764 * first two and last two bits are determined by frame
765 * sync and ignored here; the resulting patterns
766 * represent zero (0-1 bits), one (2-4 bits) and
767 * position identifier (5-6 bits). The remaining
768 * patterns represent errors and are treated as zeros.
770 bitz = up->dcycles & 0xfc;
775 irig_decode(peer, BIT0);
781 irig_decode(peer, BIT1);
786 irig_decode(peer, BITP);
790 irig_decode(peer, 0);
791 up->errflg |= IRIG_ERR_DECODE;
798 * irig_decode - decode the data
800 * This routine assembles bits into digits, digits into subfields and
801 * subfields into the timecode field. Bits can have values of zero, one
802 * or position identifier. There are four bits per digit, two digits per
803 * subfield and ten subfields per field. The last bit in every subfield
804 * and the first bit in the first subfield are position identifiers.
808 struct peer *peer, /* peer structure pointer */
809 int bit /* data bit (0, 1 or 2) */
812 struct refclockproc *pp;
816 #endif /* IRIG_SUCKS */
821 char syncchar; /* sync character (Spectracom) */
822 char sbs[6]; /* binary seconds since 0h */
823 char spare[2]; /* mulligan digits */
826 up = (struct irigunit *)pp->unitptr;
829 * Assemble subfield bits.
834 } else if (bit == BITP && up->lastbit == BITP) {
837 * Frame sync - two adjacent position identifiers.
838 * Monitor the reference timestamp and wiggle the
839 * clock, but only if no errors have occurred.
844 if (up->errflg == 0) {
849 * You really don't wanna know what comes down
850 * here. Leave it to say Solaris 2.8 broke the
851 * nice clean audio stream, apparently affected
852 * by a 5-ms sawtooth jitter. Sundown on
853 * Solaris. This leaves a little twilight.
855 * The scheme involves differentiation, forward
856 * learning and integration. The sawtooth has a
857 * period of 11 seconds. The timestamp
858 * differences are integrated and subtracted
862 L_SUB(<emp, &pp->lastref);
867 pp->lastref = pp->lastrec;
868 if (!L_ISNEG(<emp))
871 L_ADD(&up->wigwag, <emp);
872 L_SUB(&pp->lastrec, &up->wigwag);
873 up->wiggle[up->wp] = ltemp;
876 * Bottom fisher. To understand this, you have
877 * to know about velocity microphones and AM
878 * transmitters. No further explanation is
879 * offered, as this is truly a black art.
881 up->wigbot[up->wp] = pp->lastrec;
882 for (i = 0; i < WIGGLE; i++) {
884 up->wigbot[i].l_ui++;
885 L_SUB(&pp->lastrec, &up->wigbot[i]);
886 if (L_ISNEG(&pp->lastrec))
890 pp->lastrec = up->wigbot[i];
894 up->wuggle = pp->lastrec;
895 refclock_process(pp);
896 #else /* IRIG_SUCKS */
897 pp->lastref = pp->lastrec;
898 up->wuggle = pp->lastrec;
899 refclock_process(pp);
900 #endif /* IRIG_SUCKS */
904 up->bitcnt = (up->bitcnt + 1) % SUBFLD;
905 if (up->bitcnt == 0) {
908 * End of subfield. Encode two hexadecimal digits in
909 * little-endian timecode field.
911 if (up->fieldcnt == 0)
914 up->xptr = 2 * FIELD;
915 up->timecode[--up->xptr] = hexchar[(up->bits >> 5) &
917 up->timecode[--up->xptr] = hexchar[up->bits & 0xf];
918 up->fieldcnt = (up->fieldcnt + 1) % FIELD;
919 if (up->fieldcnt == 0) {
922 * End of field. Decode the timecode and wind
923 * the clock. Not all IRIG generators have the
924 * year; if so, it is nonzero after year 2000.
925 * Not all have the hardware status bit; if so,
926 * it is lit when the source is okay and dim
927 * when bad. We watch this only if the year is
928 * nonzero. Not all are configured for signature
929 * control. If so, all BCD digits are set to
930 * zero if the source is bad. In this case the
931 * refclock_process() will reject the timecode
934 up->xptr = 2 * FIELD;
935 if (sscanf((char *)up->timecode,
936 "%6s%2d%c%2s%3d%2d%2d%2d", sbs, &pp->year,
937 &syncchar, spare, &pp->day, &pp->hour,
938 &pp->minute, &pp->second) != 8)
939 pp->leap = LEAP_NOTINSYNC;
941 pp->leap = LEAP_NOWARNING;
942 up->second = (up->second + up->decim) % 60;
945 if (pp->second != up->second)
946 up->errflg |= IRIG_ERR_CHECK;
947 up->second = pp->second;
948 sprintf(pp->a_lastcode,
949 "%02x %c %2d %3d %02d:%02d:%02d %4.0f %3d %6.3f %2d %6.1f %6.1f %s",
950 up->errflg, syncchar, pp->year, pp->day,
951 pp->hour, pp->minute, pp->second,
952 up->maxsignal, up->gain, up->modndx,
953 up->tc, up->exing * 1e6 / SECOND, up->freq *
954 1e6 / SECOND, ulfptoa(&up->wuggle, 6));
955 pp->lencode = strlen(pp->a_lastcode);
956 if (pp->sloppyclockflag & CLK_FLAG4) {
957 record_clock_stats(&peer->srcadr,
972 * irig_poll - called by the transmit procedure
974 * This routine sweeps up the timecode updates since the last poll. For
975 * IRIG-B there should be at least 60 updates; for IRIG-E there should
976 * be at least 6. If nothing is heard, a timeout event is declared and
977 * any orphaned timecode updates are sent to foster care.
981 int unit, /* instance number (not used) */
982 struct peer *peer /* peer structure pointer */
985 struct refclockproc *pp;
989 up = (struct irigunit *)pp->unitptr;
991 if (pp->coderecv == pp->codeproc) {
992 refclock_report(peer, CEVNT_TIMEOUT);
996 refclock_receive(peer);
997 record_clock_stats(&peer->srcadr, pp->a_lastcode);
1000 printf("irig: %s\n", pp->a_lastcode);
1009 * irig_gain - adjust codec gain
1011 * This routine is called once each second. If the signal envelope
1012 * amplitude is too low, the codec gain is bumped up by four units; if
1013 * too high, it is bumped down. The decoder is relatively insensitive to
1014 * amplitude, so this crudity works just fine. The input port is set and
1015 * the error flag is cleared, mostly to be ornery.
1019 struct peer *peer /* peer structure pointer */
1022 struct refclockproc *pp;
1023 struct irigunit *up;
1026 up = (struct irigunit *)pp->unitptr;
1029 * Apparently, the codec uses only the high order bits of the
1030 * gain control field. Thus, it may take awhile for changes to
1031 * wiggle the hardware bits.
1033 if (up->clipcnt == 0) {
1035 if (up->gain > MAXGAIN)
1037 } else if (up->clipcnt > MAXCLP) {
1042 audio_gain(up->gain, up->mongain, up->port);
1047 int refclock_irig_bs;
1048 #endif /* REFCLOCK */