2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012-2016 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, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
22 from common.srdhelper import bcd2int
24 class SamplerateError(Exception):
27 class Decoder(srd.Decoder):
31 longname = 'DCF77 time protocol'
32 desc = 'European longwave time signal (77.5kHz carrier signal).'
36 tags = ['Clock/timing']
38 {'id': 'data', 'name': 'DATA', 'desc': 'DATA line'},
41 ('start-of-minute', 'Start of minute'),
42 ('special-bit', 'Special bit (civil warnings, weather forecast)'),
43 ('call-bit', 'Call bit'),
44 ('summer-time', 'Summer time announcement'),
47 ('leap-second', 'Leap second bit'),
48 ('start-of-time', 'Start of encoded time'),
50 ('minute-parity', 'Minute parity bit'),
52 ('hour-parity', 'Hour parity bit'),
53 ('day', 'Day of month'),
54 ('day-of-week', 'Day of week'),
57 ('date-parity', 'Date parity bit'),
58 ('raw-bit', 'Raw bit'),
59 ('unknown-bit', 'Unknown bit'),
60 ('warning', 'Warning'),
63 ('bits', 'Bits', (17, 18)),
64 ('fields', 'Fields', tuple(range(0, 16 + 1))),
65 ('warnings', 'Warnings', (19,)),
72 self.samplerate = None
73 self.state = 'WAIT FOR RISING EDGE'
74 self.ss_bit = self.ss_bit_old = self.es_bit = self.ss_block = 0
76 self.bitcount = 0 # Counter for the DCF77 bits (0..58)
77 self.dcf77_bitnumber_is_known = 0
80 self.out_ann = self.register(srd.OUTPUT_ANN)
82 def metadata(self, key, value):
83 if key == srd.SRD_CONF_SAMPLERATE:
84 self.samplerate = value
87 # Annotation for a single DCF77 bit.
88 self.put(self.ss_bit, self.es_bit, self.out_ann, data)
91 # Annotation for a multi-bit DCF77 field.
92 self.put(self.ss_block, self.samplenum, self.out_ann, data)
94 # TODO: Which range to use? Only the 100ms/200ms or full second?
95 def handle_dcf77_bit(self, bit):
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'.
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]])
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:
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)
115 # Output specific "decoded" annotations for the respective DCF77 bits.
117 # Start of minute: DCF bit 0.
119 self.putx([0, ['Start of minute (always 0)',
120 'Start of minute', 'SoM']])
122 self.putx([19, ['Start of minute != 0', 'SoM != 0']])
123 elif c in range(1, 14 + 1):
124 # Special bits (civil warnings, weather forecast): DCF77 bits 1-14.
127 self.ss_block = self.ss_bit
129 self.tmp |= (bit << (c - 1))
131 s = '{:014b}'.format(self.tmp)
132 self.putb([1, ['Special bits: %s' % s, 'SB: %s' % s]])
134 s = '' if (bit == 1) else 'not '
135 self.putx([2, ['Call bit: %sset' % s, 'CB: %sset' % s]])
136 # TODO: Previously this bit indicated use of the backup antenna.
138 s = '' if (bit == 1) else 'not '
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]])
144 s = '' if (bit == 1) else 'not '
145 x = 'yes' if (bit == 1) else 'no'
146 self.putx([4, ['CEST: %sin effect' % s, 'CEST: %s' % x]])
148 s = '' if (bit == 1) else 'not '
149 x = 'yes' if (bit == 1) else 'no'
150 self.putx([5, ['CET: %sin effect' % s, 'CET: %s' % x]])
152 s = '' if (bit == 1) else 'not '
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]])
158 # Start of encoded time: DCF bit 20.
160 self.putx([7, ['Start of encoded time (always 1)',
161 'Start of encoded time', 'SoeT']])
163 self.putx([19, ['Start of encoded time != 1', 'SoeT != 1']])
164 elif c in range(21, 27 + 1):
165 # Minutes (0-59): DCF77 bits 21-27 (BCD format).
168 self.ss_block = self.ss_bit
170 self.tmp |= (bit << (c - 21))
172 m = bcd2int(self.tmp)
173 self.putb([8, ['Minutes: %d' % m, 'Min: %d' % m]])
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!'
179 self.putx([9, ['Minute parity: %s' % s, 'Min parity: %s' % s]])
180 elif c in range(29, 34 + 1):
181 # Hours (0-23): DCF77 bits 29-34 (BCD format).
184 self.ss_block = self.ss_bit
186 self.tmp |= (bit << (c - 29))
188 self.putb([10, ['Hours: %d' % bcd2int(self.tmp)]])
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!'
194 self.putx([11, ['Hour parity: %s' % s]])
195 elif c in range(36, 41 + 1):
196 # Day of month (1-31): DCF77 bits 36-41 (BCD format).
199 self.ss_block = self.ss_bit
201 self.tmp |= (bit << (c - 36))
203 self.putb([12, ['Day: %d' % bcd2int(self.tmp)]])
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.
209 self.ss_block = self.ss_bit
211 self.tmp |= (bit << (c - 42))
213 d = bcd2int(self.tmp)
215 dn = calendar.day_name[d - 1] # day_name[0] == Monday
216 self.putb([13, ['Day of week: %d (%s)' % (d, dn),
217 'DoW: %d (%s)' % (d, dn)]])
219 self.putb([19, ['Day of week: %d (%s)' % (d, 'invalid'),
220 'DoW: %d (%s)' % (d, 'inv')]])
221 elif c in range(45, 49 + 1):
222 # Month (1-12): DCF77 bits 45-49 (BCD format).
225 self.ss_block = self.ss_bit
227 self.tmp |= (bit << (c - 45))
229 m = bcd2int(self.tmp)
231 mn = calendar.month_name[m] # month_name[1] == January
232 self.putb([14, ['Month: %d (%s)' % (m, mn),
233 'Mon: %d (%s)' % (m, mn)]])
235 self.putb([19, ['Month: %d (%s)' % (m, 'invalid'),
236 'Mon: %d (%s)' % (m, 'inv')]])
237 elif c in range(50, 57 + 1):
238 # Year (0-99): DCF77 bits 50-57 (BCD format).
241 self.ss_block = self.ss_bit
243 self.tmp |= (bit << (c - 50))
245 self.putb([15, ['Year: %d' % bcd2int(self.tmp)]])
247 # Even parity over date bits (36-58): DCF77 bit 58.
248 parity = self.datebits.count(1)
249 s = 'OK' if ((parity % 2) == 0) else 'INVALID!'
250 self.putx([16, ['Date parity: %s' % s, 'DP: %s' % s]])
253 self.putx([19, ['Invalid DCF77 bit: %d' % c,
254 'Invalid bit: %d' % c, 'Inv: %d' % c]])
257 if not self.samplerate:
258 raise SamplerateError('Cannot decode without samplerate.')
260 if self.state == 'WAIT FOR RISING EDGE':
261 # Wait until the next rising edge occurs.
264 # Save the sample number where the DCF77 bit begins.
265 self.ss_bit = self.samplenum
267 # Calculate the length (in ms) between two rising edges.
268 len_edges = self.ss_bit - self.ss_bit_old
269 len_edges_ms = int((len_edges / self.samplerate) * 1000)
271 # The time between two rising edges is usually around 1000ms.
272 # For DCF77 bit 59, there is no rising edge at all, i.e. the
273 # time between DCF77 bit 59 and DCF77 bit 0 (of the next
274 # minute) is around 2000ms. Thus, if we see an edge with a
275 # 2000ms distance to the last one, this edge marks the
276 # beginning of a new minute (and DCF77 bit 0 of that minute).
277 if len_edges_ms in range(1600, 2400 + 1):
279 self.ss_bit_old = self.ss_bit
280 self.dcf77_bitnumber_is_known = 1
282 self.ss_bit_old = self.ss_bit
283 self.state = 'GET BIT'
285 elif self.state == 'GET BIT':
286 # Wait until the next falling edge occurs.
289 # Save the sample number where the DCF77 bit ends.
290 self.es_bit = self.samplenum
292 # Calculate the length (in ms) of the current high period.
293 len_high = self.samplenum - self.ss_bit
294 len_high_ms = int((len_high / self.samplerate) * 1000)
296 # If the high signal was 100ms long, that encodes a 0 bit.
297 # If it was 200ms long, that encodes a 1 bit.
298 if len_high_ms in range(40, 160 + 1):
300 elif len_high_ms in range(161, 260 + 1):
306 self.handle_dcf77_bit(bit)
309 self.putx([19, ['Invalid bit timing', 'Inv timing', 'Inv']])
311 self.state = 'WAIT FOR RISING EDGE'