]>
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 | ||
156509ca | 21 | # USB (full-speed) protocol decoder |
03325c23 | 22 | |
677d597b | 23 | import sigrokdecode as srd |
03325c23 | 24 | |
9611dc5f | 25 | # States |
03325c23 | 26 | SE0, J, K, SE1 = 0, 1, 2, 3 |
0690a587 UH |
27 | |
28 | # ... | |
03325c23 | 29 | syms = { |
c66baa8c UH |
30 | (0, 0): SE0, |
31 | (1, 0): J, | |
32 | (0, 1): K, | |
33 | (1, 1): SE1, | |
03325c23 GM |
34 | } |
35 | ||
bbe99ad5 UH |
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 | ||
03325c23 | 50 | def bitstr_to_num(bitstr): |
eb7082c9 UH |
51 | if not bitstr: |
52 | return 0 | |
03325c23 GM |
53 | l = list(bitstr) |
54 | l.reverse() | |
55 | return int(''.join(l), 2) | |
56 | ||
57 | def packet_decode(packet): | |
03325c23 GM |
58 | sync = packet[:8] |
59 | pid = packet[8:16] | |
60 | pid = pids.get(pid, pid) | |
eb7082c9 | 61 | |
9611dc5f | 62 | # Remove CRC. |
03325c23 GM |
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:]) | |
eb7082c9 | 70 | data = 'DEV %d EP %d' % (dev, ep) |
03325c23 GM |
71 | |
72 | elif pid in ('DATA0', 'DATA1'): | |
73 | data = packet[16:-16] | |
eb7082c9 | 74 | tmp = '' |
03325c23 | 75 | while data: |
0690a587 | 76 | tmp += '%02x ' % bitstr_to_num(data[:8]) |
03325c23 GM |
77 | data = data[8:] |
78 | data = tmp | |
79 | else: | |
80 | data = packet[16:] | |
81 | ||
eb7082c9 UH |
82 | if sync != '00000001': |
83 | return 'SYNC INVALID!' | |
9611dc5f | 84 | |
03325c23 GM |
85 | return pid + ' ' + data |
86 | ||
677d597b | 87 | class Decoder(srd.Decoder): |
a2c2afd9 | 88 | api_version = 1 |
67e847fd | 89 | id = 'usb' |
03325c23 | 90 | name = 'USB' |
0690a587 | 91 | longname = 'Universal Serial Bus' |
9a12a6e7 | 92 | desc = 'Universal Serial Bus' |
03325c23 | 93 | longdesc = '...longdesc...' |
03325c23 GM |
94 | license = 'gplv2+' |
95 | inputs = ['logic'] | |
96 | outputs = ['usb'] | |
ea42f53e UH |
97 | probes = [ |
98 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
99 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
100 | ] | |
b77614bc | 101 | optional_probes = [] |
03325c23 | 102 | options = {} |
0690a587 UH |
103 | annotations = [ |
104 | ['TODO', 'TODO'] | |
105 | ] | |
03325c23 GM |
106 | |
107 | def __init__(self): | |
d0e93c76 | 108 | pass |
03325c23 GM |
109 | |
110 | def start(self, metadata): | |
03325c23 | 111 | self.rate = metadata['samplerate'] |
0690a587 | 112 | |
56202222 UH |
113 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb') |
114 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb') | |
0690a587 | 115 | |
03325c23 | 116 | if self.rate < 48000000: |
decde15e UH |
117 | raise Exception('Sample rate (%d) not sufficient for USB ' |
118 | 'decoding, need at least 48MHz' % self.rate) | |
0690a587 | 119 | |
9611dc5f | 120 | # Initialise decoder state. |
03325c23 GM |
121 | self.sym = J |
122 | self.scount = 0 | |
123 | self.packet = '' | |
124 | ||
decde15e UH |
125 | def report(self): |
126 | pass | |
b327337f | 127 | |
decde15e | 128 | def decode(self, ss, es, data): |
0690a587 | 129 | for (samplenum, (dm, dp)) in data: |
03325c23 GM |
130 | |
131 | self.scount += 1 | |
132 | ||
ea42f53e | 133 | sym = syms[dp, dm] |
0690a587 UH |
134 | |
135 | # ... | |
03325c23 GM |
136 | if sym == self.sym: |
137 | continue | |
138 | ||
139 | if self.scount == 1: | |
9611dc5f UH |
140 | # We ignore single sample width pulses. |
141 | # I sometimes get these with the OLS. | |
03325c23 GM |
142 | self.sym = sym |
143 | self.scount = 0 | |
144 | continue | |
145 | ||
9611dc5f | 146 | # How many bits since the last transition? |
0690a587 | 147 | if self.packet != '' or self.sym != J: |
b327337f | 148 | bitcount = int((self.scount - 1) * 12000000 / self.rate) |
03325c23 GM |
149 | else: |
150 | bitcount = 0 | |
151 | ||
152 | if self.sym == SE0: | |
153 | if bitcount == 1: | |
9611dc5f | 154 | # End-Of-Packet (EOP) |
0690a587 UH |
155 | self.put(0, 0, self.out_ann, |
156 | [0, [packet_decode(self.packet), self.packet]]) | |
03325c23 | 157 | else: |
9611dc5f | 158 | # Longer than EOP, assume reset. |
0690a587 | 159 | self.put(0, 0, self.out_ann, [0, ['RESET']]) |
03325c23 GM |
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 | |
0690a587 | 167 | |
9611dc5f | 168 | # Handle bit stuffing. |
03325c23 GM |
169 | if bitcount < 6 and sym != SE0: |
170 | self.packet += '0' | |
171 | elif bitcount > 6: | |
0690a587 | 172 | self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']]) |
9611dc5f | 173 | |
03325c23 GM |
174 | self.scount = 0 |
175 | self.sym = sym | |
176 |