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