]> sigrok.org Git - libsigrokdecode.git/blame - decoders/jitter/pd.py
license: remove FSF postal address from boiler plate license text
[libsigrokdecode.git] / decoders / jitter / pd.py
CommitLineData
c2049e2c
SB
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
c2049e2c
SB
18##
19
20import sigrokdecode as srd
21
22# Helper dictionary for edge detection.
23edge_detector = {
24 'rising': lambda x, y: bool(not x and y),
25 'falling': lambda x, y: bool(x and not y),
26 'both': lambda x, y: bool(x ^ y),
27}
28
29class SamplerateError(Exception):
30 pass
31
32class Decoder(srd.Decoder):
54dad54a 33 api_version = 3
c2049e2c
SB
34 id = 'jitter'
35 name = 'Jitter'
36 longname = 'Timing jitter calculation'
37 desc = 'Retrieves the timing jitter between two digital signals.'
38 license = 'gplv2+'
39 inputs = ['logic']
40 outputs = ['jitter']
41 channels = (
42 {'id': 'clk', 'name': 'Clock', 'desc': 'Clock reference channel'},
43 {'id': 'sig', 'name': 'Resulting signal', 'desc': 'Resulting signal controlled by the clock'},
44 )
45 options = (
46 {'id': 'clk_polarity', 'desc': 'Clock edge polarity',
47 'default': 'rising', 'values': ('rising', 'falling', 'both')},
48 {'id': 'sig_polarity', 'desc': 'Resulting signal edge polarity',
49 'default': 'rising', 'values': ('rising', 'falling', 'both')},
50 )
51 annotations = (
52 ('jitter', 'Jitter value'),
53 ('clk_missed', 'Clock missed'),
54 ('sig_missed', 'Signal missed'),
55 )
56 annotation_rows = (
57 ('jitter', 'Jitter values', (0,)),
58 ('clk_missed', 'Clock missed', (1,)),
59 ('sig_missed', 'Signal missed', (2,)),
60 )
5f987d6f 61 binary = (
05aaa486 62 ('ascii-float', 'Jitter values as newline-separated ASCII floats'),
5f987d6f 63 )
c2049e2c 64
92b7b49f 65 def __init__(self):
c2049e2c
SB
66 self.state = 'CLK'
67 self.samplerate = None
54dad54a 68 self.oldclk, self.oldsig = 0, 0
c2049e2c
SB
69 self.clk_start = None
70 self.sig_start = None
71 self.clk_missed = 0
72 self.sig_missed = 0
73
74 def start(self):
75 self.clk_edge = edge_detector[self.options['clk_polarity']]
76 self.sig_edge = edge_detector[self.options['sig_polarity']]
77 self.out_ann = self.register(srd.OUTPUT_ANN)
2f370328 78 self.out_binary = self.register(srd.OUTPUT_BINARY)
c2049e2c
SB
79 self.out_clk_missed = self.register(srd.OUTPUT_META,
80 meta=(int, 'Clock missed', 'Clock transition missed'))
81 self.out_sig_missed = self.register(srd.OUTPUT_META,
82 meta=(int, 'Signal missed', 'Resulting signal transition missed'))
83
84 def metadata(self, key, value):
85 if key == srd.SRD_CONF_SAMPLERATE:
86 self.samplerate = value
87
88 # Helper function for jitter time annotations.
89 def putx(self, delta):
90 # Adjust granularity.
91 if delta == 0 or delta >= 1:
a9f7935a 92 delta_s = '%.1fs' % (delta)
c2049e2c 93 elif delta <= 1e-12:
a9f7935a 94 delta_s = '%.1ffs' % (delta * 1e15)
c2049e2c 95 elif delta <= 1e-9:
a9f7935a 96 delta_s = '%.1fps' % (delta * 1e12)
c2049e2c 97 elif delta <= 1e-6:
a9f7935a 98 delta_s = '%.1fns' % (delta * 1e9)
c2049e2c 99 elif delta <= 1e-3:
a9f7935a 100 delta_s = '%.1fμs' % (delta * 1e6)
c2049e2c 101 else:
a9f7935a 102 delta_s = '%.1fms' % (delta * 1e3)
c2049e2c
SB
103
104 self.put(self.clk_start, self.sig_start, self.out_ann, [0, [delta_s]])
105
05aaa486 106 # Helper function for ASCII float jitter values (one value per line).
5f987d6f
SB
107 def putb(self, delta):
108 if delta is None:
109 return
70aa53ec
UH
110 # Format the delta to an ASCII float value terminated by a newline.
111 x = str(delta) + '\n'
2f370328 112 self.put(self.clk_start, self.sig_start, self.out_binary,
2824e811 113 [0, x.encode('UTF-8')])
5f987d6f 114
c2049e2c
SB
115 # Helper function for missed clock and signal annotations.
116 def putm(self, data):
117 self.put(self.samplenum, self.samplenum, self.out_ann, data)
118
34cfd419
UH
119 def handle_clk(self, clk, sig):
120 if self.clk_start == self.samplenum:
121 # Clock transition already treated.
122 # We have done everything we can with this sample.
123 return True
124
125 if self.clk_edge(self.oldclk, clk):
126 # Clock edge found.
127 # We note the sample and move to the next state.
128 self.clk_start = self.samplenum
129 self.state = 'SIG'
130 return False
131 else:
132 if self.sig_start is not None \
133 and self.sig_start != self.samplenum \
134 and self.sig_edge(self.oldsig, sig):
135 # If any transition in the resulting signal
136 # occurs while we are waiting for a clock,
137 # we increase the missed signal counter.
138 self.sig_missed += 1
961699aa 139 self.put(self.samplenum, self.samplenum, self.out_sig_missed, self.sig_missed)
34cfd419
UH
140 self.putm([2, ['Missed signal', 'MS']])
141 # No clock edge found, we have done everything we
142 # can with this sample.
143 return True
144
145 def handle_sig(self, clk, sig):
146 if self.sig_start == self.samplenum:
147 # Signal transition already treated.
148 # We have done everything we can with this sample.
149 return True
150
151 if self.sig_edge(self.oldsig, sig):
152 # Signal edge found.
153 # We note the sample, calculate the jitter
154 # and move to the next state.
155 self.sig_start = self.samplenum
156 self.state = 'CLK'
157 # Calculate and report the timing jitter.
5f987d6f
SB
158 delta = (self.sig_start - self.clk_start) / self.samplerate
159 self.putx(delta)
160 self.putb(delta)
34cfd419
UH
161 return False
162 else:
163 if self.clk_start != self.samplenum \
164 and self.clk_edge(self.oldclk, clk):
165 # If any transition in the clock signal
166 # occurs while we are waiting for a resulting
167 # signal, we increase the missed clock counter.
168 self.clk_missed += 1
961699aa 169 self.put(self.samplenum, self.samplenum, self.out_clk_missed, self.clk_missed)
34cfd419
UH
170 self.putm([1, ['Missed clock', 'MC']])
171 # No resulting signal edge found, we have done
172 # everything we can with this sample.
173 return True
174
54dad54a 175 def decode(self):
c2049e2c
SB
176 if not self.samplerate:
177 raise SamplerateError('Cannot decode without samplerate.')
54dad54a
UH
178 while True:
179 # Wait for a transition on CLK and/or SIG.
180 clk, sig = self.wait([{0: 'e'}, {1: 'e'}])
c2049e2c
SB
181
182 # State machine:
183 # For each sample we can move 2 steps forward in the state machine.
184 while True:
c2049e2c
SB
185 # Clock state has the lead.
186 if self.state == 'CLK':
34cfd419 187 if self.handle_clk(clk, sig):
c2049e2c 188 break
c2049e2c 189 if self.state == 'SIG':
34cfd419 190 if self.handle_sig(clk, sig):
c2049e2c 191 break
c2049e2c
SB
192
193 # Save current CLK/SIG values for the next round.
194 self.oldclk, self.oldsig = clk, sig