2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5 ## Copyright (C) 2012 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!
29 # (<dp>, <dm>): <symbol/state>
36 # (<dp>, <dm>): <symbol/state>
43 class Decoder(srd.Decoder):
46 name = 'USB signalling'
47 longname = 'Universal Serial Bus (LS/FS) signalling'
48 desc = 'USB 1.x (low-speed and full-speed) signalling protocol.'
51 outputs = ['usb_signalling']
53 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
54 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
58 'signalling': ['Signalling', 'full-speed'],
61 ['Text', 'Human-readable text']
65 self.sym = 'J' # The "idle" state is J.
71 def start(self, metadata):
72 self.samplerate = metadata['samplerate']
73 self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_signalling')
74 self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_signalling')
79 def decode(self, ss, es, data):
80 for (self.samplenum, (dp, dm)) in data:
82 # Note: self.samplenum is the absolute sample number, whereas
83 # self.scount only counts the number of samples since the
84 # last change in the D+/D- lines.
87 if self.options['signalling'] == 'low-speed':
88 sym = symbols_ls[dp, dm]
89 elif self.options['signalling'] == 'full-speed':
90 sym = symbols_fs[dp, dm]
92 self.put(0, 0, self.out_ann, [0, [sym]])
93 self.put(0, 0, self.out_proto, ['SYM', sym])
95 # Wait for a symbol change (i.e., change in D+/D- lines).
100 ## self.syms.append(sym + ' ')
101 ## if len(self.syms) == 16:
102 ## self.put(0, 0, self.out_ann, [0, [''.join(self.syms)]])
106 # How many bits since the last transition?
107 if self.packet != '' or self.sym != 'J':
108 if self.options['signalling'] == 'low-speed':
109 bitrate = 1500000 # 1.5Mb/s (+/- 1.5%)
110 elif self.options['signalling'] == 'full-speed':
111 bitrate = 12000000 # 12Mb/s (+/- 0.25%)
112 bitcount = int((self.scount - 1) * bitrate / self.samplerate)
116 if self.sym == 'SE0':
118 # End-Of-Packet (EOP)
119 # self.put(0, 0, self.out_ann,
120 # [0, [packet_decode(self.packet), self.packet]])
121 if self.packet != '': # FIXME?
122 self.put(0, 0, self.out_ann, [0, ['PACKET: %s' % self.packet]])
123 self.put(0, 0, self.out_proto, ['PACKET', self.packet])
125 # Longer than EOP, assume reset.
126 self.put(0, 0, self.out_ann, [0, ['RESET']])
127 self.put(0, 0, self.out_proto, ['RESET', None])
128 # self.put(0, 0, self.out_ann, [0, [self.packet]])
134 # Add bits to the packet string.
135 self.packet += '1' * bitcount
137 # Handle bit stuffing.
138 if bitcount < 6 and sym != 'SE0':
141 self.put(0, 0, self.out_ann, [0, ['BIT STUFF ERROR']])
142 self.put(0, 0, self.out_proto, ['BIT STUFF ERROR', None])