2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 # USB (low-speed and full-speed) protocol decoder
24 import sigrokdecode as srd
27 # The first 4 bits are the 'packet type' field, the last 4 bits are the
28 # 'check field' (each bit in the check field must be the inverse of the resp.
29 # bit in the 'packet type' field; if not, that's a 'PID error').
30 # For the 4-bit strings, the left-most '1' or '0' is the LSB, i.e. it's sent
34 '10000111': ['OUT', 'Address & EP number in host-to-function transaction'],
35 '10010110': ['IN', 'Address & EP number in function-to-host transaction'],
36 '10100101': ['SOF', 'Start-Of-Frame marker & frame number'],
37 '10110100': ['SETUP', 'Address & EP number in host-to-function transaction for SETUP to a control pipe'],
40 # Note: DATA2 and MDATA are HS-only.
41 '11000011': ['DATA0', 'Data packet PID even'],
42 '11010010': ['DATA1', 'Data packet PID odd'],
43 '11100001': ['DATA2', 'Data packet PID HS, high bandwidth isosynchronous transaction in a microframe'],
44 '11110000': ['MDATA', 'Data packet PID HS for split and high-bandwidth isosynchronous transactions'],
47 '01001011': ['ACK', 'Receiver accepts error-free packet'],
48 '01011010': ['NAK', 'Receiver cannot accept or transmitter cannot send'],
49 '01111000': ['STALL', 'EP halted or control pipe request unsupported'],
50 '01101001': ['NYET', 'No response yet from receiver'],
53 '00111100': ['PRE', 'Host-issued preamble; enables downstream bus traffic to low-speed devices'],
54 '00111100': ['ERR', 'Split transaction error handshake'],
55 '00011110': ['SPLIT', 'HS split transaction token'],
56 '00101101': ['PING', 'HS flow control probe for a bulk/control EP'],
57 '00001111': ['Reserved', 'Reserved PID'],
60 def bitstr_to_num(bitstr):
65 return int(''.join(l), 2)
67 def packet_decode(packet):
70 pid = pids.get(pid, (pid, ''))[0]
73 if pid in ('OUT', 'IN', 'SOF', 'SETUP'):
76 data = str(bitstr_to_num(data))
78 dev = bitstr_to_num(data[:7])
79 ep = bitstr_to_num(data[7:])
80 data = 'DEV %d EP %d' % (dev, ep)
81 elif pid in ('DATA0', 'DATA1'):
85 tmp += '%02x ' % bitstr_to_num(data[:8])
91 # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001).
92 if sync != '00000001':
93 return 'SYNC INVALID: %s' % sync
95 return pid + ' ' + data
97 class Decoder(srd.Decoder):
100 name = 'USB protocol'
101 longname = 'Universal Serial Bus (LS/FS) protocol'
102 desc = 'USB 1.x (low-speed and full-speed) serial protocol.'
104 inputs = ['usb_signalling']
105 outputs = ['usb_protocol']
109 'signalling': ['Signalling', 'full-speed'],
112 ['Text', 'Human-readable text']
122 def start(self, metadata):
123 self.samplerate = metadata['samplerate']
124 self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_protocol')
125 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_protocol')
130 def decode(self, ss, es, data):
131 (ptype, pdata) = data
133 if ptype == 'PACKET':
134 self.put(0, 0, self.out_ann, [0, [packet_decode(pdata)]])