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