]>
Commit | Line | Data |
---|---|---|
2dc6d41c UH |
1 | ## |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz> | |
5 | ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | ## | |
7 | ## This program is free software; you can redistribute it and/or modify | |
8 | ## it under the terms of the GNU General Public License as published by | |
9 | ## the Free Software Foundation; either version 2 of the License, or | |
10 | ## (at your option) any later version. | |
11 | ## | |
12 | ## This program is distributed in the hope that it will be useful, | |
13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ## GNU General Public License for more details. | |
16 | ## | |
17 | ## You should have received a copy of the GNU General Public License | |
18 | ## along with this program; if not, write to the Free Software | |
19 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | ## | |
21 | ||
22 | # USB (low-speed and full-speed) protocol decoder | |
23 | ||
24 | import sigrokdecode as srd | |
25 | ||
26 | # Packet IDs (PIDs). | |
27 | # The first 4 bits are the 'packet type' field, the last 4 bits are the | |
28 | # 'check field' (each bit in the check field must be the inverse of the resp. | |
29 | # bit in the 'packet type' field; if not, that's a 'PID error'). | |
30 | # For the 4-bit strings, the left-most '1' or '0' is the LSB, i.e. it's sent | |
31 | # to the bus first. | |
32 | pids = { | |
33 | # Tokens | |
34 | '10000111': ['OUT', 'Address & EP number in host-to-function transaction'], | |
35 | '10010110': ['IN', 'Address & EP number in function-to-host transaction'], | |
36 | '10100101': ['SOF', 'Start-Of-Frame marker & frame number'], | |
37 | '10110100': ['SETUP', 'Address & EP number in host-to-function transaction for SETUP to a control pipe'], | |
38 | ||
39 | # Data | |
40 | # Note: DATA2 and MDATA are HS-only. | |
41 | '11000011': ['DATA0', 'Data packet PID even'], | |
42 | '11010010': ['DATA1', 'Data packet PID odd'], | |
43 | '11100001': ['DATA2', 'Data packet PID HS, high bandwidth isosynchronous transaction in a microframe'], | |
44 | '11110000': ['MDATA', 'Data packet PID HS for split and high-bandwidth isosynchronous transactions'], | |
45 | ||
46 | # Handshake | |
47 | '01001011': ['ACK', 'Receiver accepts error-free packet'], | |
48 | '01011010': ['NAK', 'Receiver cannot accept or transmitter cannot send'], | |
49 | '01111000': ['STALL', 'EP halted or control pipe request unsupported'], | |
50 | '01101001': ['NYET', 'No response yet from receiver'], | |
51 | ||
52 | # Special | |
53 | '00111100': ['PRE', 'Host-issued preamble; enables downstream bus traffic to low-speed devices'], | |
54 | '00111100': ['ERR', 'Split transaction error handshake'], | |
55 | '00011110': ['SPLIT', 'HS split transaction token'], | |
56 | '00101101': ['PING', 'HS flow control probe for a bulk/control EP'], | |
57 | '00001111': ['Reserved', 'Reserved PID'], | |
58 | } | |
59 | ||
60 | def bitstr_to_num(bitstr): | |
61 | if not bitstr: | |
62 | return 0 | |
63 | l = list(bitstr) | |
64 | l.reverse() | |
65 | return int(''.join(l), 2) | |
66 | ||
67 | def packet_decode(packet): | |
68 | sync = packet[:8] | |
69 | pid = packet[8:16] | |
70 | pid = pids.get(pid, (pid, ''))[0] | |
71 | ||
72 | # Remove CRC. | |
73 | if pid in ('OUT', 'IN', 'SOF', 'SETUP'): | |
74 | data = packet[16:-5] | |
75 | if pid == 'SOF': | |
76 | data = str(bitstr_to_num(data)) | |
77 | else: | |
78 | dev = bitstr_to_num(data[:7]) | |
79 | ep = bitstr_to_num(data[7:]) | |
80 | data = 'DEV %d EP %d' % (dev, ep) | |
81 | elif pid in ('DATA0', 'DATA1'): | |
82 | data = packet[16:-16] | |
83 | tmp = '' | |
84 | while data: | |
85 | tmp += '%02x ' % bitstr_to_num(data[:8]) | |
86 | data = data[8:] | |
87 | data = tmp | |
88 | else: | |
89 | data = packet[16:] | |
90 | ||
91 | # The SYNC pattern for low-speed/full-speed is KJKJKJKK (0001). | |
92 | if sync != '00000001': | |
93 | return 'SYNC INVALID: %s' % sync | |
94 | ||
95 | return pid + ' ' + data | |
96 | ||
97 | class Decoder(srd.Decoder): | |
98 | api_version = 1 | |
99 | id = 'usb_protocol' | |
100 | name = 'USB protocol' | |
101 | longname = 'Universal Serial Bus (LS/FS) protocol' | |
102 | desc = 'USB 1.x (low-speed and full-speed) serial protocol.' | |
103 | license = 'gplv2+' | |
104 | inputs = ['usb_signalling'] | |
105 | outputs = ['usb_protocol'] | |
106 | probes = [] | |
107 | optional_probes = [] | |
108 | options = { | |
109 | 'signalling': ['Signalling', 'full-speed'], | |
110 | } | |
111 | annotations = [ | |
112 | ['Text', 'Human-readable text'] | |
113 | ] | |
114 | ||
115 | def __init__(self): | |
116 | self.sym = 'J' | |
117 | self.samplenum = 0 | |
118 | self.scount = 0 | |
119 | self.packet = '' | |
120 | self.state = 'IDLE' | |
121 | ||
122 | def start(self, metadata): | |
123 | self.samplerate = metadata['samplerate'] | |
124 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_protocol') | |
125 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_protocol') | |
126 | ||
127 | def report(self): | |
128 | pass | |
129 | ||
130 | def decode(self, ss, es, data): | |
131 | (ptype, pdata) = data | |
132 | ||
133 | if ptype == 'PACKET': | |
134 | self.put(0, 0, self.out_ann, [0, [packet_decode(pdata)]]) | |
135 | ||
136 | # TODO. | |
137 |