2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
5 ## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 import sigrokdecode as srd
23 from collections import deque
25 class SamplerateError(Exception):
28 def normalize_time(t):
30 return '%.3f s (%.3f Hz)' % (t, (1/t))
33 return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t))
35 return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000)
38 return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000)
40 return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000)
41 elif t >= 0.000000001:
42 if 1/t/1000/1000/1000:
43 return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000)
45 return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000)
49 class Decoder(srd.Decoder):
53 longname = 'Timing calculation with frequency and averaging'
54 desc = 'Calculate time between edges.'
59 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
63 ('average', 'Average'),
66 ('time', 'Time', (0,)),
67 ('average', 'Average', (1,)),
70 { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
74 self.samplerate = None
76 self.last_samplenum = None
80 def metadata(self, key, value):
81 if key == srd.SRD_CONF_SAMPLERATE:
82 self.samplerate = value
85 self.out_ann = self.register(srd.OUTPUT_ANN)
86 self.initial_pins = [0]
89 if not self.samplerate:
90 raise SamplerateError('Cannot decode without samplerate.')
92 pin = self.wait({0: 'e'})
94 if self.oldpin is None:
96 self.last_samplenum = self.samplenum
99 if self.oldpin != pin:
100 samples = self.samplenum - self.last_samplenum
101 t = samples / self.samplerate
104 # Don't insert the first chunk into the averaging as it is
105 # not complete probably.
106 if self.last_samplenum is None or self.chunks < 2:
107 # Report the timing normalized.
108 self.put(self.last_samplenum, self.samplenum, self.out_ann,
109 [0, [normalize_time(t)]])
112 self.last_n.append(t)
114 if len(self.last_n) > self.options['avg_period']:
115 self.last_n.popleft()
117 # Report the timing normalized.
118 self.put(self.last_samplenum, self.samplenum, self.out_ann,
119 [0, [normalize_time(t)]])
120 self.put(self.last_samplenum, self.samplenum, self.out_ann,
121 [1, [normalize_time(sum(self.last_n) / len(self.last_n))]])
123 # Store data for next round.
124 self.last_samplenum = self.samplenum