2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5 ## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 import sigrokdecode as srd
41 - 'J', 'K', 'SE0', or 'SE1'
45 - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
48 # Low-/full-speed symbols.
49 # Note: Low-speed J and K are inverted compared to the full-speed J and K!
52 # (<dp>, <dm>): <symbol/state>
59 # (<dp>, <dm>): <symbol/state>
66 # (<dp>, <dm>): <symbol/state>
72 # After a PREamble PID, the bus segment between Host and Hub uses LS signalling
73 # rate and FS signalling polarity (USB 2.0 spec, 11.8.4: "For both upstream and
74 # downstream low-speed data, the hub is responsible for inverting the polarity of
75 # the data before transmitting to/from a low-speed port."
77 # (<dp>, <dm>): <symbol/state>
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%)
95 'SE0': [2, ['SE0', '0']],
96 'SE1': [3, ['SE1', '1']],
99 class SamplerateError(Exception):
102 class Decoder(srd.Decoder):
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.'
110 outputs = ['usb_signalling']
112 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
113 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
116 {'id': 'signalling', 'desc': 'Signalling',
117 'default': 'automatic', 'values': ('automatic', 'full-speed', 'low-speed')},
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)'),
127 ('stuffbit', 'Stuff bit'),
129 ('keep-alive', 'Low-speed keep-alive'),
133 ('bits', 'Bits', (4, 5, 6, 7, 8, 9, 10)),
134 ('symbols', 'Symbols', (0, 1, 2, 3)),
138 self.samplerate = None
139 self.oldsym = 'J' # The "idle" state is J.
144 self.samplepos = None
145 self.samplenum_target = None
146 self.samplenum_edge = None
147 self.samplenum_lastedge = 0
150 self.consecutive_ones = 0
155 self.out_python = self.register(srd.OUTPUT_PYTHON)
156 self.out_ann = self.register(srd.OUTPUT_ANN)
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()
165 def update_bitrate(self):
166 self.bitrate = bitrates[self.signalling]
167 self.bitwidth = float(self.samplerate) / float(self.bitrate)
169 def putpx(self, data):
170 s = self.samplenum_edge
171 self.put(s, s, self.out_python, data)
173 def putx(self, data):
174 s = self.samplenum_edge
175 self.put(s, s, self.out_ann, data)
177 def putpm(self, data):
178 e = self.samplenum_edge
179 self.put(self.ss_block, e, self.out_python, data)
181 def putm(self, data):
182 e = self.samplenum_edge
183 self.put(self.ss_block, e, self.out_ann, data)
185 def putpb(self, data):
186 s, e = self.samplenum_lastedge, self.samplenum_edge
187 self.put(s, e, self.out_python, data)
189 def putb(self, data):
190 s, e = self.samplenum_lastedge, self.samplenum_edge
191 self.put(s, e, self.out_ann, data)
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))
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':
203 self.consecutive_ones = 0
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'
212 def handle_bit(self, b):
213 if self.consecutive_ones == 6:
216 self.putpb(['STUFF BIT', None])
217 self.putb([7, ['Stuff bit: 0', 'SB: 0', '0']])
218 self.consecutive_ones = 0
220 self.putpb(['ERR', None])
221 self.putb([8, ['Bit stuff error', 'BS ERR', 'B']])
224 # Normal bit (not a stuff bit).
225 self.putpb(['BIT', b])
226 self.putb([6, ['%s' % b]])
228 self.consecutive_ones += 1
230 self.consecutive_ones = 0
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])
242 self.putpm(['EOP', None])
243 self.putm([5, ['EOP', 'E']])
244 self.state = 'WAIT IDLE'
246 self.putpm(['ERR', None])
247 self.putm([8, ['EOP Error', 'EErr', 'E']])
250 def get_bit(self, sym):
251 self.set_new_target_samplenum()
252 b = '0' if self.oldsym != sym else '1'
255 # Start of an EOP. Change state, save edge
256 self.state = 'GET EOP'
257 self.ss_block = self.samplenum_lastedge
260 self.putpb(['SYM', sym])
261 self.putb(sym_annotation[sym])
262 if len(self.bits) <= 16:
264 if len(self.bits) == 16 and self.bits == '0000000100111100':
265 # Sync and low-speed PREamble seen
266 self.putpx(['EOP', None])
268 self.signalling = 'low-speed-rp'
269 self.update_bitrate()
272 edgesym = symbols[self.signalling][tuple(self.edgepins)]
273 if edgesym not in ('SE0', 'SE1'):
275 self.bitwidth = self.bitwidth - (0.001 * self.bitwidth)
276 self.samplepos = self.samplepos - (0.01 * self.bitwidth)
278 self.bitwidth = self.bitwidth + (0.001 * self.bitwidth)
279 self.samplepos = self.samplepos + (0.01 * self.bitwidth)
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']])
293 self.signalling = 'full-speed'
294 self.update_bitrate()
296 self.signalling = 'low-speed'
297 self.update_bitrate()
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:
306 if self.state == 'IDLE':
307 # Ignore identical samples early on (for performance reasons).
308 if self.oldpins == pins:
311 sym = symbols[self.signalling][tuple(pins)]
313 self.samplenum_lastedge = self.samplenum
314 self.state = 'WAIT IDLE'
316 self.wait_for_sop(sym)
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:
322 if self.samplenum < self.samplenum_target:
324 sym = symbols[self.signalling][tuple(pins)]
325 if self.state == 'GET BIT':
327 elif self.state == 'GET EOP':
330 elif self.state == 'WAIT IDLE':
331 if tuple(pins) == (0, 0):
333 if self.samplenum - self.samplenum_lastedge > 1:
334 sym = symbols[self.options['signalling']][tuple(pins)]
335 self.handle_idle(sym)
337 sym = symbols[self.signalling][tuple(pins)]
338 self.wait_for_sop(sym)
341 elif self.state == 'INIT':
342 sym = symbols[self.options['signalling']][tuple(pins)]
343 self.handle_idle(sym)