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