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>
37 '10000111': 'OUT', # Tokens
41 '11000011': 'DATA0', # Data
43 '01001011': 'ACK', # Handshake
49 def get_sym(signalling, dp, dm):
50 # Note: Low-speed J and K are inverted compared to the full-speed J and K!
51 if signalling == 'low-speed':
59 elif signalling == 'full-speed':
62 def bitstr_to_num(bitstr):
67 return int(''.join(l), 2)
69 def packet_decode(packet):
72 pid = pids.get(pid, pid)
75 if pid in ('OUT', 'IN', 'SOF', 'SETUP'):
78 data = str(bitstr_to_num(data))
80 dev = bitstr_to_num(data[:7])
81 ep = bitstr_to_num(data[7:])
82 data = 'DEV %d EP %d' % (dev, ep)
83 elif pid in ('DATA0', 'DATA1'):
87 tmp += '%02x ' % bitstr_to_num(data[:8])
93 # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001).
94 if sync != '00000001':
95 return 'SYNC INVALID!'
97 return pid + ' ' + data
99 class Decoder(srd.Decoder):
103 longname = 'Universal Serial Bus (LS/FS)'
104 desc = 'USB 1.x (low-speed and full-speed) serial protocol.'
109 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
110 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
114 'signalling': ['Signalling', 'full-speed'],
117 ['Text', 'Human-readable text']
126 def start(self, metadata):
127 self.samplerate = metadata['samplerate']
129 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb')
130 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb')
135 def decode(self, ss, es, data):
136 for (self.samplenum, (dp, dm)) in data:
138 # Note: self.samplenum is the absolute sample number, whereas
139 # self.scount only counts the number of samples since the
140 # last change in the D+/D- lines.
143 sym = get_sym(self.options['signalling'], dp, dm)
145 # Wait for a symbol change (i.e., change in D+/D- lines).
150 # We ignore single sample width "pulses", i.e., symbol changes
151 # (D+/D- line changes). I sometimes get these with the OLS.
156 # How many bits since the last transition?
157 if self.packet != '' or self.sym != 'J':
158 if self.options['signalling'] == 'low-speed':
159 bitrate = 1500000 # 1.5Mb/s (+/- 1.5%)
160 elif self.options['signalling'] == 'full-speed':
161 bitrate = 12000000 # 12Mb/s (+/- 0.25%)
162 bitcount = int((self.scount - 1) * bitrate / self.samplerate)
166 if self.sym == 'SE0':
168 # End-Of-Packet (EOP)
169 self.put(0, 0, self.out_ann,
170 [0, [packet_decode(self.packet), self.packet]])
172 # Longer than EOP, assume reset.
173 self.put(0, 0, self.out_ann, [0, ['RESET']])
179 # Add bits to the packet string.
180 self.packet += '1' * bitcount
182 # Handle bit stuffing.
183 if bitcount < 6 and sym != 'SE0':
186 self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']])