]>
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 UH |
21 | import sigrokdecode as srd |
22 | import calendar | |
23 | ||
2b0915c1 UH |
24 | # Return the specified BCD number (max. 8 bits) as integer. |
25 | def bcd2int(b): | |
26 | return (b & 0x0f) + ((b >> 4) * 10) | |
27 | ||
28 | class Decoder(srd.Decoder): | |
a2c2afd9 | 29 | api_version = 1 |
2b0915c1 UH |
30 | id = 'dcf77' |
31 | name = 'DCF77' | |
3d3da57d | 32 | longname = 'DCF77 time protocol' |
a465436e | 33 | desc = 'European longwave time signal (77.5kHz carrier signal).' |
2b0915c1 UH |
34 | license = 'gplv2+' |
35 | inputs = ['logic'] | |
36 | outputs = ['dcf77'] | |
37 | probes = [ | |
38 | {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'}, | |
39 | ] | |
54f1b2b7 | 40 | optional_probes = [] |
2b0915c1 UH |
41 | options = {} |
42 | annotations = [ | |
9f2f42c0 UH |
43 | ['start-of-minute', 'Start of minute'], |
44 | ['special-bits', 'Special bits (civil warnings, weather forecast)'], | |
45 | ['call-bit', 'Call bit'], | |
46 | ['summer-time', 'Summer time announcement'], | |
8a18c4e7 UH |
47 | ['cest', 'CEST bit'], |
48 | ['cet', 'CET bit'], | |
9f2f42c0 UH |
49 | ['leap-second', 'Leap second bit'], |
50 | ['start-of-time', 'Start of encoded time'], | |
8a18c4e7 | 51 | ['minute', 'Minute'], |
9f2f42c0 | 52 | ['minute-parity', 'Minute parity bit'], |
8a18c4e7 | 53 | ['hour', 'Hour'], |
9f2f42c0 | 54 | ['hour-parity', 'Hour parity bit'], |
8a18c4e7 | 55 | ['day', 'Day of month'], |
9f2f42c0 | 56 | ['day-of-week', 'Day of week'], |
8a18c4e7 UH |
57 | ['month', 'Month'], |
58 | ['year', 'Year'], | |
9f2f42c0 UH |
59 | ['date-parity', 'Date parity bit'], |
60 | ['raw-bits', 'Raw bits'], | |
61 | ['unknown-bits', 'Unknown bits'], | |
8a18c4e7 | 62 | ['warnings', 'Human-readable warnings'], |
2b0915c1 UH |
63 | ] |
64 | ||
65 | def __init__(self, **kwargs): | |
f372d597 | 66 | self.samplerate = None |
2b716038 | 67 | self.state = 'WAIT FOR RISING EDGE' |
2fcd7c22 | 68 | self.oldpins = None |
2b0915c1 UH |
69 | self.oldval = None |
70 | self.samplenum = 0 | |
3e9b87f7 | 71 | self.ss_bit = self.ss_bit_old = self.es_bit = self.ss_block = 0 |
69ada057 | 72 | self.datebits = [] |
2b0915c1 UH |
73 | self.bitcount = 0 # Counter for the DCF77 bits (0..58) |
74 | self.dcf77_bitnumber_is_known = 0 | |
75 | ||
f372d597 | 76 | def start(self): |
c515eed7 | 77 | # self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 78 | self.out_ann = self.register(srd.OUTPUT_ANN) |
2b0915c1 | 79 | |
f372d597 BV |
80 | def metadata(self, key, value): |
81 | if key == srd.SRD_CONF_SAMPLERATE: | |
82 | self.samplerate = value | |
83 | ||
09bca511 | 84 | def putx(self, data): |
3e9b87f7 | 85 | # Annotation for a single DCF77 bit. |
aa4cf4c6 | 86 | self.put(self.ss_bit, self.es_bit, self.out_ann, data) |
09bca511 | 87 | |
3e9b87f7 UH |
88 | def putb(self, data): |
89 | # Annotation for a multi-bit DCF77 field. | |
90 | self.put(self.ss_block, self.samplenum, self.out_ann, data) | |
91 | ||
2b0915c1 UH |
92 | # TODO: Which range to use? Only the 100ms/200ms or full second? |
93 | def handle_dcf77_bit(self, bit): | |
94 | c = self.bitcount | |
2b0915c1 UH |
95 | |
96 | # Create one annotation for each DCF77 bit (containing the 0/1 value). | |
97 | # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the | |
98 | # 0..58 bits it is (because we haven't seen a 'new minute' marker yet). | |
99 | # Otherwise, use 'DCF77 bit x: val'. | |
99f5e74a UH |
100 | s = 'B' if self.dcf77_bitnumber_is_known else 'Unknown b' |
101 | ann = 17 if self.dcf77_bitnumber_is_known else 18 | |
102 | self.putx([ann, ['%sit %d: %d' % (s, c, bit), '%d' % bit]]) | |
2b0915c1 UH |
103 | |
104 | # If we're not sure yet which of the 0..58 DCF77 bits we have, return. | |
105 | # We don't want to decode bogus data. | |
106 | if not self.dcf77_bitnumber_is_known: | |
107 | return | |
108 | ||
69ada057 UH |
109 | # Collect bits 36-58, we'll need them for a parity check later. |
110 | if c in range(36, 58 + 1): | |
111 | self.datebits.append(bit) | |
112 | ||
2b0915c1 UH |
113 | # Output specific "decoded" annotations for the respective DCF77 bits. |
114 | if c == 0: | |
115 | # Start of minute: DCF bit 0. | |
116 | if bit == 0: | |
99f5e74a UH |
117 | self.putx([0, ['Start of minute (always 0)', |
118 | 'Start of minute', 'SoM']]) | |
2b0915c1 | 119 | else: |
99f5e74a | 120 | self.putx([19, ['Start of minute != 0', 'SoM != 0']]) |
2b0915c1 UH |
121 | elif c in range(1, 14 + 1): |
122 | # Special bits (civil warnings, weather forecast): DCF77 bits 1-14. | |
123 | if c == 1: | |
124 | self.tmp = bit | |
3e9b87f7 | 125 | self.ss_block = self.ss_bit |
2b0915c1 UH |
126 | else: |
127 | self.tmp |= (bit << (c - 1)) | |
128 | if c == 14: | |
99f5e74a UH |
129 | s = bin(self.tmp)[2:].zfill(14) |
130 | self.putb([1, ['Special bits: %s' % s, 'SB: %s' % s]]) | |
2b0915c1 UH |
131 | elif c == 15: |
132 | s = '' if (bit == 1) else 'not ' | |
99f5e74a | 133 | self.putx([2, ['Call bit: %sset' % s, 'CB: %sset' % s]]) |
2b0915c1 UH |
134 | # TODO: Previously this bit indicated use of the backup antenna. |
135 | elif c == 16: | |
136 | s = '' if (bit == 1) else 'not ' | |
99f5e74a UH |
137 | x = 'yes' if (bit == 1) else 'no' |
138 | self.putx([3, ['Summer time announcement: %sactive' % s, | |
139 | 'Summer time: %sactive' % s, | |
140 | 'Summer time: %s' % x, 'ST: %s' % x]]) | |
2b0915c1 UH |
141 | elif c == 17: |
142 | s = '' if (bit == 1) else 'not ' | |
99f5e74a UH |
143 | x = 'yes' if (bit == 1) else 'no' |
144 | self.putx([4, ['CEST: %sin effect' % s, 'CEST: %s' % x]]) | |
2b0915c1 UH |
145 | elif c == 18: |
146 | s = '' if (bit == 1) else 'not ' | |
99f5e74a UH |
147 | x = 'yes' if (bit == 1) else 'no' |
148 | self.putx([5, ['CET: %sin effect' % s, 'CET: %s' % x]]) | |
2b0915c1 UH |
149 | elif c == 19: |
150 | s = '' if (bit == 1) else 'not ' | |
99f5e74a UH |
151 | x = 'yes' if (bit == 1) else 'no' |
152 | self.putx([6, ['Leap second announcement: %sactive' % s, | |
153 | 'Leap second: %sactive' % s, | |
154 | 'Leap second: %s' % x, 'LS: %s' % x]]) | |
2b0915c1 UH |
155 | elif c == 20: |
156 | # Start of encoded time: DCF bit 20. | |
157 | if bit == 1: | |
99f5e74a UH |
158 | self.putx([7, ['Start of encoded time (always 1)', |
159 | 'Start of encoded time', 'SoeT']]) | |
2b0915c1 | 160 | else: |
99f5e74a | 161 | self.putx([19, ['Start of encoded time != 1', 'SoeT != 1']]) |
2b0915c1 UH |
162 | elif c in range(21, 27 + 1): |
163 | # Minutes (0-59): DCF77 bits 21-27 (BCD format). | |
164 | if c == 21: | |
165 | self.tmp = bit | |
3e9b87f7 | 166 | self.ss_block = self.ss_bit |
2b0915c1 UH |
167 | else: |
168 | self.tmp |= (bit << (c - 21)) | |
169 | if c == 27: | |
99f5e74a UH |
170 | m = bcd2int(self.tmp) |
171 | self.putb([8, ['Minutes: %d' % m, 'Min: %d' % m]]) | |
2b0915c1 UH |
172 | elif c == 28: |
173 | # Even parity over minute bits (21-28): DCF77 bit 28. | |
174 | self.tmp |= (bit << (c - 21)) | |
175 | parity = bin(self.tmp).count('1') | |
176 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
99f5e74a | 177 | self.putx([9, ['Minute parity: %s' % s, 'Min parity: %s' % s]]) |
2b0915c1 UH |
178 | elif c in range(29, 34 + 1): |
179 | # Hours (0-23): DCF77 bits 29-34 (BCD format). | |
180 | if c == 29: | |
181 | self.tmp = bit | |
3e9b87f7 | 182 | self.ss_block = self.ss_bit |
2b0915c1 UH |
183 | else: |
184 | self.tmp |= (bit << (c - 29)) | |
185 | if c == 34: | |
3e9b87f7 | 186 | self.putb([10, ['Hours: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
187 | elif c == 35: |
188 | # Even parity over hour bits (29-35): DCF77 bit 35. | |
189 | self.tmp |= (bit << (c - 29)) | |
190 | parity = bin(self.tmp).count('1') | |
191 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' | |
8a18c4e7 | 192 | self.putx([11, ['Hour parity: %s' % s]]) |
2b0915c1 UH |
193 | elif c in range(36, 41 + 1): |
194 | # Day of month (1-31): DCF77 bits 36-41 (BCD format). | |
195 | if c == 36: | |
196 | self.tmp = bit | |
3e9b87f7 | 197 | self.ss_block = self.ss_bit |
2b0915c1 UH |
198 | else: |
199 | self.tmp |= (bit << (c - 36)) | |
200 | if c == 41: | |
3e9b87f7 | 201 | self.putb([12, ['Day: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
202 | elif c in range(42, 44 + 1): |
203 | # Day of week (1-7): DCF77 bits 42-44 (BCD format). | |
204 | # A value of 1 means Monday, 7 means Sunday. | |
205 | if c == 42: | |
206 | self.tmp = bit | |
3e9b87f7 | 207 | self.ss_block = self.ss_bit |
2b0915c1 UH |
208 | else: |
209 | self.tmp |= (bit << (c - 42)) | |
210 | if c == 44: | |
211 | d = bcd2int(self.tmp) | |
212 | dn = calendar.day_name[d - 1] # day_name[0] == Monday | |
99f5e74a UH |
213 | self.putb([13, ['Day of week: %d (%s)' % (d, dn), |
214 | 'DoW: %d (%s)' % (d, dn)]]) | |
2b0915c1 UH |
215 | elif c in range(45, 49 + 1): |
216 | # Month (1-12): DCF77 bits 45-49 (BCD format). | |
217 | if c == 45: | |
218 | self.tmp = bit | |
3e9b87f7 | 219 | self.ss_block = self.ss_bit |
2b0915c1 UH |
220 | else: |
221 | self.tmp |= (bit << (c - 45)) | |
222 | if c == 49: | |
223 | m = bcd2int(self.tmp) | |
224 | mn = calendar.month_name[m] # month_name[1] == January | |
99f5e74a UH |
225 | self.putx([14, ['Month: %d (%s)' % (m, mn), |
226 | 'Mon: %d (%s)' % (m, mn)]]) | |
2b0915c1 UH |
227 | elif c in range(50, 57 + 1): |
228 | # Year (0-99): DCF77 bits 50-57 (BCD format). | |
229 | if c == 50: | |
230 | self.tmp = bit | |
3e9b87f7 | 231 | self.ss_block = self.ss_bit |
2b0915c1 UH |
232 | else: |
233 | self.tmp |= (bit << (c - 50)) | |
234 | if c == 57: | |
3e9b87f7 | 235 | self.putb([15, ['Year: %d' % bcd2int(self.tmp)]]) |
2b0915c1 UH |
236 | elif c == 58: |
237 | # Even parity over date bits (36-58): DCF77 bit 58. | |
69ada057 | 238 | parity = self.datebits.count(1) |
2b0915c1 | 239 | s = 'OK' if ((parity % 2) == 0) else 'INVALID!' |
99f5e74a | 240 | self.putx([16, ['Date parity: %s' % s, 'DP: %s' %s]]) |
69ada057 | 241 | self.datebits = [] |
2b0915c1 UH |
242 | else: |
243 | raise Exception('Invalid DCF77 bit: %d' % c) | |
244 | ||
245 | def decode(self, ss, es, data): | |
f372d597 BV |
246 | if self.samplerate is None: |
247 | raise Exception("Cannot decode without samplerate.") | |
2fcd7c22 UH |
248 | for (self.samplenum, pins) in data: |
249 | ||
250 | # Ignore identical samples early on (for performance reasons). | |
251 | if self.oldpins == pins: | |
252 | continue | |
54f1b2b7 | 253 | self.oldpins, (val,) = pins, pins |
2b0915c1 | 254 | |
2b716038 | 255 | if self.state == 'WAIT FOR RISING EDGE': |
2b0915c1 UH |
256 | # Wait until the next rising edge occurs. |
257 | if not (self.oldval == 0 and val == 1): | |
258 | self.oldval = val | |
259 | continue | |
260 | ||
261 | # Save the sample number where the DCF77 bit begins. | |
aa4cf4c6 | 262 | self.ss_bit = self.samplenum |
2b0915c1 UH |
263 | |
264 | # Calculate the length (in ms) between two rising edges. | |
aa4cf4c6 | 265 | len_edges = self.ss_bit - self.ss_bit_old |
2b0915c1 UH |
266 | len_edges_ms = int((len_edges / self.samplerate) * 1000) |
267 | ||
268 | # The time between two rising edges is usually around 1000ms. | |
269 | # For DCF77 bit 59, there is no rising edge at all, i.e. the | |
270 | # time between DCF77 bit 59 and DCF77 bit 0 (of the next | |
271 | # minute) is around 2000ms. Thus, if we see an edge with a | |
272 | # 2000ms distance to the last one, this edge marks the | |
273 | # beginning of a new minute (and DCF77 bit 0 of that minute). | |
274 | if len_edges_ms in range(1600, 2400 + 1): | |
2b0915c1 | 275 | self.bitcount = 0 |
aa4cf4c6 | 276 | self.ss_bit_old = self.ss_bit |
2b0915c1 | 277 | self.dcf77_bitnumber_is_known = 1 |
2b0915c1 | 278 | |
aa4cf4c6 | 279 | self.ss_bit_old = self.ss_bit |
abbc1285 | 280 | self.state = 'GET BIT' |
2b0915c1 | 281 | |
abbc1285 | 282 | elif self.state == 'GET BIT': |
2b0915c1 UH |
283 | # Wait until the next falling edge occurs. |
284 | if not (self.oldval == 1 and val == 0): | |
285 | self.oldval = val | |
286 | continue | |
287 | ||
09bca511 | 288 | # Save the sample number where the DCF77 bit ends. |
aa4cf4c6 | 289 | self.es_bit = self.samplenum |
09bca511 | 290 | |
2b0915c1 | 291 | # Calculate the length (in ms) of the current high period. |
aa4cf4c6 | 292 | len_high = self.samplenum - self.ss_bit |
2b0915c1 UH |
293 | len_high_ms = int((len_high / self.samplerate) * 1000) |
294 | ||
295 | # If the high signal was 100ms long, that encodes a 0 bit. | |
296 | # If it was 200ms long, that encodes a 1 bit. | |
297 | if len_high_ms in range(40, 160 + 1): | |
298 | bit = 0 | |
299 | elif len_high_ms in range(161, 260 + 1): | |
300 | bit = 1 | |
301 | else: | |
302 | bit = -1 # TODO: Error? | |
303 | ||
abbc1285 | 304 | # There's no bit 59, make sure none is decoded. |
2b0915c1 UH |
305 | if bit in (0, 1) and self.bitcount in range(0, 58 + 1): |
306 | self.handle_dcf77_bit(bit) | |
307 | self.bitcount += 1 | |
308 | ||
2b716038 | 309 | self.state = 'WAIT FOR RISING EDGE' |
2b0915c1 UH |
310 | |
311 | else: | |
0eeeb544 | 312 | raise Exception('Invalid state: %s' % self.state) |
2b0915c1 UH |
313 | |
314 | self.oldval = val | |
315 |