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 wrong
79 * 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 -d -d on the ntpd command
101 * line), the driver produces one line for each timecode in the
104 * 00 1 98 23 19:26:52 721 143 0.694 47 20 0.083 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 * field phase (0-79), time constant (2-20), modulation index (0-1),
115 * carrier phase error (0+-0.5) and carrier frequency error (PPM). The
116 * last field is the on-time 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. With an UltrSPARC 30, this thing can keep
120 * the clock within a few tens of microseconds relative to the IRIG-B
121 * signal. Accuracy with IRIG-E is about ten times worse.
123 * Unlike other drivers, which can have multiple instantiations, this
124 * one supports only one. It does not seem likely that more than one
125 * audio codec would be useful in a single machine. More than one would
126 * probably chew up too much CPU time anyway.
130 * Fudge flag2 selects the audio input port, where 0 is the mike port
131 * (default) and 1 is the line-in port. It does not seem useful to
132 * select the compact disc player port. Fudge flag3 enables audio
133 * monitoring of the input signal. For this purpose, the speaker volume
134 * must be set before the driver is started. Fudge flag4 causes the
135 * debugging output described above to be recorded in the clockstats
136 * file. Any of these flags can be changed during operation with the
141 * Interface definitions
143 #define DEVICE_AUDIO "/dev/audio" /* audio device name */
144 #define PRECISION (-17) /* precision assumed (about 10 us) */
145 #define REFID "IRIG" /* reference ID */
146 #define DESCRIPTION "Generic IRIG Audio Driver" /* WRU */
148 #define SECOND 8000 /* nominal sample rate (Hz) */
149 #define BAUD 80 /* samples per baud interval */
150 #define OFFSET 128 /* companded sample offset */
151 #define SIZE 256 /* decompanding table size */
152 #define CYCLE 8 /* samples per carrier cycle */
153 #define SUBFLD 10 /* bits per subfield */
154 #define FIELD 10 /* subfields per field */
155 #define MINTC 2 /* min PLL time constant */
156 #define MAXTC 20 /* max PLL time constant max */
157 #define MAXSIG 6000. /* maximum signal level */
158 #define DRPOUT 100. /* dropout signal level */
159 #define MODMIN 0.5 /* minimum modulation index */
160 #define MAXFREQ (250e-6 * SECOND) /* freq tolerance (.025%) */
161 #define PI 3.1415926535 /* the real thing */
164 * Experimentally determined fudge factors
166 #define IRIG_B .0019 /* IRIG-B phase delay */
167 #define IRIG_E .0019 /* IRIG-E phase delay */
170 * Data bit definitions
172 #define BIT0 0 /* zero */
173 #define BIT1 1 /* one */
174 #define BITP 2 /* position identifier */
177 * Error flags (up->errflg)
179 #define IRIG_ERR_AMP 0x01 /* low carrier amplitude */
180 #define IRIG_ERR_FREQ 0x02 /* frequency tolerance exceeded */
181 #define IRIG_ERR_MOD 0x04 /* low modulation index */
182 #define IRIG_ERR_SYNCH 0x08 /* frame synch error */
183 #define IRIG_ERR_DECODE 0x10 /* frame decoding error */
184 #define IRIG_ERR_CHECK 0x20 /* second numbering discrepancy */
185 #define IRIG_ERR_ERROR 0x40 /* codec error (overrun) */
188 * IRIG unit control structure
191 u_char timecode[21]; /* timecode string */
192 l_fp timestamp; /* audio sample timestamp */
193 l_fp tick; /* audio sample increment */
194 double comp[SIZE]; /* decompanding table */
195 double integ[BAUD]; /* baud integrator */
196 double phase, freq; /* logical clock phase and frequency */
197 double zxing; /* phase detector integrator */
198 double yxing; /* phase detector display */
199 double modndx; /* modulation index */
200 double irig_b; /* IRIG-B signal amplitude */
201 double irig_e; /* IRIG-E signal amplitude */
202 int errflg; /* error flags */
203 int bufcnt; /* samples in buffer */
204 int bufptr; /* buffer index pointer */
205 int pollcnt; /* poll counter */
206 int port; /* codec port */
207 int gain; /* codec gain */
208 int clipcnt; /* sample clipped count */
209 int seccnt; /* second interval counter */
210 int decim; /* sample decimation factor */
215 double hpf[5]; /* IRIG-B filter shift register */
216 double lpf[5]; /* IRIG-E filter shift register */
217 double intmin, intmax; /* integrated envelope min and max */
218 double envmax; /* peak amplitude */
219 double envmin; /* noise amplitude */
220 double maxsignal; /* integrated peak amplitude */
221 double noise; /* integrated noise amplitude */
222 double lastenv[CYCLE]; /* last cycle amplitudes */
223 double lastint[CYCLE]; /* last integrated cycle amplitudes */
224 double lastsig; /* last carrier sample */
225 double xxing; /* phase detector interpolated output */
226 double fdelay; /* filter delay */
227 int envphase; /* envelope phase */
228 int envptr; /* envelope phase pointer */
229 int carphase; /* carrier phase */
230 int envsw; /* envelope state */
231 int envxing; /* envelope slice crossing */
232 int tc; /* time constant */
233 int tcount; /* time constant counter */
234 int badcnt; /* decimation interval counter */
239 l_fp montime; /* reference timestamp for eyeball */
240 int timecnt; /* timecode counter */
241 int pulse; /* cycle counter */
242 int cycles; /* carrier cycles */
243 int dcycles; /* data cycles */
244 int xptr; /* translate table pointer */
245 int lastbit; /* last code element length */
246 int second; /* previous second */
247 int fieldcnt; /* subfield count in field */
248 int bits; /* demodulated bits */
249 int bitcnt; /* bit count in subfield */
253 * Function prototypes
255 static int irig_start P((int, struct peer *));
256 static void irig_shutdown P((int, struct peer *));
257 static void irig_receive P((struct recvbuf *));
258 static void irig_poll P((int, struct peer *));
261 * More function prototypes
263 static void irig_base P((struct peer *, double));
264 static void irig_rf P((struct peer *, double));
265 static void irig_decode P((struct peer *, int));
266 static void irig_gain P((struct peer *));
271 struct refclock refclock_irig = {
272 irig_start, /* start up driver */
273 irig_shutdown, /* shut down driver */
274 irig_poll, /* transmit poll message */
275 noentry, /* not used (old irig_control) */
276 noentry, /* initialize driver (not used) */
277 noentry, /* not used (old irig_buginfo) */
278 NOFLAGS /* not used */
284 static char hexchar[] = { /* really quick decoding table */
285 '0', '8', '4', 'c', /* 0000 0001 0010 0011 */
286 '2', 'a', '6', 'e', /* 0100 0101 0110 0111 */
287 '1', '9', '5', 'd', /* 1000 1001 1010 1011 */
288 '3', 'b', '7', 'f' /* 1100 1101 1110 1111 */
293 * irig_start - open the devices and initialize data for processing
297 int unit, /* instance number (not used) */
298 struct peer *peer /* peer structure pointer */
301 struct refclockproc *pp;
307 int fd; /* file descriptor */
309 double step; /* codec adjustment */
314 fd = audio_init(DEVICE_AUDIO);
323 * Allocate and initialize unit structure
325 if (!(up = (struct irigunit *)
326 emalloc(sizeof(struct irigunit)))) {
330 memset((char *)up, 0, sizeof(struct irigunit));
332 pp->unitptr = (caddr_t)up;
333 pp->io.clock_recv = irig_receive;
334 pp->io.srcclock = (caddr_t)peer;
337 if (!io_addclock(&pp->io)) {
344 * Initialize miscellaneous variables
346 peer->precision = PRECISION;
347 pp->clockdesc = DESCRIPTION;
348 memcpy((char *)&pp->refid, REFID, 4);
356 * The companded samples are encoded sign-magnitude. The table
357 * contains all the 256 values in the interest of speed.
359 up->comp[0] = up->comp[OFFSET] = 0.;
360 up->comp[1] = 1; up->comp[OFFSET + 1] = -1.;
361 up->comp[2] = 3; up->comp[OFFSET + 2] = -3.;
363 for (i = 3; i < OFFSET; i++) {
364 up->comp[i] = up->comp[i - 1] + step;
365 up->comp[OFFSET + i] = -up->comp[i];
369 DTOLFP(1. / SECOND, &up->tick);
375 * irig_shutdown - shut down the clock
379 int unit, /* instance number (not used) */
380 struct peer *peer /* peer structure pointer */
383 struct refclockproc *pp;
387 up = (struct irigunit *)pp->unitptr;
388 io_closeclock(&pp->io);
394 * irig_receive - receive data from the audio device
396 * This routine reads input samples and adjusts the logical clock to
397 * track the irig clock by dropping or duplicating codec samples.
401 struct recvbuf *rbufp /* receive buffer structure pointer */
405 struct refclockproc *pp;
411 double sample; /* codec sample */
412 u_char *dpt; /* buffer pointer */
413 l_fp ltemp; /* l_fp temp */
415 peer = (struct peer *)rbufp->recv_srcclock;
417 up = (struct irigunit *)pp->unitptr;
420 * Main loop - read until there ain't no more. Note codec
421 * samples are bit-inverted.
423 up->timestamp = rbufp->recv_time;
424 up->bufcnt = rbufp->recv_length;
425 DTOLFP((double)up->bufcnt / SECOND, <emp);
426 L_SUB(&up->timestamp, <emp);
427 dpt = rbufp->recv_buffer;
428 for (up->bufptr = 0; up->bufptr < up->bufcnt; up->bufptr++) {
429 sample = up->comp[~*dpt++ & 0xff];
432 * Clip noise spikes greater than MAXSIG. If no clips,
433 * increase the gain a tad; if the clips are too high,
434 * decrease a tad. Choose either IRIG-B or IRIG-E
435 * according to the energy at the respective filter
438 if (sample > MAXSIG) {
441 } else if (sample < -MAXSIG) {
447 * Variable frequency oscillator. A phase change of one
448 * unit produces a change of 360 degrees; a frequency
449 * change of one unit produces a change of 1 Hz.
451 up->phase += up->freq / SECOND;
452 if (up->phase >= .5) {
454 } else if (up->phase < -.5) {
456 irig_rf(peer, sample);
457 irig_rf(peer, sample);
459 irig_rf(peer, sample);
461 L_ADD(&up->timestamp, &up->tick);
464 * Once each second, determine the IRIG format, codec
467 up->seccnt = (up->seccnt + 1) % SECOND;
468 if (up->seccnt == 0) {
469 if (up->irig_b > up->irig_e) {
476 if (pp->sloppyclockflag & CLK_FLAG2)
481 up->irig_b = up->irig_e = 0;
486 * Squawk to the monitor speaker if enabled.
488 if (pp->sloppyclockflag & CLK_FLAG3)
489 if (write(pp->io.fd, (u_char *)&rbufp->recv_space,
490 (u_int)up->bufcnt) < 0)
495 * irig_rf - RF processing
497 * This routine filters the RF signal using a highpass filter for IRIG-B
498 * and a lowpass filter for IRIG-E. In case of IRIG-E, the samples are
499 * decimated by a factor of ten. The lowpass filter functions also as a
500 * decimation filter in this case. Note that the codec filters function
501 * as roofing filters to attenuate both the high and low ends of the
502 * passband. IIR filter coefficients were determined using Matlab Signal
503 * Processing Toolkit.
507 struct peer *peer, /* peer structure pointer */
508 double sample /* current signal sample */
511 struct refclockproc *pp;
517 double irig_b, irig_e; /* irig filter outputs */
520 up = (struct irigunit *)pp->unitptr;
523 * IRIG-B filter. 4th-order elliptic, 800-Hz highpass, 0.3 dB
524 * passband ripple, -50 dB stopband ripple, phase delay -.0022
527 irig_b = (up->hpf[4] = up->hpf[3]) * 2.322484e-01;
528 irig_b += (up->hpf[3] = up->hpf[2]) * -1.103929e+00;
529 irig_b += (up->hpf[2] = up->hpf[1]) * 2.351081e+00;
530 irig_b += (up->hpf[1] = up->hpf[0]) * -2.335036e+00;
531 up->hpf[0] = sample - irig_b;
532 irig_b = up->hpf[0] * 4.335855e-01
533 + up->hpf[1] * -1.695859e+00
534 + up->hpf[2] * 2.525004e+00
535 + up->hpf[3] * -1.695859e+00
536 + up->hpf[4] * 4.335855e-01;
537 up->irig_b += irig_b * irig_b;
540 * IRIG-E filter. 4th-order elliptic, 130-Hz lowpass, 0.3 dB
541 * passband ripple, -50 dB stopband ripple, phase delay .0219 s.
543 irig_e = (up->lpf[4] = up->lpf[3]) * 8.694604e-01;
544 irig_e += (up->lpf[3] = up->lpf[2]) * -3.589893e+00;
545 irig_e += (up->lpf[2] = up->lpf[1]) * 5.570154e+00;
546 irig_e += (up->lpf[1] = up->lpf[0]) * -3.849667e+00;
547 up->lpf[0] = sample - irig_e;
548 irig_e = up->lpf[0] * 3.215696e-03
549 + up->lpf[1] * -1.174951e-02
550 + up->lpf[2] * 1.712074e-02
551 + up->lpf[3] * -1.174951e-02
552 + up->lpf[4] * 3.215696e-03;
553 up->irig_e += irig_e * irig_e;
556 * Decimate by a factor of either 1 (IRIG-B) or 10 (IRIG-E).
558 up->badcnt = (up->badcnt + 1) % up->decim;
559 if (up->badcnt == 0) {
561 irig_base(peer, irig_b);
563 irig_base(peer, irig_e);
568 * irig_base - baseband processing
570 * This routine processes the baseband signal and demodulates the AM
571 * carrier using a synchronous detector. It then synchronizes to the
572 * data frame at the baud rate and decodes the data pulses.
576 struct peer *peer, /* peer structure pointer */
577 double sample /* current signal sample */
580 struct refclockproc *pp;
586 double lope; /* integrator output */
587 double env; /* envelope detector output */
588 double dtemp; /* double temp */
589 int i; /* index temp */
592 up = (struct irigunit *)pp->unitptr;
595 * Synchronous baud integrator. Corresponding samples of current
596 * and past baud intervals are integrated to refine the envelope
597 * amplitude and phase estimate. We keep one cycle of both the
598 * raw and integrated data for later use.
600 up->envphase = (up->envphase + 1) % BAUD;
601 up->carphase = (up->carphase + 1) % CYCLE;
602 up->integ[up->envphase] += (sample - up->integ[up->envphase]) /
604 lope = up->integ[up->envphase];
605 up->lastenv[up->carphase] = sample;
606 up->lastint[up->carphase] = lope;
609 * Phase detector. Sample amplitudes are integrated over the
610 * baud interval. Cycle phase is determined from these
611 * amplitudes using an eight-sample cyclic buffer. A phase
612 * change of 360 degrees produces an output change of one unit.
614 if (up->lastsig > 0 && lope <= 0) {
615 up->xxing = lope / (up->lastsig - lope);
616 up->zxing += (up->carphase - 4 + up->xxing) / 8.;
621 * Update signal/noise estimates and PLL phase/frequency.
623 if (up->envphase == 0) {
626 * Update envelope signal and noise estimates and mess
629 up->maxsignal = up->intmax;
630 up->noise = up->intmin;
631 if (up->maxsignal < DRPOUT)
632 up->errflg |= IRIG_ERR_AMP;
634 up->modndx = (up->intmax - up->intmin) / up->intmax;
637 if (up->modndx < MODMIN)
638 up->errflg |= IRIG_ERR_MOD;
639 up->intmin = 1e6; up->intmax = 0;
640 if (up->errflg & (IRIG_ERR_AMP | IRIG_ERR_FREQ |
641 IRIG_ERR_MOD | IRIG_ERR_SYNCH)) {
647 * Update PLL phase and frequency. The PLL time constant
648 * is set initially to stabilize the frequency within a
649 * minute or two, then increases to the maximum. The
650 * frequency is clamped so that the PLL capture range
651 * cannot be exceeded.
653 dtemp = up->zxing * up->decim / BAUD;
656 up->phase += dtemp / up->tc;
657 up->freq += dtemp / (4. * up->tc * up->tc);
658 if (up->freq > MAXFREQ) {
660 up->errflg |= IRIG_ERR_FREQ;
661 } else if (up->freq < -MAXFREQ) {
663 up->errflg |= IRIG_ERR_FREQ;
668 * Synchronous demodulator. There are eight samples in the cycle
669 * and ten cycles in the baud interval. The amplitude of each
670 * cycle is determined at the last sample in the cycle. The
671 * beginning of the data pulse is determined from the integrated
672 * samples, while the end of the pulse is determined from the
673 * raw samples. The raw data bits are demodulated relative to
674 * the slice level and left-shifted in the decoding register.
676 if (up->carphase != 7)
678 env = (up->lastenv[2] - up->lastenv[6]) / 2.;
679 lope = (up->lastint[2] - up->lastint[6]) / 2.;
680 if (lope > up->intmax)
682 if (lope < up->intmin)
686 * Pulse code demodulator and reference timestamp. The decoder
687 * looks for a sequence of ten bits; the first two bits must be
688 * one, the last two bits must be zero. Frame synch is asserted
689 * when three correct frames have been found.
691 up->pulse = (up->pulse + 1) % 10;
694 else if (up->pulse == 9)
697 if (env >= (up->envmax + up->envmin) / 2.)
700 if (lope >= (up->maxsignal + up->noise) / 2.)
702 if ((up->cycles & 0x303c0f03) == 0x300c0300) {
707 * The PLL time constant starts out small, in order to
708 * sustain a frequency tolerance of 250 PPM. It
709 * gradually increases as the loop settles down. Note
710 * that small wiggles are not believed, unless they
711 * persist for lots of samples.
714 up->errflg |= IRIG_ERR_SYNCH;
716 dtemp = BAUD - up->zxing;
717 i = up->envxing - up->envphase;
722 if (up->tcount > 50 * up->tc) {
727 up->envxing = up->envphase;
729 dtemp -= up->envxing - up->envphase;
733 up->envxing = up->envphase;
737 * Determine a reference timestamp, accounting for the
738 * codec delay and filter delay. Note the timestamp is
739 * for the previous frame, so we have to backtrack for
740 * this plus the delay since the last carrier positive
743 DTOLFP(up->decim * (dtemp / SECOND + 1.) + up->fdelay,
745 pp->lastrec = up->timestamp;
746 L_SUB(&pp->lastrec, <emp);
749 * The data bits are collected in ten-bit frames. The
750 * first two and last two bits are determined by frame
751 * sync and ignored here; the resulting patterns
752 * represent zero (0-1 bits), one (2-4 bits) and
753 * position identifier (5-6 bits). The remaining
754 * patterns represent errors and are treated as zeros.
756 bitz = up->dcycles & 0xfc;
761 irig_decode(peer, BIT0);
767 irig_decode(peer, BIT1);
772 irig_decode(peer, BITP);
776 irig_decode(peer, 0);
777 up->errflg |= IRIG_ERR_DECODE;
784 * irig_decode - decode the data
786 * This routine assembles bits into digits, digits into subfields and
787 * subfields into the timecode field. Bits can have values of zero, one
788 * or position identifier. There are four bits per digit, two digits per
789 * subfield and ten subfields per field. The last bit in every subfield
790 * and the first bit in the first subfield are position identifiers.
794 struct peer *peer, /* peer structure pointer */
795 int bit /* data bit (0, 1 or 2) */
798 struct refclockproc *pp;
804 char syncchar; /* sync character (Spectracom only) */
805 char sbs[6]; /* binary seconds since 0h */
806 char spare[2]; /* mulligan digits */
809 up = (struct irigunit *)pp->unitptr;
812 * Assemble subfield bits.
817 } else if (bit == BITP && up->lastbit == BITP) {
820 * Frame sync - two adjacent position identifiers.
821 * Monitor the reference timestamp and wiggle the
822 * clock, but only if no errors have occurred.
827 up->montime = pp->lastrec;
828 if (up->errflg == 0) {
830 refclock_process(pp);
832 if (up->timecnt >= MAXSTAGE) {
833 refclock_receive(peer);
839 up->bitcnt = (up->bitcnt + 1) % SUBFLD;
840 if (up->bitcnt == 0) {
843 * End of subfield. Encode two hexadecimal digits in
844 * little-endian timecode field.
846 if (up->fieldcnt == 0)
849 up->xptr = 2 * FIELD;
850 up->timecode[--up->xptr] = hexchar[(up->bits >> 5) &
852 up->timecode[--up->xptr] = hexchar[up->bits & 0xf];
853 up->fieldcnt = (up->fieldcnt + 1) % FIELD;
854 if (up->fieldcnt == 0) {
857 * End of field. Decode the timecode, adjust the
858 * gain and set the input port. Set the port
859 * here on the assumption somebody might even
862 up->xptr = 2 * FIELD;
863 if (sscanf((char *)up->timecode,
864 "%6s%2d%c%2s%3d%2d%2d%2d",
865 sbs, &pp->year, &syncchar, spare, &pp->day,
866 &pp->hour, &pp->minute, &pp->second) != 8)
867 pp->leap = LEAP_NOTINSYNC;
869 pp->leap = LEAP_NOWARNING;
870 up->second = (up->second + up->decim) % 60;
871 if (pp->second != up->second)
872 up->errflg |= IRIG_ERR_CHECK;
873 up->second = pp->second;
874 sprintf(pp->a_lastcode,
875 "%02x %c %2d %3d %02d:%02d:%02d %4.0f %3d %6.3f %2d %2d %6.3f %6.1f %s",
876 up->errflg, syncchar, pp->year, pp->day,
877 pp->hour, pp->minute, pp->second,
878 up->maxsignal, up->gain, up->modndx,
879 up->envxing, up->tc, up->yxing, up->freq *
880 1e6 / SECOND, ulfptoa(&up->montime, 6));
881 pp->lencode = strlen(pp->a_lastcode);
882 if (up->timecnt == 0 || pp->sloppyclockflag &
884 record_clock_stats(&peer->srcadr,
888 printf("irig: %s\n", pp->a_lastcode);
897 * irig_poll - called by the transmit procedure
899 * This routine keeps track of status. If nothing is heard for two
900 * successive poll intervals, a timeout event is declared and any
901 * orphaned timecode updates are sent to foster care.
905 int unit, /* instance number (not used) */
906 struct peer *peer /* peer structure pointer */
909 struct refclockproc *pp;
913 up = (struct irigunit *)pp->unitptr;
916 * Keep book for tattletales
918 if (up->pollcnt == 0) {
919 refclock_report(peer, CEVNT_TIMEOUT);
930 * irig_gain - adjust codec gain
932 * This routine is called once each second. If the signal envelope
933 * amplitude is too low, the codec gain is bumped up by four units; if
934 * too high, it is bumped down. The decoder is relatively insensitive to
935 * amplitude, so this crudity works just fine. The input port is set and
936 * the error flag is cleared, mostly to be ornery.
940 struct peer *peer /* peer structure pointer */
943 struct refclockproc *pp;
947 up = (struct irigunit *)pp->unitptr;
950 * Apparently, the codec uses only the high order bits of the
951 * gain control field. Thus, it may take awhile for changes to
952 * wiggle the hardware bits.
954 if (up->clipcnt == 0) {
958 } else if (up->clipcnt > SECOND / 100) {
963 audio_gain(up->gain, up->port);
969 int refclock_irig_bs;
970 #endif /* REFCLOCK */