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.
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'],
69 ['sop', 'Start of packet (SOP)'],
70 ['eop', 'End of packet (EOP)'],
72 ['stuffbit', 'Stuff bit'],
77 self.oldsym = 'J' # The "idle" state is J.
85 self.samplenum_target = None
87 self.consecutive_ones = 0
90 def start(self, metadata):
91 self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_signalling')
92 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_signalling')
93 self.bitrate = bitrates[self.options['signalling']]
94 self.bitwidth = float(metadata['samplerate']) / float(self.bitrate)
99 def putpx(self, data):
100 self.put(self.samplenum, self.samplenum, self.out_proto, data)
102 def putx(self, data):
103 self.put(self.samplenum, self.samplenum, self.out_ann, data)
105 def putpb(self, data):
106 s, halfbit = self.samplenum, int(self.bitwidth / 2)
107 self.put(s - halfbit, s + halfbit, self.out_proto, data)
109 def putb(self, data):
110 s, halfbit = self.samplenum, int(self.bitwidth / 2)
111 self.put(s - halfbit, s + halfbit, self.out_ann, data)
113 def set_new_target_samplenum(self):
114 bitpos = self.ss_sop + (self.bitwidth / 2)
115 bitpos += self.bitnum * self.bitwidth
116 self.samplenum_target = int(bitpos)
118 def wait_for_sop(self, sym):
119 # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
123 self.ss_sop = self.samplenum
124 self.set_new_target_samplenum()
125 self.putpx(['SOP', None])
126 self.putx([1, ['SOP']])
127 self.state = 'GET BIT'
129 def handle_bit(self, sym, b):
130 if self.consecutive_ones == 6 and b == '0':
131 # Stuff bit. Don't add to the packet, reset self.consecutive_ones.
132 self.putb([4, ['SB: %s/%s' % (sym, b)]])
133 self.consecutive_ones = 0
135 # Normal bit. Add it to the packet, update self.consecutive_ones.
136 self.putb([3, ['%s/%s' % (sym, b)]])
139 self.consecutive_ones += 1
141 self.consecutive_ones = 0
143 def get_eop(self, sym):
144 # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
145 self.syms.append(sym)
146 self.putpb(['SYM', sym])
147 self.putb([0, ['%s' % sym]])
149 self.set_new_target_samplenum()
151 if self.syms[-2:] == ['SE0', 'J']:
152 # Got an EOP, i.e. we now have a full packet.
153 self.putpb(['PACKET', self.packet])
154 self.putb([5, ['PACKET: %s' % self.packet]])
155 self.bitnum, self.packet, self.syms, self.state = 0, '', [], 'IDLE'
156 self.consecutive_ones = 0
158 def get_bit(self, sym):
160 # Start of an EOP. Change state, run get_eop() for this bit.
161 self.state = 'GET EOP'
164 self.syms.append(sym)
165 self.putpb(['SYM', sym])
166 b = '0' if self.oldsym != sym else '1'
167 self.handle_bit(sym, b)
169 self.set_new_target_samplenum()
172 def decode(self, ss, es, data):
173 for (self.samplenum, pins) in data:
175 if self.state == 'IDLE':
176 # Ignore identical samples early on (for performance reasons).
177 if self.oldpins == pins:
180 sym = symbols[self.options['signalling']][tuple(pins)]
181 self.wait_for_sop(sym)
182 elif self.state in ('GET BIT', 'GET EOP'):
183 # Wait until we're in the middle of the desired bit.
184 if self.samplenum < self.samplenum_target:
186 sym = symbols[self.options['signalling']][tuple(pins)]
187 if self.state == 'GET BIT':
189 elif self.state == 'GET EOP':
192 raise Exception('Invalid state: %s' % self.state)