]> sigrok.org Git - libsigrokdecode.git/blob - decoders/usb_signalling/pd.py
deecf1a84b9a55bbea3749a5dbcd077fc975441e
[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 import sigrokdecode as srd
23
24 '''
25 OUTPUT_PYTHON format:
26
27 Packet:
28 [<ptype>, <pdata>]
29
30 <ptype>, <pdata>:
31  - 'SOP', None
32  - 'SYM', <sym>
33  - 'BIT', <bit>
34  - 'STUFF BIT', None
35  - 'EOP', None
36
37 <sym>:
38  - 'J', 'K', 'SE0', or 'SE1'
39
40 <bit>:
41  - 0 or 1
42  - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
43 '''
44
45 # Low-/full-speed symbols.
46 # Note: Low-speed J and K are inverted compared to the full-speed J and K!
47 symbols = {
48     'low-speed': {
49         # (<dp>, <dm>): <symbol/state>
50         (0, 0): 'SE0',
51         (1, 0): 'K',
52         (0, 1): 'J',
53         (1, 1): 'SE1',
54     },
55     'full-speed': {
56         # (<dp>, <dm>): <symbol/state>
57         (0, 0): 'SE0',
58         (1, 0): 'J',
59         (0, 1): 'K',
60         (1, 1): 'SE1',
61     },
62 }
63
64 bitrates = {
65     'low-speed': 1500000,   # 1.5Mb/s (+/- 1.5%)
66     'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
67 }
68
69 sym_idx = {
70     'J': 0,
71     'K': 1,
72     'SE0': 2,
73     'SE1': 3,
74 }
75
76 class Decoder(srd.Decoder):
77     api_version = 1
78     id = 'usb_signalling'
79     name = 'USB signalling'
80     longname = 'Universal Serial Bus (LS/FS) signalling'
81     desc = 'USB (low-speed and full-speed) signalling protocol.'
82     license = 'gplv2+'
83     inputs = ['logic']
84     outputs = ['usb_signalling']
85     probes = (
86         {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
87         {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
88     )
89     options = (
90         {'id': 'signalling', 'desc': 'Signalling',
91             'default': 'full-speed', 'values': ('full-speed', 'low-speed')},
92     )
93     annotations = (
94         ('sym-j', 'J symbol'),
95         ('sym-k', 'K symbol'),
96         ('sym-se0', 'SE0 symbol'),
97         ('sym-se1', 'SE1 symbol'),
98         ('sop', 'Start of packet (SOP)'),
99         ('eop', 'End of packet (EOP)'),
100         ('bit', 'Bit'),
101         ('stuffbit', 'Stuff bit'),
102     )
103     annotation_rows = (
104         ('bits', 'Bits', (4, 5, 6, 7)),
105         ('symbols', 'Symbols', (0, 1, 2, 3)),
106     )
107
108     def __init__(self):
109         self.samplerate = None
110         self.oldsym = 'J' # The "idle" state is J.
111         self.ss_sop = None
112         self.ss_block = None
113         self.samplenum = 0
114         self.syms = []
115         self.bitrate = None
116         self.bitwidth = None
117         self.bitnum = 0
118         self.samplenum_target = None
119         self.oldpins = None
120         self.consecutive_ones = 0
121         self.state = 'IDLE'
122
123     def start(self):
124         self.out_python = self.register(srd.OUTPUT_PYTHON)
125         self.out_ann = self.register(srd.OUTPUT_ANN)
126
127     def metadata(self, key, value):
128         if key == srd.SRD_CONF_SAMPLERATE:
129             self.samplerate = value
130             self.bitrate = bitrates[self.options['signalling']]
131             self.bitwidth = float(self.samplerate) / float(self.bitrate)
132             self.halfbit = int(self.bitwidth / 2)
133
134     def putpx(self, data):
135         self.put(self.samplenum, self.samplenum, self.out_python, data)
136
137     def putx(self, data):
138         self.put(self.samplenum, self.samplenum, self.out_ann, data)
139
140     def putpm(self, data):
141         s, h = self.samplenum, self.halfbit
142         self.put(self.ss_block - h, s + h, self.out_python, data)
143
144     def putm(self, data):
145         s, h = self.samplenum, self.halfbit
146         self.put(self.ss_block - h, s + h, self.out_ann, data)
147
148     def putpb(self, data):
149         s, h = self.samplenum, self.halfbit
150         self.put(s - h, s + h, self.out_python, data)
151
152     def putb(self, data):
153         s, h = self.samplenum, self.halfbit
154         self.put(s - h, s + h, self.out_ann, data)
155
156     def set_new_target_samplenum(self):
157         bitpos = self.ss_sop + (self.bitwidth / 2)
158         bitpos += self.bitnum * self.bitwidth
159         self.samplenum_target = int(bitpos)
160
161     def wait_for_sop(self, sym):
162         # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
163         if sym != 'K':
164             self.oldsym = sym
165             return
166         self.ss_sop = self.samplenum
167         self.set_new_target_samplenum()
168         self.putpx(['SOP', None])
169         self.putx([4, ['SOP', 'S']])
170         self.state = 'GET BIT'
171
172     def handle_bit(self, sym, b):
173         if self.consecutive_ones == 6 and b == '0':
174             # Stuff bit.
175             self.putpb(['STUFF BIT', None])
176             self.putb([7, ['Stuff bit: %s' % b, 'SB: %s' % b, '%s' % b]])
177             self.putb([sym_idx[sym], ['%s' % sym]])
178             self.consecutive_ones = 0
179         else:
180             # Normal bit (not a stuff bit).
181             self.putpb(['BIT', b])
182             self.putb([6, ['%s' % b]])
183             self.putb([sym_idx[sym], ['%s' % sym]])
184             if b == '1':
185                 self.consecutive_ones += 1
186             else:
187                 self.consecutive_ones = 0
188
189     def get_eop(self, sym):
190         # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
191         self.syms.append(sym)
192         self.putpb(['SYM', sym])
193         self.putb([sym_idx[sym], ['%s' % sym, '%s' % sym[0]]])
194         self.bitnum += 1
195         self.set_new_target_samplenum()
196         self.oldsym = sym
197         if self.syms[-2:] == ['SE0', 'J']:
198             # Got an EOP.
199             self.putpm(['EOP', None])
200             self.putm([5, ['EOP', 'E']])
201             self.bitnum, self.syms, self.state = 0, [], 'IDLE'
202             self.consecutive_ones = 0
203
204     def get_bit(self, sym):
205         if sym == 'SE0':
206             # Start of an EOP. Change state, run get_eop() for this bit.
207             self.state = 'GET EOP'
208             self.ss_block = self.samplenum
209             self.get_eop(sym)
210             return
211         self.syms.append(sym)
212         self.putpb(['SYM', sym])
213         b = '0' if self.oldsym != sym else '1'
214         self.handle_bit(sym, b)
215         self.bitnum += 1
216         self.set_new_target_samplenum()
217         self.oldsym = sym
218
219     def decode(self, ss, es, data):
220         if self.samplerate is None:
221             raise Exception("Cannot decode without samplerate.")
222         for (self.samplenum, pins) in data:
223             # State machine.
224             if self.state == 'IDLE':
225                 # Ignore identical samples early on (for performance reasons).
226                 if self.oldpins == pins:
227                     continue
228                 self.oldpins = pins
229                 sym = symbols[self.options['signalling']][tuple(pins)]
230                 self.wait_for_sop(sym)
231             elif self.state in ('GET BIT', 'GET EOP'):
232                 # Wait until we're in the middle of the desired bit.
233                 if self.samplenum < self.samplenum_target:
234                     continue
235                 sym = symbols[self.options['signalling']][tuple(pins)]
236                 if self.state == 'GET BIT':
237                     self.get_bit(sym)
238                 elif self.state == 'GET EOP':
239                     self.get_eop(sym)
240             else:
241                 raise Exception('Invalid state: %s' % self.state)
242