]>
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): | |
28 | if t >= 1.0: | |
2cd9b197 | 29 | return '%.3f s (%.3f Hz)' % (t, (1/t)) |
92adde51 | 30 | elif 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) | |
92adde51 | 35 | elif 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) | |
92adde51 | 40 | elif 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 | ||
48 | class Decoder(srd.Decoder): | |
831e976a | 49 | api_version = 3 |
92adde51 BE |
50 | id = 'timing' |
51 | name = 'Timing' | |
2cd9b197 | 52 | longname = 'Timing calculation with frequency and averaging' |
92adde51 BE |
53 | desc = 'Calculate time between edges.' |
54 | license = 'gplv2+' | |
55 | inputs = ['logic'] | |
56 | outputs = ['timing'] | |
57 | channels = ( | |
58 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, | |
59 | ) | |
60 | annotations = ( | |
61 | ('time', 'Time'), | |
2cd9b197 | 62 | ('average', 'Average'), |
92adde51 BE |
63 | ) |
64 | annotation_rows = ( | |
65 | ('time', 'Time', (0,)), | |
2cd9b197 UH |
66 | ('average', 'Average', (1,)), |
67 | ) | |
68 | options = ( | |
69 | { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 }, | |
92adde51 BE |
70 | ) |
71 | ||
92b7b49f | 72 | def __init__(self): |
92adde51 BE |
73 | self.samplerate = None |
74 | self.oldpin = None | |
75 | self.last_samplenum = None | |
2cd9b197 UH |
76 | self.last_n = deque() |
77 | self.chunks = 0 | |
92adde51 BE |
78 | |
79 | def metadata(self, key, value): | |
80 | if key == srd.SRD_CONF_SAMPLERATE: | |
81 | self.samplerate = value | |
82 | ||
83 | def start(self): | |
84 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
831e976a | 85 | self.initial_pins = [0] |
92adde51 | 86 | |
831e976a | 87 | def decode(self): |
92adde51 BE |
88 | if not self.samplerate: |
89 | raise SamplerateError('Cannot decode without samplerate.') | |
831e976a UH |
90 | while True: |
91 | pin = self.wait({0: 'e'}) | |
92adde51 | 92 | |
92adde51 BE |
93 | if self.oldpin is None: |
94 | self.oldpin = pin | |
963d71d5 | 95 | self.last_samplenum = self.samplenum |
92adde51 BE |
96 | continue |
97 | ||
98 | if self.oldpin != pin: | |
963d71d5 | 99 | samples = self.samplenum - self.last_samplenum |
92adde51 | 100 | t = samples / self.samplerate |
2cd9b197 UH |
101 | self.chunks += 1 |
102 | ||
103 | # Don't insert the first chunk into the averaging as it is | |
104 | # not complete probably. | |
105 | if self.last_samplenum is None or self.chunks < 2: | |
106 | # Report the timing normalized. | |
107 | self.put(self.last_samplenum, self.samplenum, self.out_ann, | |
108 | [0, [normalize_time(t)]]) | |
109 | else: | |
110 | if t > 0: | |
111 | self.last_n.append(t) | |
112 | ||
113 | if len(self.last_n) > self.options['avg_period']: | |
114 | self.last_n.popleft() | |
92adde51 | 115 | |
2cd9b197 UH |
116 | # Report the timing normalized. |
117 | self.put(self.last_samplenum, self.samplenum, self.out_ann, | |
118 | [0, [normalize_time(t)]]) | |
119 | self.put(self.last_samplenum, self.samplenum, self.out_ann, | |
120 | [1, [normalize_time(sum(self.last_n) / len(self.last_n))]]) | |
92adde51 BE |
121 | |
122 | # Store data for next round. | |
963d71d5 | 123 | self.last_samplenum = self.samplenum |
92adde51 | 124 | self.oldpin = pin |