]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/usb_signalling/pd.py
license: remove FSF postal address from boiler plate license text
[libsigrokdecode.git] / decoders / usb_signalling / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5## Copyright (C) 2012-2013 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, see <http://www.gnu.org/licenses/>.
19##
20
21import sigrokdecode as srd
22
23'''
24OUTPUT_PYTHON format:
25
26Packet:
27[<ptype>, <pdata>]
28
29<ptype>, <pdata>:
30 - 'SOP', None
31 - 'SYM', <sym>
32 - 'BIT', <bit>
33 - 'STUFF BIT', None
34 - 'EOP', None
35 - 'ERR', None
36 - 'KEEP ALIVE', None
37 - 'RESET', None
38
39<sym>:
40 - 'J', 'K', 'SE0', or 'SE1'
41
42<bit>:
43 - '0' or '1'
44 - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
45'''
46
47# Low-/full-speed symbols.
48# Note: Low-speed J and K are inverted compared to the full-speed J and K!
49symbols = {
50 'low-speed': {
51 # (<dp>, <dm>): <symbol/state>
52 (0, 0): 'SE0',
53 (1, 0): 'K',
54 (0, 1): 'J',
55 (1, 1): 'SE1',
56 },
57 'full-speed': {
58 # (<dp>, <dm>): <symbol/state>
59 (0, 0): 'SE0',
60 (1, 0): 'J',
61 (0, 1): 'K',
62 (1, 1): 'SE1',
63 },
64 'automatic': {
65 # (<dp>, <dm>): <symbol/state>
66 (0, 0): 'SE0',
67 (1, 0): 'FS_J',
68 (0, 1): 'LS_J',
69 (1, 1): 'SE1',
70 },
71 # After a PREamble PID, the bus segment between Host and Hub uses LS
72 # signalling rate and FS signalling polarity (USB 2.0 spec, 11.8.4: "For
73 # both upstream and downstream low-speed data, the hub is responsible for
74 # inverting the polarity of the data before transmitting to/from a
75 # low-speed port.").
76 'low-speed-rp': {
77 # (<dp>, <dm>): <symbol/state>
78 (0, 0): 'SE0',
79 (1, 0): 'J',
80 (0, 1): 'K',
81 (1, 1): 'SE1',
82 },
83}
84
85bitrates = {
86 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%)
87 'low-speed-rp': 1500000, # 1.5Mb/s (+/- 1.5%)
88 'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
89 'automatic': None
90}
91
92sym_annotation = {
93 'J': [0, ['J']],
94 'K': [1, ['K']],
95 'SE0': [2, ['SE0', '0']],
96 'SE1': [3, ['SE1', '1']],
97}
98
99class SamplerateError(Exception):
100 pass
101
102class Decoder(srd.Decoder):
103 api_version = 2
104 id = 'usb_signalling'
105 name = 'USB signalling'
106 longname = 'Universal Serial Bus (LS/FS) signalling'
107 desc = 'USB (low-speed and full-speed) signalling protocol.'
108 license = 'gplv2+'
109 inputs = ['logic']
110 outputs = ['usb_signalling']
111 channels = (
112 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
113 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
114 )
115 options = (
116 {'id': 'signalling', 'desc': 'Signalling',
117 'default': 'automatic', 'values': ('automatic', 'full-speed', 'low-speed')},
118 )
119 annotations = (
120 ('sym-j', 'J symbol'),
121 ('sym-k', 'K symbol'),
122 ('sym-se0', 'SE0 symbol'),
123 ('sym-se1', 'SE1 symbol'),
124 ('sop', 'Start of packet (SOP)'),
125 ('eop', 'End of packet (EOP)'),
126 ('bit', 'Bit'),
127 ('stuffbit', 'Stuff bit'),
128 ('error', 'Error'),
129 ('keep-alive', 'Low-speed keep-alive'),
130 ('reset', 'Reset'),
131 )
132 annotation_rows = (
133 ('bits', 'Bits', (4, 5, 6, 7, 8, 9, 10)),
134 ('symbols', 'Symbols', (0, 1, 2, 3)),
135 )
136
137 def __init__(self):
138 self.samplerate = None
139 self.oldsym = 'J' # The "idle" state is J.
140 self.ss_block = None
141 self.samplenum = 0
142 self.bitrate = None
143 self.bitwidth = None
144 self.samplepos = None
145 self.samplenum_target = None
146 self.samplenum_edge = None
147 self.samplenum_lastedge = 0
148 self.oldpins = None
149 self.edgepins = None
150 self.consecutive_ones = 0
151 self.bits = None
152 self.state = 'INIT'
153
154 def start(self):
155 self.out_python = self.register(srd.OUTPUT_PYTHON)
156 self.out_ann = self.register(srd.OUTPUT_ANN)
157
158 def metadata(self, key, value):
159 if key == srd.SRD_CONF_SAMPLERATE:
160 self.samplerate = value
161 self.signalling = self.options['signalling']
162 if self.signalling != 'automatic':
163 self.update_bitrate()
164
165 def update_bitrate(self):
166 self.bitrate = bitrates[self.signalling]
167 self.bitwidth = float(self.samplerate) / float(self.bitrate)
168
169 def putpx(self, data):
170 s = self.samplenum_edge
171 self.put(s, s, self.out_python, data)
172
173 def putx(self, data):
174 s = self.samplenum_edge
175 self.put(s, s, self.out_ann, data)
176
177 def putpm(self, data):
178 e = self.samplenum_edge
179 self.put(self.ss_block, e, self.out_python, data)
180
181 def putm(self, data):
182 e = self.samplenum_edge
183 self.put(self.ss_block, e, self.out_ann, data)
184
185 def putpb(self, data):
186 s, e = self.samplenum_lastedge, self.samplenum_edge
187 self.put(s, e, self.out_python, data)
188
189 def putb(self, data):
190 s, e = self.samplenum_lastedge, self.samplenum_edge
191 self.put(s, e, self.out_ann, data)
192
193 def set_new_target_samplenum(self):
194 self.samplepos += self.bitwidth;
195 self.samplenum_target = int(self.samplepos)
196 self.samplenum_lastedge = self.samplenum_edge
197 self.samplenum_edge = int(self.samplepos - (self.bitwidth / 2))
198
199 def wait_for_sop(self, sym):
200 # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
201 if sym != 'K' or self.oldsym != 'J':
202 return
203 self.consecutive_ones = 0
204 self.bits = ''
205 self.update_bitrate()
206 self.samplepos = self.samplenum - (self.bitwidth / 2) + 0.5
207 self.set_new_target_samplenum()
208 self.putpx(['SOP', None])
209 self.putx([4, ['SOP', 'S']])
210 self.state = 'GET BIT'
211
212 def handle_bit(self, b):
213 if self.consecutive_ones == 6:
214 if b == '0':
215 # Stuff bit.
216 self.putpb(['STUFF BIT', None])
217 self.putb([7, ['Stuff bit: 0', 'SB: 0', '0']])
218 self.consecutive_ones = 0
219 else:
220 self.putpb(['ERR', None])
221 self.putb([8, ['Bit stuff error', 'BS ERR', 'B']])
222 self.state = 'IDLE'
223 else:
224 # Normal bit (not a stuff bit).
225 self.putpb(['BIT', b])
226 self.putb([6, ['%s' % b]])
227 if b == '1':
228 self.consecutive_ones += 1
229 else:
230 self.consecutive_ones = 0
231
232 def get_eop(self, sym):
233 # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
234 self.set_new_target_samplenum()
235 self.putpb(['SYM', sym])
236 self.putb(sym_annotation[sym])
237 self.oldsym = sym
238 if sym == 'SE0':
239 pass
240 elif sym == 'J':
241 # Got an EOP.
242 self.putpm(['EOP', None])
243 self.putm([5, ['EOP', 'E']])
244 self.state = 'WAIT IDLE'
245 else:
246 self.putpm(['ERR', None])
247 self.putm([8, ['EOP Error', 'EErr', 'E']])
248 self.state = 'IDLE'
249
250 def get_bit(self, sym):
251 self.set_new_target_samplenum()
252 b = '0' if self.oldsym != sym else '1'
253 self.oldsym = sym
254 if sym == 'SE0':
255 # Start of an EOP. Change state, save edge
256 self.state = 'GET EOP'
257 self.ss_block = self.samplenum_lastedge
258 else:
259 self.handle_bit(b)
260 self.putpb(['SYM', sym])
261 self.putb(sym_annotation[sym])
262 if len(self.bits) <= 16:
263 self.bits += b
264 if len(self.bits) == 16 and self.bits == '0000000100111100':
265 # Sync and low-speed PREamble seen
266 self.putpx(['EOP', None])
267 self.state = 'IDLE'
268 self.signalling = 'low-speed-rp'
269 self.update_bitrate()
270 self.oldsym = 'J'
271 if b == '0':
272 edgesym = symbols[self.signalling][tuple(self.edgepins)]
273 if edgesym not in ('SE0', 'SE1'):
274 if edgesym == sym:
275 self.bitwidth = self.bitwidth - (0.001 * self.bitwidth)
276 self.samplepos = self.samplepos - (0.01 * self.bitwidth)
277 else:
278 self.bitwidth = self.bitwidth + (0.001 * self.bitwidth)
279 self.samplepos = self.samplepos + (0.01 * self.bitwidth)
280
281 def handle_idle(self, sym):
282 self.samplenum_edge = self.samplenum
283 se0_length = float(self.samplenum - self.samplenum_lastedge) / self.samplerate
284 if se0_length > 2.5e-6: # 2.5us
285 self.putpb(['RESET', None])
286 self.putb([10, ['Reset', 'Res', 'R']])
287 self.signalling = self.options['signalling']
288 elif se0_length > 1.2e-6 and self.signalling == 'low-speed':
289 self.putpb(['KEEP ALIVE', None])
290 self.putb([9, ['Keep-alive', 'KA', 'A']])
291
292 if sym == 'FS_J':
293 self.signalling = 'full-speed'
294 self.update_bitrate()
295 elif sym == 'LS_J':
296 self.signalling = 'low-speed'
297 self.update_bitrate()
298 self.oldsym = 'J'
299 self.state = 'IDLE'
300
301 def decode(self, ss, es, data):
302 if not self.samplerate:
303 raise SamplerateError('Cannot decode without samplerate.')
304 for (self.samplenum, pins) in data:
305 # State machine.
306 if self.state == 'IDLE':
307 # Ignore identical samples early on (for performance reasons).
308 if self.oldpins == pins:
309 continue
310 self.oldpins = pins
311 sym = symbols[self.signalling][tuple(pins)]
312 if sym == 'SE0':
313 self.samplenum_lastedge = self.samplenum
314 self.state = 'WAIT IDLE'
315 else:
316 self.wait_for_sop(sym)
317 self.edgepins = pins
318 elif self.state in ('GET BIT', 'GET EOP'):
319 # Wait until we're in the middle of the desired bit.
320 if self.samplenum == self.samplenum_edge:
321 self.edgepins = pins
322 if self.samplenum < self.samplenum_target:
323 continue
324 sym = symbols[self.signalling][tuple(pins)]
325 if self.state == 'GET BIT':
326 self.get_bit(sym)
327 elif self.state == 'GET EOP':
328 self.get_eop(sym)
329 self.oldpins = pins
330 elif self.state == 'WAIT IDLE':
331 if tuple(pins) == (0, 0):
332 continue
333 if self.samplenum - self.samplenum_lastedge > 1:
334 sym = symbols[self.options['signalling']][tuple(pins)]
335 self.handle_idle(sym)
336 else:
337 sym = symbols[self.signalling][tuple(pins)]
338 self.wait_for_sop(sym)
339 self.oldpins = pins
340 self.edgepins = pins
341 elif self.state == 'INIT':
342 sym = symbols[self.options['signalling']][tuple(pins)]
343 self.handle_idle(sym)
344 self.oldpins = pins