]>
Commit | Line | Data |
---|---|---|
92adde51 BE |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2014 Torsten Duwe <duwe@suse.de> | |
5 | ## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com> | |
6 | ## | |
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. | |
11 | ## | |
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. | |
16 | ## | |
17 | ## You should have received a copy of the GNU General Public License | |
4539e9ca | 18 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
92adde51 BE |
19 | ## |
20 | ||
21 | import sigrokdecode as srd | |
2cd9b197 | 22 | from collections import deque |
92adde51 BE |
23 | |
24 | class SamplerateError(Exception): | |
25 | pass | |
26 | ||
27 | def normalize_time(t): | |
a9360134 | 28 | if abs(t) >= 1.0: |
2cd9b197 | 29 | return '%.3f s (%.3f Hz)' % (t, (1/t)) |
a9360134 | 30 | elif abs(t) >= 0.001: |
2cd9b197 UH |
31 | if 1/t/1000 < 1: |
32 | return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t)) | |
33 | else: | |
34 | return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000) | |
a9360134 | 35 | elif abs(t) >= 0.000001: |
2cd9b197 UH |
36 | if 1/t/1000/1000 < 1: |
37 | return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000) | |
38 | else: | |
39 | return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000) | |
a9360134 | 40 | elif abs(t) >= 0.000000001: |
2cd9b197 UH |
41 | if 1/t/1000/1000/1000: |
42 | return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000) | |
43 | else: | |
44 | return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000) | |
92adde51 BE |
45 | else: |
46 | return '%f' % t | |
47 | ||
14c40481 GS |
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. | |
51 | if fmt == 'full': | |
52 | return [normalize_time(t)] | |
53 | # End of "forward compatibility". | |
54 | ||
55 | if fmt == 'samples': | |
56 | # See below. No unit text, on purpose. | |
57 | return ['{:d}'.format(t)] | |
58 | ||
59 | # Use caller specified scale, or automatically find one. | |
60 | scale, unit = None, None | |
61 | if fmt == 'terse-auto': | |
62 | if abs(t) >= 1e0: | |
63 | scale, unit = 1e0, 's' | |
64 | elif abs(t) >= 1e-3: | |
65 | scale, unit = 1e3, 'ms' | |
66 | elif abs(t) >= 1e-6: | |
67 | scale, unit = 1e6, 'us' | |
68 | elif abs(t) >= 1e-9: | |
69 | scale, unit = 1e9, 'ns' | |
70 | elif abs(t) >= 1e-12: | |
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': | |
76 | scale, unit = 1e0, '' | |
77 | elif fmt == 'terse-ms': | |
78 | scale, unit = 1e3, '' | |
79 | elif fmt == 'terse-us': | |
80 | scale, unit = 1e6, '' | |
81 | elif fmt == 'terse-ns': | |
82 | scale, unit = 1e9, '' | |
83 | elif fmt == 'terse-ps': | |
84 | scale, unit = 1e12, '' | |
85 | if scale: | |
86 | t *= scale | |
87 | return ['{:.0f}{}'.format(t, unit), '{:.0f}'.format(t)] | |
88 | ||
89 | # Unspecified format, and nothing auto-detected. | |
90 | return ['{:f}'.format(t)] | |
7e09e39c | 91 | |
6a1b97e6 GS |
92 | class Pin: |
93 | (DATA,) = range(1) | |
94 | ||
95 | class Ann: | |
7e09e39c | 96 | (TIME, TERSE, AVG, DELTA,) = range(4) |
6a1b97e6 | 97 | |
92adde51 | 98 | class Decoder(srd.Decoder): |
831e976a | 99 | api_version = 3 |
92adde51 BE |
100 | id = 'timing' |
101 | name = 'Timing' | |
2cd9b197 | 102 | longname = 'Timing calculation with frequency and averaging' |
92adde51 BE |
103 | desc = 'Calculate time between edges.' |
104 | license = 'gplv2+' | |
105 | inputs = ['logic'] | |
6cbba91f | 106 | outputs = [] |
d6d8a8a4 | 107 | tags = ['Clock/timing', 'Util'] |
92adde51 BE |
108 | channels = ( |
109 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, | |
110 | ) | |
111 | annotations = ( | |
112 | ('time', 'Time'), | |
7e09e39c | 113 | ('terse', 'Terse'), |
2cd9b197 | 114 | ('average', 'Average'), |
b8c44f69 | 115 | ('delta', 'Delta'), |
92adde51 BE |
116 | ) |
117 | annotation_rows = ( | |
7e09e39c | 118 | ('times', 'Times', (Ann.TIME, Ann.TERSE,)), |
6a1b97e6 GS |
119 | ('averages', 'Averages', (Ann.AVG,)), |
120 | ('deltas', 'Deltas', (Ann.DELTA,)), | |
2cd9b197 UH |
121 | ) |
122 | options = ( | |
123 | { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 }, | |
6298af07 GS |
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') }, | |
14c40481 GS |
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', | |
131 | 'samples') }, | |
92adde51 BE |
132 | ) |
133 | ||
92b7b49f | 134 | def __init__(self): |
10aeb8ea GS |
135 | self.reset() |
136 | ||
137 | def reset(self): | |
92adde51 | 138 | self.samplerate = None |
92adde51 BE |
139 | |
140 | def metadata(self, key, value): | |
141 | if key == srd.SRD_CONF_SAMPLERATE: | |
142 | self.samplerate = value | |
143 | ||
144 | def start(self): | |
145 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
146 | ||
831e976a | 147 | def decode(self): |
92adde51 BE |
148 | if not self.samplerate: |
149 | raise SamplerateError('Cannot decode without samplerate.') | |
3e962c2f GS |
150 | edge = self.options['edge'] |
151 | avg_period = self.options['avg_period'] | |
74c9c926 | 152 | delta = self.options['delta'] == 'yes' |
14c40481 | 153 | fmt = self.options['format'] |
c945a82d | 154 | ss = None |
3e962c2f GS |
155 | last_n = deque() |
156 | last_t = None | |
831e976a | 157 | while True: |
3e962c2f | 158 | if edge == 'rising': |
6a1b97e6 | 159 | pin = self.wait({Pin.DATA: 'r'}) |
3e962c2f | 160 | elif edge == 'falling': |
6a1b97e6 | 161 | pin = self.wait({Pin.DATA: 'f'}) |
b8c44f69 | 162 | else: |
6a1b97e6 | 163 | pin = self.wait({Pin.DATA: 'e'}) |
92adde51 | 164 | |
c945a82d GS |
165 | if not ss: |
166 | ss = self.samplenum | |
92adde51 | 167 | continue |
c945a82d | 168 | es = self.samplenum |
6298af07 GS |
169 | sa = es - ss |
170 | t = sa / self.samplerate | |
92adde51 | 171 | |
14c40481 | 172 | if fmt == 'full': |
74c9c926 | 173 | cls, txt = Ann.TIME, [normalize_time(t)] |
14c40481 GS |
174 | elif fmt == 'samples': |
175 | cls, txt = Ann.TERSE, terse_times(sa, fmt) | |
176 | else: | |
177 | cls, txt = Ann.TERSE, terse_times(t, fmt) | |
178 | if txt: | |
74c9c926 | 179 | self.put(ss, es, self.out_ann, [cls, txt]) |
6298af07 | 180 | |
3e962c2f | 181 | if avg_period > 0: |
74c9c926 GS |
182 | if t > 0: |
183 | last_n.append(t) | |
184 | if len(last_n) > avg_period: | |
185 | last_n.popleft() | |
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]]) | |
189 | if last_t and delta: | |
190 | cls, txt = Ann.DELTA, normalize_time(t - last_t) | |
191 | self.put(ss, es, self.out_ann, [cls, [txt]]) | |
2cd9b197 | 192 | |
3e962c2f | 193 | last_t = t |
c945a82d | 194 | ss = es |