]> sigrok.org Git - libsigrokdecode.git/blame - decoders/usb_signalling/pd.py
usb_signalling: Track USB symbol width to compensate frequency errors
[libsigrokdecode.git] / decoders / usb_signalling / pd.py
CommitLineData
2dc6d41c 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
2dc6d41c
UH
3##
4## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
7d4b5fac 5## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
2dc6d41c
UH
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
18## along with this program; if not, write to the Free Software
19## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20##
21
2dc6d41c
UH
22import sigrokdecode as srd
23
a56b8fe1 24'''
c515eed7 25OUTPUT_PYTHON format:
a56b8fe1
UH
26
27Packet:
28[<ptype>, <pdata>]
29
30<ptype>, <pdata>:
31 - 'SOP', None
32 - 'SYM', <sym>
33 - 'BIT', <bit>
34 - 'STUFF BIT', None
35 - 'EOP', None
a56b8fe1
UH
36
37<sym>:
38 - 'J', 'K', 'SE0', or 'SE1'
39
40<bit>:
41 - 0 or 1
42 - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
a56b8fe1
UH
43'''
44
d1970f14 45# Low-/full-speed symbols.
2dc6d41c 46# Note: Low-speed J and K are inverted compared to the full-speed J and K!
7dc75721
UH
47symbols = {
48 'low-speed': {
2dc6d41c
UH
49 # (<dp>, <dm>): <symbol/state>
50 (0, 0): 'SE0',
51 (1, 0): 'K',
52 (0, 1): 'J',
53 (1, 1): 'SE1',
7dc75721
UH
54 },
55 'full-speed': {
2dc6d41c
UH
56 # (<dp>, <dm>): <symbol/state>
57 (0, 0): 'SE0',
58 (1, 0): 'J',
59 (0, 1): 'K',
60 (1, 1): 'SE1',
7dc75721 61 },
2dc6d41c
UH
62}
63
5bb55598
UH
64bitrates = {
65 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%)
66 'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
67}
68
85c03d60
UH
69sym_idx = {
70 'J': 0,
71 'K': 1,
72 'SE0': 2,
73 'SE1': 3,
74}
75
21cda951
UH
76class SamplerateError(Exception):
77 pass
78
2dc6d41c 79class Decoder(srd.Decoder):
12851357 80 api_version = 2
2dc6d41c
UH
81 id = 'usb_signalling'
82 name = 'USB signalling'
83 longname = 'Universal Serial Bus (LS/FS) signalling'
9e1437a0 84 desc = 'USB (low-speed and full-speed) signalling protocol.'
2dc6d41c
UH
85 license = 'gplv2+'
86 inputs = ['logic']
87 outputs = ['usb_signalling']
6a15597a 88 channels = (
2dc6d41c
UH
89 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
90 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
da9bcbd9 91 )
84c1c0b5
BV
92 options = (
93 {'id': 'signalling', 'desc': 'Signalling',
94 'default': 'full-speed', 'values': ('full-speed', 'low-speed')},
95 )
da9bcbd9 96 annotations = (
85c03d60
UH
97 ('sym-j', 'J symbol'),
98 ('sym-k', 'K symbol'),
99 ('sym-se0', 'SE0 symbol'),
100 ('sym-se1', 'SE1 symbol'),
da9bcbd9
BV
101 ('sop', 'Start of packet (SOP)'),
102 ('eop', 'End of packet (EOP)'),
103 ('bit', 'Bit'),
104 ('stuffbit', 'Stuff bit'),
105 )
066d6594 106 annotation_rows = (
85c03d60
UH
107 ('bits', 'Bits', (4, 5, 6, 7)),
108 ('symbols', 'Symbols', (0, 1, 2, 3)),
066d6594 109 )
2dc6d41c
UH
110
111 def __init__(self):
f372d597 112 self.samplerate = None
d1970f14 113 self.oldsym = 'J' # The "idle" state is J.
fdd5ee5e
UH
114 self.ss_sop = None
115 self.ss_block = None
2dc6d41c 116 self.samplenum = 0
2dc6d41c 117 self.syms = []
5bb55598
UH
118 self.bitrate = None
119 self.bitwidth = None
a241cfb6 120 self.samplepos = None
d1970f14 121 self.samplenum_target = None
a241cfb6 122 self.samplenum_edge = None
2fcd7c22 123 self.oldpins = None
a241cfb6 124 self.edgepins = None
d1970f14
UH
125 self.consecutive_ones = 0
126 self.state = 'IDLE'
2dc6d41c 127
f372d597 128 def start(self):
c515eed7 129 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 130 self.out_ann = self.register(srd.OUTPUT_ANN)
f372d597
BV
131
132 def metadata(self, key, value):
133 if key == srd.SRD_CONF_SAMPLERATE:
134 self.samplerate = value
135 self.bitrate = bitrates[self.options['signalling']]
136 self.bitwidth = float(self.samplerate) / float(self.bitrate)
137 self.halfbit = int(self.bitwidth / 2)
2dc6d41c 138
d1970f14 139 def putpx(self, data):
c515eed7 140 self.put(self.samplenum, self.samplenum, self.out_python, data)
7d4b5fac
UH
141
142 def putx(self, data):
143 self.put(self.samplenum, self.samplenum, self.out_ann, data)
144
fdd5ee5e
UH
145 def putpm(self, data):
146 s, h = self.samplenum, self.halfbit
c515eed7 147 self.put(self.ss_block - h, s + h, self.out_python, data)
fdd5ee5e
UH
148
149 def putm(self, data):
150 s, h = self.samplenum, self.halfbit
9059125f 151 self.put(self.ss_block - h, s + h, self.out_ann, data)
fdd5ee5e 152
d1970f14 153 def putpb(self, data):
fdd5ee5e 154 s, h = self.samplenum, self.halfbit
a241cfb6 155 self.put(self.samplenum_edge, s + h, self.out_python, data)
d1970f14
UH
156
157 def putb(self, data):
fdd5ee5e 158 s, h = self.samplenum, self.halfbit
a241cfb6 159 self.put(self.samplenum_edge, s + h, self.out_ann, data)
d1970f14
UH
160
161 def set_new_target_samplenum(self):
a241cfb6
SB
162 self.samplepos += self.bitwidth;
163 self.samplenum_target = int(self.samplepos)
164 self.samplenum_edge = int(self.samplepos - (self.bitwidth / 2))
d1970f14
UH
165
166 def wait_for_sop(self, sym):
167 # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
168 if sym != 'K':
169 self.oldsym = sym
170 return
171 self.ss_sop = self.samplenum
a241cfb6 172 self.samplepos = self.ss_sop - (self.bitwidth / 2) + 0.5
d1970f14
UH
173 self.set_new_target_samplenum()
174 self.putpx(['SOP', None])
85c03d60 175 self.putx([4, ['SOP', 'S']])
d1970f14
UH
176 self.state = 'GET BIT'
177
178 def handle_bit(self, sym, b):
179 if self.consecutive_ones == 6 and b == '0':
9059125f 180 # Stuff bit.
a56b8fe1 181 self.putpb(['STUFF BIT', None])
85c03d60
UH
182 self.putb([7, ['Stuff bit: %s' % b, 'SB: %s' % b, '%s' % b]])
183 self.putb([sym_idx[sym], ['%s' % sym]])
d1970f14
UH
184 self.consecutive_ones = 0
185 else:
9059125f 186 # Normal bit (not a stuff bit).
a56b8fe1 187 self.putpb(['BIT', b])
85c03d60
UH
188 self.putb([6, ['%s' % b]])
189 self.putb([sym_idx[sym], ['%s' % sym]])
d1970f14
UH
190 if b == '1':
191 self.consecutive_ones += 1
192 else:
193 self.consecutive_ones = 0
194
195 def get_eop(self, sym):
196 # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
197 self.syms.append(sym)
198 self.putpb(['SYM', sym])
85c03d60 199 self.putb([sym_idx[sym], ['%s' % sym, '%s' % sym[0]]])
d1970f14
UH
200 self.set_new_target_samplenum()
201 self.oldsym = sym
202 if self.syms[-2:] == ['SE0', 'J']:
9059125f 203 # Got an EOP.
fdd5ee5e 204 self.putpm(['EOP', None])
85c03d60 205 self.putm([5, ['EOP', 'E']])
a241cfb6 206 self.syms, self.state = [], 'IDLE'
d1970f14 207 self.consecutive_ones = 0
a241cfb6 208 self.bitwidth = float(self.samplerate) / float(self.bitrate)
d1970f14
UH
209
210 def get_bit(self, sym):
211 if sym == 'SE0':
212 # Start of an EOP. Change state, run get_eop() for this bit.
213 self.state = 'GET EOP'
fdd5ee5e 214 self.ss_block = self.samplenum
d1970f14
UH
215 self.get_eop(sym)
216 return
217 self.syms.append(sym)
218 self.putpb(['SYM', sym])
219 b = '0' if self.oldsym != sym else '1'
a241cfb6
SB
220 if (self.oldsym != sym):
221 # edge
222 edgesym = symbols[self.options['signalling']][tuple(self.edgepins)]
223 if (edgesym not in ('SE0', 'SE1')):
224 if (edgesym == sym):
225 self.bitwidth = self.bitwidth - (0.001 * self.bitwidth)
226 self.samplepos = self.samplepos - (0.01 * self.bitwidth)
227 else:
228 self.bitwidth = self.bitwidth + (0.001 * self.bitwidth)
229 self.samplepos = self.samplepos + (0.01 * self.bitwidth)
d1970f14 230 self.handle_bit(sym, b)
d1970f14
UH
231 self.set_new_target_samplenum()
232 self.oldsym = sym
233
2dc6d41c 234 def decode(self, ss, es, data):
21cda951
UH
235 if not self.samplerate:
236 raise SamplerateError('Cannot decode without samplerate.')
2fcd7c22 237 for (self.samplenum, pins) in data:
d1970f14
UH
238 # State machine.
239 if self.state == 'IDLE':
240 # Ignore identical samples early on (for performance reasons).
241 if self.oldpins == pins:
242 continue
243 self.oldpins = pins
244 sym = symbols[self.options['signalling']][tuple(pins)]
245 self.wait_for_sop(sym)
a241cfb6 246 self.edgepins = pins
d1970f14
UH
247 elif self.state in ('GET BIT', 'GET EOP'):
248 # Wait until we're in the middle of the desired bit.
a241cfb6
SB
249 if self.samplenum == self.samplenum_edge:
250 self.edgepins = pins
d1970f14
UH
251 if self.samplenum < self.samplenum_target:
252 continue
253 sym = symbols[self.options['signalling']][tuple(pins)]
254 if self.state == 'GET BIT':
255 self.get_bit(sym)
256 elif self.state == 'GET EOP':
257 self.get_eop(sym)