]>
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 | ||
2dc6d41c UH |
22 | import sigrokdecode as srd |
23 | ||
a56b8fe1 | 24 | ''' |
c515eed7 | 25 | OUTPUT_PYTHON format: |
a56b8fe1 UH |
26 | |
27 | Packet: | |
28 | [<ptype>, <pdata>] | |
29 | ||
30 | <ptype>, <pdata>: | |
31 | - 'SOP', None | |
32 | - 'SYM', <sym> | |
33 | - 'BIT', <bit> | |
34 | - 'STUFF BIT', None | |
35 | - 'EOP', None | |
a56b8fe1 UH |
36 | |
37 | <sym>: | |
38 | - 'J', 'K', 'SE0', or 'SE1' | |
39 | ||
40 | <bit>: | |
41 | - 0 or 1 | |
42 | - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'. | |
a56b8fe1 UH |
43 | ''' |
44 | ||
d1970f14 | 45 | # Low-/full-speed symbols. |
2dc6d41c | 46 | # Note: Low-speed J and K are inverted compared to the full-speed J and K! |
7dc75721 UH |
47 | symbols = { |
48 | 'low-speed': { | |
2dc6d41c UH |
49 | # (<dp>, <dm>): <symbol/state> |
50 | (0, 0): 'SE0', | |
51 | (1, 0): 'K', | |
52 | (0, 1): 'J', | |
53 | (1, 1): 'SE1', | |
7dc75721 UH |
54 | }, |
55 | 'full-speed': { | |
2dc6d41c UH |
56 | # (<dp>, <dm>): <symbol/state> |
57 | (0, 0): 'SE0', | |
58 | (1, 0): 'J', | |
59 | (0, 1): 'K', | |
60 | (1, 1): 'SE1', | |
7dc75721 | 61 | }, |
2dc6d41c UH |
62 | } |
63 | ||
5bb55598 UH |
64 | bitrates = { |
65 | 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%) | |
66 | 'full-speed': 12000000, # 12Mb/s (+/- 0.25%) | |
67 | } | |
68 | ||
2dc6d41c UH |
69 | class Decoder(srd.Decoder): |
70 | api_version = 1 | |
71 | id = 'usb_signalling' | |
72 | name = 'USB signalling' | |
73 | longname = 'Universal Serial Bus (LS/FS) signalling' | |
9e1437a0 | 74 | desc = 'USB (low-speed and full-speed) signalling protocol.' |
2dc6d41c UH |
75 | license = 'gplv2+' |
76 | inputs = ['logic'] | |
77 | outputs = ['usb_signalling'] | |
da9bcbd9 | 78 | probes = ( |
2dc6d41c UH |
79 | {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'}, |
80 | {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'}, | |
da9bcbd9 | 81 | ) |
84c1c0b5 BV |
82 | options = ( |
83 | {'id': 'signalling', 'desc': 'Signalling', | |
84 | 'default': 'full-speed', 'values': ('full-speed', 'low-speed')}, | |
85 | ) | |
da9bcbd9 BV |
86 | annotations = ( |
87 | ('sym', 'Symbol'), | |
88 | ('sop', 'Start of packet (SOP)'), | |
89 | ('eop', 'End of packet (EOP)'), | |
90 | ('bit', 'Bit'), | |
91 | ('stuffbit', 'Stuff bit'), | |
92 | ) | |
066d6594 UH |
93 | annotation_rows = ( |
94 | ('bits', 'Bits', (1, 2, 3, 4)), | |
95 | ('symbols', 'Symbols', (0,)), | |
96 | ) | |
2dc6d41c UH |
97 | |
98 | def __init__(self): | |
f372d597 | 99 | self.samplerate = None |
d1970f14 | 100 | self.oldsym = 'J' # The "idle" state is J. |
fdd5ee5e UH |
101 | self.ss_sop = None |
102 | self.ss_block = None | |
2dc6d41c | 103 | self.samplenum = 0 |
2dc6d41c | 104 | self.syms = [] |
5bb55598 UH |
105 | self.bitrate = None |
106 | self.bitwidth = None | |
d1970f14 UH |
107 | self.bitnum = 0 |
108 | self.samplenum_target = None | |
2fcd7c22 | 109 | self.oldpins = None |
d1970f14 UH |
110 | self.consecutive_ones = 0 |
111 | self.state = 'IDLE' | |
2dc6d41c | 112 | |
f372d597 | 113 | def start(self): |
c515eed7 | 114 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 115 | self.out_ann = self.register(srd.OUTPUT_ANN) |
f372d597 BV |
116 | |
117 | def metadata(self, key, value): | |
118 | if key == srd.SRD_CONF_SAMPLERATE: | |
119 | self.samplerate = value | |
120 | self.bitrate = bitrates[self.options['signalling']] | |
121 | self.bitwidth = float(self.samplerate) / float(self.bitrate) | |
122 | self.halfbit = int(self.bitwidth / 2) | |
2dc6d41c | 123 | |
d1970f14 | 124 | def putpx(self, data): |
c515eed7 | 125 | self.put(self.samplenum, self.samplenum, self.out_python, data) |
7d4b5fac UH |
126 | |
127 | def putx(self, data): | |
128 | self.put(self.samplenum, self.samplenum, self.out_ann, data) | |
129 | ||
fdd5ee5e UH |
130 | def putpm(self, data): |
131 | s, h = self.samplenum, self.halfbit | |
c515eed7 | 132 | self.put(self.ss_block - h, s + h, self.out_python, data) |
fdd5ee5e UH |
133 | |
134 | def putm(self, data): | |
135 | s, h = self.samplenum, self.halfbit | |
9059125f | 136 | self.put(self.ss_block - h, s + h, self.out_ann, data) |
fdd5ee5e | 137 | |
d1970f14 | 138 | def putpb(self, data): |
fdd5ee5e | 139 | s, h = self.samplenum, self.halfbit |
c515eed7 | 140 | self.put(s - h, s + h, self.out_python, data) |
d1970f14 UH |
141 | |
142 | def putb(self, data): | |
fdd5ee5e UH |
143 | s, h = self.samplenum, self.halfbit |
144 | self.put(s - h, s + h, self.out_ann, data) | |
d1970f14 UH |
145 | |
146 | def set_new_target_samplenum(self): | |
147 | bitpos = self.ss_sop + (self.bitwidth / 2) | |
148 | bitpos += self.bitnum * self.bitwidth | |
149 | self.samplenum_target = int(bitpos) | |
150 | ||
151 | def wait_for_sop(self, sym): | |
152 | # Wait for a Start of Packet (SOP), i.e. a J->K symbol change. | |
153 | if sym != 'K': | |
154 | self.oldsym = sym | |
155 | return | |
156 | self.ss_sop = self.samplenum | |
157 | self.set_new_target_samplenum() | |
158 | self.putpx(['SOP', None]) | |
edad8134 | 159 | self.putx([1, ['SOP']]) |
d1970f14 UH |
160 | self.state = 'GET BIT' |
161 | ||
162 | def handle_bit(self, sym, b): | |
163 | if self.consecutive_ones == 6 and b == '0': | |
9059125f | 164 | # Stuff bit. |
a56b8fe1 | 165 | self.putpb(['STUFF BIT', None]) |
066d6594 UH |
166 | self.putb([4, ['SB: %s' % b]]) |
167 | self.putb([0, ['%s' % sym]]) | |
d1970f14 UH |
168 | self.consecutive_ones = 0 |
169 | else: | |
9059125f | 170 | # Normal bit (not a stuff bit). |
a56b8fe1 | 171 | self.putpb(['BIT', b]) |
066d6594 UH |
172 | self.putb([3, ['%s' % b]]) |
173 | self.putb([0, ['%s' % sym]]) | |
d1970f14 UH |
174 | if b == '1': |
175 | self.consecutive_ones += 1 | |
176 | else: | |
177 | self.consecutive_ones = 0 | |
178 | ||
179 | def get_eop(self, sym): | |
180 | # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J. | |
181 | self.syms.append(sym) | |
182 | self.putpb(['SYM', sym]) | |
183 | self.putb([0, ['%s' % sym]]) | |
184 | self.bitnum += 1 | |
185 | self.set_new_target_samplenum() | |
186 | self.oldsym = sym | |
187 | if self.syms[-2:] == ['SE0', 'J']: | |
9059125f | 188 | # Got an EOP. |
fdd5ee5e UH |
189 | self.putpm(['EOP', None]) |
190 | self.putm([2, ['EOP']]) | |
9059125f | 191 | self.bitnum, self.syms, self.state = 0, [], 'IDLE' |
d1970f14 UH |
192 | self.consecutive_ones = 0 |
193 | ||
194 | def get_bit(self, sym): | |
195 | if sym == 'SE0': | |
196 | # Start of an EOP. Change state, run get_eop() for this bit. | |
197 | self.state = 'GET EOP' | |
fdd5ee5e | 198 | self.ss_block = self.samplenum |
d1970f14 UH |
199 | self.get_eop(sym) |
200 | return | |
201 | self.syms.append(sym) | |
202 | self.putpb(['SYM', sym]) | |
203 | b = '0' if self.oldsym != sym else '1' | |
204 | self.handle_bit(sym, b) | |
205 | self.bitnum += 1 | |
206 | self.set_new_target_samplenum() | |
207 | self.oldsym = sym | |
208 | ||
2dc6d41c | 209 | def decode(self, ss, es, data): |
f372d597 BV |
210 | if self.samplerate is None: |
211 | raise Exception("Cannot decode without samplerate.") | |
2fcd7c22 | 212 | for (self.samplenum, pins) in data: |
d1970f14 UH |
213 | # State machine. |
214 | if self.state == 'IDLE': | |
215 | # Ignore identical samples early on (for performance reasons). | |
216 | if self.oldpins == pins: | |
217 | continue | |
218 | self.oldpins = pins | |
219 | sym = symbols[self.options['signalling']][tuple(pins)] | |
220 | self.wait_for_sop(sym) | |
221 | elif self.state in ('GET BIT', 'GET EOP'): | |
222 | # Wait until we're in the middle of the desired bit. | |
223 | if self.samplenum < self.samplenum_target: | |
224 | continue | |
225 | sym = symbols[self.options['signalling']][tuple(pins)] | |
226 | if self.state == 'GET BIT': | |
227 | self.get_bit(sym) | |
228 | elif self.state == 'GET EOP': | |
229 | self.get_eop(sym) | |
2dc6d41c | 230 | else: |
d1970f14 | 231 | raise Exception('Invalid state: %s' % self.state) |
2dc6d41c | 232 |