]>
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 (full-speed) protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
25 | # Symbols (used as states of our state machine, too) | |
26 | syms = { | |
27 | (0, 0): 'SE0', | |
28 | (1, 0): 'J', | |
29 | (0, 1): 'K', | |
30 | (1, 1): 'SE1', | |
31 | } | |
32 | ||
33 | # ... | |
34 | pids = { | |
35 | '10000111': 'OUT', # Tokens | |
36 | '10010110': 'IN', | |
37 | '10100101': 'SOF', | |
38 | '10110100': 'SETUP', | |
39 | '11000011': 'DATA0', # Data | |
40 | '11010010': 'DATA1', | |
41 | '01001011': 'ACK', # Handshake | |
42 | '01011010': 'NAK', | |
43 | '01111000': 'STALL', | |
44 | '01101001': 'NYET', | |
45 | } | |
46 | ||
47 | def bitstr_to_num(bitstr): | |
48 | if not bitstr: | |
49 | return 0 | |
50 | l = list(bitstr) | |
51 | l.reverse() | |
52 | return int(''.join(l), 2) | |
53 | ||
54 | def packet_decode(packet): | |
55 | sync = packet[:8] | |
56 | pid = packet[8:16] | |
57 | pid = pids.get(pid, pid) | |
58 | ||
59 | # Remove CRC. | |
60 | if pid in ('OUT', 'IN', 'SOF', 'SETUP'): | |
61 | data = packet[16:-5] | |
62 | if pid == 'SOF': | |
63 | data = str(bitstr_to_num(data)) | |
64 | else: | |
65 | dev = bitstr_to_num(data[:7]) | |
66 | ep = bitstr_to_num(data[7:]) | |
67 | data = 'DEV %d EP %d' % (dev, ep) | |
68 | ||
69 | elif pid in ('DATA0', 'DATA1'): | |
70 | data = packet[16:-16] | |
71 | tmp = '' | |
72 | while data: | |
73 | tmp += '%02x ' % bitstr_to_num(data[:8]) | |
74 | data = data[8:] | |
75 | data = tmp | |
76 | else: | |
77 | data = packet[16:] | |
78 | ||
79 | if sync != '00000001': | |
80 | return 'SYNC INVALID!' | |
81 | ||
82 | return pid + ' ' + data | |
83 | ||
84 | class Decoder(srd.Decoder): | |
85 | api_version = 1 | |
86 | id = 'usb' | |
87 | name = 'USB' | |
88 | longname = 'Universal Serial Bus' | |
89 | desc = 'Universal Serial Bus' | |
90 | license = 'gplv2+' | |
91 | inputs = ['logic'] | |
92 | outputs = ['usb'] | |
93 | probes = [ | |
94 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
95 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
96 | ] | |
97 | optional_probes = [] | |
98 | options = {} | |
99 | annotations = [ | |
100 | ['TODO', 'TODO'] | |
101 | ] | |
102 | ||
103 | def __init__(self): | |
104 | pass | |
105 | ||
106 | def start(self, metadata): | |
107 | self.samplerate = metadata['samplerate'] | |
108 | ||
109 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb') | |
110 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb') | |
111 | ||
112 | if self.samplerate < 48000000: | |
113 | raise Exception('Samplerate (%d) not sufficient for USB ' | |
114 | 'decoding, need at least 48MHz' % self.samplerate) | |
115 | ||
116 | # Initialise decoder state. | |
117 | self.sym = 'J' | |
118 | self.scount = 0 | |
119 | self.packet = '' | |
120 | ||
121 | def report(self): | |
122 | pass | |
123 | ||
124 | def decode(self, ss, es, data): | |
125 | for (samplenum, (dm, dp)) in data: | |
126 | ||
127 | self.scount += 1 | |
128 | ||
129 | sym = syms[dp, dm] | |
130 | ||
131 | # ... | |
132 | if sym == self.sym: | |
133 | continue | |
134 | ||
135 | if self.scount == 1: | |
136 | # We ignore single sample width pulses. | |
137 | # I sometimes get these with the OLS. | |
138 | self.sym = sym | |
139 | self.scount = 0 | |
140 | continue | |
141 | ||
142 | # How many bits since the last transition? | |
143 | if self.packet != '' or self.sym != 'J': | |
144 | bitcount = int((self.scount - 1) * 12000000 / self.samplerate) | |
145 | else: | |
146 | bitcount = 0 | |
147 | ||
148 | if self.sym == 'SE0': | |
149 | if bitcount == 1: | |
150 | # End-Of-Packet (EOP) | |
151 | self.put(0, 0, self.out_ann, | |
152 | [0, [packet_decode(self.packet), self.packet]]) | |
153 | else: | |
154 | # Longer than EOP, assume reset. | |
155 | self.put(0, 0, self.out_ann, [0, ['RESET']]) | |
156 | self.scount = 0 | |
157 | self.sym = sym | |
158 | self.packet = '' | |
159 | continue | |
160 | ||
161 | # Add bits to the packet string. | |
162 | self.packet += '1' * bitcount | |
163 | ||
164 | # Handle bit stuffing. | |
165 | if bitcount < 6 and sym != 'SE0': | |
166 | self.packet += '0' | |
167 | elif bitcount > 6: | |
168 | self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']]) | |
169 | ||
170 | self.scount = 0 | |
171 | self.sym = sym | |
172 |