]> sigrok.org Git - libsigrokdecode.git/blame - decoders/usb_packet/pd.py
usb_packet: Document CRC5 ERROR and CRC16 ERROR packets.
[libsigrokdecode.git] / decoders / usb_packet / 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>
a87d7847 5## Copyright (C) 2012-2014 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
0eb16a7b 24'''
c515eed7 25OUTPUT_PYTHON format:
0eb16a7b
UH
26
27Packet:
28[<ptype>, <pdata>]
29
30<ptype>, <pdata>:
31 - 'SYNC', <sync>
32 - 'PID', <pid>
33 - 'ADDR', <addr>
34 - 'EP', <ep>
35 - 'CRC5', <crc5>
91a19cb3 36 - 'CRC5 ERROR', <crc5>
0eb16a7b 37 - 'CRC16', <crc16>
91a19cb3 38 - 'CRC16 ERROR', <crc16>
0eb16a7b
UH
39 - 'EOP', <eop>
40 - 'FRAMENUM', <framenum>
41 - 'DATABYTE', <databyte>
42 - 'HUBADDR', <hubaddr>
43 - 'SC', <sc>
44 - 'PORT', <port>
45 - 'S', <s>
46 - 'E/U', <e/u>
47 - 'ET', <et>
48 - 'PACKET', [<pcategory>, <pname>, <pinfo>]
49
50<pcategory>, <pname>, <pinfo>:
51 - 'TOKEN', 'OUT', [<sync>, <pid>, <addr>, <ep>, <crc5>, <eop>]
52 - 'TOKEN', 'IN', [<sync>, <pid>, <addr>, <ep>, <crc5>, <eop>]
53 - 'TOKEN', 'SOF', [<sync>, <pid>, <framenum>, <crc5>, <eop>]
54 - 'TOKEN', 'SETUP', [<sync>, <pid>, <addr>, <ep>, <crc5>, <eop>]
55 - 'DATA', 'DATA0', [<sync>, <pid>, <databytes>, <crc16>, <eop>]
56 - 'DATA', 'DATA1', [<sync>, <pid>, <databytes>, <crc16>, <eop>]
57 - 'DATA', 'DATA2', [<sync>, <pid>, <databytes>, <crc16>, <eop>]
58 - 'DATA', 'MDATA', [<sync>, <pid>, <databytes>, <crc16>, <eop>]
59 - 'HANDSHAKE', 'ACK', [<sync>, <pid>, <eop>]
eafe8f08 60 - 'HANDSHAKE', 'NAK', [<sync>, <pid>, <eop>]
0eb16a7b
UH
61 - 'HANDSHAKE', 'STALL', [<sync>, <pid>, <eop>]
62 - 'HANDSHAKE', 'NYET', [<sync>, <pid>, <eop>]
63 - 'SPECIAL', 'PRE', [<sync>, <pid>, <addr>, <ep>, <crc5>, <eop>]
64 - 'SPECIAL', 'ERR', [<sync>, <pid>, <eop>]
65 - 'SPECIAL', 'SPLIT',
66 [<sync>, <pid>, <hubaddr>, <sc>, <port>, <s>, <e/u>, <et>, <crc5>, <eop>]
67 - 'SPECIAL', 'PING', [<sync>, <pid>, <addr>, <ep>, <crc5>, <eop>]
68 - 'SPECIAL', 'Reserved', None
69
70<sync>: SYNC field bitstring, normally '00000001' (8 chars).
71<pid>: Packet ID bitstring, e.g. '11000011' for DATA0 (8 chars).
72<addr>: Address field number, 0-127 (7 bits).
73<ep>: Endpoint number, 0-15 (4 bits).
74<crc5>: CRC-5 number (5 bits).
75<crc16>: CRC-16 number (16 bits).
76<eop>: End of packet marker. List of symbols, usually ['SE0', 'SE0', 'J'].
77<framenum>: USB (micro)frame number, 0-2047 (11 bits).
78<databyte>: A single data byte, e.g. 0x55.
79<databytes>: List of data bytes, e.g. [0x55, 0xaa, 0x99] (0 - 1024 bytes).
80<hubaddr>: TODO
81<sc>: TODO
82<port>: TODO
83<s>: TODO
84<e/u>: TODO
85<et>: TODO
86'''
87
2dc6d41c
UH
88# Packet IDs (PIDs).
89# The first 4 bits are the 'packet type' field, the last 4 bits are the
90# 'check field' (each bit in the check field must be the inverse of the resp.
91# bit in the 'packet type' field; if not, that's a 'PID error').
92# For the 4-bit strings, the left-most '1' or '0' is the LSB, i.e. it's sent
93# to the bus first.
94pids = {
95 # Tokens
96 '10000111': ['OUT', 'Address & EP number in host-to-function transaction'],
97 '10010110': ['IN', 'Address & EP number in function-to-host transaction'],
98 '10100101': ['SOF', 'Start-Of-Frame marker & frame number'],
99 '10110100': ['SETUP', 'Address & EP number in host-to-function transaction for SETUP to a control pipe'],
100
101 # Data
102 # Note: DATA2 and MDATA are HS-only.
103 '11000011': ['DATA0', 'Data packet PID even'],
104 '11010010': ['DATA1', 'Data packet PID odd'],
105 '11100001': ['DATA2', 'Data packet PID HS, high bandwidth isosynchronous transaction in a microframe'],
106 '11110000': ['MDATA', 'Data packet PID HS for split and high-bandwidth isosynchronous transactions'],
107
108 # Handshake
109 '01001011': ['ACK', 'Receiver accepts error-free packet'],
110 '01011010': ['NAK', 'Receiver cannot accept or transmitter cannot send'],
111 '01111000': ['STALL', 'EP halted or control pipe request unsupported'],
112 '01101001': ['NYET', 'No response yet from receiver'],
113
114 # Special
115 '00111100': ['PRE', 'Host-issued preamble; enables downstream bus traffic to low-speed devices'],
116 '00111100': ['ERR', 'Split transaction error handshake'],
117 '00011110': ['SPLIT', 'HS split transaction token'],
118 '00101101': ['PING', 'HS flow control probe for a bulk/control EP'],
119 '00001111': ['Reserved', 'Reserved PID'],
120}
121
0eb16a7b
UH
122def get_category(pidname):
123 if pidname in ('OUT', 'IN', 'SOF', 'SETUP'):
124 return 'TOKEN'
125 elif pidname in ('DATA0', 'DATA1', 'DATA2', 'MDATA'):
126 return 'DATA'
eafe8f08 127 elif pidname in ('ACK', 'NAK', 'STALL', 'NYET'):
0eb16a7b
UH
128 return 'HANDSHAKE'
129 else:
130 return 'SPECIAL'
131
a87d7847
UH
132def ann_index(pidname):
133 l = ['OUT', 'IN', 'SOF', 'SETUP', 'DATA0', 'DATA1', 'DATA2', 'MDATA',
134 'ACK', 'NAK', 'STALL', 'NYET', 'PRE', 'ERR', 'SPLIT', 'PING',
135 'Reserved']
136 if pidname not in l:
137 return 28
138 return l.index(pidname) + 11
139
2dc6d41c
UH
140def bitstr_to_num(bitstr):
141 if not bitstr:
142 return 0
143 l = list(bitstr)
144 l.reverse()
145 return int(''.join(l), 2)
146
0e3cb15e
SB
147def reverse_number(num, count):
148 out = list(count * '0')
149 for i in range(0, count):
64b45b20 150 if num >> i & 1:
0e3cb15e
SB
151 out[i] = '1';
152 return int(''.join(out), 2)
153
154def calc_crc5(bitstr):
155 poly5 = 0x25
156 crc5 = 0x1f
157 for bit in bitstr:
158 crc5 <<= 1
64b45b20 159 if int(bit) != (crc5 >> 5):
0e3cb15e
SB
160 crc5 ^= poly5
161 crc5 &= 0x1f
162 crc5 ^= 0x1f
163 return reverse_number(crc5, 5)
164
165def calc_crc16(bitstr):
166 poly16 = 0x18005
167 crc16 = 0xffff
168 for bit in bitstr:
169 crc16 <<= 1
64b45b20 170 if int(bit) != (crc16 >> 16):
0e3cb15e
SB
171 crc16 ^= poly16
172 crc16 &= 0xffff
173 crc16 ^= 0xffff
174 return reverse_number(crc16, 16)
175
2dc6d41c 176class Decoder(srd.Decoder):
12851357 177 api_version = 2
a015bd49
UH
178 id = 'usb_packet'
179 name = 'USB packet'
180 longname = 'Universal Serial Bus (LS/FS) packet'
181 desc = 'USB (low-speed and full-speed) packet protocol.'
2dc6d41c
UH
182 license = 'gplv2+'
183 inputs = ['usb_signalling']
a015bd49 184 outputs = ['usb_packet']
84c1c0b5 185 options = (
b0918d40
UH
186 {'id': 'signalling', 'desc': 'Signalling',
187 'default': 'full-speed', 'values': ('full-speed', 'low-speed')},
84c1c0b5 188 )
da9bcbd9
BV
189 annotations = (
190 ('sync-ok', 'SYNC'),
191 ('sync-err', 'SYNC (error)'),
192 ('pid', 'PID'),
193 ('framenum', 'FRAMENUM'),
194 ('addr', 'ADDR'),
195 ('ep', 'EP'),
196 ('crc5-ok', 'CRC5'),
197 ('crc5-err', 'CRC5 (error)'),
198 ('data', 'DATA'),
199 ('crc16-ok', 'CRC16'),
200 ('crc16-err', 'CRC16 (error)'),
201 ('packet-out', 'Packet: OUT'),
202 ('packet-in', 'Packet: IN'),
203 ('packet-sof', 'Packet: SOF'),
204 ('packet-setup', 'Packet: SETUP'),
205 ('packet-data0', 'Packet: DATA0'),
206 ('packet-data1', 'Packet: DATA1'),
207 ('packet-data2', 'Packet: DATA2'),
208 ('packet-mdata', 'Packet: MDATA'),
209 ('packet-ack', 'Packet: ACK'),
210 ('packet-nak', 'Packet: NAK'),
211 ('packet-stall', 'Packet: STALL'),
212 ('packet-nyet', 'Packet: NYET'),
213 ('packet-pre', 'Packet: PRE'),
214 ('packet-err', 'Packet: ERR'),
215 ('packet-split', 'Packet: SPLIT'),
216 ('packet-ping', 'Packet: PING'),
217 ('packet-reserved', 'Packet: Reserved'),
218 ('packet-invalid', 'Packet: Invalid'),
219 )
a87d7847 220 annotation_rows = (
fa62a8f9
UH
221 ('fields', 'Packet fields', tuple(range(10 + 1))),
222 ('packet', 'Packets', tuple(range(11, 28 + 1))),
a87d7847 223 )
2dc6d41c
UH
224
225 def __init__(self):
0eb16a7b
UH
226 self.bits = []
227 self.packet = []
228 self.packet_summary = ''
229 self.ss = self.es = None
230 self.ss_packet = self.es_packet = None
231 self.state = 'WAIT FOR SOP'
232
233 def putpb(self, data):
c515eed7 234 self.put(self.ss, self.es, self.out_python, data)
0eb16a7b
UH
235
236 def putb(self, data):
237 self.put(self.ss, self.es, self.out_ann, data)
238
239 def putpp(self, data):
c515eed7 240 self.put(self.ss_packet, self.es_packet, self.out_python, data)
0eb16a7b
UH
241
242 def putp(self, data):
243 self.put(self.ss_packet, self.es_packet, self.out_ann, data)
2dc6d41c 244
8915b346 245 def start(self):
c515eed7 246 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 247 self.out_ann = self.register(srd.OUTPUT_ANN)
2dc6d41c 248
0eb16a7b
UH
249 def handle_packet(self):
250 packet = ''
251 for (bit, ss, es) in self.bits:
252 packet += bit
253
3e84c443
UH
254 if len(packet) < 8:
255 self.putp([28, ['Invalid packet (shorter than 8 bits)']])
256 return
257
0eb16a7b
UH
258 # Bits[0:7]: SYNC
259 sync = packet[:7 + 1]
260 self.ss, self.es = self.bits[0][1], self.bits[7][2]
261 # The SYNC pattern for low-speed/full-speed is KJKJKJKK (00000001).
262 if sync != '00000001':
263 self.putpb(['SYNC ERROR', sync])
7a233067
UH
264 self.putb([1, ['SYNC ERROR: %s' % sync, 'SYNC ERR: %s' % sync,
265 'SYNC ERR', 'SE', 'S']])
0eb16a7b
UH
266 else:
267 self.putpb(['SYNC', sync])
7a233067 268 self.putb([0, ['SYNC: %s' % sync, 'SYNC', 'S']])
0eb16a7b
UH
269 self.packet.append(sync)
270
3e84c443
UH
271 if len(packet) < 16:
272 self.putp([28, ['Invalid packet (shorter than 16 bits)']])
273 return
274
0eb16a7b
UH
275 # Bits[8:15]: PID
276 pid = packet[8:15 + 1]
3e84c443 277 pidname = pids.get(pid, ('UNKNOWN', 'Unknown PID'))[0]
0eb16a7b
UH
278 self.ss, self.es = self.bits[8][1], self.bits[15][2]
279 self.putpb(['PID', pidname])
7a233067 280 self.putb([2, ['PID: %s' % pidname, pidname, pidname[0]]])
0eb16a7b
UH
281 self.packet.append(pid)
282 self.packet_summary += pidname
283
284 if pidname in ('OUT', 'IN', 'SOF', 'SETUP', 'PRE', 'PING'):
3e84c443
UH
285 if len(packet) < 32:
286 self.putp([28, ['Invalid packet (shorter than 32 bits)']])
287 return
288
0eb16a7b
UH
289 if pidname == 'SOF':
290 # Bits[16:26]: Framenum
291 framenum = bitstr_to_num(packet[16:26 + 1])
292 self.ss, self.es = self.bits[16][1], self.bits[26][2]
293 self.putpb(['FRAMENUM', framenum])
7a233067 294 self.putb([3, ['Frame: %d' % framenum, 'Frame', 'Fr', 'F']])
0eb16a7b
UH
295 self.packet.append(framenum)
296 self.packet_summary += ' %d' % framenum
297 else:
298 # Bits[16:22]: Addr
299 addr = bitstr_to_num(packet[16:22 + 1])
300 self.ss, self.es = self.bits[16][1], self.bits[22][2]
301 self.putpb(['ADDR', addr])
7a233067
UH
302 self.putb([4, ['Address: %d' % addr, 'Addr: %d' % addr,
303 'Addr', 'A']])
0eb16a7b
UH
304 self.packet.append(addr)
305 self.packet_summary += ' ADDR %d' % addr
306
307 # Bits[23:26]: EP
308 ep = bitstr_to_num(packet[23:26 + 1])
309 self.ss, self.es = self.bits[23][1], self.bits[26][2]
310 self.putpb(['EP', ep])
7a233067 311 self.putb([5, ['Endpoint: %d' % ep, 'EP: %d' % ep, 'EP', 'E']])
0eb16a7b
UH
312 self.packet.append(ep)
313 self.packet_summary += ' EP %d' % ep
314
315 # Bits[27:31]: CRC5
316 crc5 = bitstr_to_num(packet[27:31 + 1])
0e3cb15e 317 crc5_calc = calc_crc5(packet[16:27])
0eb16a7b 318 self.ss, self.es = self.bits[27][1], self.bits[31][2]
64b45b20 319 if crc5 == crc5_calc:
0e3cb15e
SB
320 self.putpb(['CRC5', crc5])
321 self.putb([6, ['CRC5: 0x%02X' % crc5, 'CRC5', 'C']])
322 else:
323 self.putpb(['CRC5 ERROR', crc5])
324 self.putb([7, ['CRC5 ERROR: 0x%02X' % crc5, 'CRC5 ERR', 'CE', 'C']])
0eb16a7b
UH
325 self.packet.append(crc5)
326 elif pidname in ('DATA0', 'DATA1', 'DATA2', 'MDATA'):
327 # Bits[16:packetlen-16]: Data
328 data = packet[16:-16]
329 # TODO: len(data) must be a multiple of 8.
330 databytes = []
331 self.packet_summary += ' ['
332 for i in range(0, len(data), 8):
333 db = bitstr_to_num(data[i:i + 8])
334 self.ss, self.es = self.bits[16 + i][1], self.bits[23 + i][2]
335 self.putpb(['DATABYTE', db])
7a233067
UH
336 self.putb([8, ['Databyte: %02X' % db, 'Data: %02X' % db,
337 'DB: %02X' % db, '%02X' % db]])
0eb16a7b 338 databytes.append(db)
7a233067 339 self.packet_summary += ' %02X' % db
0eb16a7b
UH
340 self.packet_summary += ' ]'
341
7a233067 342 # Convenience Python output (no annotation) for all bytes together.
0eb16a7b
UH
343 self.ss, self.es = self.bits[16][1], self.bits[-16][2]
344 self.putpb(['DATABYTES', databytes])
345 self.packet.append(databytes)
346
347 # Bits[packetlen-16:packetlen]: CRC16
348 crc16 = bitstr_to_num(packet[-16:])
0e3cb15e 349 crc16_calc = calc_crc16(packet[16:-16])
0eb16a7b 350 self.ss, self.es = self.bits[-16][1], self.bits[-1][2]
64b45b20 351 if crc16 == crc16_calc:
0e3cb15e
SB
352 self.putpb(['CRC16', crc16])
353 self.putb([9, ['CRC16: 0x%04X' % crc16, 'CRC16', 'C']])
354 else:
355 self.putpb(['CRC16 ERROR', crc16])
356 self.putb([10, ['CRC16 ERROR: 0x%04X' % crc16, 'CRC16 ERR', 'CE', 'C']])
0eb16a7b
UH
357 self.packet.append(crc16)
358 elif pidname in ('ACK', 'NAK', 'STALL', 'NYET', 'ERR'):
359 pass # Nothing to do, these only have SYNC+PID+EOP fields.
360 else:
361 pass # TODO: Handle 'SPLIT' and possibly 'Reserved' packets.
362
363 # Output a (summary of) the whole packet.
364 pcategory, pname, pinfo = get_category(pidname), pidname, self.packet
365 self.putpp(['PACKET', [pcategory, pname, pinfo]])
a87d7847 366 self.putp([ann_index(pidname), ['%s' % self.packet_summary]])
0eb16a7b
UH
367
368 self.packet, self.packet_summary = [], ''
369
2dc6d41c
UH
370 def decode(self, ss, es, data):
371 (ptype, pdata) = data
372
0eb16a7b
UH
373 # We only care about certain packet types for now.
374 if ptype not in ('SOP', 'BIT', 'EOP'):
375 return
2dc6d41c 376
0eb16a7b
UH
377 # State machine.
378 if self.state == 'WAIT FOR SOP':
379 if ptype != 'SOP':
380 return
381 self.ss_packet = ss
382 self.state = 'GET BIT'
383 elif self.state == 'GET BIT':
384 if ptype == 'BIT':
385 self.bits.append([pdata, ss, es])
386 elif ptype == 'EOP':
387 self.es_packet = es
388 self.handle_packet()
3e84c443 389 self.packet, self.packet_summary = [], ''
0eb16a7b
UH
390 self.bits, self.state = [], 'WAIT FOR SOP'
391 else:
392 pass # TODO: Error