]> sigrok.org Git - libsigrokdecode.git/blame - decoders/usb/usb.py
srd: Cosmetics.
[libsigrokdecode.git] / decoders / usb / usb.py
CommitLineData
03325c23
GM
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5##
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.
10##
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.
15##
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
19##
20
156509ca 21# USB (full-speed) protocol decoder
03325c23 22
677d597b 23import sigrokdecode as srd
03325c23 24
2b716038 25# Symbols (used as states of our state machine, too)
03325c23 26syms = {
b189c8b1 27 # (<dp>, <dm>): <state>
2b716038
UH
28 (0, 0): 'SE0',
29 (1, 0): 'J',
30 (0, 1): 'K',
31 (1, 1): 'SE1',
03325c23
GM
32}
33
bbe99ad5
UH
34# ...
35pids = {
36 '10000111': 'OUT', # Tokens
37 '10010110': 'IN',
38 '10100101': 'SOF',
39 '10110100': 'SETUP',
40 '11000011': 'DATA0', # Data
41 '11010010': 'DATA1',
42 '01001011': 'ACK', # Handshake
43 '01011010': 'NAK',
44 '01111000': 'STALL',
45 '01101001': 'NYET',
46}
47
03325c23 48def bitstr_to_num(bitstr):
eb7082c9
UH
49 if not bitstr:
50 return 0
03325c23
GM
51 l = list(bitstr)
52 l.reverse()
53 return int(''.join(l), 2)
54
55def packet_decode(packet):
03325c23
GM
56 sync = packet[:8]
57 pid = packet[8:16]
58 pid = pids.get(pid, pid)
eb7082c9 59
9611dc5f 60 # Remove CRC.
03325c23
GM
61 if pid in ('OUT', 'IN', 'SOF', 'SETUP'):
62 data = packet[16:-5]
63 if pid == 'SOF':
64 data = str(bitstr_to_num(data))
65 else:
66 dev = bitstr_to_num(data[:7])
67 ep = bitstr_to_num(data[7:])
eb7082c9 68 data = 'DEV %d EP %d' % (dev, ep)
03325c23
GM
69 elif pid in ('DATA0', 'DATA1'):
70 data = packet[16:-16]
eb7082c9 71 tmp = ''
03325c23 72 while data:
0690a587 73 tmp += '%02x ' % bitstr_to_num(data[:8])
03325c23
GM
74 data = data[8:]
75 data = tmp
76 else:
77 data = packet[16:]
78
eb7082c9
UH
79 if sync != '00000001':
80 return 'SYNC INVALID!'
9611dc5f 81
03325c23
GM
82 return pid + ' ' + data
83
677d597b 84class Decoder(srd.Decoder):
a2c2afd9 85 api_version = 1
67e847fd 86 id = 'usb'
03325c23 87 name = 'USB'
0690a587 88 longname = 'Universal Serial Bus'
a465436e 89 desc = 'USB 1.x (full-speed) serial protocol.'
03325c23
GM
90 license = 'gplv2+'
91 inputs = ['logic']
92 outputs = ['usb']
ea42f53e
UH
93 probes = [
94 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
95 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
96 ]
b77614bc 97 optional_probes = []
03325c23 98 options = {}
0690a587 99 annotations = [
ee3e279c 100 ['Text', 'Human-readable text']
0690a587 101 ]
03325c23
GM
102
103 def __init__(self):
b189c8b1
UH
104 self.sym = 'J'
105 self.samplenum = 0
106 self.scount = 0
107 self.packet = ''
03325c23
GM
108
109 def start(self, metadata):
5a2c4dc4 110 self.samplerate = metadata['samplerate']
0690a587 111
5a2c4dc4
UH
112 if self.samplerate < 48000000:
113 raise Exception('Samplerate (%d) not sufficient for USB '
114 'decoding, need at least 48MHz' % self.samplerate)
0690a587 115
b189c8b1
UH
116 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb')
117 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb')
03325c23 118
decde15e
UH
119 def report(self):
120 pass
b327337f 121
decde15e 122 def decode(self, ss, es, data):
b189c8b1
UH
123 for (self.samplenum, (dp, dm)) in data:
124
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.
128 self.scount += 1
03325c23 129
ea42f53e 130 sym = syms[dp, dm]
0690a587 131
b189c8b1 132 # Wait for a symbol change (i.e., change in D+/D- lines).
03325c23
GM
133 if sym == self.sym:
134 continue
135
b189c8b1
UH
136 if self.scount == 1:
137 # We ignore single sample width "pulses", i.e., symbol changes
138 # (D+/D- line changes). I sometimes get these with the OLS.
03325c23 139 self.sym = sym
b189c8b1 140 self.scount = 0
03325c23
GM
141 continue
142
9611dc5f 143 # How many bits since the last transition?
2b716038 144 if self.packet != '' or self.sym != 'J':
b189c8b1 145 bitcount = int((self.scount - 1) * 12000000 / self.samplerate)
03325c23
GM
146 else:
147 bitcount = 0
148
2b716038 149 if self.sym == 'SE0':
03325c23 150 if bitcount == 1:
9611dc5f 151 # End-Of-Packet (EOP)
0690a587
UH
152 self.put(0, 0, self.out_ann,
153 [0, [packet_decode(self.packet), self.packet]])
03325c23 154 else:
9611dc5f 155 # Longer than EOP, assume reset.
0690a587 156 self.put(0, 0, self.out_ann, [0, ['RESET']])
b189c8b1 157 self.scount = 0
03325c23
GM
158 self.sym = sym
159 self.packet = ''
160 continue
161
162 # Add bits to the packet string.
163 self.packet += '1' * bitcount
0690a587 164
9611dc5f 165 # Handle bit stuffing.
2b716038 166 if bitcount < 6 and sym != 'SE0':
03325c23
GM
167 self.packet += '0'
168 elif bitcount > 6:
0690a587 169 self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']])
9611dc5f 170
b189c8b1 171 self.scount = 0
03325c23
GM
172 self.sym = sym
173