]> sigrok.org Git - libsigrokdecode.git/blame - decoders/timing/pd.py
decoders: Add/update tags for each PD.
[libsigrokdecode.git] / decoders / timing / pd.py
CommitLineData
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
21import sigrokdecode as srd
2cd9b197 22from collections import deque
92adde51
BE
23
24class SamplerateError(Exception):
25 pass
26
27def 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
48class 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']
d6d8a8a4 57 tags = ['Clock/timing', 'Util']
92adde51
BE
58 channels = (
59 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
60 )
61 annotations = (
62 ('time', 'Time'),
2cd9b197 63 ('average', 'Average'),
b8c44f69 64 ('delta', 'Delta'),
92adde51
BE
65 )
66 annotation_rows = (
67 ('time', 'Time', (0,)),
2cd9b197 68 ('average', 'Average', (1,)),
b8c44f69 69 ('delta', 'Delta', (2,)),
2cd9b197
UH
70 )
71 options = (
72 { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
b8c44f69
KP
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') },
92adde51
BE
75 )
76
92b7b49f 77 def __init__(self):
10aeb8ea
GS
78 self.reset()
79
80 def reset(self):
92adde51 81 self.samplerate = None
92adde51 82 self.last_samplenum = None
2cd9b197
UH
83 self.last_n = deque()
84 self.chunks = 0
b8c44f69
KP
85 self.level_changed = False
86 self.last_t = None
92adde51
BE
87
88 def metadata(self, key, value):
89 if key == srd.SRD_CONF_SAMPLERATE:
90 self.samplerate = value
91
92 def start(self):
93 self.out_ann = self.register(srd.OUTPUT_ANN)
b8c44f69 94 self.edge = self.options['edge']
92adde51 95
831e976a 96 def decode(self):
92adde51
BE
97 if not self.samplerate:
98 raise SamplerateError('Cannot decode without samplerate.')
831e976a 99 while True:
b8c44f69
KP
100 if self.edge == 'rising':
101 pin = self.wait({0: 'r'})
102 elif self.edge == 'falling':
103 pin = self.wait({0: 'f'})
104 else:
105 pin = self.wait({0: 'e'})
92adde51 106
b8c44f69 107 if not self.last_samplenum:
963d71d5 108 self.last_samplenum = self.samplenum
92adde51 109 continue
b8c44f69
KP
110 samples = self.samplenum - self.last_samplenum
111 t = samples / self.samplerate
92adde51 112
b8c44f69
KP
113 if t > 0:
114 self.last_n.append(t)
115 if len(self.last_n) > self.options['avg_period']:
116 self.last_n.popleft()
2cd9b197 117
b8c44f69
KP
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)]])
2cd9b197 126
b8c44f69
KP
127 self.last_t = t
128 self.last_samplenum = self.samplenum