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