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 # USB signalling (low-speed and full-speed) protocol decoder
24 import sigrokdecode as srd
26 # Low-/full-speed symbols (used as states of our state machine, too).
27 # Note: Low-speed J and K are inverted compared to the full-speed J and K!
30 # (<dp>, <dm>): <symbol/state>
37 # (<dp>, <dm>): <symbol/state>
46 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%)
47 'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
50 class Decoder(srd.Decoder):
53 name = 'USB signalling'
54 longname = 'Universal Serial Bus (LS/FS) signalling'
55 desc = 'USB (low-speed and full-speed) signalling protocol.'
58 outputs = ['usb_signalling']
60 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
61 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
65 'signalling': ['Signalling', 'full-speed'],
68 ['Text', 'Human-readable text']
72 self.sym = 'J' # The "idle" state is J.
81 def start(self, metadata):
82 self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_signalling')
83 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_signalling')
84 self.bitrate = bitrates[self.options['signalling']]
85 self.bitwidth = float(metadata['samplerate']) / float(self.bitrate)
91 self.put(self.samplenum, self.samplenum, self.out_proto, data)
94 self.put(self.samplenum, self.samplenum, self.out_ann, data)
96 def decode(self, ss, es, data):
97 for (self.samplenum, pins) in data:
99 # Note: self.samplenum is the absolute sample number, whereas
100 # self.scount only counts the number of samples since the
101 # last change in the D+/D- lines.
104 # Ignore identical samples early on (for performance reasons).
105 if self.oldpins == pins:
107 self.oldpins, (dp, dm) = pins, pins
109 sym = symbols[self.options['signalling']][dp, dm]
111 self.putx([0, [sym]])
112 self.putp(['SYM', sym])
114 # Wait for a symbol change (i.e., change in D+/D- lines).
119 ## self.syms.append(sym + ' ')
120 ## if len(self.syms) == 16:
121 ## self.putx([0, [''.join(self.syms)]])
125 # How many bits since the last transition?
126 if self.packet != '' or self.sym != 'J':
127 bitcount = int((self.scount - 1) / self.bitwidth)
131 if self.sym == 'SE0':
133 # End-Of-Packet (EOP)
134 # self.putx([0, [packet_decode(self.packet), self.packet]])
135 if self.packet != '': # FIXME?
136 self.putx([0, ['PACKET: %s' % self.packet]])
137 self.putp(['PACKET', self.packet])
139 # Longer than EOP, assume reset.
140 self.putx([0, ['RESET']])
141 self.putp(['RESET', None])
142 # self.putx([0, [self.packet]])
148 # Add bits to the packet string.
149 self.packet += '1' * bitcount
151 # Handle bit stuffing.
152 if bitcount < 6 and sym != 'SE0':
155 self.putx([0, ['BIT STUFF ERROR']])
156 self.putp(['BIT STUFF ERROR', None])