2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012-2013 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'},
45 ['start_of_minute', 'Start of minute'],
46 ['special_bits', 'Special bits (civil warnings, weather forecast)'],
47 ['call_bit', 'Call bit'],
48 ['summer_time', 'Summer time announcement'],
51 ['leap_second', 'Leap second bit'],
52 ['start_of_time', 'Start of encoded time'],
54 ['minute_parity', 'Minute parity bit'],
56 ['hour_parity', 'Hour parity bit'],
57 ['day', 'Day of month'],
58 ['day_of_week', 'Day of week'],
61 ['date_parity', 'Date parity bit'],
62 ['raw_bits', 'Raw bits'],
63 ['unknown_bits', 'Unknown bits'],
64 ['warnings', 'Human-readable warnings'],
67 def __init__(self, **kwargs):
68 self.state = 'WAIT FOR RISING EDGE'
72 self.ss_bit = self.ss_bit_old = self.es_bit = 0
73 self.bitcount = 0 # Counter for the DCF77 bits (0..58)
74 self.dcf77_bitnumber_is_known = 0
76 def start(self, metadata):
77 self.samplerate = metadata['samplerate']
78 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'dcf77')
79 self.out_ann = self.add(srd.OUTPUT_ANN, 'dcf77')
85 self.put(self.ss_bit, self.es_bit, self.out_ann, data)
87 # TODO: Which range to use? Only the 100ms/200ms or full second?
88 def handle_dcf77_bit(self, bit):
91 # Create one annotation for each DCF77 bit (containing the 0/1 value).
92 # Use 'Unknown DCF77 bit x: val' if we're not sure yet which of the
93 # 0..58 bits it is (because we haven't seen a 'new minute' marker yet).
94 # Otherwise, use 'DCF77 bit x: val'.
95 s = '' if self.dcf77_bitnumber_is_known else 'Unknown '
96 self.putx([18, ['%sDCF77 bit %d: %d' % (s, c, bit)]])
98 # If we're not sure yet which of the 0..58 DCF77 bits we have, return.
99 # We don't want to decode bogus data.
100 if not self.dcf77_bitnumber_is_known:
103 # Output specific "decoded" annotations for the respective DCF77 bits.
105 # Start of minute: DCF bit 0.
107 self.putx([0, ['Start of minute (always 0)']])
109 self.putx([19, ['Start of minute != 0']])
110 elif c in range(1, 14 + 1):
111 # Special bits (civil warnings, weather forecast): DCF77 bits 1-14.
115 self.tmp |= (bit << (c - 1))
117 self.putx([1, ['Special bits: %s' % bin(self.tmp)]])
119 s = '' if (bit == 1) else 'not '
120 self.putx([2, ['Call bit is %sset' % s]])
121 # TODO: Previously this bit indicated use of the backup antenna.
123 s = '' if (bit == 1) else 'not '
124 self.putx([3, ['Summer time announcement %sactive' % s]])
126 s = '' if (bit == 1) else 'not '
127 self.putx([4, ['CEST is %sin effect' % s]])
129 s = '' if (bit == 1) else 'not '
130 self.putx([5, ['CET is %sin effect' % s]])
132 s = '' if (bit == 1) else 'not '
133 self.putx([6, ['Leap second announcement %sactive' % s]])
135 # Start of encoded time: DCF bit 20.
137 self.putx([7, ['Start of encoded time (always 1)']])
139 self.putx([19, ['ERROR: Start of encoded time != 1']])
140 elif c in range(21, 27 + 1):
141 # Minutes (0-59): DCF77 bits 21-27 (BCD format).
145 self.tmp |= (bit << (c - 21))
147 self.putx([8, ['Minutes: %d' % bcd2int(self.tmp)]])
149 # Even parity over minute bits (21-28): DCF77 bit 28.
150 self.tmp |= (bit << (c - 21))
151 parity = bin(self.tmp).count('1')
152 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
153 self.putx([9, ['Minute parity: %s' % s]])
154 elif c in range(29, 34 + 1):
155 # Hours (0-23): DCF77 bits 29-34 (BCD format).
159 self.tmp |= (bit << (c - 29))
161 self.putx([10, ['Hours: %d' % bcd2int(self.tmp)]])
163 # Even parity over hour bits (29-35): DCF77 bit 35.
164 self.tmp |= (bit << (c - 29))
165 parity = bin(self.tmp).count('1')
166 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
167 self.putx([11, ['Hour parity: %s' % s]])
168 elif c in range(36, 41 + 1):
169 # Day of month (1-31): DCF77 bits 36-41 (BCD format).
173 self.tmp |= (bit << (c - 36))
175 self.putx([12, ['Day: %d' % bcd2int(self.tmp)]])
176 elif c in range(42, 44 + 1):
177 # Day of week (1-7): DCF77 bits 42-44 (BCD format).
178 # A value of 1 means Monday, 7 means Sunday.
182 self.tmp |= (bit << (c - 42))
184 d = bcd2int(self.tmp)
185 dn = calendar.day_name[d - 1] # day_name[0] == Monday
186 self.putx([13, ['Day of week: %d (%s)' % (d, dn)]])
187 elif c in range(45, 49 + 1):
188 # Month (1-12): DCF77 bits 45-49 (BCD format).
192 self.tmp |= (bit << (c - 45))
194 m = bcd2int(self.tmp)
195 mn = calendar.month_name[m] # month_name[1] == January
196 self.putx([14, ['Month: %d (%s)' % (m, mn)]])
197 elif c in range(50, 57 + 1):
198 # Year (0-99): DCF77 bits 50-57 (BCD format).
202 self.tmp |= (bit << (c - 50))
204 self.putx([15, ['Year: %d' % bcd2int(self.tmp)]])
206 # Even parity over date bits (36-58): DCF77 bit 58.
207 self.tmp |= (bit << (c - 50))
208 parity = bin(self.tmp).count('1')
209 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
210 self.putx([16, ['Date parity: %s' % s]])
212 raise Exception('Invalid DCF77 bit: %d' % c)
214 def decode(self, ss, es, data):
215 for (self.samplenum, pins) in data:
217 # Ignore identical samples early on (for performance reasons).
218 if self.oldpins == pins:
220 self.oldpins, (val,) = pins, pins
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):
228 # Save the sample number where the DCF77 bit begins.
229 self.ss_bit = self.samplenum
231 # Calculate the length (in ms) between two rising edges.
232 len_edges = self.ss_bit - self.ss_bit_old
233 len_edges_ms = int((len_edges / self.samplerate) * 1000)
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):
243 self.ss_bit_old = self.ss_bit
244 self.dcf77_bitnumber_is_known = 1
246 self.ss_bit_old = self.ss_bit
247 self.state = 'GET BIT'
249 elif self.state == 'GET BIT':
250 # Wait until the next falling edge occurs.
251 if not (self.oldval == 1 and val == 0):
255 # Save the sample number where the DCF77 bit ends.
256 self.es_bit = self.samplenum
258 # Calculate the length (in ms) of the current high period.
259 len_high = self.samplenum - self.ss_bit
260 len_high_ms = int((len_high / self.samplerate) * 1000)
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):
266 elif len_high_ms in range(161, 260 + 1):
269 bit = -1 # TODO: Error?
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)
276 self.state = 'WAIT FOR RISING EDGE'
279 raise Exception('Invalid state: %s' % self.state)