]>
Commit | Line | Data |
---|---|---|
2b0915c1 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
2b0915c1 | 3 | ## |
09bca511 | 4 | ## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> |
2b0915c1 UH |
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 | |
09bca511 | 59 | self.bit_end = 0 |
2b0915c1 UH |
60 | self.bitcount = 0 # Counter for the DCF77 bits (0..58) |
61 | self.dcf77_bitnumber_is_known = 0 | |
62 | ||
63 | def start(self, metadata): | |
64 | self.samplerate = metadata['samplerate'] | |
65 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'dcf77') | |
66 | self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77') | |
67 | ||
68 | def report(self): | |
69 | pass | |
70 | ||
09bca511 UH |
71 | def putx(self, data): |
72 | self.put(self.bit_start, self.bit_end, self.out_ann, data) | |
73 | ||
2b0915c1 UH |
74 | # TODO: Which range to use? Only the 100ms/200ms or full second? |
75 | def handle_dcf77_bit(self, bit): | |
76 | c = self.bitcount | |
2b0915c1 UH |
77 | |
78 | # Create one annotation for each DCF77 bit (containing the 0/1 value). | |
79 | # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the | |
80 | # 0..58 bits it is (because we haven't seen a 'new minute' marker yet). | |
81 | # Otherwise, use 'DCF77 bit x: val'. | |
82 | s = '' if self.dcf77_bitnumber_is_known else 'Unknown ' | |
09bca511 | 83 | self.putx([0, ['%sDCF77 bit %d: %d' % (s, c, bit)]]) |
2b0915c1 UH |
84 | |
85 | # If we're not sure yet which of the 0..58 DCF77 bits we have, return. | |
86 | # We don't want to decode bogus data. | |
87 | if not self.dcf77_bitnumber_is_known: | |
88 | return | |
89 | ||
90 | # Output specific "decoded" annotations for the respective DCF77 bits. | |
91 | if c == 0: | |
92 | # Start of minute: DCF bit 0. | |
93 | if bit == 0: | |
09bca511 | 94 | self.putx([0, ['Start of minute (always 0)']]) |
2b0915c1 | 95 | else: |
09bca511 | 96 | self.putx([0, ['ERROR: Start of minute != 0']]) |
2b0915c1 UH |
97 | elif c in range(1, 14 + 1): |
98 | # Special bits (civil warnings, weather forecast): DCF77 bits 1-14. | |
99 | if c == 1: | |
100 | self.tmp = bit | |
101 | else: | |
102 | self.tmp |= (bit << (c - 1)) | |
103 | if c == 14: | |
09bca511 | 104 | self.putx([0, ['Special bits: %s' % bin(self.tmp)]]) |
2b0915c1 UH |
105 | elif c == 15: |
106 | s = '' if (bit == 1) else 'not ' | |
09bca511 | 107 | self.putx([0, ['Call bit is %sset' % s]]) |
2b0915c1 UH |
108 | # TODO: Previously this bit indicated use of the backup antenna. |
109 | elif c == 16: | |
110 | s = '' if (bit == 1) else 'not ' | |
09bca511 | 111 | self.putx([0, ['Summer time announcement %sactive' % s]]) |
2b0915c1 UH |
112 | elif c == 17: |
113 | s = '' if (bit == 1) else 'not ' | |
09bca511 | 114 | self.putx([0, ['CEST is %sin effect' % s]]) |
2b0915c1 UH |
115 | elif c == 18: |
116 | s = '' if (bit == 1) else 'not ' | |
09bca511 | 117 | self.putx([0, ['CET is %sin effect' % s]]) |
2b0915c1 UH |
118 | elif c == 19: |
119 | s = '' if (bit == 1) else 'not ' | |
09bca511 | 120 | self.putx([0, ['Leap second announcement %sactive' % s]]) |
2b0915c1 UH |
121 | elif c == 20: |
122 | # Start of encoded time: DCF bit 20. | |
123 | if bit == 1: | |
09bca511 | 124 | self.putx([0, ['Start of encoded time (always 1)']]) |
2b0915c1 | 125 | else: |
09bca511 | 126 | self.putx([0, ['ERROR: Start of encoded time != 1']]) |
2b0915c1 UH |
127 | elif c in range(21, 27 + 1): |
128 | # Minutes (0-59): DCF77 bits 21-27 (BCD format). | |
129 | if c == 21: | |
130 | self.tmp = bit | |
131 | else: | |
132 | self.tmp |= (bit << (c - 21)) | |
133 | if c == 27: | |
09bca511 | 134 | self.putx([0, ['Minutes: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
135 | elif c == 28: |
136 | # Even parity over minute bits (21-28): DCF77 bit 28. | |
137 | self.tmp |= (bit << (c - 21)) | |
138 | parity = bin(self.tmp).count('1') | |
139 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
09bca511 | 140 | self.putx([0, ['Minute parity: %s' % s]]) |
2b0915c1 UH |
141 | elif c in range(29, 34 + 1): |
142 | # Hours (0-23): DCF77 bits 29-34 (BCD format). | |
143 | if c == 29: | |
144 | self.tmp = bit | |
145 | else: | |
146 | self.tmp |= (bit << (c - 29)) | |
147 | if c == 34: | |
09bca511 | 148 | self.putx([0, ['Hours: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
149 | elif c == 35: |
150 | # Even parity over hour bits (29-35): DCF77 bit 35. | |
151 | self.tmp |= (bit << (c - 29)) | |
152 | parity = bin(self.tmp).count('1') | |
153 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
09bca511 | 154 | self.putx([0, ['Hour parity: %s' % s]]) |
2b0915c1 UH |
155 | elif c in range(36, 41 + 1): |
156 | # Day of month (1-31): DCF77 bits 36-41 (BCD format). | |
157 | if c == 36: | |
158 | self.tmp = bit | |
159 | else: | |
160 | self.tmp |= (bit << (c - 36)) | |
161 | if c == 41: | |
09bca511 | 162 | self.putx([0, ['Day: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
163 | elif c in range(42, 44 + 1): |
164 | # Day of week (1-7): DCF77 bits 42-44 (BCD format). | |
165 | # A value of 1 means Monday, 7 means Sunday. | |
166 | if c == 42: | |
167 | self.tmp = bit | |
168 | else: | |
169 | self.tmp |= (bit << (c - 42)) | |
170 | if c == 44: | |
171 | d = bcd2int(self.tmp) | |
172 | dn = calendar.day_name[d - 1] # day_name[0] == Monday | |
09bca511 | 173 | self.putx([0, ['Day of week: %d (%s)' % (d, dn)]]) |
2b0915c1 UH |
174 | elif c in range(45, 49 + 1): |
175 | # Month (1-12): DCF77 bits 45-49 (BCD format). | |
176 | if c == 45: | |
177 | self.tmp = bit | |
178 | else: | |
179 | self.tmp |= (bit << (c - 45)) | |
180 | if c == 49: | |
181 | m = bcd2int(self.tmp) | |
182 | mn = calendar.month_name[m] # month_name[1] == January | |
09bca511 | 183 | self.putx([0, ['Month: %d (%s)' % (m, mn)]]) |
2b0915c1 UH |
184 | elif c in range(50, 57 + 1): |
185 | # Year (0-99): DCF77 bits 50-57 (BCD format). | |
186 | if c == 50: | |
187 | self.tmp = bit | |
188 | else: | |
189 | self.tmp |= (bit << (c - 50)) | |
190 | if c == 57: | |
09bca511 | 191 | self.putx([0, ['Year: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
192 | elif c == 58: |
193 | # Even parity over date bits (36-58): DCF77 bit 58. | |
194 | self.tmp |= (bit << (c - 50)) | |
195 | parity = bin(self.tmp).count('1') | |
196 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
09bca511 | 197 | self.putx([0, ['Date parity: %s' % s]]) |
2b0915c1 UH |
198 | else: |
199 | raise Exception('Invalid DCF77 bit: %d' % c) | |
200 | ||
201 | def decode(self, ss, es, data): | |
2fcd7c22 UH |
202 | for (self.samplenum, pins) in data: |
203 | ||
204 | # Ignore identical samples early on (for performance reasons). | |
205 | if self.oldpins == pins: | |
206 | continue | |
207 | self.oldpins, (val, pon) = pins, pins | |
fcd8c14d UH |
208 | |
209 | # Always remember the old PON state. | |
210 | if self.oldpon != pon: | |
211 | self.oldpon = pon | |
212 | ||
213 | # Warn if PON goes low. | |
214 | if self.oldpon == 1 and pon == 0: | |
215 | self.pon_ss = self.samplenum | |
216 | self.put(self.samplenum, self.samplenum, self.out_ann, | |
217 | [1, ['Warning: PON goes low, DCF77 reception ' | |
218 | 'no longer possible']]) | |
219 | elif self.oldpon == 0 and pon == 1: | |
220 | self.put(self.samplenum, self.samplenum, self.out_ann, | |
221 | [0, ['PON goes high, DCF77 reception now possible']]) | |
222 | self.put(self.pon_ss, self.samplenum, self.out_ann, | |
223 | [1, ['Warning: PON low, DCF77 reception disabled']]) | |
224 | ||
225 | # Ignore samples where PON == 0, they can't contain DCF77 signals. | |
226 | if pon == 0: | |
227 | continue | |
2b0915c1 | 228 | |
2b716038 | 229 | if self.state == 'WAIT FOR RISING EDGE': |
2b0915c1 UH |
230 | # Wait until the next rising edge occurs. |
231 | if not (self.oldval == 0 and val == 1): | |
232 | self.oldval = val | |
233 | continue | |
234 | ||
235 | # Save the sample number where the DCF77 bit begins. | |
236 | self.bit_start = self.samplenum | |
237 | ||
238 | # Calculate the length (in ms) between two rising edges. | |
239 | len_edges = self.bit_start - self.bit_start_old | |
240 | len_edges_ms = int((len_edges / self.samplerate) * 1000) | |
241 | ||
242 | # The time between two rising edges is usually around 1000ms. | |
243 | # For DCF77 bit 59, there is no rising edge at all, i.e. the | |
244 | # time between DCF77 bit 59 and DCF77 bit 0 (of the next | |
245 | # minute) is around 2000ms. Thus, if we see an edge with a | |
246 | # 2000ms distance to the last one, this edge marks the | |
247 | # beginning of a new minute (and DCF77 bit 0 of that minute). | |
248 | if len_edges_ms in range(1600, 2400 + 1): | |
2b0915c1 UH |
249 | self.bitcount = 0 |
250 | self.bit_start_old = self.bit_start | |
251 | self.dcf77_bitnumber_is_known = 1 | |
2b0915c1 UH |
252 | |
253 | self.bit_start_old = self.bit_start | |
abbc1285 | 254 | self.state = 'GET BIT' |
2b0915c1 | 255 | |
abbc1285 | 256 | elif self.state == 'GET BIT': |
2b0915c1 UH |
257 | # Wait until the next falling edge occurs. |
258 | if not (self.oldval == 1 and val == 0): | |
259 | self.oldval = val | |
260 | continue | |
261 | ||
09bca511 UH |
262 | # Save the sample number where the DCF77 bit ends. |
263 | self.bit_end = self.samplenum | |
264 | ||
2b0915c1 UH |
265 | # Calculate the length (in ms) of the current high period. |
266 | len_high = self.samplenum - self.bit_start | |
267 | len_high_ms = int((len_high / self.samplerate) * 1000) | |
268 | ||
269 | # If the high signal was 100ms long, that encodes a 0 bit. | |
270 | # If it was 200ms long, that encodes a 1 bit. | |
271 | if len_high_ms in range(40, 160 + 1): | |
272 | bit = 0 | |
273 | elif len_high_ms in range(161, 260 + 1): | |
274 | bit = 1 | |
275 | else: | |
276 | bit = -1 # TODO: Error? | |
277 | ||
abbc1285 | 278 | # There's no bit 59, make sure none is decoded. |
2b0915c1 UH |
279 | if bit in (0, 1) and self.bitcount in range(0, 58 + 1): |
280 | self.handle_dcf77_bit(bit) | |
281 | self.bitcount += 1 | |
282 | ||
2b716038 | 283 | self.state = 'WAIT FOR RISING EDGE' |
2b0915c1 UH |
284 | |
285 | else: | |
0eeeb544 | 286 | raise Exception('Invalid state: %s' % self.state) |
2b0915c1 UH |
287 | |
288 | self.oldval = val | |
289 |