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