]> sigrok.org Git - libsigrokdecode.git/blob - decoders/usb_signalling/pd.py
usb_signalling: Fix decode of individual bits.
[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.
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.oldsym = 'J' # The "idle" state is J.
73         self.ss_sop = -1
74         self.samplenum = 0
75         self.packet = ''
76         self.syms = []
77         self.bitrate = None
78         self.bitwidth = None
79         self.bitnum = 0
80         self.samplenum_target = None
81         self.oldpins = None
82         self.consecutive_ones = 0
83         self.state = 'IDLE'
84
85     def start(self, metadata):
86         self.out_proto = self.add(srd.OUTPUT_PROTO, 'usb_signalling')
87         self.out_ann = self.add(srd.OUTPUT_ANN, 'usb_signalling')
88         self.bitrate = bitrates[self.options['signalling']]
89         self.bitwidth = float(metadata['samplerate']) / float(self.bitrate)
90
91     def report(self):
92         pass
93
94     def putpx(self, data):
95         self.put(self.samplenum, self.samplenum, self.out_proto, data)
96
97     def putx(self, data):
98         self.put(self.samplenum, self.samplenum, self.out_ann, data)
99
100     def putpb(self, data):
101         s, halfbit = self.samplenum, int(self.bitwidth / 2)
102         self.put(s - halfbit, s + halfbit, self.out_proto, data)
103
104     def putb(self, data):
105         s, halfbit = self.samplenum, int(self.bitwidth / 2)
106         self.put(s - halfbit, s + halfbit, self.out_ann, data)
107
108     def set_new_target_samplenum(self):
109         bitpos = self.ss_sop + (self.bitwidth / 2)
110         bitpos += self.bitnum * self.bitwidth
111         self.samplenum_target = int(bitpos)
112
113     def wait_for_sop(self, sym):
114         # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
115         if sym != 'K':
116             self.oldsym = sym
117             return
118         self.ss_sop = self.samplenum
119         self.set_new_target_samplenum()
120         self.putpx(['SOP', None])
121         self.putx([0, ['SOP']])
122         self.state = 'GET BIT'
123
124     def handle_bit(self, sym, b):
125         if self.consecutive_ones == 6 and b == '0':
126             # Stuff bit. Don't add to the packet, reset self.consecutive_ones.
127             self.putb([0, ['SB: %s/%s' % (sym, b)]])
128             self.consecutive_ones = 0
129         else:
130             # Normal bit. Add it to the packet, update self.consecutive_ones.
131             self.putb([0, ['%s/%s' % (sym, b)]])
132             self.packet += b
133             if b == '1':
134                 self.consecutive_ones += 1
135             else:
136                 self.consecutive_ones = 0
137
138     def get_eop(self, sym):
139         # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
140         self.syms.append(sym)
141         self.putpb(['SYM', sym])
142         self.putb([0, ['%s' % sym]])
143         self.bitnum += 1
144         self.set_new_target_samplenum()
145         self.oldsym = sym
146         if self.syms[-2:] == ['SE0', 'J']:
147             # Got an EOP, i.e. we now have a full packet.
148             self.putpb(['PACKET', self.packet])
149             self.putb([0, ['PACKET: %s' % self.packet]])
150             self.bitnum, self.packet, self.syms, self.state = 0, '', [], 'IDLE'
151             self.consecutive_ones = 0
152
153     def get_bit(self, sym):
154         if sym == 'SE0':
155             # Start of an EOP. Change state, run get_eop() for this bit.
156             self.state = 'GET EOP'
157             self.get_eop(sym)
158             return
159         self.syms.append(sym)
160         self.putpb(['SYM', sym])
161         b = '0' if self.oldsym != sym else '1'
162         self.handle_bit(sym, b)
163         self.bitnum += 1
164         self.set_new_target_samplenum()
165         self.oldsym = sym
166
167     def decode(self, ss, es, data):
168         for (self.samplenum, pins) in data:
169             # State machine.
170             if self.state == 'IDLE':
171                 # Ignore identical samples early on (for performance reasons).
172                 if self.oldpins == pins:
173                     continue
174                 self.oldpins = pins
175                 sym = symbols[self.options['signalling']][tuple(pins)]
176                 self.wait_for_sop(sym)
177             elif self.state in ('GET BIT', 'GET EOP'):
178                 # Wait until we're in the middle of the desired bit.
179                 if self.samplenum < self.samplenum_target:
180                     continue
181                 sym = symbols[self.options['signalling']][tuple(pins)]
182                 if self.state == 'GET BIT':
183                     self.get_bit(sym)
184                 elif self.state == 'GET EOP':
185                     self.get_eop(sym)
186             else:
187                 raise Exception('Invalid state: %s' % self.state)
188