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