]> sigrok.org Git - libsigrokdecode.git/blob - decoders/usb_signalling/pd.py
70ada3767af7c10fd140f78246c416bf709a4d7c
[libsigrokdecode.git] / decoders / usb_signalling / pd.py
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
22 # USB signalling (low-speed and full-speed) protocol decoder
23
24 import sigrokdecode as srd
25
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!
28 symbols_ls = {
29         # (<dp>, <dm>): <symbol/state>
30         (0, 0): 'SE0',
31         (1, 0): 'K',
32         (0, 1): 'J',
33         (1, 1): 'SE1',
34 }
35 symbols_fs = {
36         # (<dp>, <dm>): <symbol/state>
37         (0, 0): 'SE0',
38         (1, 0): 'J',
39         (0, 1): 'K',
40         (1, 1): 'SE1',
41 }
42
43 bitrates = {
44     'low-speed': 1500000,   # 1.5Mb/s (+/- 1.5%)
45     'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
46 }
47
48 class Decoder(srd.Decoder):
49     api_version = 1
50     id = 'usb_signalling'
51     name = 'USB signalling'
52     longname = 'Universal Serial Bus (LS/FS) signalling'
53     desc = 'USB (low-speed and full-speed) signalling protocol.'
54     license = 'gplv2+'
55     inputs = ['logic']
56     outputs = ['usb_signalling']
57     probes = [
58         {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
59         {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
60     ]
61     optional_probes = []
62     options = {
63         'signalling': ['Signalling', 'full-speed'],
64     }
65     annotations = [
66         ['Text', 'Human-readable text']
67     ]
68
69     def __init__(self):
70         self.sym = 'J' # The "idle" state is J.
71         self.samplenum = 0
72         self.scount = 0
73         self.packet = ''
74         self.syms = []
75         self.bitrate = None
76         self.bitwidth = None
77         self.oldpins = None
78
79     def start(self, metadata):
80         self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_signalling')
81         self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_signalling')
82         self.bitrate = bitrates[self.options['signalling']]
83         self.bitwidth = float(metadata['samplerate']) / float(self.bitrate)
84
85     def report(self):
86         pass
87
88     def putp(self, data):
89         self.put(self.samplenum, self.samplenum, self.out_proto, data)
90
91     def putx(self, data):
92         self.put(self.samplenum, self.samplenum, self.out_ann, data)
93
94     def decode(self, ss, es, data):
95         for (self.samplenum, pins) in data:
96
97             # Note: self.samplenum is the absolute sample number, whereas
98             # self.scount only counts the number of samples since the
99             # last change in the D+/D- lines.
100             self.scount += 1
101
102             # Ignore identical samples early on (for performance reasons).
103             if self.oldpins == pins:
104                 continue
105             self.oldpins, (dp, dm) = pins, pins
106
107             if self.options['signalling'] == 'low-speed':
108                 sym = symbols_ls[dp, dm]
109             elif self.options['signalling'] == 'full-speed':
110                 sym = symbols_fs[dp, dm]
111
112             self.putx([0, [sym]])
113             self.putp(['SYM', sym])
114
115             # Wait for a symbol change (i.e., change in D+/D- lines).
116             if sym == self.sym:
117                 continue
118
119             ## # Debug code:
120             ## self.syms.append(sym + ' ')
121             ## if len(self.syms) == 16:
122             ##     self.putx([0, [''.join(self.syms)]])
123             ##     self.syms = []
124             # continue
125
126             # How many bits since the last transition?
127             if self.packet != '' or self.sym != 'J':
128                 bitcount = int((self.scount - 1) / self.bitwidth)
129             else:
130                 bitcount = 0
131
132             if self.sym == 'SE0':
133                 if bitcount == 1:
134                     # End-Of-Packet (EOP)
135                     # self.putx([0, [packet_decode(self.packet), self.packet]])
136                     if self.packet != '': # FIXME?
137                         self.putx([0, ['PACKET: %s' % self.packet]])
138                         self.putp(['PACKET', self.packet])
139                 else:
140                     # Longer than EOP, assume reset.
141                     self.putx([0, ['RESET']])
142                     self.putp(['RESET', None])
143                 # self.putx([0, [self.packet]])
144                 self.scount = 0
145                 self.sym = sym
146                 self.packet = ''
147                 continue
148
149             # Add bits to the packet string.
150             self.packet += '1' * bitcount
151
152             # Handle bit stuffing.
153             if bitcount < 6 and sym != 'SE0':
154                 self.packet += '0'
155             elif bitcount > 6:
156                 self.putx([0, ['BIT STUFF ERROR']])
157                 self.putp(['BIT STUFF ERROR', None])
158
159             self.scount = 0
160             self.sym = sym
161