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