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