]>
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 | ||
c2937021 | 35 | # Packet IDs |
bbe99ad5 UH |
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 | ||
c2937021 UH |
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 | ||
03325c23 | 62 | def bitstr_to_num(bitstr): |
eb7082c9 UH |
63 | if not bitstr: |
64 | return 0 | |
03325c23 GM |
65 | l = list(bitstr) |
66 | l.reverse() | |
67 | return int(''.join(l), 2) | |
68 | ||
69 | def packet_decode(packet): | |
03325c23 GM |
70 | sync = packet[:8] |
71 | pid = packet[8:16] | |
72 | pid = pids.get(pid, pid) | |
eb7082c9 | 73 | |
9611dc5f | 74 | # Remove CRC. |
03325c23 GM |
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:]) | |
eb7082c9 | 82 | data = 'DEV %d EP %d' % (dev, ep) |
03325c23 GM |
83 | elif pid in ('DATA0', 'DATA1'): |
84 | data = packet[16:-16] | |
eb7082c9 | 85 | tmp = '' |
03325c23 | 86 | while data: |
0690a587 | 87 | tmp += '%02x ' % bitstr_to_num(data[:8]) |
03325c23 GM |
88 | data = data[8:] |
89 | data = tmp | |
90 | else: | |
91 | data = packet[16:] | |
92 | ||
c2937021 | 93 | # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001). |
eb7082c9 UH |
94 | if sync != '00000001': |
95 | return 'SYNC INVALID!' | |
9611dc5f | 96 | |
03325c23 GM |
97 | return pid + ' ' + data |
98 | ||
677d597b | 99 | class Decoder(srd.Decoder): |
a2c2afd9 | 100 | api_version = 1 |
67e847fd | 101 | id = 'usb' |
03325c23 | 102 | name = 'USB' |
c2937021 UH |
103 | longname = 'Universal Serial Bus (LS/FS)' |
104 | desc = 'USB 1.x (low-speed and full-speed) serial protocol.' | |
03325c23 GM |
105 | license = 'gplv2+' |
106 | inputs = ['logic'] | |
107 | outputs = ['usb'] | |
ea42f53e UH |
108 | probes = [ |
109 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
110 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
111 | ] | |
b77614bc | 112 | optional_probes = [] |
c2937021 UH |
113 | options = { |
114 | 'signalling': ['Signalling', 'full-speed'], | |
115 | } | |
0690a587 | 116 | annotations = [ |
ee3e279c | 117 | ['Text', 'Human-readable text'] |
0690a587 | 118 | ] |
03325c23 GM |
119 | |
120 | def __init__(self): | |
b189c8b1 UH |
121 | self.sym = 'J' |
122 | self.samplenum = 0 | |
123 | self.scount = 0 | |
124 | self.packet = '' | |
03325c23 GM |
125 | |
126 | def start(self, metadata): | |
5a2c4dc4 | 127 | self.samplerate = metadata['samplerate'] |
0690a587 | 128 | |
b189c8b1 UH |
129 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb') |
130 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb') | |
03325c23 | 131 | |
decde15e UH |
132 | def report(self): |
133 | pass | |
b327337f | 134 | |
decde15e | 135 | def decode(self, ss, es, data): |
b189c8b1 UH |
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 | |
03325c23 | 142 | |
c2937021 | 143 | sym = get_sym(self.options['signalling'], dp, dm) |
0690a587 | 144 | |
b189c8b1 | 145 | # Wait for a symbol change (i.e., change in D+/D- lines). |
03325c23 GM |
146 | if sym == self.sym: |
147 | continue | |
148 | ||
b189c8b1 UH |
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. | |
03325c23 | 152 | self.sym = sym |
b189c8b1 | 153 | self.scount = 0 |
03325c23 GM |
154 | continue |
155 | ||
9611dc5f | 156 | # How many bits since the last transition? |
2b716038 | 157 | if self.packet != '' or self.sym != 'J': |
c2937021 UH |
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) | |
03325c23 GM |
163 | else: |
164 | bitcount = 0 | |
165 | ||
2b716038 | 166 | if self.sym == 'SE0': |
03325c23 | 167 | if bitcount == 1: |
9611dc5f | 168 | # End-Of-Packet (EOP) |
0690a587 UH |
169 | self.put(0, 0, self.out_ann, |
170 | [0, [packet_decode(self.packet), self.packet]]) | |
03325c23 | 171 | else: |
9611dc5f | 172 | # Longer than EOP, assume reset. |
0690a587 | 173 | self.put(0, 0, self.out_ann, [0, ['RESET']]) |
b189c8b1 | 174 | self.scount = 0 |
03325c23 GM |
175 | self.sym = sym |
176 | self.packet = '' | |
177 | continue | |
178 | ||
179 | # Add bits to the packet string. | |
180 | self.packet += '1' * bitcount | |
0690a587 | 181 | |
9611dc5f | 182 | # Handle bit stuffing. |
2b716038 | 183 | if bitcount < 6 and sym != 'SE0': |
03325c23 GM |
184 | self.packet += '0' |
185 | elif bitcount > 6: | |
0690a587 | 186 | self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']]) |
9611dc5f | 187 | |
b189c8b1 | 188 | self.scount = 0 |
03325c23 GM |
189 | self.sym = sym |
190 |