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