2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
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.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 # USB (low-speed and full-speed) protocol decoder
23 import sigrokdecode as srd
25 # Full-speed symbols (used as states of our state machine, too).
26 # Note: Low-speed J and K are inverted compared to the full-speed J and K!
28 # (<dp>, <dm>): <symbol/state>
36 # The first 4 bits are the 'packet type' field, the last 4 bits are the
37 # 'check field' (each bit in the check field must be the inverse of the resp.
38 # bit in the 'packet type' field; if not, that's a 'PID error').
39 # For the 4-bit strings, the left-most '1' or '0' is the LSB, i.e. it's sent
43 '10000111': ['OUT', 'Address & EP number in host-to-function transaction'],
44 '10010110': ['IN', 'Address & EP number in function-to-host transaction'],
45 '10100101': ['SOF', 'Start-Of-Frame marker & frame number'],
46 '10110100': ['SETUP', 'Address & EP number in host-to-function transaction for SETUP to a control pipe'],
49 # Note: DATA2 and MDATA are HS-only.
50 '11000011': ['DATA0', 'Data packet PID even'],
51 '11010010': ['DATA1', 'Data packet PID odd'],
52 '11100001': ['DATA2', 'Data packet PID HS, high bandwidth isosynchronous transaction in a microframe'],
53 '11110000': ['MDATA', 'Data packet PID HS for split and high-bandwidth isosynchronous transactions'],
56 '01001011': ['ACK', 'Receiver accepts error-free packet'],
57 '01011010': ['NAK', 'Receiver cannot accept or transmitter cannot send'],
58 '01111000': ['STALL', 'EP halted or control pipe request unsupported'],
59 '01101001': ['NYET', 'No response yet from receiver'],
62 '00111100': ['PRE', 'Host-issued preamble; enables downstream bus traffic to low-speed devices'],
63 '00111100': ['ERR', 'Split transaction error handshake'],
64 '00011110': ['SPLIT', 'HS split transaction token'],
65 '00101101': ['PING', 'HS flow control probe for a bulk/control EP'],
66 '00001111': ['Reserved', 'Reserved PID'],
69 def get_sym(signalling, dp, dm):
70 # Note: Low-speed J and K are inverted compared to the full-speed J and K!
71 if signalling == 'low-speed':
79 elif signalling == 'full-speed':
82 def bitstr_to_num(bitstr):
87 return int(''.join(l), 2)
89 def packet_decode(packet):
92 pid = pids.get(pid, (pid, ''))[0]
95 if pid in ('OUT', 'IN', 'SOF', 'SETUP'):
98 data = str(bitstr_to_num(data))
100 dev = bitstr_to_num(data[:7])
101 ep = bitstr_to_num(data[7:])
102 data = 'DEV %d EP %d' % (dev, ep)
103 elif pid in ('DATA0', 'DATA1'):
104 data = packet[16:-16]
107 tmp += '%02x ' % bitstr_to_num(data[:8])
113 # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001).
114 if sync != '00000001':
115 return 'SYNC INVALID!'
117 return pid + ' ' + data
119 class Decoder(srd.Decoder):
123 longname = 'Universal Serial Bus (LS/FS)'
124 desc = 'USB 1.x (low-speed and full-speed) serial protocol.'
129 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
130 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
134 'signalling': ['Signalling', 'full-speed'],
137 ['Text', 'Human-readable text']
146 def start(self, metadata):
147 self.samplerate = metadata['samplerate']
149 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb')
150 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb')
155 def decode(self, ss, es, data):
156 for (self.samplenum, (dp, dm)) in data:
158 # Note: self.samplenum is the absolute sample number, whereas
159 # self.scount only counts the number of samples since the
160 # last change in the D+/D- lines.
163 sym = get_sym(self.options['signalling'], dp, dm)
165 # Wait for a symbol change (i.e., change in D+/D- lines).
169 # if self.scount == 1:
170 # # We ignore single sample width "pulses", i.e., symbol changes
171 # # (D+/D- line changes). I sometimes get these with the OLS.
176 # How many bits since the last transition?
177 if self.packet != '' or self.sym != 'J':
178 if self.options['signalling'] == 'low-speed':
179 bitrate = 1500000 # 1.5Mb/s (+/- 1.5%)
180 elif self.options['signalling'] == 'full-speed':
181 bitrate = 12000000 # 12Mb/s (+/- 0.25%)
182 bitcount = int((self.scount - 1) * bitrate / self.samplerate)
186 if self.sym == 'SE0':
188 # End-Of-Packet (EOP)
189 self.put(0, 0, self.out_ann,
190 [0, [packet_decode(self.packet), self.packet]])
192 # Longer than EOP, assume reset.
193 self.put(0, 0, self.out_ann, [0, ['RESET']])
199 # Add bits to the packet string.
200 self.packet += '1' * bitcount
202 # Handle bit stuffing.
203 if bitcount < 6 and sym != 'SE0':
206 self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']])