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 (full-speed) protocol decoder
23 import sigrokdecode as srd
25 # Symbols (used as states of our state machine, too)
27 # (<dp>, <dm>): <state>
36 '10000111': 'OUT', # Tokens
40 '11000011': 'DATA0', # Data
42 '01001011': 'ACK', # Handshake
48 def bitstr_to_num(bitstr):
53 return int(''.join(l), 2)
55 def packet_decode(packet):
58 pid = pids.get(pid, pid)
61 if pid in ('OUT', 'IN', 'SOF', 'SETUP'):
64 data = str(bitstr_to_num(data))
66 dev = bitstr_to_num(data[:7])
67 ep = bitstr_to_num(data[7:])
68 data = 'DEV %d EP %d' % (dev, ep)
69 elif pid in ('DATA0', 'DATA1'):
73 tmp += '%02x ' % bitstr_to_num(data[:8])
79 if sync != '00000001':
80 return 'SYNC INVALID!'
82 return pid + ' ' + data
84 class Decoder(srd.Decoder):
88 longname = 'Universal Serial Bus'
89 desc = 'USB 1.x (full-speed) serial protocol.'
94 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
95 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
100 ['Text', 'Human-readable text']
109 def start(self, metadata):
110 self.samplerate = metadata['samplerate']
112 if self.samplerate < 48000000:
113 raise Exception('Samplerate (%d) not sufficient for USB '
114 'decoding, need at least 48MHz' % self.samplerate)
116 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb')
117 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb')
122 def decode(self, ss, es, data):
123 for (self.samplenum, (dp, dm)) in data:
125 # Note: self.samplenum is the absolute sample number, whereas
126 # self.scount only counts the number of samples since the
127 # last change in the D+/D- lines.
132 # Wait for a symbol change (i.e., change in D+/D- lines).
137 # We ignore single sample width "pulses", i.e., symbol changes
138 # (D+/D- line changes). I sometimes get these with the OLS.
143 # How many bits since the last transition?
144 if self.packet != '' or self.sym != 'J':
145 bitcount = int((self.scount - 1) * 12000000 / self.samplerate)
149 if self.sym == 'SE0':
151 # End-Of-Packet (EOP)
152 self.put(0, 0, self.out_ann,
153 [0, [packet_decode(self.packet), self.packet]])
155 # Longer than EOP, assume reset.
156 self.put(0, 0, self.out_ann, [0, ['RESET']])
162 # Add bits to the packet string.
163 self.packet += '1' * bitcount
165 # Handle bit stuffing.
166 if bitcount < 6 and sym != 'SE0':
169 self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']])