]>
Commit | Line | Data |
---|---|---|
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 (low-speed and full-speed) protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
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! | |
27 | syms = { | |
28 | # (<dp>, <dm>): <symbol/state> | |
29 | (0, 0): 'SE0', | |
30 | (1, 0): 'J', | |
31 | (0, 1): 'K', | |
32 | (1, 1): 'SE1', | |
33 | } | |
34 | ||
35 | # Packet IDs | |
36 | pids = { | |
37 | '10000111': 'OUT', # Tokens | |
38 | '10010110': 'IN', | |
39 | '10100101': 'SOF', | |
40 | '10110100': 'SETUP', | |
41 | '11000011': 'DATA0', # Data | |
42 | '11010010': 'DATA1', | |
43 | '01001011': 'ACK', # Handshake | |
44 | '01011010': 'NAK', | |
45 | '01111000': 'STALL', | |
46 | '01101001': 'NYET', | |
47 | } | |
48 | ||
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': | |
52 | s = syms[dp, dm] | |
53 | if s == 'J': | |
54 | return 'K' | |
55 | elif s == 'K': | |
56 | return 'J' | |
57 | else: | |
58 | return s | |
59 | elif signalling == 'full-speed': | |
60 | return syms[dp, dm] | |
61 | ||
62 | def bitstr_to_num(bitstr): | |
63 | if not bitstr: | |
64 | return 0 | |
65 | l = list(bitstr) | |
66 | l.reverse() | |
67 | return int(''.join(l), 2) | |
68 | ||
69 | def packet_decode(packet): | |
70 | sync = packet[:8] | |
71 | pid = packet[8:16] | |
72 | pid = pids.get(pid, pid) | |
73 | ||
74 | # Remove CRC. | |
75 | if pid in ('OUT', 'IN', 'SOF', 'SETUP'): | |
76 | data = packet[16:-5] | |
77 | if pid == 'SOF': | |
78 | data = str(bitstr_to_num(data)) | |
79 | else: | |
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'): | |
84 | data = packet[16:-16] | |
85 | tmp = '' | |
86 | while data: | |
87 | tmp += '%02x ' % bitstr_to_num(data[:8]) | |
88 | data = data[8:] | |
89 | data = tmp | |
90 | else: | |
91 | data = packet[16:] | |
92 | ||
93 | # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001). | |
94 | if sync != '00000001': | |
95 | return 'SYNC INVALID!' | |
96 | ||
97 | return pid + ' ' + data | |
98 | ||
99 | class Decoder(srd.Decoder): | |
100 | api_version = 1 | |
101 | id = 'usb' | |
102 | name = 'USB' | |
103 | longname = 'Universal Serial Bus (LS/FS)' | |
104 | desc = 'USB 1.x (low-speed and full-speed) serial protocol.' | |
105 | license = 'gplv2+' | |
106 | inputs = ['logic'] | |
107 | outputs = ['usb'] | |
108 | probes = [ | |
109 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
110 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
111 | ] | |
112 | optional_probes = [] | |
113 | options = { | |
114 | 'signalling': ['Signalling', 'full-speed'], | |
115 | } | |
116 | annotations = [ | |
117 | ['Text', 'Human-readable text'] | |
118 | ] | |
119 | ||
120 | def __init__(self): | |
121 | self.sym = 'J' | |
122 | self.samplenum = 0 | |
123 | self.scount = 0 | |
124 | self.packet = '' | |
125 | ||
126 | def start(self, metadata): | |
127 | self.samplerate = metadata['samplerate'] | |
128 | ||
129 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb') | |
130 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb') | |
131 | ||
132 | def report(self): | |
133 | pass | |
134 | ||
135 | def decode(self, ss, es, data): | |
136 | for (self.samplenum, (dp, dm)) in data: | |
137 | ||
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. | |
141 | self.scount += 1 | |
142 | ||
143 | sym = get_sym(self.options['signalling'], dp, dm) | |
144 | ||
145 | # Wait for a symbol change (i.e., change in D+/D- lines). | |
146 | if sym == self.sym: | |
147 | continue | |
148 | ||
149 | if self.scount == 1: | |
150 | # We ignore single sample width "pulses", i.e., symbol changes | |
151 | # (D+/D- line changes). I sometimes get these with the OLS. | |
152 | self.sym = sym | |
153 | self.scount = 0 | |
154 | continue | |
155 | ||
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) | |
163 | else: | |
164 | bitcount = 0 | |
165 | ||
166 | if self.sym == 'SE0': | |
167 | if bitcount == 1: | |
168 | # End-Of-Packet (EOP) | |
169 | self.put(0, 0, self.out_ann, | |
170 | [0, [packet_decode(self.packet), self.packet]]) | |
171 | else: | |
172 | # Longer than EOP, assume reset. | |
173 | self.put(0, 0, self.out_ann, [0, ['RESET']]) | |
174 | self.scount = 0 | |
175 | self.sym = sym | |
176 | self.packet = '' | |
177 | continue | |
178 | ||
179 | # Add bits to the packet string. | |
180 | self.packet += '1' * bitcount | |
181 | ||
182 | # Handle bit stuffing. | |
183 | if bitcount < 6 and sym != 'SE0': | |
184 | self.packet += '0' | |
185 | elif bitcount > 6: | |
186 | self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']]) | |
187 | ||
188 | self.scount = 0 | |
189 | self.sym = sym | |
190 |