]>
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 (PIDs). | |
36 | # The first 4 bits are the 'packet type' field, the last 4 bits are the | |
37 | # 'check field' (each bit in the check field must be the inverse of the resp. | |
38 | # bit in the 'packet type' field; if not, that's a 'PID error'). | |
39 | # For the 4-bit strings, the left-most '1' or '0' is the LSB, i.e. it's sent | |
40 | # to the bus first. | |
41 | pids = { | |
42 | # Tokens | |
43 | '10000111': ['OUT', 'Address & EP number in host-to-function transaction'], | |
44 | '10010110': ['IN', 'Address & EP number in function-to-host transaction'], | |
45 | '10100101': ['SOF', 'Start-Of-Frame marker & frame number'], | |
46 | '10110100': ['SETUP', 'Address & EP number in host-to-function transaction for SETUP to a control pipe'], | |
47 | ||
48 | # Data | |
49 | # Note: DATA2 and MDATA are HS-only. | |
50 | '11000011': ['DATA0', 'Data packet PID even'], | |
51 | '11010010': ['DATA1', 'Data packet PID odd'], | |
52 | '11100001': ['DATA2', 'Data packet PID HS, high bandwidth isosynchronous transaction in a microframe'], | |
53 | '11110000': ['MDATA', 'Data packet PID HS for split and high-bandwidth isosynchronous transactions'], | |
54 | ||
55 | # Handshake | |
56 | '01001011': ['ACK', 'Receiver accepts error-free packet'], | |
57 | '01011010': ['NAK', 'Receiver cannot accept or transmitter cannot send'], | |
58 | '01111000': ['STALL', 'EP halted or control pipe request unsupported'], | |
59 | '01101001': ['NYET', 'No response yet from receiver'], | |
60 | ||
61 | # Special | |
62 | '00111100': ['PRE', 'Host-issued preamble; enables downstream bus traffic to low-speed devices'], | |
63 | '00111100': ['ERR', 'Split transaction error handshake'], | |
64 | '00011110': ['SPLIT', 'HS split transaction token'], | |
65 | '00101101': ['PING', 'HS flow control probe for a bulk/control EP'], | |
66 | '00001111': ['Reserved', 'Reserved PID'], | |
67 | } | |
68 | ||
69 | def get_sym(signalling, dp, dm): | |
70 | # Note: Low-speed J and K are inverted compared to the full-speed J and K! | |
71 | if signalling == 'low-speed': | |
72 | s = syms[dp, dm] | |
73 | if s == 'J': | |
74 | return 'K' | |
75 | elif s == 'K': | |
76 | return 'J' | |
77 | else: | |
78 | return s | |
79 | elif signalling == 'full-speed': | |
80 | return syms[dp, dm] | |
81 | ||
82 | def bitstr_to_num(bitstr): | |
83 | if not bitstr: | |
84 | return 0 | |
85 | l = list(bitstr) | |
86 | l.reverse() | |
87 | return int(''.join(l), 2) | |
88 | ||
89 | def packet_decode(packet): | |
90 | sync = packet[:8] | |
91 | pid = packet[8:16] | |
92 | pid = pids.get(pid, (pid, ''))[0] | |
93 | ||
94 | # Remove CRC. | |
95 | if pid in ('OUT', 'IN', 'SOF', 'SETUP'): | |
96 | data = packet[16:-5] | |
97 | if pid == 'SOF': | |
98 | data = str(bitstr_to_num(data)) | |
99 | else: | |
100 | dev = bitstr_to_num(data[:7]) | |
101 | ep = bitstr_to_num(data[7:]) | |
102 | data = 'DEV %d EP %d' % (dev, ep) | |
103 | elif pid in ('DATA0', 'DATA1'): | |
104 | data = packet[16:-16] | |
105 | tmp = '' | |
106 | while data: | |
107 | tmp += '%02x ' % bitstr_to_num(data[:8]) | |
108 | data = data[8:] | |
109 | data = tmp | |
110 | else: | |
111 | data = packet[16:] | |
112 | ||
113 | # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001). | |
114 | if sync != '00000001': | |
115 | return 'SYNC INVALID!' | |
116 | ||
117 | return pid + ' ' + data | |
118 | ||
119 | class Decoder(srd.Decoder): | |
120 | api_version = 1 | |
121 | id = 'usb' | |
122 | name = 'USB' | |
123 | longname = 'Universal Serial Bus (LS/FS)' | |
124 | desc = 'USB 1.x (low-speed and full-speed) serial protocol.' | |
125 | license = 'gplv2+' | |
126 | inputs = ['logic'] | |
127 | outputs = ['usb'] | |
128 | probes = [ | |
129 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
130 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
131 | ] | |
132 | optional_probes = [] | |
133 | options = { | |
134 | 'signalling': ['Signalling', 'full-speed'], | |
135 | } | |
136 | annotations = [ | |
137 | ['Text', 'Human-readable text'] | |
138 | ] | |
139 | ||
140 | def __init__(self): | |
141 | self.sym = 'J' | |
142 | self.samplenum = 0 | |
143 | self.scount = 0 | |
144 | self.packet = '' | |
145 | ||
146 | def start(self, metadata): | |
147 | self.samplerate = metadata['samplerate'] | |
148 | ||
149 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb') | |
150 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb') | |
151 | ||
152 | def report(self): | |
153 | pass | |
154 | ||
155 | def decode(self, ss, es, data): | |
156 | for (self.samplenum, (dp, dm)) in data: | |
157 | ||
158 | # Note: self.samplenum is the absolute sample number, whereas | |
159 | # self.scount only counts the number of samples since the | |
160 | # last change in the D+/D- lines. | |
161 | self.scount += 1 | |
162 | ||
163 | sym = get_sym(self.options['signalling'], dp, dm) | |
164 | ||
165 | # Wait for a symbol change (i.e., change in D+/D- lines). | |
166 | if sym == self.sym: | |
167 | continue | |
168 | ||
169 | # if self.scount == 1: | |
170 | # # We ignore single sample width "pulses", i.e., symbol changes | |
171 | # # (D+/D- line changes). I sometimes get these with the OLS. | |
172 | # self.sym = sym | |
173 | # self.scount = 0 | |
174 | # continue | |
175 | ||
176 | # How many bits since the last transition? | |
177 | if self.packet != '' or self.sym != 'J': | |
178 | if self.options['signalling'] == 'low-speed': | |
179 | bitrate = 1500000 # 1.5Mb/s (+/- 1.5%) | |
180 | elif self.options['signalling'] == 'full-speed': | |
181 | bitrate = 12000000 # 12Mb/s (+/- 0.25%) | |
182 | bitcount = int((self.scount - 1) * bitrate / self.samplerate) | |
183 | else: | |
184 | bitcount = 0 | |
185 | ||
186 | if self.sym == 'SE0': | |
187 | if bitcount == 1: | |
188 | # End-Of-Packet (EOP) | |
189 | self.put(0, 0, self.out_ann, | |
190 | [0, [packet_decode(self.packet), self.packet]]) | |
191 | else: | |
192 | # Longer than EOP, assume reset. | |
193 | self.put(0, 0, self.out_ann, [0, ['RESET']]) | |
194 | self.scount = 0 | |
195 | self.sym = sym | |
196 | self.packet = '' | |
197 | continue | |
198 | ||
199 | # Add bits to the packet string. | |
200 | self.packet += '1' * bitcount | |
201 | ||
202 | # Handle bit stuffing. | |
203 | if bitcount < 6 and sym != 'SE0': | |
204 | self.packet += '0' | |
205 | elif bitcount > 6: | |
206 | self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']]) | |
207 | ||
208 | self.scount = 0 | |
209 | self.sym = sym | |
210 |