]>
Commit | Line | Data |
---|---|---|
2dc6d41c | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
2dc6d41c UH |
3 | ## |
4 | ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz> | |
7d4b5fac | 5 | ## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> |
2dc6d41c UH |
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 signalling (low-speed and full-speed) protocol decoder | |
23 | ||
24 | import sigrokdecode as srd | |
25 | ||
d1970f14 | 26 | # Low-/full-speed symbols. |
2dc6d41c | 27 | # Note: Low-speed J and K are inverted compared to the full-speed J and K! |
7dc75721 UH |
28 | symbols = { |
29 | 'low-speed': { | |
2dc6d41c UH |
30 | # (<dp>, <dm>): <symbol/state> |
31 | (0, 0): 'SE0', | |
32 | (1, 0): 'K', | |
33 | (0, 1): 'J', | |
34 | (1, 1): 'SE1', | |
7dc75721 UH |
35 | }, |
36 | 'full-speed': { | |
2dc6d41c UH |
37 | # (<dp>, <dm>): <symbol/state> |
38 | (0, 0): 'SE0', | |
39 | (1, 0): 'J', | |
40 | (0, 1): 'K', | |
41 | (1, 1): 'SE1', | |
7dc75721 | 42 | }, |
2dc6d41c UH |
43 | } |
44 | ||
5bb55598 UH |
45 | bitrates = { |
46 | 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%) | |
47 | 'full-speed': 12000000, # 12Mb/s (+/- 0.25%) | |
48 | } | |
49 | ||
2dc6d41c UH |
50 | class Decoder(srd.Decoder): |
51 | api_version = 1 | |
52 | id = 'usb_signalling' | |
53 | name = 'USB signalling' | |
54 | longname = 'Universal Serial Bus (LS/FS) signalling' | |
9e1437a0 | 55 | desc = 'USB (low-speed and full-speed) signalling protocol.' |
2dc6d41c UH |
56 | license = 'gplv2+' |
57 | inputs = ['logic'] | |
58 | outputs = ['usb_signalling'] | |
59 | probes = [ | |
60 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, | |
61 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
62 | ] | |
63 | optional_probes = [] | |
64 | options = { | |
65 | 'signalling': ['Signalling', 'full-speed'], | |
66 | } | |
67 | annotations = [ | |
edad8134 UH |
68 | ['symbol', 'Symbol'], |
69 | ['sop', 'Start of packet (SOP)'], | |
70 | ['eop', 'End of packet (EOP)'], | |
71 | ['bit', 'Bit'], | |
72 | ['stuffbit', 'Stuff bit'], | |
73 | ['packet', 'Packet'], | |
2dc6d41c UH |
74 | ] |
75 | ||
76 | def __init__(self): | |
d1970f14 | 77 | self.oldsym = 'J' # The "idle" state is J. |
fdd5ee5e UH |
78 | self.ss_sop = None |
79 | self.ss_block = None | |
2dc6d41c | 80 | self.samplenum = 0 |
2dc6d41c UH |
81 | self.packet = '' |
82 | self.syms = [] | |
5bb55598 UH |
83 | self.bitrate = None |
84 | self.bitwidth = None | |
d1970f14 UH |
85 | self.bitnum = 0 |
86 | self.samplenum_target = None | |
2fcd7c22 | 87 | self.oldpins = None |
d1970f14 UH |
88 | self.consecutive_ones = 0 |
89 | self.state = 'IDLE' | |
2dc6d41c UH |
90 | |
91 | def start(self, metadata): | |
2dc6d41c UH |
92 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_signalling') |
93 | self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_signalling') | |
5bb55598 UH |
94 | self.bitrate = bitrates[self.options['signalling']] |
95 | self.bitwidth = float(metadata['samplerate']) / float(self.bitrate) | |
fdd5ee5e | 96 | self.halfbit = int(self.bitwidth / 2) |
2dc6d41c UH |
97 | |
98 | def report(self): | |
99 | pass | |
100 | ||
d1970f14 | 101 | def putpx(self, data): |
7d4b5fac UH |
102 | self.put(self.samplenum, self.samplenum, self.out_proto, data) |
103 | ||
104 | def putx(self, data): | |
105 | self.put(self.samplenum, self.samplenum, self.out_ann, data) | |
106 | ||
fdd5ee5e UH |
107 | def putpm(self, data): |
108 | s, h = self.samplenum, self.halfbit | |
109 | self.put(self.ss_block - h, self.samplenum + h, self.out_proto, data) | |
110 | ||
111 | def putm(self, data): | |
112 | s, h = self.samplenum, self.halfbit | |
113 | self.put(self.ss_block - h, self.samplenum + h, self.out_ann, data) | |
114 | ||
d1970f14 | 115 | def putpb(self, data): |
fdd5ee5e UH |
116 | s, h = self.samplenum, self.halfbit |
117 | self.put(s - h, s + h, self.out_proto, data) | |
d1970f14 UH |
118 | |
119 | def putb(self, data): | |
fdd5ee5e UH |
120 | s, h = self.samplenum, self.halfbit |
121 | self.put(s - h, s + h, self.out_ann, data) | |
d1970f14 UH |
122 | |
123 | def set_new_target_samplenum(self): | |
124 | bitpos = self.ss_sop + (self.bitwidth / 2) | |
125 | bitpos += self.bitnum * self.bitwidth | |
126 | self.samplenum_target = int(bitpos) | |
127 | ||
128 | def wait_for_sop(self, sym): | |
129 | # Wait for a Start of Packet (SOP), i.e. a J->K symbol change. | |
130 | if sym != 'K': | |
131 | self.oldsym = sym | |
132 | return | |
133 | self.ss_sop = self.samplenum | |
134 | self.set_new_target_samplenum() | |
135 | self.putpx(['SOP', None]) | |
edad8134 | 136 | self.putx([1, ['SOP']]) |
d1970f14 UH |
137 | self.state = 'GET BIT' |
138 | ||
139 | def handle_bit(self, sym, b): | |
140 | if self.consecutive_ones == 6 and b == '0': | |
141 | # Stuff bit. Don't add to the packet, reset self.consecutive_ones. | |
edad8134 | 142 | self.putb([4, ['SB: %s/%s' % (sym, b)]]) |
d1970f14 UH |
143 | self.consecutive_ones = 0 |
144 | else: | |
145 | # Normal bit. Add it to the packet, update self.consecutive_ones. | |
edad8134 | 146 | self.putb([3, ['%s/%s' % (sym, b)]]) |
d1970f14 UH |
147 | self.packet += b |
148 | if b == '1': | |
149 | self.consecutive_ones += 1 | |
150 | else: | |
151 | self.consecutive_ones = 0 | |
152 | ||
153 | def get_eop(self, sym): | |
154 | # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J. | |
155 | self.syms.append(sym) | |
156 | self.putpb(['SYM', sym]) | |
157 | self.putb([0, ['%s' % sym]]) | |
158 | self.bitnum += 1 | |
159 | self.set_new_target_samplenum() | |
160 | self.oldsym = sym | |
161 | if self.syms[-2:] == ['SE0', 'J']: | |
162 | # Got an EOP, i.e. we now have a full packet. | |
fdd5ee5e UH |
163 | self.putpm(['EOP', None]) |
164 | self.putm([2, ['EOP']]) | |
d1970f14 | 165 | self.putpb(['PACKET', self.packet]) |
edad8134 | 166 | self.putb([5, ['PACKET: %s' % self.packet]]) |
d1970f14 UH |
167 | self.bitnum, self.packet, self.syms, self.state = 0, '', [], 'IDLE' |
168 | self.consecutive_ones = 0 | |
169 | ||
170 | def get_bit(self, sym): | |
171 | if sym == 'SE0': | |
172 | # Start of an EOP. Change state, run get_eop() for this bit. | |
173 | self.state = 'GET EOP' | |
fdd5ee5e | 174 | self.ss_block = self.samplenum |
d1970f14 UH |
175 | self.get_eop(sym) |
176 | return | |
177 | self.syms.append(sym) | |
178 | self.putpb(['SYM', sym]) | |
179 | b = '0' if self.oldsym != sym else '1' | |
180 | self.handle_bit(sym, b) | |
181 | self.bitnum += 1 | |
182 | self.set_new_target_samplenum() | |
183 | self.oldsym = sym | |
184 | ||
2dc6d41c | 185 | def decode(self, ss, es, data): |
2fcd7c22 | 186 | for (self.samplenum, pins) in data: |
d1970f14 UH |
187 | # State machine. |
188 | if self.state == 'IDLE': | |
189 | # Ignore identical samples early on (for performance reasons). | |
190 | if self.oldpins == pins: | |
191 | continue | |
192 | self.oldpins = pins | |
193 | sym = symbols[self.options['signalling']][tuple(pins)] | |
194 | self.wait_for_sop(sym) | |
195 | elif self.state in ('GET BIT', 'GET EOP'): | |
196 | # Wait until we're in the middle of the desired bit. | |
197 | if self.samplenum < self.samplenum_target: | |
198 | continue | |
199 | sym = symbols[self.options['signalling']][tuple(pins)] | |
200 | if self.state == 'GET BIT': | |
201 | self.get_bit(sym) | |
202 | elif self.state == 'GET EOP': | |
203 | self.get_eop(sym) | |
2dc6d41c | 204 | else: |
d1970f14 | 205 | raise Exception('Invalid state: %s' % self.state) |
2dc6d41c | 206 |