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