]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/usb_signalling/pd.py
usb_signalling: decode RESET and Keep-Alive signalling conditions
[libsigrokdecode.git] / decoders / usb_signalling / pd.py
... / ...
CommitLineData
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
22import sigrokdecode as srd
23
24'''
25OUTPUT_PYTHON format:
26
27Packet:
28[<ptype>, <pdata>]
29
30<ptype>, <pdata>:
31 - 'SOP', None
32 - 'SYM', <sym>
33 - 'BIT', <bit>
34 - 'STUFF BIT', None
35 - 'EOP', None
36 - 'ERR', None
37 - 'KEEP ALIVE', None
38 - 'RESET', None
39
40<sym>:
41 - 'J', 'K', 'SE0', or 'SE1'
42
43<bit>:
44 - '0' or '1'
45 - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
46'''
47
48# Low-/full-speed symbols.
49# Note: Low-speed J and K are inverted compared to the full-speed J and K!
50symbols = {
51 'low-speed': {
52 # (<dp>, <dm>): <symbol/state>
53 (0, 0): 'SE0',
54 (1, 0): 'K',
55 (0, 1): 'J',
56 (1, 1): 'SE1',
57 },
58 'full-speed': {
59 # (<dp>, <dm>): <symbol/state>
60 (0, 0): 'SE0',
61 (1, 0): 'J',
62 (0, 1): 'K',
63 (1, 1): 'SE1',
64 },
65}
66
67bitrates = {
68 'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%)
69 'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
70}
71
72sym_annotation = {
73 'J': [0, ['J']],
74 'K': [1, ['K']],
75 'SE0': [2, ['SE0', '0']],
76 'SE1': [3, ['SE1', '1']],
77}
78
79class SamplerateError(Exception):
80 pass
81
82class Decoder(srd.Decoder):
83 api_version = 2
84 id = 'usb_signalling'
85 name = 'USB signalling'
86 longname = 'Universal Serial Bus (LS/FS) signalling'
87 desc = 'USB (low-speed and full-speed) signalling protocol.'
88 license = 'gplv2+'
89 inputs = ['logic']
90 outputs = ['usb_signalling']
91 channels = (
92 {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
93 {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
94 )
95 options = (
96 {'id': 'signalling', 'desc': 'Signalling',
97 'default': 'full-speed', 'values': ('full-speed', 'low-speed')},
98 )
99 annotations = (
100 ('sym-j', 'J symbol'),
101 ('sym-k', 'K symbol'),
102 ('sym-se0', 'SE0 symbol'),
103 ('sym-se1', 'SE1 symbol'),
104 ('sop', 'Start of packet (SOP)'),
105 ('eop', 'End of packet (EOP)'),
106 ('bit', 'Bit'),
107 ('stuffbit', 'Stuff bit'),
108 ('error', 'Error'),
109 ('keep-alive', 'Low-speed keep-alive'),
110 ('reset', 'Reset'),
111 )
112 annotation_rows = (
113 ('bits', 'Bits', (4, 5, 6, 7, 8, 9, 10)),
114 ('symbols', 'Symbols', (0, 1, 2, 3)),
115 )
116
117 def __init__(self):
118 self.samplerate = None
119 self.oldsym = 'J' # The "idle" state is J.
120 self.ss_block = None
121 self.samplenum = 0
122 self.bitrate = None
123 self.bitwidth = None
124 self.samplepos = None
125 self.samplenum_target = None
126 self.samplenum_edge = None
127 self.samplenum_lastedge = 0
128 self.oldpins = None
129 self.edgepins = None
130 self.consecutive_ones = 0
131 self.state = 'INIT'
132
133 def start(self):
134 self.out_python = self.register(srd.OUTPUT_PYTHON)
135 self.out_ann = self.register(srd.OUTPUT_ANN)
136
137 def metadata(self, key, value):
138 if key == srd.SRD_CONF_SAMPLERATE:
139 self.samplerate = value
140 self.bitrate = bitrates[self.options['signalling']]
141 self.bitwidth = float(self.samplerate) / float(self.bitrate)
142
143 def putpx(self, data):
144 s = self.samplenum_edge
145 self.put(s, s, self.out_python, data)
146
147 def putx(self, data):
148 s = self.samplenum_edge
149 self.put(s, s, self.out_ann, data)
150
151 def putpm(self, data):
152 e = self.samplenum_edge
153 self.put(self.ss_block, e, self.out_python, data)
154
155 def putm(self, data):
156 e = self.samplenum_edge
157 self.put(self.ss_block, e, self.out_ann, data)
158
159 def putpb(self, data):
160 s, e = self.samplenum_lastedge, self.samplenum_edge
161 self.put(s, e, self.out_python, data)
162
163 def putb(self, data):
164 s, e = self.samplenum_lastedge, self.samplenum_edge
165 self.put(s, e, self.out_ann, data)
166
167 def set_new_target_samplenum(self):
168 self.samplepos += self.bitwidth;
169 self.samplenum_target = int(self.samplepos)
170 self.samplenum_lastedge = self.samplenum_edge
171 self.samplenum_edge = int(self.samplepos - (self.bitwidth / 2))
172
173 def wait_for_sop(self, sym):
174 # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
175 if sym != 'K' or self.oldsym != 'J':
176 return
177 self.consecutive_ones = 0
178 self.samplepos = self.samplenum - (self.bitwidth / 2) + 0.5
179 self.set_new_target_samplenum()
180 self.putpx(['SOP', None])
181 self.putx([4, ['SOP', 'S']])
182 self.state = 'GET BIT'
183
184 def handle_bit(self, b):
185 if self.consecutive_ones == 6:
186 if b == '0':
187 # Stuff bit.
188 self.putpb(['STUFF BIT', None])
189 self.putb([7, ['Stuff bit: 0', 'SB: 0', '0']])
190 self.consecutive_ones = 0
191 else:
192 self.putpb(['ERR', None])
193 self.putb([8, ['Bit stuff error', 'BS ERR', 'B']])
194 self.state = 'IDLE'
195 else:
196 # Normal bit (not a stuff bit).
197 self.putpb(['BIT', b])
198 self.putb([6, ['%s' % b]])
199 if b == '1':
200 self.consecutive_ones += 1
201 else:
202 self.consecutive_ones = 0
203
204 def get_eop(self, sym):
205 # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
206 self.set_new_target_samplenum()
207 self.putpb(['SYM', sym])
208 self.putb(sym_annotation[sym])
209 self.oldsym = sym
210 if sym == 'SE0':
211 pass
212 elif sym == 'J':
213 # Got an EOP.
214 self.putpm(['EOP', None])
215 self.putm([5, ['EOP', 'E']])
216 self.state = 'IDLE'
217 self.bitwidth = float(self.samplerate) / float(self.bitrate)
218 else:
219 self.putpm(['ERR', None])
220 self.putm([8, ['EOP Error', 'EErr', 'E']])
221 self.state = 'IDLE'
222
223 def get_bit(self, sym):
224 self.set_new_target_samplenum()
225 if sym == 'SE0':
226 # Start of an EOP. Change state, save edge
227 self.state = 'GET EOP'
228 self.ss_block = self.samplenum_lastedge
229 else:
230 b = '0' if self.oldsym != sym else '1'
231 self.handle_bit(b)
232 self.putpb(['SYM', sym])
233 self.putb(sym_annotation[sym])
234 if self.oldsym != sym:
235 edgesym = symbols[self.options['signalling']][tuple(self.edgepins)]
236 if edgesym not in ('SE0', 'SE1'):
237 if edgesym == sym:
238 self.bitwidth = self.bitwidth - (0.001 * self.bitwidth)
239 self.samplepos = self.samplepos - (0.01 * self.bitwidth)
240 else:
241 self.bitwidth = self.bitwidth + (0.001 * self.bitwidth)
242 self.samplepos = self.samplepos + (0.01 * self.bitwidth)
243 self.oldsym = sym
244
245 def handle_idle(self, sym):
246 self.samplenum_edge = self.samplenum
247 se0_length = float(self.samplenum - self.samplenum_lastedge) / self.samplerate
248 if se0_length > 2.5e-6: # 2.5us
249 self.putpb(['RESET', None])
250 self.putb([10, ['Reset', 'Res', 'R']])
251 elif se0_length > 1.2e-6 and self.options['signalling'] == 'low-speed':
252 self.putpb(['KEEP ALIVE', None])
253 self.putb([9, ['Keep-alive', 'KA', 'A']])
254 self.state = 'IDLE'
255
256 def decode(self, ss, es, data):
257 if not self.samplerate:
258 raise SamplerateError('Cannot decode without samplerate.')
259 for (self.samplenum, pins) in data:
260 # State machine.
261 if self.state == 'IDLE':
262 # Ignore identical samples early on (for performance reasons).
263 if self.oldpins == pins:
264 continue
265 self.oldpins = pins
266 sym = symbols[self.options['signalling']][tuple(pins)]
267 if sym == 'SE0':
268 self.samplenum_lastedge = self.samplenum
269 self.state = 'WAIT IDLE'
270 else:
271 self.wait_for_sop(sym)
272 self.edgepins = pins
273 elif self.state in ('GET BIT', 'GET EOP'):
274 # Wait until we're in the middle of the desired bit.
275 if self.samplenum == self.samplenum_edge:
276 self.edgepins = pins
277 if self.samplenum < self.samplenum_target:
278 continue
279 sym = symbols[self.options['signalling']][tuple(pins)]
280 if self.state == 'GET BIT':
281 self.get_bit(sym)
282 elif self.state == 'GET EOP':
283 self.get_eop(sym)
284 elif self.state == 'WAIT IDLE':
285 if self.oldpins == pins:
286 continue
287 sym = symbols[self.options['signalling']][tuple(pins)]
288 if self.samplenum - self.samplenum_lastedge > 1:
289 self.handle_idle(sym)
290 else:
291 self.wait_for_sop(sym)
292 self.oldpins = pins
293 self.edgepins = pins
294 elif self.state == 'INIT':
295 sym = symbols[self.options['signalling']][tuple(pins)]
296 self.handle_idle(sym)
297 self.oldpins = pins