clk_wharton.c revision 1.1 1 1.1 kardel /* $NetBSD: clk_wharton.c,v 1.1 2009/12/13 16:55:22 kardel Exp $ */
2 1.1 kardel
3 1.1 kardel /*
4 1.1 kardel * /src/NTP/ntp4-dev/libparse/clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
5 1.1 kardel *
6 1.1 kardel * clk_wharton.c,v 4.2 2004/11/14 15:29:41 kardel RELEASE_20050508_A
7 1.1 kardel *
8 1.1 kardel * From Philippe De Muyter <phdm (at) macqel.be>, 1999
9 1.1 kardel */
10 1.1 kardel #ifdef HAVE_CONFIG_H
11 1.1 kardel #include <config.h>
12 1.1 kardel #endif
13 1.1 kardel
14 1.1 kardel #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_WHARTON_400A)
15 1.1 kardel /*
16 1.1 kardel * Support for WHARTON 400A Series clock + 404.2 serial interface.
17 1.1 kardel *
18 1.1 kardel * Copyright (C) 1999, 2000 by Philippe De Muyter <phdm (at) macqel.be>
19 1.1 kardel *
20 1.1 kardel * This program is distributed in the hope that it will be useful, but WITHOUT
21 1.1 kardel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 1.1 kardel * FITNESS FOR A PARTICULAR PURPOSE.
23 1.1 kardel *
24 1.1 kardel */
25 1.1 kardel
26 1.1 kardel #include "ntp_fp.h"
27 1.1 kardel #include "ascii.h"
28 1.1 kardel #include "parse.h"
29 1.1 kardel
30 1.1 kardel #ifndef PARSESTREAM
31 1.1 kardel #include "ntp_stdlib.h"
32 1.1 kardel #include <stdio.h>
33 1.1 kardel #else
34 1.1 kardel #include "sys/parsestreams.h"
35 1.1 kardel extern void printf (const char *, ...);
36 1.1 kardel #endif
37 1.1 kardel
38 1.1 kardel /*
39 1.1 kardel * In private e-mail alastair (at) wharton.co.uk said :
40 1.1 kardel * "If you are going to use the 400A and 404.2 system [for ntp] I recommend
41 1.1 kardel * that you set the 400A to output the message every second. The start of
42 1.1 kardel * transmission of the first byte of the message is synchronised to the
43 1.1 kardel * second edge."
44 1.1 kardel * The WHARTON 400A Series is able to send date/time serial messages
45 1.1 kardel * in 7 output formats. We use format 1 here because it is the shortest.
46 1.1 kardel * For use with this driver, the WHARTON 400A Series clock must be set-up
47 1.1 kardel * as follows :
48 1.1 kardel * Programmable Selected
49 1.1 kardel * Option No Option
50 1.1 kardel * BST or CET display 3 9 or 11
51 1.1 kardel * No external controller 7 0
52 1.1 kardel * Serial Output Format 1 9 1
53 1.1 kardel * Baud rate 9600 bps 10 96
54 1.1 kardel * Bit length 8 bits 11 8
55 1.1 kardel * Parity even 12 E
56 1.1 kardel *
57 1.1 kardel * WHARTON 400A Series output format 1 is as follows :
58 1.1 kardel *
59 1.1 kardel * Timestamp STXssmmhhDDMMYYSETX
60 1.1 kardel * Pos 0 12345678901234
61 1.1 kardel * 0 00000000011111
62 1.1 kardel *
63 1.1 kardel * STX start transmission (ASCII 0x02)
64 1.1 kardel * ETX end transmission (ASCII 0x03)
65 1.1 kardel * ss Second expressed in reversed decimal (units then tens)
66 1.1 kardel * mm Minute expressed in reversed decimal
67 1.1 kardel * hh Hour expressed in reversed decimal
68 1.1 kardel * DD Day of month expressed in reversed decimal
69 1.1 kardel * MM Month expressed in reversed decimal (January is 1)
70 1.1 kardel * YY Year (without century) expressed in reversed decimal
71 1.1 kardel * S Status byte : 0x30 +
72 1.1 kardel * bit 0 0 = MSF source 1 = DCF source
73 1.1 kardel * bit 1 0 = Winter time 1 = Summer time
74 1.1 kardel * bit 2 0 = not synchronised 1 = synchronised
75 1.1 kardel * bit 3 0 = no early warning 1 = early warning
76 1.1 kardel *
77 1.1 kardel */
78 1.1 kardel
79 1.1 kardel /*
80 1.1 kardel * cvt_wharton_400a
81 1.1 kardel *
82 1.1 kardel * convert simple type format
83 1.1 kardel */
84 1.1 kardel static u_long
85 1.1 kardel cvt_wharton_400a(
86 1.1 kardel unsigned char *buffer,
87 1.1 kardel int size,
88 1.1 kardel struct format *format,
89 1.1 kardel clocktime_t *clock_time,
90 1.1 kardel void *local
91 1.1 kardel )
92 1.1 kardel {
93 1.1 kardel int i;
94 1.1 kardel
95 1.1 kardel /* The given `size' includes a terminating null-character. */
96 1.1 kardel if (size != 16 || buffer[0] != STX || buffer[14] != ETX
97 1.1 kardel || buffer[13] < '0' || buffer[13] > ('0' + 0xf))
98 1.1 kardel return CVT_NONE;
99 1.1 kardel for (i = 1; i < 13; i += 1)
100 1.1 kardel if (buffer[i] < '0' || buffer[i] > '9')
101 1.1 kardel return CVT_NONE;
102 1.1 kardel clock_time->second = (buffer[2] - '0') * 10 + buffer[1] - '0';
103 1.1 kardel clock_time->minute = (buffer[4] - '0') * 10 + buffer[3] - '0';
104 1.1 kardel clock_time->hour = (buffer[6] - '0') * 10 + buffer[5] - '0';
105 1.1 kardel clock_time->day = (buffer[8] - '0') * 10 + buffer[7] - '0';
106 1.1 kardel clock_time->month = (buffer[10] - '0') * 10 + buffer[9] - '0';
107 1.1 kardel clock_time->year = (buffer[12] - '0') * 10 + buffer[11] - '0';
108 1.1 kardel clock_time->usecond = 0;
109 1.1 kardel if (buffer[13] & 0x1) /* We have CET time */
110 1.1 kardel clock_time->utcoffset = -1*60*60;
111 1.1 kardel else /* We have BST time */
112 1.1 kardel clock_time->utcoffset = 0;
113 1.1 kardel if (buffer[13] & 0x2) {
114 1.1 kardel clock_time->flags |= PARSEB_DST;
115 1.1 kardel clock_time->utcoffset += -1*60*60;
116 1.1 kardel }
117 1.1 kardel if (!(buffer[13] & 0x4))
118 1.1 kardel clock_time->flags |= PARSEB_NOSYNC;
119 1.1 kardel if (buffer[13] & 0x8)
120 1.1 kardel clock_time->flags |= PARSEB_ANNOUNCE;
121 1.1 kardel
122 1.1 kardel return CVT_OK;
123 1.1 kardel }
124 1.1 kardel
125 1.1 kardel /*
126 1.1 kardel * inp_wharton_400a
127 1.1 kardel *
128 1.1 kardel * grep data from input stream
129 1.1 kardel */
130 1.1 kardel static u_long
131 1.1 kardel inp_wharton_400a(
132 1.1 kardel parse_t *parseio,
133 1.1 kardel unsigned int ch,
134 1.1 kardel timestamp_t *tstamp
135 1.1 kardel )
136 1.1 kardel {
137 1.1 kardel unsigned int rtc;
138 1.1 kardel
139 1.1 kardel parseprintf(DD_PARSE, ("inp_wharton_400a(0x%lx, 0x%x, ...)\n", (long)parseio, ch));
140 1.1 kardel
141 1.1 kardel switch (ch)
142 1.1 kardel {
143 1.1 kardel case STX:
144 1.1 kardel parseprintf(DD_PARSE, ("inp_wharton_400a: STX seen\n"));
145 1.1 kardel
146 1.1 kardel parseio->parse_index = 1;
147 1.1 kardel parseio->parse_data[0] = ch;
148 1.1 kardel parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
149 1.1 kardel return PARSE_INP_SKIP;
150 1.1 kardel
151 1.1 kardel case ETX:
152 1.1 kardel parseprintf(DD_PARSE, ("inp_wharton_400a: ETX seen\n"));
153 1.1 kardel if ((rtc = parse_addchar(parseio, ch)) == PARSE_INP_SKIP)
154 1.1 kardel return parse_end(parseio);
155 1.1 kardel else
156 1.1 kardel return rtc;
157 1.1 kardel
158 1.1 kardel default:
159 1.1 kardel return parse_addchar(parseio, ch);
160 1.1 kardel }
161 1.1 kardel }
162 1.1 kardel
163 1.1 kardel clockformat_t clock_wharton_400a =
164 1.1 kardel {
165 1.1 kardel inp_wharton_400a, /* input handling function */
166 1.1 kardel cvt_wharton_400a, /* conversion function */
167 1.1 kardel 0, /* no PPS monitoring */
168 1.1 kardel 0, /* conversion configuration */
169 1.1 kardel "WHARTON 400A Series clock Output Format 1", /* String format name */
170 1.1 kardel 15, /* string buffer */
171 1.1 kardel 0 /* no private data (complete pakets) */
172 1.1 kardel };
173 1.1 kardel
174 1.1 kardel #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
175 1.1 kardel int clk_wharton_400a_bs;
176 1.1 kardel #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_WHARTON_400A) */
177 1.1 kardel
178 1.1 kardel /*
179 1.1 kardel * clk_wharton.c,v
180 1.1 kardel * Revision 4.1 1999/02/28 15:27:24 kardel
181 1.1 kardel * wharton clock integration
182 1.1 kardel *
183 1.1 kardel */
184