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 class Decoder(srd.Decoder):
52 longname = 'Timing calculation with frequency and averaging'
53 desc = 'Calculate time between edges.'
57 tags = ['Clock/timing', 'Util']
59 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
63 ('average', 'Average'),
67 ('times', 'Times', (0,)),
68 ('averages', 'Averages', (1,)),
69 ('deltas', 'Deltas', (2,)),
72 { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
73 { 'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling') },
74 { 'id': 'delta', 'desc': 'Show delta from last', 'default': 'no', 'values': ('yes', 'no') },
81 self.samplerate = None
82 self.last_samplenum = None
85 self.level_changed = False
88 def metadata(self, key, value):
89 if key == srd.SRD_CONF_SAMPLERATE:
90 self.samplerate = value
93 self.out_ann = self.register(srd.OUTPUT_ANN)
94 self.edge = self.options['edge']
97 if not self.samplerate:
98 raise SamplerateError('Cannot decode without samplerate.')
100 if self.edge == 'rising':
101 pin = self.wait({0: 'r'})
102 elif self.edge == 'falling':
103 pin = self.wait({0: 'f'})
105 pin = self.wait({0: 'e'})
107 if not self.last_samplenum:
108 self.last_samplenum = self.samplenum
110 samples = self.samplenum - self.last_samplenum
111 t = samples / self.samplerate
114 self.last_n.append(t)
115 if len(self.last_n) > self.options['avg_period']:
116 self.last_n.popleft()
118 self.put(self.last_samplenum, self.samplenum, self.out_ann,
119 [0, [normalize_time(t)]])
120 if self.options['avg_period'] > 0:
121 self.put(self.last_samplenum, self.samplenum, self.out_ann,
122 [1, [normalize_time(sum(self.last_n) / len(self.last_n))]])
123 if self.last_t and self.options['delta'] == 'yes':
124 self.put(self.last_samplenum, self.samplenum, self.out_ann,
125 [2, [normalize_time(t - self.last_t)]])
128 self.last_samplenum = self.samplenum