]>
Commit | Line | Data |
---|---|---|
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 | ||
c2937021 | 21 | # USB (low-speed and full-speed) protocol decoder |
03325c23 | 22 | |
677d597b | 23 | import sigrokdecode as srd |
03325c23 | 24 | |
c2937021 UH |
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! | |
03325c23 | 27 | syms = { |
c2937021 | 28 | # (<dp>, <dm>): <symbol/state> |
2b716038 UH |
29 | (0, 0): 'SE0', |
30 | (1, 0): 'J', | |
31 | (0, 1): 'K', | |
32 | (1, 1): 'SE1', | |
03325c23 GM |
33 | } |
34 | ||
3cf771a5 UH |
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. | |
bbe99ad5 | 41 | pids = { |
3cf771a5 UH |
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'], | |
bbe99ad5 UH |
67 | } |
68 | ||
c2937021 UH |
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 | ||
03325c23 | 82 | def bitstr_to_num(bitstr): |
eb7082c9 UH |
83 | if not bitstr: |
84 | return 0 | |
03325c23 GM |
85 | l = list(bitstr) |
86 | l.reverse() | |
87 | return int(''.join(l), 2) | |
88 | ||
89 | def packet_decode(packet): | |
03325c23 GM |
90 | sync = packet[:8] |
91 | pid = packet[8:16] | |
3cf771a5 | 92 | pid = pids.get(pid, (pid, ''))[0] |
eb7082c9 | 93 | |
9611dc5f | 94 | # Remove CRC. |
03325c23 GM |
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:]) | |
eb7082c9 | 102 | data = 'DEV %d EP %d' % (dev, ep) |
03325c23 GM |
103 | elif pid in ('DATA0', 'DATA1'): |
104 | data = packet[16:-16] | |
eb7082c9 | 105 | tmp = '' |
03325c23 | 106 | while data: |
0690a587 | 107 | tmp += '%02x ' % bitstr_to_num(data[:8]) |
03325c23 GM |
108 | data = data[8:] |
109 | data = tmp | |
110 | else: | |
111 | data = packet[16:] | |
112 | ||
c2937021 | 113 | # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001). |
eb7082c9 UH |
114 | if sync != '00000001': |
115 | return 'SYNC INVALID!' | |
9611dc5f | 116 | |
03325c23 GM |
117 | return pid + ' ' + data |
118 | ||
677d597b | 119 | class Decoder(srd.Decoder): |
a2c2afd9 | 120 | api_version = 1 |
67e847fd | 121 | id = 'usb' |
03325c23 | 122 | name = 'USB' |
c2937021 UH |
123 | longname = 'Universal Serial Bus (LS/FS)' |
124 | desc = 'USB 1.x (low-speed and full-speed) serial protocol.' | |
03325c23 GM |
125 | license = 'gplv2+' |
126 | inputs = ['logic'] | |
127 | outputs = ['usb'] | |
ea42f53e UH |
128 | probes = [ |
129 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
130 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
131 | ] | |
b77614bc | 132 | optional_probes = [] |
c2937021 UH |
133 | options = { |
134 | 'signalling': ['Signalling', 'full-speed'], | |
135 | } | |
0690a587 | 136 | annotations = [ |
ee3e279c | 137 | ['Text', 'Human-readable text'] |
0690a587 | 138 | ] |
03325c23 GM |
139 | |
140 | def __init__(self): | |
b189c8b1 UH |
141 | self.sym = 'J' |
142 | self.samplenum = 0 | |
143 | self.scount = 0 | |
144 | self.packet = '' | |
03325c23 GM |
145 | |
146 | def start(self, metadata): | |
5a2c4dc4 | 147 | self.samplerate = metadata['samplerate'] |
0690a587 | 148 | |
b189c8b1 UH |
149 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb') |
150 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb') | |
03325c23 | 151 | |
decde15e UH |
152 | def report(self): |
153 | pass | |
b327337f | 154 | |
decde15e | 155 | def decode(self, ss, es, data): |
b189c8b1 UH |
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 | |
03325c23 | 162 | |
c2937021 | 163 | sym = get_sym(self.options['signalling'], dp, dm) |
0690a587 | 164 | |
b189c8b1 | 165 | # Wait for a symbol change (i.e., change in D+/D- lines). |
03325c23 GM |
166 | if sym == self.sym: |
167 | continue | |
168 | ||
2dc6d41c UH |
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 | |
03325c23 | 175 | |
9611dc5f | 176 | # How many bits since the last transition? |
2b716038 | 177 | if self.packet != '' or self.sym != 'J': |
c2937021 UH |
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) | |
03325c23 GM |
183 | else: |
184 | bitcount = 0 | |
185 | ||
2b716038 | 186 | if self.sym == 'SE0': |
03325c23 | 187 | if bitcount == 1: |
9611dc5f | 188 | # End-Of-Packet (EOP) |
0690a587 UH |
189 | self.put(0, 0, self.out_ann, |
190 | [0, [packet_decode(self.packet), self.packet]]) | |
03325c23 | 191 | else: |
9611dc5f | 192 | # Longer than EOP, assume reset. |
0690a587 | 193 | self.put(0, 0, self.out_ann, [0, ['RESET']]) |
b189c8b1 | 194 | self.scount = 0 |
03325c23 GM |
195 | self.sym = sym |
196 | self.packet = '' | |
197 | continue | |
198 | ||
199 | # Add bits to the packet string. | |
200 | self.packet += '1' * bitcount | |
0690a587 | 201 | |
9611dc5f | 202 | # Handle bit stuffing. |
2b716038 | 203 | if bitcount < 6 and sym != 'SE0': |
03325c23 GM |
204 | self.packet += '0' |
205 | elif bitcount > 6: | |
0690a587 | 206 | self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']]) |
9611dc5f | 207 | |
b189c8b1 | 208 | self.scount = 0 |
03325c23 GM |
209 | self.sym = sym |
210 |