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, see <http://www.gnu.org/licenses/>.
21 import sigrokdecode as srd
22 from collections import deque
24 class SamplerateError(Exception):
27 def normalize_time(t):
29 return '%.3f s (%.3f Hz)' % (t, (1/t))
32 return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t))
34 return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000)
35 elif abs(t) >= 0.000001:
37 return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000)
39 return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000)
40 elif abs(t) >= 0.000000001:
41 if 1/t/1000/1000/1000:
42 return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000)
44 return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000)
48 def terse_times(t, fmt):
49 # Strictly speaking these variants are not used in the current
50 # implementation, but can reduce diffs during future maintenance.
52 return [normalize_time(t)]
53 # End of "forward compatibility".
56 # See below. No unit text, on purpose.
57 return ['{:d}'.format(t)]
59 # Use caller specified scale, or automatically find one.
60 scale, unit = None, None
61 if fmt == 'terse-auto':
63 scale, unit = 1e0, 's'
65 scale, unit = 1e3, 'ms'
67 scale, unit = 1e6, 'us'
69 scale, unit = 1e9, 'ns'
71 scale, unit = 1e12, 'ps'
72 # Beware! Uses unit-less text when the user picked the scale. For
73 # more consistent output with less clutter, thus faster navigation
74 # by humans. Can also un-hide text at higher distance zoom levels.
75 elif fmt == 'terse-s':
77 elif fmt == 'terse-ms':
79 elif fmt == 'terse-us':
81 elif fmt == 'terse-ns':
83 elif fmt == 'terse-ps':
84 scale, unit = 1e12, ''
87 return ['{:.0f}{}'.format(t, unit), '{:.0f}'.format(t)]
89 # Unspecified format, and nothing auto-detected.
90 return ['{:f}'.format(t)]
96 (TIME, TERSE, AVG, DELTA,) = range(4)
98 class Decoder(srd.Decoder):
102 longname = 'Timing calculation with frequency and averaging'
103 desc = 'Calculate time between edges.'
107 tags = ['Clock/timing', 'Util']
109 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
114 ('average', 'Average'),
118 ('times', 'Times', (Ann.TIME, Ann.TERSE,)),
119 ('averages', 'Averages', (Ann.AVG,)),
120 ('deltas', 'Deltas', (Ann.DELTA,)),
123 { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
124 { 'id': 'edge', 'desc': 'Edges to check',
125 'default': 'any', 'values': ('any', 'rising', 'falling') },
126 { 'id': 'delta', 'desc': 'Show delta from last',
127 'default': 'no', 'values': ('yes', 'no') },
128 { 'id': 'format', 'desc': 'Format of \'time\' annotation',
129 'default': 'full', 'values': ('full', 'terse-auto',
130 'terse-s', 'terse-ms', 'terse-us', 'terse-ns', 'terse-ps',
138 self.samplerate = None
140 def metadata(self, key, value):
141 if key == srd.SRD_CONF_SAMPLERATE:
142 self.samplerate = value
145 self.out_ann = self.register(srd.OUTPUT_ANN)
148 if not self.samplerate:
149 raise SamplerateError('Cannot decode without samplerate.')
150 edge = self.options['edge']
151 avg_period = self.options['avg_period']
152 delta = self.options['delta'] == 'yes'
153 fmt = self.options['format']
159 pin = self.wait({Pin.DATA: 'r'})
160 elif edge == 'falling':
161 pin = self.wait({Pin.DATA: 'f'})
163 pin = self.wait({Pin.DATA: 'e'})
170 t = sa / self.samplerate
173 cls, txt = Ann.TIME, [normalize_time(t)]
174 elif fmt == 'samples':
175 cls, txt = Ann.TERSE, terse_times(sa, fmt)
177 cls, txt = Ann.TERSE, terse_times(t, fmt)
179 self.put(ss, es, self.out_ann, [cls, txt])
184 if len(last_n) > avg_period:
186 average = sum(last_n) / len(last_n)
187 cls, txt = Ann.AVG, normalize_time(average)
188 self.put(ss, es, self.out_ann, [cls, [txt]])
190 cls, txt = Ann.DELTA, normalize_time(t - last_t)
191 self.put(ss, es, self.out_ann, [cls, [txt]])