2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 # DCF77 protocol decoder
23 import sigrokdecode as srd
26 # Return the specified BCD number (max. 8 bits) as integer.
28 return (b & 0x0f) + ((b >> 4) * 10)
30 class Decoder(srd.Decoder):
34 longname = 'DCF77 time protocol'
35 desc = 'European longwave time signal (77.5kHz carrier signal).'
40 {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'},
43 {'id': 'pon', 'name': 'PON', 'desc': 'TODO'},
47 ['Text', 'Human-readable text'],
50 def __init__(self, **kwargs):
51 self.state = 'WAIT FOR RISING EDGE'
55 self.bit_start_old = 0
56 self.bitcount = 0 # Counter for the DCF77 bits (0..58)
57 self.dcf77_bitnumber_is_known = 0
59 def start(self, metadata):
60 self.samplerate = metadata['samplerate']
61 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'dcf77')
62 self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77')
67 # TODO: Which range to use? Only the 100ms/200ms or full second?
68 def handle_dcf77_bit(self, bit):
73 # Create one annotation for each DCF77 bit (containing the 0/1 value).
74 # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the
75 # 0..58 bits it is (because we haven't seen a 'new minute' marker yet).
76 # Otherwise, use 'DCF77 bit x: val'.
77 s = '' if self.dcf77_bitnumber_is_known else 'Unknown '
78 self.put(ss, es, a, [0, ['%sDCF77 bit %d: %d' % (s, c, bit)]])
80 # If we're not sure yet which of the 0..58 DCF77 bits we have, return.
81 # We don't want to decode bogus data.
82 if not self.dcf77_bitnumber_is_known:
85 # Output specific "decoded" annotations for the respective DCF77 bits.
87 # Start of minute: DCF bit 0.
89 self.put(ss, es, a, [0, ['Start of minute (always 0)']])
91 self.put(ss, es, a, [0, ['ERROR: Start of minute != 0']])
92 elif c in range(1, 14 + 1):
93 # Special bits (civil warnings, weather forecast): DCF77 bits 1-14.
97 self.tmp |= (bit << (c - 1))
99 self.put(ss, es, a, [0, ['Special bits: %s' % bin(self.tmp)]])
101 s = '' if (bit == 1) else 'not '
102 self.put(ss, es, a, [0, ['Call bit is %sset' % s]])
103 # TODO: Previously this bit indicated use of the backup antenna.
105 s = '' if (bit == 1) else 'not '
106 self.put(ss, es, a, [0, ['Summer time announcement %sactive' % s]])
108 s = '' if (bit == 1) else 'not '
109 self.put(ss, es, a, [0, ['CEST is %sin effect' % s]])
111 s = '' if (bit == 1) else 'not '
112 self.put(ss, es, a, [0, ['CET is %sin effect' % s]])
114 s = '' if (bit == 1) else 'not '
115 self.put(ss, es, a, [0, ['Leap second announcement %sactive' % s]])
117 # Start of encoded time: DCF bit 20.
119 self.put(ss, es, a, [0, ['Start of encoded time (always 1)']])
122 [0, ['ERROR: Start of encoded time != 1']])
123 elif c in range(21, 27 + 1):
124 # Minutes (0-59): DCF77 bits 21-27 (BCD format).
128 self.tmp |= (bit << (c - 21))
130 self.put(ss, es, a, [0, ['Minutes: %d' % bcd2int(self.tmp)]])
132 # Even parity over minute bits (21-28): DCF77 bit 28.
133 self.tmp |= (bit << (c - 21))
134 parity = bin(self.tmp).count('1')
135 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
136 self.put(ss, es, a, [0, ['Minute parity: %s' % s]])
137 elif c in range(29, 34 + 1):
138 # Hours (0-23): DCF77 bits 29-34 (BCD format).
142 self.tmp |= (bit << (c - 29))
144 self.put(ss, es, a, [0, ['Hours: %d' % bcd2int(self.tmp)]])
146 # Even parity over hour bits (29-35): DCF77 bit 35.
147 self.tmp |= (bit << (c - 29))
148 parity = bin(self.tmp).count('1')
149 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
150 self.put(ss, es, a, [0, ['Hour parity: %s' % s]])
151 elif c in range(36, 41 + 1):
152 # Day of month (1-31): DCF77 bits 36-41 (BCD format).
156 self.tmp |= (bit << (c - 36))
158 self.put(ss, es, a, [0, ['Day: %d' % bcd2int(self.tmp)]])
159 elif c in range(42, 44 + 1):
160 # Day of week (1-7): DCF77 bits 42-44 (BCD format).
161 # A value of 1 means Monday, 7 means Sunday.
165 self.tmp |= (bit << (c - 42))
167 d = bcd2int(self.tmp)
168 dn = calendar.day_name[d - 1] # day_name[0] == Monday
169 self.put(ss, es, a, [0, ['Day of week: %d (%s)' % (d, dn)]])
170 elif c in range(45, 49 + 1):
171 # Month (1-12): DCF77 bits 45-49 (BCD format).
175 self.tmp |= (bit << (c - 45))
177 m = bcd2int(self.tmp)
178 mn = calendar.month_name[m] # month_name[1] == January
179 self.put(ss, es, a, [0, ['Month: %d (%s)' % (m, mn)]])
180 elif c in range(50, 57 + 1):
181 # Year (0-99): DCF77 bits 50-57 (BCD format).
185 self.tmp |= (bit << (c - 50))
187 self.put(ss, es, a, [0, ['Year: %d' % bcd2int(self.tmp)]])
189 # Even parity over date bits (36-58): DCF77 bit 58.
190 self.tmp |= (bit << (c - 50))
191 parity = bin(self.tmp).count('1')
192 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
193 self.put(ss, es, a, [0, ['Date parity: %s' % s]])
195 raise Exception('Invalid DCF77 bit: %d' % c)
197 def decode(self, ss, es, data):
198 for (self.samplenum, (val)) in data: # TODO: Handle optional PON.
200 if self.state == 'WAIT FOR RISING EDGE':
201 # Wait until the next rising edge occurs.
202 if not (self.oldval == 0 and val == 1):
206 # Save the sample number where the DCF77 bit begins.
207 self.bit_start = self.samplenum
209 # Calculate the length (in ms) between two rising edges.
210 len_edges = self.bit_start - self.bit_start_old
211 len_edges_ms = int((len_edges / self.samplerate) * 1000)
213 # The time between two rising edges is usually around 1000ms.
214 # For DCF77 bit 59, there is no rising edge at all, i.e. the
215 # time between DCF77 bit 59 and DCF77 bit 0 (of the next
216 # minute) is around 2000ms. Thus, if we see an edge with a
217 # 2000ms distance to the last one, this edge marks the
218 # beginning of a new minute (and DCF77 bit 0 of that minute).
219 if len_edges_ms in range(1600, 2400 + 1):
220 self.put(ss, es, self.out_ann, [0, ['New minute starts']])
222 self.bit_start_old = self.bit_start
223 self.dcf77_bitnumber_is_known = 1
224 # Don't switch to 'GET BIT' state this time.
227 self.bit_start_old = self.bit_start
228 self.state = 'GET BIT'
230 elif self.state == 'GET BIT':
231 # Wait until the next falling edge occurs.
232 if not (self.oldval == 1 and val == 0):
236 # Calculate the length (in ms) of the current high period.
237 len_high = self.samplenum - self.bit_start
238 len_high_ms = int((len_high / self.samplerate) * 1000)
240 # If the high signal was 100ms long, that encodes a 0 bit.
241 # If it was 200ms long, that encodes a 1 bit.
242 if len_high_ms in range(40, 160 + 1):
244 elif len_high_ms in range(161, 260 + 1):
247 bit = -1 # TODO: Error?
249 # There's no bit 59, make sure none is decoded.
250 if bit in (0, 1) and self.bitcount in range(0, 58 + 1):
251 self.handle_dcf77_bit(bit)
254 self.state = 'WAIT FOR RISING EDGE'
257 raise Exception('Invalid state: %d' % self.state)