]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/usb_signalling/pd.py
srd_inst_decode(): Make the code API version dependent.
[libsigrokdecode.git] / decoders / usb_signalling / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
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
22import sigrokdecode as srd
23
24'''
25OUTPUT_PYTHON format:
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
36 - 'ERR', None
37 - 'KEEP ALIVE', None
38 - 'RESET', None
39
40<sym>:
41 - 'J', 'K', 'SE0', or 'SE1'
42
43<bit>:
44 - '0' or '1'
45 - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
46'''
47
48# Low-/full-speed symbols.
49# Note: Low-speed J and K are inverted compared to the full-speed J and K!
50symbols = {
51 'low-speed': {
52 # (<dp>, <dm>): <symbol/state>
53 (0, 0): 'SE0',
54 (1, 0): 'K',
55 (0, 1): 'J',
56 (1, 1): 'SE1',
57 },
58 'full-speed': {
59 # (<dp>, <dm>): <symbol/state>
60 (0, 0): 'SE0',
61 (1, 0): 'J',
62 (0, 1): 'K',
63 (1, 1): 'SE1',
64 },
65 'automatic': {
66 # (<dp>, <dm>): <symbol/state>
67 (0, 0): 'SE0',
68 (1, 0): 'FS_J',
69 (0, 1): 'LS_J',
70 (1, 1): 'SE1',
71 },
72 # After a PREamble PID, the bus segment between Host and Hub uses LS
73 # signalling rate and FS signalling polarity (USB 2.0 spec, 11.8.4: "For
74 # both upstream and downstream low-speed data, the hub is responsible for
75 # inverting the polarity of the data before transmitting to/from a
76 # low-speed port.").
77 'low-speed-rp': {
78 # (<dp>, <dm>): <symbol/state>
79 (0, 0): 'SE0',
80 (1, 0): 'J',
81 (0, 1): 'K',
82 (1, 1): 'SE1',
83 },
84}
85
86bitrates = {
87 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%)
88 'low-speed-rp': 1500000, # 1.5Mb/s (+/- 1.5%)
89 'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
90 'automatic': None
91}
92
93sym_annotation = {
94 'J': [0, ['J']],
95 'K': [1, ['K']],
96 'SE0': [2, ['SE0', '0']],
97 'SE1': [3, ['SE1', '1']],
98}
99
100class SamplerateError(Exception):
101 pass
102
103class Decoder(srd.Decoder):
104 api_version = 2
105 id = 'usb_signalling'
106 name = 'USB signalling'
107 longname = 'Universal Serial Bus (LS/FS) signalling'
108 desc = 'USB (low-speed and full-speed) signalling protocol.'
109 license = 'gplv2+'
110 inputs = ['logic']
111 outputs = ['usb_signalling']
112 channels = (
113 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
114 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
115 )
116 options = (
117 {'id': 'signalling', 'desc': 'Signalling',
118 'default': 'automatic', 'values': ('automatic', 'full-speed', 'low-speed')},
119 )
120 annotations = (
121 ('sym-j', 'J symbol'),
122 ('sym-k', 'K symbol'),
123 ('sym-se0', 'SE0 symbol'),
124 ('sym-se1', 'SE1 symbol'),
125 ('sop', 'Start of packet (SOP)'),
126 ('eop', 'End of packet (EOP)'),
127 ('bit', 'Bit'),
128 ('stuffbit', 'Stuff bit'),
129 ('error', 'Error'),
130 ('keep-alive', 'Low-speed keep-alive'),
131 ('reset', 'Reset'),
132 )
133 annotation_rows = (
134 ('bits', 'Bits', (4, 5, 6, 7, 8, 9, 10)),
135 ('symbols', 'Symbols', (0, 1, 2, 3)),
136 )
137
138 def __init__(self):
139 self.samplerate = None
140 self.oldsym = 'J' # The "idle" state is J.
141 self.ss_block = None
142 self.samplenum = 0
143 self.bitrate = None
144 self.bitwidth = None
145 self.samplepos = None
146 self.samplenum_target = None
147 self.samplenum_edge = None
148 self.samplenum_lastedge = 0
149 self.oldpins = None
150 self.edgepins = None
151 self.consecutive_ones = 0
152 self.bits = None
153 self.state = 'INIT'
154
155 def start(self):
156 self.out_python = self.register(srd.OUTPUT_PYTHON)
157 self.out_ann = self.register(srd.OUTPUT_ANN)
158
159 def metadata(self, key, value):
160 if key == srd.SRD_CONF_SAMPLERATE:
161 self.samplerate = value
162 self.signalling = self.options['signalling']
163 if self.signalling != 'automatic':
164 self.update_bitrate()
165
166 def update_bitrate(self):
167 self.bitrate = bitrates[self.signalling]
168 self.bitwidth = float(self.samplerate) / float(self.bitrate)
169
170 def putpx(self, data):
171 s = self.samplenum_edge
172 self.put(s, s, self.out_python, data)
173
174 def putx(self, data):
175 s = self.samplenum_edge
176 self.put(s, s, self.out_ann, data)
177
178 def putpm(self, data):
179 e = self.samplenum_edge
180 self.put(self.ss_block, e, self.out_python, data)
181
182 def putm(self, data):
183 e = self.samplenum_edge
184 self.put(self.ss_block, e, self.out_ann, data)
185
186 def putpb(self, data):
187 s, e = self.samplenum_lastedge, self.samplenum_edge
188 self.put(s, e, self.out_python, data)
189
190 def putb(self, data):
191 s, e = self.samplenum_lastedge, self.samplenum_edge
192 self.put(s, e, self.out_ann, data)
193
194 def set_new_target_samplenum(self):
195 self.samplepos += self.bitwidth;
196 self.samplenum_target = int(self.samplepos)
197 self.samplenum_lastedge = self.samplenum_edge
198 self.samplenum_edge = int(self.samplepos - (self.bitwidth / 2))
199
200 def wait_for_sop(self, sym):
201 # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
202 if sym != 'K' or self.oldsym != 'J':
203 return
204 self.consecutive_ones = 0
205 self.bits = ''
206 self.update_bitrate()
207 self.samplepos = self.samplenum - (self.bitwidth / 2) + 0.5
208 self.set_new_target_samplenum()
209 self.putpx(['SOP', None])
210 self.putx([4, ['SOP', 'S']])
211 self.state = 'GET BIT'
212
213 def handle_bit(self, b):
214 if self.consecutive_ones == 6:
215 if b == '0':
216 # Stuff bit.
217 self.putpb(['STUFF BIT', None])
218 self.putb([7, ['Stuff bit: 0', 'SB: 0', '0']])
219 self.consecutive_ones = 0
220 else:
221 self.putpb(['ERR', None])
222 self.putb([8, ['Bit stuff error', 'BS ERR', 'B']])
223 self.state = 'IDLE'
224 else:
225 # Normal bit (not a stuff bit).
226 self.putpb(['BIT', b])
227 self.putb([6, ['%s' % b]])
228 if b == '1':
229 self.consecutive_ones += 1
230 else:
231 self.consecutive_ones = 0
232
233 def get_eop(self, sym):
234 # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
235 self.set_new_target_samplenum()
236 self.putpb(['SYM', sym])
237 self.putb(sym_annotation[sym])
238 self.oldsym = sym
239 if sym == 'SE0':
240 pass
241 elif sym == 'J':
242 # Got an EOP.
243 self.putpm(['EOP', None])
244 self.putm([5, ['EOP', 'E']])
245 self.state = 'WAIT IDLE'
246 else:
247 self.putpm(['ERR', None])
248 self.putm([8, ['EOP Error', 'EErr', 'E']])
249 self.state = 'IDLE'
250
251 def get_bit(self, sym):
252 self.set_new_target_samplenum()
253 b = '0' if self.oldsym != sym else '1'
254 self.oldsym = sym
255 if sym == 'SE0':
256 # Start of an EOP. Change state, save edge
257 self.state = 'GET EOP'
258 self.ss_block = self.samplenum_lastedge
259 else:
260 self.handle_bit(b)
261 self.putpb(['SYM', sym])
262 self.putb(sym_annotation[sym])
263 if len(self.bits) <= 16:
264 self.bits += b
265 if len(self.bits) == 16 and self.bits == '0000000100111100':
266 # Sync and low-speed PREamble seen
267 self.putpx(['EOP', None])
268 self.state = 'IDLE'
269 self.signalling = 'low-speed-rp'
270 self.update_bitrate()
271 self.oldsym = 'J'
272 if b == '0':
273 edgesym = symbols[self.signalling][tuple(self.edgepins)]
274 if edgesym not in ('SE0', 'SE1'):
275 if edgesym == sym:
276 self.bitwidth = self.bitwidth - (0.001 * self.bitwidth)
277 self.samplepos = self.samplepos - (0.01 * self.bitwidth)
278 else:
279 self.bitwidth = self.bitwidth + (0.001 * self.bitwidth)
280 self.samplepos = self.samplepos + (0.01 * self.bitwidth)
281
282 def handle_idle(self, sym):
283 self.samplenum_edge = self.samplenum
284 se0_length = float(self.samplenum - self.samplenum_lastedge) / self.samplerate
285 if se0_length > 2.5e-6: # 2.5us
286 self.putpb(['RESET', None])
287 self.putb([10, ['Reset', 'Res', 'R']])
288 self.signalling = self.options['signalling']
289 elif se0_length > 1.2e-6 and self.signalling == 'low-speed':
290 self.putpb(['KEEP ALIVE', None])
291 self.putb([9, ['Keep-alive', 'KA', 'A']])
292
293 if sym == 'FS_J':
294 self.signalling = 'full-speed'
295 self.update_bitrate()
296 elif sym == 'LS_J':
297 self.signalling = 'low-speed'
298 self.update_bitrate()
299 self.oldsym = 'J'
300 self.state = 'IDLE'
301
302 def decode(self, ss, es, data):
303 if not self.samplerate:
304 raise SamplerateError('Cannot decode without samplerate.')
305 for (self.samplenum, pins) in data:
306 # State machine.
307 if self.state == 'IDLE':
308 # Ignore identical samples early on (for performance reasons).
309 if self.oldpins == pins:
310 continue
311 self.oldpins = pins
312 sym = symbols[self.signalling][tuple(pins)]
313 if sym == 'SE0':
314 self.samplenum_lastedge = self.samplenum
315 self.state = 'WAIT IDLE'
316 else:
317 self.wait_for_sop(sym)
318 self.edgepins = pins
319 elif self.state in ('GET BIT', 'GET EOP'):
320 # Wait until we're in the middle of the desired bit.
321 if self.samplenum == self.samplenum_edge:
322 self.edgepins = pins
323 if self.samplenum < self.samplenum_target:
324 continue
325 sym = symbols[self.signalling][tuple(pins)]
326 if self.state == 'GET BIT':
327 self.get_bit(sym)
328 elif self.state == 'GET EOP':
329 self.get_eop(sym)
330 self.oldpins = pins
331 elif self.state == 'WAIT IDLE':
332 if tuple(pins) == (0, 0):
333 continue
334 if self.samplenum - self.samplenum_lastedge > 1:
335 sym = symbols[self.options['signalling']][tuple(pins)]
336 self.handle_idle(sym)
337 else:
338 sym = symbols[self.signalling][tuple(pins)]
339 self.wait_for_sop(sym)
340 self.oldpins = pins
341 self.edgepins = pins
342 elif self.state == 'INIT':
343 sym = symbols[self.options['signalling']][tuple(pins)]
344 self.handle_idle(sym)
345 self.oldpins = pins