]>
Commit | Line | Data |
---|---|---|
2b0915c1 UH |
1 | ## |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
2b0915c1 | 21 | # DCF77 protocol decoder |
2b0915c1 UH |
22 | |
23 | import sigrokdecode as srd | |
24 | import calendar | |
25 | ||
2b0915c1 UH |
26 | # Return the specified BCD number (max. 8 bits) as integer. |
27 | def bcd2int(b): | |
28 | return (b & 0x0f) + ((b >> 4) * 10) | |
29 | ||
30 | class Decoder(srd.Decoder): | |
a2c2afd9 | 31 | api_version = 1 |
2b0915c1 UH |
32 | id = 'dcf77' |
33 | name = 'DCF77' | |
3d3da57d | 34 | longname = 'DCF77 time protocol' |
a465436e | 35 | desc = 'European longwave time signal (77.5kHz carrier signal).' |
2b0915c1 UH |
36 | license = 'gplv2+' |
37 | inputs = ['logic'] | |
38 | outputs = ['dcf77'] | |
39 | probes = [ | |
40 | {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'}, | |
41 | ] | |
b77614bc | 42 | optional_probes = [ |
fcd8c14d | 43 | {'id': 'pon', 'name': 'PON', 'desc': 'Power on'}, |
decde15e | 44 | ] |
2b0915c1 UH |
45 | options = {} |
46 | annotations = [ | |
ee3e279c | 47 | ['Text', 'Human-readable text'], |
fcd8c14d | 48 | ['Warnings', 'Human-readable warnings'], |
2b0915c1 UH |
49 | ] |
50 | ||
51 | def __init__(self, **kwargs): | |
2b716038 | 52 | self.state = 'WAIT FOR RISING EDGE' |
2fcd7c22 | 53 | self.oldpins = None |
2b0915c1 | 54 | self.oldval = None |
fcd8c14d | 55 | self.oldpon = None |
2b0915c1 UH |
56 | self.samplenum = 0 |
57 | self.bit_start = 0 | |
58 | self.bit_start_old = 0 | |
59 | self.bitcount = 0 # Counter for the DCF77 bits (0..58) | |
60 | self.dcf77_bitnumber_is_known = 0 | |
61 | ||
62 | def start(self, metadata): | |
63 | self.samplerate = metadata['samplerate'] | |
64 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'dcf77') | |
65 | self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77') | |
66 | ||
67 | def report(self): | |
68 | pass | |
69 | ||
70 | # TODO: Which range to use? Only the 100ms/200ms or full second? | |
71 | def handle_dcf77_bit(self, bit): | |
72 | c = self.bitcount | |
73 | a = self.out_ann | |
74 | ss = es = 0 # FIXME | |
75 | ||
76 | # Create one annotation for each DCF77 bit (containing the 0/1 value). | |
77 | # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the | |
78 | # 0..58 bits it is (because we haven't seen a 'new minute' marker yet). | |
79 | # Otherwise, use 'DCF77 bit x: val'. | |
80 | s = '' if self.dcf77_bitnumber_is_known else 'Unknown ' | |
81 | self.put(ss, es, a, [0, ['%sDCF77 bit %d: %d' % (s, c, bit)]]) | |
82 | ||
83 | # If we're not sure yet which of the 0..58 DCF77 bits we have, return. | |
84 | # We don't want to decode bogus data. | |
85 | if not self.dcf77_bitnumber_is_known: | |
86 | return | |
87 | ||
88 | # Output specific "decoded" annotations for the respective DCF77 bits. | |
89 | if c == 0: | |
90 | # Start of minute: DCF bit 0. | |
91 | if bit == 0: | |
92 | self.put(ss, es, a, [0, ['Start of minute (always 0)']]) | |
93 | else: | |
94 | self.put(ss, es, a, [0, ['ERROR: Start of minute != 0']]) | |
95 | elif c in range(1, 14 + 1): | |
96 | # Special bits (civil warnings, weather forecast): DCF77 bits 1-14. | |
97 | if c == 1: | |
98 | self.tmp = bit | |
99 | else: | |
100 | self.tmp |= (bit << (c - 1)) | |
101 | if c == 14: | |
102 | self.put(ss, es, a, [0, ['Special bits: %s' % bin(self.tmp)]]) | |
103 | elif c == 15: | |
104 | s = '' if (bit == 1) else 'not ' | |
105 | self.put(ss, es, a, [0, ['Call bit is %sset' % s]]) | |
106 | # TODO: Previously this bit indicated use of the backup antenna. | |
107 | elif c == 16: | |
108 | s = '' if (bit == 1) else 'not ' | |
109 | self.put(ss, es, a, [0, ['Summer time announcement %sactive' % s]]) | |
110 | elif c == 17: | |
111 | s = '' if (bit == 1) else 'not ' | |
112 | self.put(ss, es, a, [0, ['CEST is %sin effect' % s]]) | |
113 | elif c == 18: | |
114 | s = '' if (bit == 1) else 'not ' | |
115 | self.put(ss, es, a, [0, ['CET is %sin effect' % s]]) | |
116 | elif c == 19: | |
117 | s = '' if (bit == 1) else 'not ' | |
118 | self.put(ss, es, a, [0, ['Leap second announcement %sactive' % s]]) | |
119 | elif c == 20: | |
120 | # Start of encoded time: DCF bit 20. | |
121 | if bit == 1: | |
122 | self.put(ss, es, a, [0, ['Start of encoded time (always 1)']]) | |
123 | else: | |
124 | self.put(ss, es, a, | |
125 | [0, ['ERROR: Start of encoded time != 1']]) | |
126 | elif c in range(21, 27 + 1): | |
127 | # Minutes (0-59): DCF77 bits 21-27 (BCD format). | |
128 | if c == 21: | |
129 | self.tmp = bit | |
130 | else: | |
131 | self.tmp |= (bit << (c - 21)) | |
132 | if c == 27: | |
133 | self.put(ss, es, a, [0, ['Minutes: %d' % bcd2int(self.tmp)]]) | |
134 | elif c == 28: | |
135 | # Even parity over minute bits (21-28): DCF77 bit 28. | |
136 | self.tmp |= (bit << (c - 21)) | |
137 | parity = bin(self.tmp).count('1') | |
138 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
139 | self.put(ss, es, a, [0, ['Minute parity: %s' % s]]) | |
140 | elif c in range(29, 34 + 1): | |
141 | # Hours (0-23): DCF77 bits 29-34 (BCD format). | |
142 | if c == 29: | |
143 | self.tmp = bit | |
144 | else: | |
145 | self.tmp |= (bit << (c - 29)) | |
146 | if c == 34: | |
147 | self.put(ss, es, a, [0, ['Hours: %d' % bcd2int(self.tmp)]]) | |
148 | elif c == 35: | |
149 | # Even parity over hour bits (29-35): DCF77 bit 35. | |
150 | self.tmp |= (bit << (c - 29)) | |
151 | parity = bin(self.tmp).count('1') | |
152 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
153 | self.put(ss, es, a, [0, ['Hour parity: %s' % s]]) | |
154 | elif c in range(36, 41 + 1): | |
155 | # Day of month (1-31): DCF77 bits 36-41 (BCD format). | |
156 | if c == 36: | |
157 | self.tmp = bit | |
158 | else: | |
159 | self.tmp |= (bit << (c - 36)) | |
160 | if c == 41: | |
161 | self.put(ss, es, a, [0, ['Day: %d' % bcd2int(self.tmp)]]) | |
162 | elif c in range(42, 44 + 1): | |
163 | # Day of week (1-7): DCF77 bits 42-44 (BCD format). | |
164 | # A value of 1 means Monday, 7 means Sunday. | |
165 | if c == 42: | |
166 | self.tmp = bit | |
167 | else: | |
168 | self.tmp |= (bit << (c - 42)) | |
169 | if c == 44: | |
170 | d = bcd2int(self.tmp) | |
171 | dn = calendar.day_name[d - 1] # day_name[0] == Monday | |
172 | self.put(ss, es, a, [0, ['Day of week: %d (%s)' % (d, dn)]]) | |
173 | elif c in range(45, 49 + 1): | |
174 | # Month (1-12): DCF77 bits 45-49 (BCD format). | |
175 | if c == 45: | |
176 | self.tmp = bit | |
177 | else: | |
178 | self.tmp |= (bit << (c - 45)) | |
179 | if c == 49: | |
180 | m = bcd2int(self.tmp) | |
181 | mn = calendar.month_name[m] # month_name[1] == January | |
182 | self.put(ss, es, a, [0, ['Month: %d (%s)' % (m, mn)]]) | |
183 | elif c in range(50, 57 + 1): | |
184 | # Year (0-99): DCF77 bits 50-57 (BCD format). | |
185 | if c == 50: | |
186 | self.tmp = bit | |
187 | else: | |
188 | self.tmp |= (bit << (c - 50)) | |
189 | if c == 57: | |
190 | self.put(ss, es, a, [0, ['Year: %d' % bcd2int(self.tmp)]]) | |
191 | elif c == 58: | |
192 | # Even parity over date bits (36-58): DCF77 bit 58. | |
193 | self.tmp |= (bit << (c - 50)) | |
194 | parity = bin(self.tmp).count('1') | |
195 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
196 | self.put(ss, es, a, [0, ['Date parity: %s' % s]]) | |
197 | else: | |
198 | raise Exception('Invalid DCF77 bit: %d' % c) | |
199 | ||
200 | def decode(self, ss, es, data): | |
2fcd7c22 UH |
201 | for (self.samplenum, pins) in data: |
202 | ||
203 | # Ignore identical samples early on (for performance reasons). | |
204 | if self.oldpins == pins: | |
205 | continue | |
206 | self.oldpins, (val, pon) = pins, pins | |
fcd8c14d UH |
207 | |
208 | # Always remember the old PON state. | |
209 | if self.oldpon != pon: | |
210 | self.oldpon = pon | |
211 | ||
212 | # Warn if PON goes low. | |
213 | if self.oldpon == 1 and pon == 0: | |
214 | self.pon_ss = self.samplenum | |
215 | self.put(self.samplenum, self.samplenum, self.out_ann, | |
216 | [1, ['Warning: PON goes low, DCF77 reception ' | |
217 | 'no longer possible']]) | |
218 | elif self.oldpon == 0 and pon == 1: | |
219 | self.put(self.samplenum, self.samplenum, self.out_ann, | |
220 | [0, ['PON goes high, DCF77 reception now possible']]) | |
221 | self.put(self.pon_ss, self.samplenum, self.out_ann, | |
222 | [1, ['Warning: PON low, DCF77 reception disabled']]) | |
223 | ||
224 | # Ignore samples where PON == 0, they can't contain DCF77 signals. | |
225 | if pon == 0: | |
226 | continue | |
2b0915c1 | 227 | |
2b716038 | 228 | if self.state == 'WAIT FOR RISING EDGE': |
2b0915c1 UH |
229 | # Wait until the next rising edge occurs. |
230 | if not (self.oldval == 0 and val == 1): | |
231 | self.oldval = val | |
232 | continue | |
233 | ||
234 | # Save the sample number where the DCF77 bit begins. | |
235 | self.bit_start = self.samplenum | |
236 | ||
237 | # Calculate the length (in ms) between two rising edges. | |
238 | len_edges = self.bit_start - self.bit_start_old | |
239 | len_edges_ms = int((len_edges / self.samplerate) * 1000) | |
240 | ||
241 | # The time between two rising edges is usually around 1000ms. | |
242 | # For DCF77 bit 59, there is no rising edge at all, i.e. the | |
243 | # time between DCF77 bit 59 and DCF77 bit 0 (of the next | |
244 | # minute) is around 2000ms. Thus, if we see an edge with a | |
245 | # 2000ms distance to the last one, this edge marks the | |
246 | # beginning of a new minute (and DCF77 bit 0 of that minute). | |
247 | if len_edges_ms in range(1600, 2400 + 1): | |
248 | self.put(ss, es, self.out_ann, [0, ['New minute starts']]) | |
249 | self.bitcount = 0 | |
250 | self.bit_start_old = self.bit_start | |
251 | self.dcf77_bitnumber_is_known = 1 | |
abbc1285 | 252 | # Don't switch to 'GET BIT' state this time. |
2b0915c1 UH |
253 | continue |
254 | ||
255 | self.bit_start_old = self.bit_start | |
abbc1285 | 256 | self.state = 'GET BIT' |
2b0915c1 | 257 | |
abbc1285 | 258 | elif self.state == 'GET BIT': |
2b0915c1 UH |
259 | # Wait until the next falling edge occurs. |
260 | if not (self.oldval == 1 and val == 0): | |
261 | self.oldval = val | |
262 | continue | |
263 | ||
264 | # Calculate the length (in ms) of the current high period. | |
265 | len_high = self.samplenum - self.bit_start | |
266 | len_high_ms = int((len_high / self.samplerate) * 1000) | |
267 | ||
268 | # If the high signal was 100ms long, that encodes a 0 bit. | |
269 | # If it was 200ms long, that encodes a 1 bit. | |
270 | if len_high_ms in range(40, 160 + 1): | |
271 | bit = 0 | |
272 | elif len_high_ms in range(161, 260 + 1): | |
273 | bit = 1 | |
274 | else: | |
275 | bit = -1 # TODO: Error? | |
276 | ||
abbc1285 | 277 | # There's no bit 59, make sure none is decoded. |
2b0915c1 UH |
278 | if bit in (0, 1) and self.bitcount in range(0, 58 + 1): |
279 | self.handle_dcf77_bit(bit) | |
280 | self.bitcount += 1 | |
281 | ||
2b716038 | 282 | self.state = 'WAIT FOR RISING EDGE' |
2b0915c1 UH |
283 | |
284 | else: | |
0eeeb544 | 285 | raise Exception('Invalid state: %s' % self.state) |
2b0915c1 UH |
286 | |
287 | self.oldval = val | |
288 |