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