xref: /netbsd-src/external/bsd/ntp/dist/ntpd/refclock_irig.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: refclock_irig.c,v 1.10 2024/08/18 20:47:18 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel  * refclock_irig - audio IRIG-B/E demodulator/decoder
5abb0f93cSkardel  */
6abb0f93cSkardel #ifdef HAVE_CONFIG_H
7abb0f93cSkardel #include <config.h>
8abb0f93cSkardel #endif
9abb0f93cSkardel 
10abb0f93cSkardel #if defined(REFCLOCK) && defined(CLOCK_IRIG)
11abb0f93cSkardel 
12abb0f93cSkardel #include "ntpd.h"
13abb0f93cSkardel #include "ntp_io.h"
14abb0f93cSkardel #include "ntp_refclock.h"
15abb0f93cSkardel #include "ntp_calendar.h"
16abb0f93cSkardel #include "ntp_stdlib.h"
17abb0f93cSkardel 
18abb0f93cSkardel #include <stdio.h>
19abb0f93cSkardel #include <ctype.h>
20abb0f93cSkardel #include <math.h>
21abb0f93cSkardel #ifdef HAVE_SYS_IOCTL_H
22abb0f93cSkardel #include <sys/ioctl.h>
23abb0f93cSkardel #endif /* HAVE_SYS_IOCTL_H */
24abb0f93cSkardel 
25abb0f93cSkardel #include "audio.h"
26abb0f93cSkardel 
27abb0f93cSkardel /*
28abb0f93cSkardel  * Audio IRIG-B/E demodulator/decoder
29abb0f93cSkardel  *
30abb0f93cSkardel  * This driver synchronizes the computer time using data encoded in
31abb0f93cSkardel  * IRIG-B/E signals commonly produced by GPS receivers and other timing
32abb0f93cSkardel  * devices. The IRIG signal is an amplitude-modulated carrier with
33abb0f93cSkardel  * pulse-width modulated data bits. For IRIG-B, the carrier frequency is
34abb0f93cSkardel  * 1000 Hz and bit rate 100 b/s; for IRIG-E, the carrier frequenchy is
35abb0f93cSkardel  * 100 Hz and bit rate 10 b/s. The driver automatically recognizes which
36abb0f93cSkardel  & format is in use.
37abb0f93cSkardel  *
38abb0f93cSkardel  * The driver requires an audio codec or sound card with sampling rate 8
39abb0f93cSkardel  * kHz and mu-law companding. This is the same standard as used by the
40abb0f93cSkardel  * telephone industry and is supported by most hardware and operating
41abb0f93cSkardel  * systems, including Solaris, SunOS, FreeBSD, NetBSD and Linux. In this
42abb0f93cSkardel  * implementation, only one audio driver and codec can be supported on a
43abb0f93cSkardel  * single machine.
44abb0f93cSkardel  *
45abb0f93cSkardel  * The program processes 8000-Hz mu-law companded samples using separate
46abb0f93cSkardel  * signal filters for IRIG-B and IRIG-E, a comb filter, envelope
47abb0f93cSkardel  * detector and automatic threshold corrector. Cycle crossings relative
48abb0f93cSkardel  * to the corrected slice level determine the width of each pulse and
49abb0f93cSkardel  * its value - zero, one or position identifier.
50abb0f93cSkardel  *
51abb0f93cSkardel  * The data encode 20 BCD digits which determine the second, minute,
52abb0f93cSkardel  * hour and day of the year and sometimes the year and synchronization
53abb0f93cSkardel  * condition. The comb filter exponentially averages the corresponding
54abb0f93cSkardel  * samples of successive baud intervals in order to reliably identify
55abb0f93cSkardel  * the reference carrier cycle. A type-II phase-lock loop (PLL) performs
56abb0f93cSkardel  * additional integration and interpolation to accurately determine the
57abb0f93cSkardel  * zero crossing of that cycle, which determines the reference
58abb0f93cSkardel  * timestamp. A pulse-width discriminator demodulates the data pulses,
59abb0f93cSkardel  * which are then encoded as the BCD digits of the timecode.
60abb0f93cSkardel  *
61abb0f93cSkardel  * The timecode and reference timestamp are updated once each second
62abb0f93cSkardel  * with IRIG-B (ten seconds with IRIG-E) and local clock offset samples
63abb0f93cSkardel  * saved for later processing. At poll intervals of 64 s, the saved
64abb0f93cSkardel  * samples are processed by a trimmed-mean filter and used to update the
65abb0f93cSkardel  * system clock.
66abb0f93cSkardel  *
67abb0f93cSkardel  * An automatic gain control feature provides protection against
68abb0f93cSkardel  * overdriven or underdriven input signal amplitudes. It is designed to
69abb0f93cSkardel  * maintain adequate demodulator signal amplitude while avoiding
70abb0f93cSkardel  * occasional noise spikes. In order to assure reliable capture, the
71abb0f93cSkardel  * decompanded input signal amplitude must be greater than 100 units and
72abb0f93cSkardel  * the codec sample frequency error less than 250 PPM (.025 percent).
73abb0f93cSkardel  *
74abb0f93cSkardel  * Monitor Data
75abb0f93cSkardel  *
76abb0f93cSkardel  * The timecode format used for debugging and data recording includes
77abb0f93cSkardel  * data helpful in diagnosing problems with the IRIG signal and codec
78abb0f93cSkardel  * connections. The driver produces one line for each timecode in the
79abb0f93cSkardel  * following format:
80abb0f93cSkardel  *
81abb0f93cSkardel  * 00 00 98 23 19:26:52 2782 143 0.694 10 0.3 66.5 3094572411.00027
82abb0f93cSkardel  *
83abb0f93cSkardel  * If clockstats is enabled, the most recent line is written to the
84abb0f93cSkardel  * clockstats file every 64 s. If verbose recording is enabled (fudge
85abb0f93cSkardel  * flag 4) each line is written as generated.
86abb0f93cSkardel  *
87abb0f93cSkardel  * The first field containes the error flags in hex, where the hex bits
88abb0f93cSkardel  * are interpreted as below. This is followed by the year of century,
89abb0f93cSkardel  * day of year and time of day. Note that the time of day is for the
90abb0f93cSkardel  * previous minute, not the current time. The status indicator and year
91abb0f93cSkardel  * are not produced by some IRIG devices and appear as zeros. Following
92abb0f93cSkardel  * these fields are the carrier amplitude (0-3000), codec gain (0-255),
93abb0f93cSkardel  * modulation index (0-1), time constant (4-10), carrier phase error
94abb0f93cSkardel  * +-.5) and carrier frequency error (PPM). The last field is the on-
95abb0f93cSkardel  * time timestamp in NTP format.
96abb0f93cSkardel  *
97abb0f93cSkardel  * The error flags are defined as follows in hex:
98abb0f93cSkardel  *
99abb0f93cSkardel  * x01	Low signal. The carrier amplitude is less than 100 units. This
100abb0f93cSkardel  *	is usually the result of no signal or wrong input port.
101abb0f93cSkardel  * x02	Frequency error. The codec frequency error is greater than 250
102abb0f93cSkardel  *	PPM. This may be due to wrong signal format or (rarely)
103abb0f93cSkardel  *	defective codec.
104abb0f93cSkardel  * x04	Modulation error. The IRIG modulation index is less than 0.5.
105abb0f93cSkardel  *	This is usually the result of an overdriven codec, wrong signal
106abb0f93cSkardel  *	format or wrong input port.
107abb0f93cSkardel  * x08	Frame synch error. The decoder frame does not match the IRIG
108abb0f93cSkardel  *	frame. This is usually the result of an overdriven codec, wrong
109abb0f93cSkardel  *	signal format or noisy IRIG signal. It may also be the result of
110abb0f93cSkardel  *	an IRIG signature check which indicates a failure of the IRIG
111abb0f93cSkardel  *	signal synchronization source.
112abb0f93cSkardel  * x10	Data bit error. The data bit length is out of tolerance. This is
113abb0f93cSkardel  *	usually the result of an overdriven codec, wrong signal format
114abb0f93cSkardel  *	or noisy IRIG signal.
115abb0f93cSkardel  * x20	Seconds numbering discrepancy. The decoder second does not match
116abb0f93cSkardel  *	the IRIG second. This is usually the result of an overdriven
117abb0f93cSkardel  *	codec, wrong signal format or noisy IRIG signal.
118abb0f93cSkardel  * x40	Codec error (overrun). The machine is not fast enough to keep up
119abb0f93cSkardel  *	with the codec.
120abb0f93cSkardel  * x80	Device status error (Spectracom).
121abb0f93cSkardel  *
122abb0f93cSkardel  *
123abb0f93cSkardel  * Once upon a time, an UltrSPARC 30 and Solaris 2.7 kept the clock
124abb0f93cSkardel  * within a few tens of microseconds relative to the IRIG-B signal.
125abb0f93cSkardel  * Accuracy with IRIG-E was about ten times worse. Unfortunately, Sun
126abb0f93cSkardel  * broke the 2.7 audio driver in 2.8, which has a 10-ms sawtooth
127abb0f93cSkardel  * modulation.
128abb0f93cSkardel  *
129abb0f93cSkardel  * Unlike other drivers, which can have multiple instantiations, this
130abb0f93cSkardel  * one supports only one. It does not seem likely that more than one
131abb0f93cSkardel  * audio codec would be useful in a single machine. More than one would
132abb0f93cSkardel  * probably chew up too much CPU time anyway.
133abb0f93cSkardel  *
134abb0f93cSkardel  * Fudge factors
135abb0f93cSkardel  *
136abb0f93cSkardel  * Fudge flag4 causes the dubugging output described above to be
137abb0f93cSkardel  * recorded in the clockstats file. Fudge flag2 selects the audio input
138abb0f93cSkardel  * port, where 0 is the mike port (default) and 1 is the line-in port.
139abb0f93cSkardel  * It does not seem useful to select the compact disc player port. Fudge
140abb0f93cSkardel  * flag3 enables audio monitoring of the input signal. For this purpose,
141abb0f93cSkardel  * the monitor gain is set t a default value. Fudgetime2 is used as a
142abb0f93cSkardel  * frequency vernier for broken codec sample frequency.
143abb0f93cSkardel  *
144abb0f93cSkardel  * Alarm codes
145abb0f93cSkardel  *
146abb0f93cSkardel  * CEVNT_BADTIME	invalid date or time
147abb0f93cSkardel  * CEVNT_TIMEOUT	no IRIG data since last poll
148abb0f93cSkardel  */
149abb0f93cSkardel /*
150abb0f93cSkardel  * Interface definitions
151abb0f93cSkardel  */
152abb0f93cSkardel #define	DEVICE_AUDIO	"/dev/audio" /* audio device name */
153abb0f93cSkardel #define	PRECISION	(-17)	/* precision assumed (about 10 us) */
154abb0f93cSkardel #define	REFID		"IRIG"	/* reference ID */
155abb0f93cSkardel #define	DESCRIPTION	"Generic IRIG Audio Driver" /* WRU */
156abb0f93cSkardel #define	AUDIO_BUFSIZ	320	/* audio buffer size (40 ms) */
157abb0f93cSkardel #define SECOND		8000	/* nominal sample rate (Hz) */
158abb0f93cSkardel #define BAUD		80	/* samples per baud interval */
159abb0f93cSkardel #define OFFSET		128	/* companded sample offset */
160abb0f93cSkardel #define SIZE		256	/* decompanding table size */
161abb0f93cSkardel #define CYCLE		8	/* samples per bit */
162abb0f93cSkardel #define SUBFLD		10	/* bits per frame */
163abb0f93cSkardel #define FIELD		100	/* bits per second */
164abb0f93cSkardel #define MINTC		2	/* min PLL time constant */
165abb0f93cSkardel #define MAXTC		10	/* max PLL time constant max */
166abb0f93cSkardel #define	MAXAMP		3000.	/* maximum signal amplitude */
167abb0f93cSkardel #define	MINAMP		2000.	/* minimum signal amplitude */
168abb0f93cSkardel #define DRPOUT		100.	/* dropout signal amplitude */
169abb0f93cSkardel #define MODMIN		0.5	/* minimum modulation index */
170abb0f93cSkardel #define MAXFREQ		(250e-6 * SECOND) /* freq tolerance (.025%) */
171abb0f93cSkardel 
172abb0f93cSkardel /*
173abb0f93cSkardel  * The on-time synchronization point is the positive-going zero crossing
174abb0f93cSkardel  * of the first cycle of the second. The IIR baseband filter phase delay
175abb0f93cSkardel  * is 1.03 ms for IRIG-B and 3.47 ms for IRIG-E. The fudge value 2.68 ms
176abb0f93cSkardel  * due to the codec and other causes was determined by calibrating to a
177abb0f93cSkardel  * PPS signal from a GPS receiver.
178abb0f93cSkardel  *
179abb0f93cSkardel  * The results with a 2.4-GHz P4 running FreeBSD 6.1 are generally
180abb0f93cSkardel  * within .02 ms short-term with .02 ms jitter. The processor load due
181abb0f93cSkardel  * to the driver is 0.51 percent.
182abb0f93cSkardel  */
183abb0f93cSkardel #define IRIG_B	((1.03 + 2.68) / 1000)	/* IRIG-B system delay (s) */
184abb0f93cSkardel #define IRIG_E	((3.47 + 2.68) / 1000)	/* IRIG-E system delay (s) */
185abb0f93cSkardel 
186abb0f93cSkardel /*
187abb0f93cSkardel  * Data bit definitions
188abb0f93cSkardel  */
189abb0f93cSkardel #define BIT0		0	/* zero */
190abb0f93cSkardel #define BIT1		1	/* one */
191abb0f93cSkardel #define BITP		2	/* position identifier */
192abb0f93cSkardel 
193abb0f93cSkardel /*
194abb0f93cSkardel  * Error flags
195abb0f93cSkardel  */
196abb0f93cSkardel #define IRIG_ERR_AMP	0x01	/* low carrier amplitude */
197abb0f93cSkardel #define IRIG_ERR_FREQ	0x02	/* frequency tolerance exceeded */
198abb0f93cSkardel #define IRIG_ERR_MOD	0x04	/* low modulation index */
199abb0f93cSkardel #define IRIG_ERR_SYNCH	0x08	/* frame synch error */
200abb0f93cSkardel #define IRIG_ERR_DECODE	0x10	/* frame decoding error */
201abb0f93cSkardel #define IRIG_ERR_CHECK	0x20	/* second numbering discrepancy */
202abb0f93cSkardel #define IRIG_ERR_ERROR	0x40	/* codec error (overrun) */
203abb0f93cSkardel #define IRIG_ERR_SIGERR	0x80	/* IRIG status error (Spectracom) */
204abb0f93cSkardel 
205abb0f93cSkardel static	char	hexchar[] = "0123456789abcdef";
206abb0f93cSkardel 
207abb0f93cSkardel /*
208abb0f93cSkardel  * IRIG unit control structure
209abb0f93cSkardel  */
210abb0f93cSkardel struct irigunit {
211abb0f93cSkardel 	u_char	timecode[2 * SUBFLD + 1]; /* timecode string */
212abb0f93cSkardel 	l_fp	timestamp;	/* audio sample timestamp */
213abb0f93cSkardel 	l_fp	tick;		/* audio sample increment */
214abb0f93cSkardel 	l_fp	refstamp;	/* reference timestamp */
215abb0f93cSkardel 	l_fp	chrstamp;	/* baud timestamp */
216abb0f93cSkardel 	l_fp	prvstamp;	/* previous baud timestamp */
217abb0f93cSkardel 	double	integ[BAUD];	/* baud integrator */
218abb0f93cSkardel 	double	phase, freq;	/* logical clock phase and frequency */
219abb0f93cSkardel 	double	zxing;		/* phase detector integrator */
220abb0f93cSkardel 	double	yxing;		/* cycle phase */
221abb0f93cSkardel 	double	exing;		/* envelope phase */
222abb0f93cSkardel 	double	modndx;		/* modulation index */
223abb0f93cSkardel 	double	irig_b;		/* IRIG-B signal amplitude */
224abb0f93cSkardel 	double	irig_e;		/* IRIG-E signal amplitude */
225abb0f93cSkardel 	int	errflg;		/* error flags */
226abb0f93cSkardel 	/*
227abb0f93cSkardel 	 * Audio codec variables
228abb0f93cSkardel 	 */
229abb0f93cSkardel 	double	comp[SIZE];	/* decompanding table */
230abb0f93cSkardel 	double	signal;		/* peak signal for AGC */
231abb0f93cSkardel 	int	port;		/* codec port */
232abb0f93cSkardel 	int	gain;		/* codec gain */
233abb0f93cSkardel 	int	mongain;	/* codec monitor gain */
234abb0f93cSkardel 	int	seccnt;		/* second interval counter */
235abb0f93cSkardel 
236abb0f93cSkardel 	/*
237abb0f93cSkardel 	 * RF variables
238abb0f93cSkardel 	 */
239abb0f93cSkardel 	double	bpf[9];		/* IRIG-B filter shift register */
240abb0f93cSkardel 	double	lpf[5];		/* IRIG-E filter shift register */
241abb0f93cSkardel 	double	envmin, envmax;	/* envelope min and max */
242abb0f93cSkardel 	double	slice;		/* envelope slice level */
243abb0f93cSkardel 	double	intmin, intmax;	/* integrated envelope min and max */
244abb0f93cSkardel 	double	maxsignal;	/* integrated peak amplitude */
245abb0f93cSkardel 	double	noise;		/* integrated noise amplitude */
246abb0f93cSkardel 	double	lastenv[CYCLE];	/* last cycle amplitudes */
247abb0f93cSkardel 	double	lastint[CYCLE];	/* last integrated cycle amplitudes */
248abb0f93cSkardel 	double	lastsig;	/* last carrier sample */
249abb0f93cSkardel 	double	fdelay;		/* filter delay */
250abb0f93cSkardel 	int	decim;		/* sample decimation factor */
251abb0f93cSkardel 	int	envphase;	/* envelope phase */
252abb0f93cSkardel 	int	envptr;		/* envelope phase pointer */
253abb0f93cSkardel 	int	envsw;		/* envelope state */
254abb0f93cSkardel 	int	envxing;	/* envelope slice crossing */
255abb0f93cSkardel 	int	tc;		/* time constant */
256abb0f93cSkardel 	int	tcount;		/* time constant counter */
257abb0f93cSkardel 	int	badcnt;		/* decimation interval counter */
258abb0f93cSkardel 
259abb0f93cSkardel 	/*
260abb0f93cSkardel 	 * Decoder variables
261abb0f93cSkardel 	 */
262abb0f93cSkardel 	int	pulse;		/* cycle counter */
263abb0f93cSkardel 	int	cycles;		/* carrier cycles */
264abb0f93cSkardel 	int	dcycles;	/* data cycles */
265abb0f93cSkardel 	int	lastbit;	/* last code element */
266abb0f93cSkardel 	int	second;		/* previous second */
267abb0f93cSkardel 	int	bitcnt;		/* bit count in frame */
268abb0f93cSkardel 	int	frmcnt;		/* bit count in second */
269abb0f93cSkardel 	int	xptr;		/* timecode pointer */
270abb0f93cSkardel 	int	bits;		/* demodulated bits */
271abb0f93cSkardel };
272abb0f93cSkardel 
273abb0f93cSkardel /*
274abb0f93cSkardel  * Function prototypes
275abb0f93cSkardel  */
276abb0f93cSkardel static	int	irig_start	(int, struct peer *);
277abb0f93cSkardel static	void	irig_shutdown	(int, struct peer *);
278abb0f93cSkardel static	void	irig_receive	(struct recvbuf *);
279abb0f93cSkardel static	void	irig_poll	(int, struct peer *);
280abb0f93cSkardel 
281abb0f93cSkardel /*
282abb0f93cSkardel  * More function prototypes
283abb0f93cSkardel  */
284abb0f93cSkardel static	void	irig_base	(struct peer *, double);
285abb0f93cSkardel static	void	irig_rf		(struct peer *, double);
286abb0f93cSkardel static	void	irig_baud	(struct peer *, int);
287abb0f93cSkardel static	void	irig_decode	(struct peer *, int);
288abb0f93cSkardel static	void	irig_gain	(struct peer *);
289abb0f93cSkardel 
290abb0f93cSkardel /*
291abb0f93cSkardel  * Transfer vector
292abb0f93cSkardel  */
293abb0f93cSkardel struct	refclock refclock_irig = {
294abb0f93cSkardel 	irig_start,		/* start up driver */
295abb0f93cSkardel 	irig_shutdown,		/* shut down driver */
296abb0f93cSkardel 	irig_poll,		/* transmit poll message */
297abb0f93cSkardel 	noentry,		/* not used (old irig_control) */
298abb0f93cSkardel 	noentry,		/* initialize driver (not used) */
299abb0f93cSkardel 	noentry,		/* not used (old irig_buginfo) */
300abb0f93cSkardel 	NOFLAGS			/* not used */
301abb0f93cSkardel };
302abb0f93cSkardel 
303abb0f93cSkardel 
304abb0f93cSkardel /*
305abb0f93cSkardel  * irig_start - open the devices and initialize data for processing
306abb0f93cSkardel  */
307abb0f93cSkardel static int
308abb0f93cSkardel irig_start(
309abb0f93cSkardel 	int	unit,		/* instance number (used for PCM) */
310abb0f93cSkardel 	struct peer *peer	/* peer structure pointer */
311abb0f93cSkardel 	)
312abb0f93cSkardel {
313abb0f93cSkardel 	struct refclockproc *pp;
314abb0f93cSkardel 	struct irigunit *up;
315abb0f93cSkardel 
316abb0f93cSkardel 	/*
317abb0f93cSkardel 	 * Local variables
318abb0f93cSkardel 	 */
319abb0f93cSkardel 	int	fd;		/* file descriptor */
320abb0f93cSkardel 	int	i;		/* index */
321abb0f93cSkardel 	double	step;		/* codec adjustment */
322abb0f93cSkardel 
323abb0f93cSkardel 	/*
324abb0f93cSkardel 	 * Open audio device
325abb0f93cSkardel 	 */
326abb0f93cSkardel 	fd = audio_init(DEVICE_AUDIO, AUDIO_BUFSIZ, unit);
327abb0f93cSkardel 	if (fd < 0)
328abb0f93cSkardel 		return (0);
329abb0f93cSkardel #ifdef DEBUG
330abb0f93cSkardel 	if (debug)
331abb0f93cSkardel 		audio_show();
332abb0f93cSkardel #endif
333abb0f93cSkardel 
334abb0f93cSkardel 	/*
335abb0f93cSkardel 	 * Allocate and initialize unit structure
336abb0f93cSkardel 	 */
3372950cc38Schristos 	up = emalloc_zero(sizeof(*up));
338abb0f93cSkardel 	pp = peer->procptr;
339abb0f93cSkardel 	pp->io.clock_recv = irig_receive;
3402950cc38Schristos 	pp->io.srcclock = peer;
341abb0f93cSkardel 	pp->io.datalen = 0;
342abb0f93cSkardel 	pp->io.fd = fd;
343abb0f93cSkardel 	if (!io_addclock(&pp->io)) {
344f003fb54Skardel 		close(fd);
345f003fb54Skardel 		pp->io.fd = -1;
346abb0f93cSkardel 		free(up);
347abb0f93cSkardel 		return (0);
348abb0f93cSkardel 	}
3492950cc38Schristos 	pp->unitptr = up;
350abb0f93cSkardel 
351abb0f93cSkardel 	/*
352abb0f93cSkardel 	 * Initialize miscellaneous variables
353abb0f93cSkardel 	 */
354abb0f93cSkardel 	peer->precision = PRECISION;
355abb0f93cSkardel 	pp->clockdesc = DESCRIPTION;
356abb0f93cSkardel 	memcpy((char *)&pp->refid, REFID, 4);
357abb0f93cSkardel 	up->tc = MINTC;
358abb0f93cSkardel 	up->decim = 1;
359abb0f93cSkardel 	up->gain = 127;
360abb0f93cSkardel 
361abb0f93cSkardel 	/*
362abb0f93cSkardel 	 * The companded samples are encoded sign-magnitude. The table
363abb0f93cSkardel 	 * contains all the 256 values in the interest of speed.
364abb0f93cSkardel 	 */
365abb0f93cSkardel 	up->comp[0] = up->comp[OFFSET] = 0.;
366abb0f93cSkardel 	up->comp[1] = 1; up->comp[OFFSET + 1] = -1.;
367abb0f93cSkardel 	up->comp[2] = 3; up->comp[OFFSET + 2] = -3.;
368abb0f93cSkardel 	step = 2.;
369abb0f93cSkardel 	for (i = 3; i < OFFSET; i++) {
370abb0f93cSkardel 		up->comp[i] = up->comp[i - 1] + step;
371abb0f93cSkardel 		up->comp[OFFSET + i] = -up->comp[i];
372abb0f93cSkardel 		if (i % 16 == 0)
373abb0f93cSkardel 			step *= 2.;
374abb0f93cSkardel 	}
375abb0f93cSkardel 	DTOLFP(1. / SECOND, &up->tick);
376abb0f93cSkardel 	return (1);
377abb0f93cSkardel }
378abb0f93cSkardel 
379abb0f93cSkardel 
380abb0f93cSkardel /*
381abb0f93cSkardel  * irig_shutdown - shut down the clock
382abb0f93cSkardel  */
383abb0f93cSkardel static void
384abb0f93cSkardel irig_shutdown(
385abb0f93cSkardel 	int	unit,		/* instance number (not used) */
386abb0f93cSkardel 	struct peer *peer	/* peer structure pointer */
387abb0f93cSkardel 	)
388abb0f93cSkardel {
389abb0f93cSkardel 	struct refclockproc *pp;
390abb0f93cSkardel 	struct irigunit *up;
391abb0f93cSkardel 
392abb0f93cSkardel 	pp = peer->procptr;
3932950cc38Schristos 	up = pp->unitptr;
394f003fb54Skardel 	if (-1 != pp->io.fd)
395abb0f93cSkardel 		io_closeclock(&pp->io);
396f003fb54Skardel 	if (NULL != up)
397abb0f93cSkardel 		free(up);
398abb0f93cSkardel }
399abb0f93cSkardel 
400abb0f93cSkardel 
401abb0f93cSkardel /*
402abb0f93cSkardel  * irig_receive - receive data from the audio device
403abb0f93cSkardel  *
404abb0f93cSkardel  * This routine reads input samples and adjusts the logical clock to
405abb0f93cSkardel  * track the irig clock by dropping or duplicating codec samples.
406abb0f93cSkardel  */
407abb0f93cSkardel static void
408abb0f93cSkardel irig_receive(
409abb0f93cSkardel 	struct recvbuf *rbufp	/* receive buffer structure pointer */
410abb0f93cSkardel 	)
411abb0f93cSkardel {
412abb0f93cSkardel 	struct peer *peer;
413abb0f93cSkardel 	struct refclockproc *pp;
414abb0f93cSkardel 	struct irigunit *up;
415abb0f93cSkardel 
416abb0f93cSkardel 	/*
417abb0f93cSkardel 	 * Local variables
418abb0f93cSkardel 	 */
419abb0f93cSkardel 	double	sample;		/* codec sample */
420abb0f93cSkardel 	u_char	*dpt;		/* buffer pointer */
421abb0f93cSkardel 	int	bufcnt;		/* buffer counter */
422abb0f93cSkardel 	l_fp	ltemp;		/* l_fp temp */
423abb0f93cSkardel 
4242950cc38Schristos 	peer = rbufp->recv_peer;
425abb0f93cSkardel 	pp = peer->procptr;
4262950cc38Schristos 	up = pp->unitptr;
427abb0f93cSkardel 
428abb0f93cSkardel 	/*
429abb0f93cSkardel 	 * Main loop - read until there ain't no more. Note codec
430abb0f93cSkardel 	 * samples are bit-inverted.
431abb0f93cSkardel 	 */
432abb0f93cSkardel 	DTOLFP((double)rbufp->recv_length / SECOND, &ltemp);
433abb0f93cSkardel 	L_SUB(&rbufp->recv_time, &ltemp);
434abb0f93cSkardel 	up->timestamp = rbufp->recv_time;
435abb0f93cSkardel 	dpt = rbufp->recv_buffer;
436abb0f93cSkardel 	for (bufcnt = 0; bufcnt < rbufp->recv_length; bufcnt++) {
437abb0f93cSkardel 		sample = up->comp[~*dpt++ & 0xff];
438abb0f93cSkardel 
439abb0f93cSkardel 		/*
440abb0f93cSkardel 		 * Variable frequency oscillator. The codec oscillator
441abb0f93cSkardel 		 * runs at the nominal rate of 8000 samples per second,
442abb0f93cSkardel 		 * or 125 us per sample. A frequency change of one unit
443abb0f93cSkardel 		 * results in either duplicating or deleting one sample
444abb0f93cSkardel 		 * per second, which results in a frequency change of
445abb0f93cSkardel 		 * 125 PPM.
446abb0f93cSkardel 		 */
447abb0f93cSkardel 		up->phase += (up->freq + clock_codec) / SECOND;
448abb0f93cSkardel 		up->phase += pp->fudgetime2 / 1e6;
449abb0f93cSkardel 		if (up->phase >= .5) {
450abb0f93cSkardel 			up->phase -= 1.;
451abb0f93cSkardel 		} else if (up->phase < -.5) {
452abb0f93cSkardel 			up->phase += 1.;
453abb0f93cSkardel 			irig_rf(peer, sample);
454abb0f93cSkardel 			irig_rf(peer, sample);
455abb0f93cSkardel 		} else {
456abb0f93cSkardel 			irig_rf(peer, sample);
457abb0f93cSkardel 		}
458abb0f93cSkardel 		L_ADD(&up->timestamp, &up->tick);
459abb0f93cSkardel 		sample = fabs(sample);
460abb0f93cSkardel 		if (sample > up->signal)
461abb0f93cSkardel 			up->signal = sample;
462abb0f93cSkardel 		up->signal += (sample - up->signal) /
463abb0f93cSkardel 		    1000;
464abb0f93cSkardel 
465abb0f93cSkardel 		/*
466abb0f93cSkardel 		 * Once each second, determine the IRIG format and gain.
467abb0f93cSkardel 		 */
468abb0f93cSkardel 		up->seccnt = (up->seccnt + 1) % SECOND;
469abb0f93cSkardel 		if (up->seccnt == 0) {
470abb0f93cSkardel 			if (up->irig_b > up->irig_e) {
471abb0f93cSkardel 				up->decim = 1;
472abb0f93cSkardel 				up->fdelay = IRIG_B;
473abb0f93cSkardel 			} else {
474abb0f93cSkardel 				up->decim = 10;
475abb0f93cSkardel 				up->fdelay = IRIG_E;
476abb0f93cSkardel 			}
477abb0f93cSkardel 			up->irig_b = up->irig_e = 0;
478abb0f93cSkardel 			irig_gain(peer);
479abb0f93cSkardel 
480abb0f93cSkardel 		}
481abb0f93cSkardel 	}
482abb0f93cSkardel 
483abb0f93cSkardel 	/*
484abb0f93cSkardel 	 * Set the input port and monitor gain for the next buffer.
485abb0f93cSkardel 	 */
486abb0f93cSkardel 	if (pp->sloppyclockflag & CLK_FLAG2)
487abb0f93cSkardel 		up->port = 2;
488abb0f93cSkardel 	else
489abb0f93cSkardel 		up->port = 1;
490abb0f93cSkardel 	if (pp->sloppyclockflag & CLK_FLAG3)
491abb0f93cSkardel 		up->mongain = MONGAIN;
492abb0f93cSkardel 	else
493abb0f93cSkardel 		up->mongain = 0;
494abb0f93cSkardel }
495abb0f93cSkardel 
496abb0f93cSkardel 
497abb0f93cSkardel /*
498abb0f93cSkardel  * irig_rf - RF processing
499abb0f93cSkardel  *
500abb0f93cSkardel  * This routine filters the RF signal using a bandass filter for IRIG-B
501abb0f93cSkardel  * and a lowpass filter for IRIG-E. In case of IRIG-E, the samples are
502abb0f93cSkardel  * decimated by a factor of ten. Note that the codec filters function as
503abb0f93cSkardel  * roofing filters to attenuate both the high and low ends of the
504abb0f93cSkardel  * passband. IIR filter coefficients were determined using Matlab Signal
505abb0f93cSkardel  * Processing Toolkit.
506abb0f93cSkardel  */
507abb0f93cSkardel static void
508abb0f93cSkardel irig_rf(
509abb0f93cSkardel 	struct peer *peer,	/* peer structure pointer */
510abb0f93cSkardel 	double	sample		/* current signal sample */
511abb0f93cSkardel 	)
512abb0f93cSkardel {
513abb0f93cSkardel 	struct refclockproc *pp;
514abb0f93cSkardel 	struct irigunit *up;
515abb0f93cSkardel 
516abb0f93cSkardel 	/*
517abb0f93cSkardel 	 * Local variables
518abb0f93cSkardel 	 */
519abb0f93cSkardel 	double	irig_b, irig_e;	/* irig filter outputs */
520abb0f93cSkardel 
521abb0f93cSkardel 	pp = peer->procptr;
5222950cc38Schristos 	up = pp->unitptr;
523abb0f93cSkardel 
524abb0f93cSkardel 	/*
525abb0f93cSkardel 	 * IRIG-B filter. Matlab 4th-order IIR elliptic, 800-1200 Hz
526abb0f93cSkardel 	 * bandpass, 0.3 dB passband ripple, -50 dB stopband ripple,
527abb0f93cSkardel 	 * phase delay 1.03 ms.
528abb0f93cSkardel 	 */
529abb0f93cSkardel 	irig_b = (up->bpf[8] = up->bpf[7]) * 6.505491e-001;
530abb0f93cSkardel 	irig_b += (up->bpf[7] = up->bpf[6]) * -3.875180e+000;
531abb0f93cSkardel 	irig_b += (up->bpf[6] = up->bpf[5]) * 1.151180e+001;
532abb0f93cSkardel 	irig_b += (up->bpf[5] = up->bpf[4]) * -2.141264e+001;
533abb0f93cSkardel 	irig_b += (up->bpf[4] = up->bpf[3]) * 2.712837e+001;
534abb0f93cSkardel 	irig_b += (up->bpf[3] = up->bpf[2]) * -2.384486e+001;
535abb0f93cSkardel 	irig_b += (up->bpf[2] = up->bpf[1]) * 1.427663e+001;
536abb0f93cSkardel 	irig_b += (up->bpf[1] = up->bpf[0]) * -5.352734e+000;
537abb0f93cSkardel 	up->bpf[0] = sample - irig_b;
538abb0f93cSkardel 	irig_b = up->bpf[0] * 4.952157e-003
539abb0f93cSkardel 	    + up->bpf[1] * -2.055878e-002
540abb0f93cSkardel 	    + up->bpf[2] * 4.401413e-002
541abb0f93cSkardel 	    + up->bpf[3] * -6.558851e-002
542abb0f93cSkardel 	    + up->bpf[4] * 7.462108e-002
543abb0f93cSkardel 	    + up->bpf[5] * -6.558851e-002
544abb0f93cSkardel 	    + up->bpf[6] * 4.401413e-002
545abb0f93cSkardel 	    + up->bpf[7] * -2.055878e-002
546abb0f93cSkardel 	    + up->bpf[8] * 4.952157e-003;
547abb0f93cSkardel 	up->irig_b += irig_b * irig_b;
548abb0f93cSkardel 
549abb0f93cSkardel 	/*
550abb0f93cSkardel 	 * IRIG-E filter. Matlab 4th-order IIR elliptic, 130-Hz lowpass,
551abb0f93cSkardel 	 * 0.3 dB passband ripple, -50 dB stopband ripple, phase delay
552abb0f93cSkardel 	 * 3.47 ms.
553abb0f93cSkardel 	 */
554abb0f93cSkardel 	irig_e = (up->lpf[4] = up->lpf[3]) * 8.694604e-001;
555abb0f93cSkardel 	irig_e += (up->lpf[3] = up->lpf[2]) * -3.589893e+000;
556abb0f93cSkardel 	irig_e += (up->lpf[2] = up->lpf[1]) * 5.570154e+000;
557abb0f93cSkardel 	irig_e += (up->lpf[1] = up->lpf[0]) * -3.849667e+000;
558abb0f93cSkardel 	up->lpf[0] = sample - irig_e;
559abb0f93cSkardel 	irig_e = up->lpf[0] * 3.215696e-003
560abb0f93cSkardel 	    + up->lpf[1] * -1.174951e-002
561abb0f93cSkardel 	    + up->lpf[2] * 1.712074e-002
562abb0f93cSkardel 	    + up->lpf[3] * -1.174951e-002
563abb0f93cSkardel 	    + up->lpf[4] * 3.215696e-003;
564abb0f93cSkardel 	up->irig_e += irig_e * irig_e;
565abb0f93cSkardel 
566abb0f93cSkardel 	/*
567abb0f93cSkardel 	 * Decimate by a factor of either 1 (IRIG-B) or 10 (IRIG-E).
568abb0f93cSkardel 	 */
569abb0f93cSkardel 	up->badcnt = (up->badcnt + 1) % up->decim;
570abb0f93cSkardel 	if (up->badcnt == 0) {
571abb0f93cSkardel 		if (up->decim == 1)
572abb0f93cSkardel 			irig_base(peer, irig_b);
573abb0f93cSkardel 		else
574abb0f93cSkardel 			irig_base(peer, irig_e);
575abb0f93cSkardel 	}
576abb0f93cSkardel }
577abb0f93cSkardel 
578abb0f93cSkardel /*
579abb0f93cSkardel  * irig_base - baseband processing
580abb0f93cSkardel  *
581abb0f93cSkardel  * This routine processes the baseband signal and demodulates the AM
582abb0f93cSkardel  * carrier using a synchronous detector. It then synchronizes to the
583abb0f93cSkardel  * data frame at the baud rate and decodes the width-modulated data
584abb0f93cSkardel  * pulses.
585abb0f93cSkardel  */
586abb0f93cSkardel static void
587abb0f93cSkardel irig_base(
588abb0f93cSkardel 	struct peer *peer,	/* peer structure pointer */
589abb0f93cSkardel 	double	sample		/* current signal sample */
590abb0f93cSkardel 	)
591abb0f93cSkardel {
592abb0f93cSkardel 	struct refclockproc *pp;
593abb0f93cSkardel 	struct irigunit *up;
594abb0f93cSkardel 
595abb0f93cSkardel 	/*
596abb0f93cSkardel 	 * Local variables
597abb0f93cSkardel 	 */
598abb0f93cSkardel 	double	lope;		/* integrator output */
599abb0f93cSkardel 	double	env;		/* envelope detector output */
600abb0f93cSkardel 	double	dtemp;
601abb0f93cSkardel 	int	carphase;	/* carrier phase */
602abb0f93cSkardel 
603abb0f93cSkardel 	pp = peer->procptr;
6042950cc38Schristos 	up = pp->unitptr;
605abb0f93cSkardel 
606abb0f93cSkardel 	/*
607abb0f93cSkardel 	 * Synchronous baud integrator. Corresponding samples of current
608abb0f93cSkardel 	 * and past baud intervals are integrated to refine the envelope
609abb0f93cSkardel 	 * amplitude and phase estimate. We keep one cycle (1 ms) of the
610abb0f93cSkardel 	 * raw data and one baud (10 ms) of the integrated data.
611abb0f93cSkardel 	 */
612abb0f93cSkardel 	up->envphase = (up->envphase + 1) % BAUD;
613abb0f93cSkardel 	up->integ[up->envphase] += (sample - up->integ[up->envphase]) /
614abb0f93cSkardel 	    (5 * up->tc);
615abb0f93cSkardel 	lope = up->integ[up->envphase];
616abb0f93cSkardel 	carphase = up->envphase % CYCLE;
617abb0f93cSkardel 	up->lastenv[carphase] = sample;
618abb0f93cSkardel 	up->lastint[carphase] = lope;
619abb0f93cSkardel 
620abb0f93cSkardel 	/*
621abb0f93cSkardel 	 * Phase detector. Find the negative-going zero crossing
622abb0f93cSkardel 	 * relative to sample 4 in the 8-sample sycle. A phase change of
623abb0f93cSkardel 	 * 360 degrees produces an output change of one unit.
624abb0f93cSkardel 	 */
625abb0f93cSkardel 	if (up->lastsig > 0 && lope <= 0)
626abb0f93cSkardel 		up->zxing += (double)(carphase - 4) / CYCLE;
627abb0f93cSkardel 	up->lastsig = lope;
628abb0f93cSkardel 
629abb0f93cSkardel 	/*
630abb0f93cSkardel 	 * End of the baud. Update signal/noise estimates and PLL
631abb0f93cSkardel 	 * phase, frequency and time constant.
632abb0f93cSkardel 	 */
633abb0f93cSkardel 	if (up->envphase == 0) {
634abb0f93cSkardel 		up->maxsignal = up->intmax; up->noise = up->intmin;
635abb0f93cSkardel 		up->intmin = 1e6; up->intmax = -1e6;
636abb0f93cSkardel 		if (up->maxsignal < DRPOUT)
637abb0f93cSkardel 			up->errflg |= IRIG_ERR_AMP;
638abb0f93cSkardel 		if (up->maxsignal > 0)
639abb0f93cSkardel 			up->modndx = (up->maxsignal - up->noise) /
640abb0f93cSkardel 			    up->maxsignal;
641abb0f93cSkardel  		else
642abb0f93cSkardel 			up->modndx = 0;
643abb0f93cSkardel 		if (up->modndx < MODMIN)
644abb0f93cSkardel 			up->errflg |= IRIG_ERR_MOD;
645abb0f93cSkardel 		if (up->errflg & (IRIG_ERR_AMP | IRIG_ERR_FREQ |
646abb0f93cSkardel 		   IRIG_ERR_MOD | IRIG_ERR_SYNCH)) {
647abb0f93cSkardel 			up->tc = MINTC;
648abb0f93cSkardel 			up->tcount = 0;
649abb0f93cSkardel 		}
650abb0f93cSkardel 
651abb0f93cSkardel 		/*
652abb0f93cSkardel 		 * Update PLL phase and frequency. The PLL time constant
653abb0f93cSkardel 		 * is set initially to stabilize the frequency within a
654abb0f93cSkardel 		 * minute or two, then increases to the maximum. The
655abb0f93cSkardel 		 * frequency is clamped so that the PLL capture range
656abb0f93cSkardel 		 * cannot be exceeded.
657abb0f93cSkardel 		 */
658abb0f93cSkardel 		dtemp = up->zxing * up->decim / BAUD;
659abb0f93cSkardel 		up->yxing = dtemp;
660abb0f93cSkardel 		up->zxing = 0.;
661abb0f93cSkardel 		up->phase += dtemp / up->tc;
662abb0f93cSkardel 		up->freq += dtemp / (4. * up->tc * up->tc);
663abb0f93cSkardel 		if (up->freq > MAXFREQ) {
664abb0f93cSkardel 			up->freq = MAXFREQ;
665abb0f93cSkardel 			up->errflg |= IRIG_ERR_FREQ;
666abb0f93cSkardel 		} else if (up->freq < -MAXFREQ) {
667abb0f93cSkardel 			up->freq = -MAXFREQ;
668abb0f93cSkardel 			up->errflg |= IRIG_ERR_FREQ;
669abb0f93cSkardel 		}
670abb0f93cSkardel 	}
671abb0f93cSkardel 
672abb0f93cSkardel 	/*
673abb0f93cSkardel 	 * Synchronous demodulator. There are eight samples in the cycle
674abb0f93cSkardel 	 * and ten cycles in the baud. Since the PLL has aligned the
675abb0f93cSkardel 	 * negative-going zero crossing at sample 4, the maximum
676abb0f93cSkardel 	 * amplitude is at sample 2 and minimum at sample 6. The
677abb0f93cSkardel 	 * beginning of the data pulse is determined from the integrated
678abb0f93cSkardel 	 * samples, while the end of the pulse is determined from the
679abb0f93cSkardel 	 * raw samples. The raw data bits are demodulated relative to
680abb0f93cSkardel 	 * the slice level and left-shifted in the decoding register.
681abb0f93cSkardel 	 */
682abb0f93cSkardel 	if (carphase != 7)
683abb0f93cSkardel 		return;
684abb0f93cSkardel 
685abb0f93cSkardel 	lope = (up->lastint[2] - up->lastint[6]) / 2.;
686abb0f93cSkardel 	if (lope > up->intmax)
687abb0f93cSkardel 		up->intmax = lope;
688abb0f93cSkardel 	if (lope < up->intmin)
689abb0f93cSkardel 		up->intmin = lope;
690abb0f93cSkardel 
691abb0f93cSkardel 	/*
692abb0f93cSkardel 	 * Pulse code demodulator and reference timestamp. The decoder
693abb0f93cSkardel 	 * looks for a sequence of ten bits; the first two bits must be
694abb0f93cSkardel 	 * one, the last two bits must be zero. Frame synch is asserted
695abb0f93cSkardel 	 * when three correct frames have been found.
696abb0f93cSkardel 	 */
697abb0f93cSkardel 	up->pulse = (up->pulse + 1) % 10;
698abb0f93cSkardel 	up->cycles <<= 1;
699abb0f93cSkardel 	if (lope >= (up->maxsignal + up->noise) / 2.)
700abb0f93cSkardel 		up->cycles |= 1;
701abb0f93cSkardel 	if ((up->cycles & 0x303c0f03) == 0x300c0300) {
702abb0f93cSkardel 		if (up->pulse != 0)
703abb0f93cSkardel 			up->errflg |= IRIG_ERR_SYNCH;
704abb0f93cSkardel 		up->pulse = 0;
705abb0f93cSkardel 	}
706abb0f93cSkardel 
707abb0f93cSkardel 	/*
708abb0f93cSkardel 	 * Assemble the baud and max/min to get the slice level for the
709abb0f93cSkardel 	 * next baud. The slice level is based on the maximum over the
710abb0f93cSkardel 	 * first two bits and the minimum over the last two bits, with
711abb0f93cSkardel 	 * the slice level halfway between the maximum and minimum.
712abb0f93cSkardel 	 */
713abb0f93cSkardel 	env = (up->lastenv[2] - up->lastenv[6]) / 2.;
714abb0f93cSkardel 	up->dcycles <<= 1;
715abb0f93cSkardel 	if (env >= up->slice)
716abb0f93cSkardel 		up->dcycles |= 1;
717abb0f93cSkardel 	switch(up->pulse) {
718abb0f93cSkardel 
719abb0f93cSkardel 	case 0:
720abb0f93cSkardel 		irig_baud(peer, up->dcycles);
721abb0f93cSkardel 		if (env < up->envmin)
722abb0f93cSkardel 			up->envmin = env;
723abb0f93cSkardel 		up->slice = (up->envmax + up->envmin) / 2;
724abb0f93cSkardel 		up->envmin = 1e6; up->envmax = -1e6;
725abb0f93cSkardel 		break;
726abb0f93cSkardel 
727abb0f93cSkardel 	case 1:
728abb0f93cSkardel 		up->envmax = env;
729abb0f93cSkardel 		break;
730abb0f93cSkardel 
731abb0f93cSkardel 	case 2:
732abb0f93cSkardel 		if (env > up->envmax)
733abb0f93cSkardel 			up->envmax = env;
734abb0f93cSkardel 		break;
735abb0f93cSkardel 
736abb0f93cSkardel 	case 9:
737abb0f93cSkardel 		up->envmin = env;
738abb0f93cSkardel 		break;
739abb0f93cSkardel 	}
740abb0f93cSkardel }
741abb0f93cSkardel 
742abb0f93cSkardel /*
743abb0f93cSkardel  * irig_baud - update the PLL and decode the pulse-width signal
744abb0f93cSkardel  */
745abb0f93cSkardel static void
746abb0f93cSkardel irig_baud(
747abb0f93cSkardel 	struct peer *peer,	/* peer structure pointer */
748abb0f93cSkardel 	int	bits		/* decoded bits */
749abb0f93cSkardel 	)
750abb0f93cSkardel {
751abb0f93cSkardel 	struct refclockproc *pp;
752abb0f93cSkardel 	struct irigunit *up;
753abb0f93cSkardel 	double	dtemp;
754abb0f93cSkardel 	l_fp	ltemp;
755abb0f93cSkardel 
756abb0f93cSkardel         pp = peer->procptr;
7572950cc38Schristos 	up = pp->unitptr;
758abb0f93cSkardel 
759abb0f93cSkardel 	/*
760abb0f93cSkardel 	 * The PLL time constant starts out small, in order to
761abb0f93cSkardel 	 * sustain a frequency tolerance of 250 PPM. It
762abb0f93cSkardel 	 * gradually increases as the loop settles down. Note
763abb0f93cSkardel 	 * that small wiggles are not believed, unless they
764abb0f93cSkardel 	 * persist for lots of samples.
765abb0f93cSkardel 	 */
766abb0f93cSkardel 	up->exing = -up->yxing;
76739ea0713Sjoerg 	if (abs(up->envxing - up->envphase) <= 1) {
768abb0f93cSkardel 		up->tcount++;
769abb0f93cSkardel 		if (up->tcount > 20 * up->tc) {
770abb0f93cSkardel 			up->tc++;
771abb0f93cSkardel 			if (up->tc > MAXTC)
772abb0f93cSkardel 				up->tc = MAXTC;
773abb0f93cSkardel 			up->tcount = 0;
774abb0f93cSkardel 			up->envxing = up->envphase;
775abb0f93cSkardel 		} else {
776abb0f93cSkardel 			up->exing -= up->envxing - up->envphase;
777abb0f93cSkardel 		}
778abb0f93cSkardel 	} else {
779abb0f93cSkardel 		up->tcount = 0;
780abb0f93cSkardel 		up->envxing = up->envphase;
781abb0f93cSkardel 	}
782abb0f93cSkardel 
783abb0f93cSkardel 	/*
784abb0f93cSkardel 	 * Strike the baud timestamp as the positive zero crossing of
785abb0f93cSkardel 	 * the first bit, accounting for the codec delay and filter
786abb0f93cSkardel 	 * delay.
787abb0f93cSkardel 	 */
788abb0f93cSkardel 	up->prvstamp = up->chrstamp;
789abb0f93cSkardel 	dtemp = up->decim * (up->exing / SECOND) + up->fdelay;
790abb0f93cSkardel 	DTOLFP(dtemp, &ltemp);
791abb0f93cSkardel 	up->chrstamp = up->timestamp;
792abb0f93cSkardel 	L_SUB(&up->chrstamp, &ltemp);
793abb0f93cSkardel 
794abb0f93cSkardel 	/*
795abb0f93cSkardel 	 * The data bits are collected in ten-bit bauds. The first two
796abb0f93cSkardel 	 * bits are not used. The resulting patterns represent runs of
797abb0f93cSkardel 	 * 0-1 bits (0), 2-4 bits (1) and 5-7 bits (PI). The remaining
798abb0f93cSkardel 	 * 8-bit run represents a soft error and is treated as 0.
799abb0f93cSkardel 	 */
800abb0f93cSkardel 	switch (up->dcycles & 0xff) {
801abb0f93cSkardel 
802abb0f93cSkardel 	case 0x00:		/* 0-1 bits (0) */
803abb0f93cSkardel 	case 0x80:
804abb0f93cSkardel 		irig_decode(peer, BIT0);
805abb0f93cSkardel 		break;
806abb0f93cSkardel 
807abb0f93cSkardel 	case 0xc0:		/* 2-4 bits (1) */
808abb0f93cSkardel 	case 0xe0:
809abb0f93cSkardel 	case 0xf0:
810abb0f93cSkardel 		irig_decode(peer, BIT1);
811abb0f93cSkardel 		break;
812abb0f93cSkardel 
813abb0f93cSkardel 	case 0xf8:		/* (5-7 bits (PI) */
814abb0f93cSkardel 	case 0xfc:
815abb0f93cSkardel 	case 0xfe:
816abb0f93cSkardel 		irig_decode(peer, BITP);
817abb0f93cSkardel 		break;
818abb0f93cSkardel 
819abb0f93cSkardel 	default:		/* 8 bits (error) */
820abb0f93cSkardel 		irig_decode(peer, BIT0);
821abb0f93cSkardel 		up->errflg |= IRIG_ERR_DECODE;
822abb0f93cSkardel 	}
823abb0f93cSkardel }
824abb0f93cSkardel 
825abb0f93cSkardel 
826abb0f93cSkardel /*
827abb0f93cSkardel  * irig_decode - decode the data
828abb0f93cSkardel  *
829abb0f93cSkardel  * This routine assembles bauds into digits, digits into frames and
830abb0f93cSkardel  * frames into the timecode fields. Bits can have values of zero, one
831abb0f93cSkardel  * or position identifier. There are four bits per digit, ten digits per
832abb0f93cSkardel  * frame and ten frames per second.
833abb0f93cSkardel  */
834abb0f93cSkardel static void
835abb0f93cSkardel irig_decode(
836abb0f93cSkardel 	struct	peer *peer,	/* peer structure pointer */
837abb0f93cSkardel 	int	bit		/* data bit (0, 1 or 2) */
838abb0f93cSkardel 	)
839abb0f93cSkardel {
840abb0f93cSkardel 	struct refclockproc *pp;
841abb0f93cSkardel 	struct irigunit *up;
842abb0f93cSkardel 
843abb0f93cSkardel 	/*
844abb0f93cSkardel 	 * Local variables
845abb0f93cSkardel 	 */
846abb0f93cSkardel 	int	syncdig;	/* sync digit (Spectracom) */
847f003fb54Skardel 	char	sbs[6 + 1];	/* binary seconds since 0h */
848f003fb54Skardel 	char	spare[2 + 1];	/* mulligan digits */
849abb0f93cSkardel 	int	temp;
850abb0f93cSkardel 
8512950cc38Schristos 	syncdig = 0;
852abb0f93cSkardel 	pp = peer->procptr;
8532950cc38Schristos 	up = pp->unitptr;
854abb0f93cSkardel 
855abb0f93cSkardel 	/*
856abb0f93cSkardel 	 * Assemble frame bits.
857abb0f93cSkardel 	 */
858abb0f93cSkardel 	up->bits >>= 1;
859abb0f93cSkardel 	if (bit == BIT1) {
860abb0f93cSkardel 		up->bits |= 0x200;
861abb0f93cSkardel 	} else if (bit == BITP && up->lastbit == BITP) {
862abb0f93cSkardel 
863abb0f93cSkardel 		/*
864abb0f93cSkardel 		 * Frame sync - two adjacent position identifiers, which
865abb0f93cSkardel 		 * mark the beginning of the second. The reference time
866abb0f93cSkardel 		 * is the beginning of the second position identifier,
867abb0f93cSkardel 		 * so copy the character timestamp to the reference
868abb0f93cSkardel 		 * timestamp.
869abb0f93cSkardel 		 */
870abb0f93cSkardel 		if (up->frmcnt != 1)
871abb0f93cSkardel 			up->errflg |= IRIG_ERR_SYNCH;
872abb0f93cSkardel 		up->frmcnt = 1;
873abb0f93cSkardel 		up->refstamp = up->prvstamp;
874abb0f93cSkardel 	}
875abb0f93cSkardel 	up->lastbit = bit;
876abb0f93cSkardel 	if (up->frmcnt % SUBFLD == 0) {
877abb0f93cSkardel 
878abb0f93cSkardel 		/*
879abb0f93cSkardel 		 * End of frame. Encode two hexadecimal digits in
880abb0f93cSkardel 		 * little-endian timecode field. Note frame 1 is shifted
881abb0f93cSkardel 		 * right one bit to account for the marker PI.
882abb0f93cSkardel 		 */
883abb0f93cSkardel 		temp = up->bits;
884abb0f93cSkardel 		if (up->frmcnt == 10)
885abb0f93cSkardel 			temp >>= 1;
886abb0f93cSkardel 		if (up->xptr >= 2) {
887abb0f93cSkardel 			up->timecode[--up->xptr] = hexchar[temp & 0xf];
888abb0f93cSkardel 			up->timecode[--up->xptr] = hexchar[(temp >> 5) &
889abb0f93cSkardel 			    0xf];
890abb0f93cSkardel 		}
891abb0f93cSkardel 		if (up->frmcnt == 0) {
892abb0f93cSkardel 
893abb0f93cSkardel 			/*
894abb0f93cSkardel 			 * End of second. Decode the timecode and wind
895abb0f93cSkardel 			 * the clock. Not all IRIG generators have the
896abb0f93cSkardel 			 * year; if so, it is nonzero after year 2000.
897abb0f93cSkardel 			 * Not all have the hardware status bit; if so,
898abb0f93cSkardel 			 * it is lit when the source is okay and dim
899abb0f93cSkardel 			 * when bad. We watch this only if the year is
900abb0f93cSkardel 			 * nonzero. Not all are configured for signature
901abb0f93cSkardel 			 * control. If so, all BCD digits are set to
902abb0f93cSkardel 			 * zero if the source is bad. In this case the
903abb0f93cSkardel 			 * refclock_process() will reject the timecode
904abb0f93cSkardel 			 * as invalid.
905abb0f93cSkardel 			 */
906abb0f93cSkardel 			up->xptr = 2 * SUBFLD;
907abb0f93cSkardel 			if (sscanf((char *)up->timecode,
908abb0f93cSkardel 			   "%6s%2d%1d%2s%3d%2d%2d%2d", sbs, &pp->year,
909abb0f93cSkardel 			    &syncdig, spare, &pp->day, &pp->hour,
910abb0f93cSkardel 			    &pp->minute, &pp->second) != 8)
911abb0f93cSkardel 				pp->leap = LEAP_NOTINSYNC;
912abb0f93cSkardel 			else
913abb0f93cSkardel 				pp->leap = LEAP_NOWARNING;
914abb0f93cSkardel 			up->second = (up->second + up->decim) % 60;
915abb0f93cSkardel 
916abb0f93cSkardel 			/*
917abb0f93cSkardel 			 * Raise an alarm if the day field is zero,
918abb0f93cSkardel 			 * which happens when signature control is
919abb0f93cSkardel 			 * enabled and the device has lost
920abb0f93cSkardel 			 * synchronization. Raise an alarm if the year
921abb0f93cSkardel 			 * field is nonzero and the sync indicator is
922abb0f93cSkardel 			 * zero, which happens when a Spectracom radio
923abb0f93cSkardel 			 * has lost synchronization. Raise an alarm if
924abb0f93cSkardel 			 * the expected second does not agree with the
925abb0f93cSkardel 			 * decoded second, which happens with a garbled
926abb0f93cSkardel 			 * IRIG signal. We are very particular.
927abb0f93cSkardel 			 */
928abb0f93cSkardel 			if (pp->day == 0 || (pp->year != 0 && syncdig ==
929abb0f93cSkardel 			    0))
930abb0f93cSkardel 				up->errflg |= IRIG_ERR_SIGERR;
931abb0f93cSkardel 			if (pp->second != up->second)
932abb0f93cSkardel 				up->errflg |= IRIG_ERR_CHECK;
933abb0f93cSkardel 			up->second = pp->second;
934abb0f93cSkardel 
935abb0f93cSkardel 			/*
936abb0f93cSkardel 			 * Wind the clock only if there are no errors
937abb0f93cSkardel 			 * and the time constant has reached the
938abb0f93cSkardel 			 * maximum.
939abb0f93cSkardel 			 */
940abb0f93cSkardel 			if (up->errflg == 0 && up->tc == MAXTC) {
941abb0f93cSkardel 				pp->lastref = pp->lastrec;
942abb0f93cSkardel 				pp->lastrec = up->refstamp;
943abb0f93cSkardel 				if (!refclock_process(pp))
944abb0f93cSkardel 					refclock_report(peer,
945abb0f93cSkardel 					    CEVNT_BADTIME);
946abb0f93cSkardel 			}
947f003fb54Skardel 			snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
948abb0f93cSkardel 			    "%02x %02d %03d %02d:%02d:%02d %4.0f %3d %6.3f %2d %6.2f %6.1f %s",
949abb0f93cSkardel 			    up->errflg, pp->year, pp->day,
950abb0f93cSkardel 			    pp->hour, pp->minute, pp->second,
951abb0f93cSkardel 			    up->maxsignal, up->gain, up->modndx,
952abb0f93cSkardel 			    up->tc, up->exing * 1e6 / SECOND, up->freq *
953abb0f93cSkardel 			    1e6 / SECOND, ulfptoa(&pp->lastrec, 6));
954abb0f93cSkardel 			pp->lencode = strlen(pp->a_lastcode);
955abb0f93cSkardel 			up->errflg = 0;
956abb0f93cSkardel 			if (pp->sloppyclockflag & CLK_FLAG4) {
957abb0f93cSkardel 				record_clock_stats(&peer->srcadr,
958abb0f93cSkardel 				    pp->a_lastcode);
959abb0f93cSkardel #ifdef DEBUG
960abb0f93cSkardel 				if (debug)
961abb0f93cSkardel 					printf("irig %s\n",
962abb0f93cSkardel 					    pp->a_lastcode);
963abb0f93cSkardel #endif /* DEBUG */
964abb0f93cSkardel 			}
965abb0f93cSkardel 		}
966abb0f93cSkardel 	}
967abb0f93cSkardel 	up->frmcnt = (up->frmcnt + 1) % FIELD;
968abb0f93cSkardel }
969abb0f93cSkardel 
970abb0f93cSkardel 
971abb0f93cSkardel /*
972abb0f93cSkardel  * irig_poll - called by the transmit procedure
973abb0f93cSkardel  *
974abb0f93cSkardel  * This routine sweeps up the timecode updates since the last poll. For
975abb0f93cSkardel  * IRIG-B there should be at least 60 updates; for IRIG-E there should
976abb0f93cSkardel  * be at least 6. If nothing is heard, a timeout event is declared.
977abb0f93cSkardel  */
978abb0f93cSkardel static void
979abb0f93cSkardel irig_poll(
980abb0f93cSkardel 	int	unit,		/* instance number (not used) */
981abb0f93cSkardel 	struct peer *peer	/* peer structure pointer */
982abb0f93cSkardel 	)
983abb0f93cSkardel {
984abb0f93cSkardel 	struct refclockproc *pp;
985abb0f93cSkardel 
986abb0f93cSkardel 	pp = peer->procptr;
987abb0f93cSkardel 
988abb0f93cSkardel 	if (pp->coderecv == pp->codeproc) {
989abb0f93cSkardel 		refclock_report(peer, CEVNT_TIMEOUT);
990abb0f93cSkardel 		return;
991abb0f93cSkardel 
992abb0f93cSkardel 	}
993abb0f93cSkardel 	refclock_receive(peer);
994abb0f93cSkardel 	if (!(pp->sloppyclockflag & CLK_FLAG4)) {
995abb0f93cSkardel 		record_clock_stats(&peer->srcadr, pp->a_lastcode);
996abb0f93cSkardel #ifdef DEBUG
997abb0f93cSkardel 		if (debug)
998abb0f93cSkardel 			printf("irig %s\n", pp->a_lastcode);
999abb0f93cSkardel #endif /* DEBUG */
1000abb0f93cSkardel 	}
1001abb0f93cSkardel 	pp->polls++;
1002abb0f93cSkardel 
1003abb0f93cSkardel }
1004abb0f93cSkardel 
1005abb0f93cSkardel 
1006abb0f93cSkardel /*
1007abb0f93cSkardel  * irig_gain - adjust codec gain
1008abb0f93cSkardel  *
1009abb0f93cSkardel  * This routine is called at the end of each second. It uses the AGC to
1010abb0f93cSkardel  * bradket the maximum signal level between MINAMP and MAXAMP to avoid
1011abb0f93cSkardel  * hunting. The routine also jiggles the input port and selectively
1012abb0f93cSkardel  * mutes the monitor.
1013abb0f93cSkardel  */
1014abb0f93cSkardel static void
1015abb0f93cSkardel irig_gain(
1016abb0f93cSkardel 	struct peer *peer	/* peer structure pointer */
1017abb0f93cSkardel 	)
1018abb0f93cSkardel {
1019abb0f93cSkardel 	struct refclockproc *pp;
1020abb0f93cSkardel 	struct irigunit *up;
1021abb0f93cSkardel 
1022abb0f93cSkardel 	pp = peer->procptr;
10232950cc38Schristos 	up = pp->unitptr;
1024abb0f93cSkardel 
1025abb0f93cSkardel 	/*
1026abb0f93cSkardel 	 * Apparently, the codec uses only the high order bits of the
1027abb0f93cSkardel 	 * gain control field. Thus, it may take awhile for changes to
1028abb0f93cSkardel 	 * wiggle the hardware bits.
1029abb0f93cSkardel 	 */
1030abb0f93cSkardel 	if (up->maxsignal < MINAMP) {
1031abb0f93cSkardel 		up->gain += 4;
1032abb0f93cSkardel 		if (up->gain > MAXGAIN)
1033abb0f93cSkardel 			up->gain = MAXGAIN;
1034abb0f93cSkardel 	} else if (up->maxsignal > MAXAMP) {
1035abb0f93cSkardel 		up->gain -= 4;
1036abb0f93cSkardel 		if (up->gain < 0)
1037abb0f93cSkardel 			up->gain = 0;
1038abb0f93cSkardel 	}
1039abb0f93cSkardel 	audio_gain(up->gain, up->mongain, up->port);
1040abb0f93cSkardel }
1041abb0f93cSkardel 
1042abb0f93cSkardel 
1043abb0f93cSkardel #else
1044*eabc0478Schristos NONEMPTY_TRANSLATION_UNIT
1045abb0f93cSkardel #endif /* REFCLOCK */
1046