summaryrefslogtreecommitdiff
path: root/decoders/usb_signalling/pd.py
blob: c0e0141d7dcfd5554d3b10ece73180b3278ed968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd

'''
OUTPUT_PYTHON format:

Packet:
[<ptype>, <pdata>]

<ptype>, <pdata>:
 - 'SOP', None
 - 'SYM', <sym>
 - 'BIT', <bit>
 - 'STUFF BIT', None
 - 'EOP', None
 - 'ERR', None
 - 'KEEP ALIVE', None
 - 'RESET', None

<sym>:
 - 'J', 'K', 'SE0', or 'SE1'

<bit>:
 - '0' or '1'
 - Note: Symbols like SE0, SE1, and the J that's part of EOP don't yield 'BIT'.
'''

# Low-/full-speed symbols.
# Note: Low-speed J and K are inverted compared to the full-speed J and K!
symbols = {
    'low-speed': {
        # (<dp>, <dm>): <symbol/state>
        (0, 0): 'SE0',
        (1, 0): 'K',
        (0, 1): 'J',
        (1, 1): 'SE1',
    },
    'full-speed': {
        # (<dp>, <dm>): <symbol/state>
        (0, 0): 'SE0',
        (1, 0): 'J',
        (0, 1): 'K',
        (1, 1): 'SE1',
    },
    'automatic': {
        # (<dp>, <dm>): <symbol/state>
        (0, 0): 'SE0',
        (1, 0): 'FS_J',
        (0, 1): 'LS_J',
        (1, 1): 'SE1',
    },
    # After a PREamble PID, the bus segment between Host and Hub uses LS
    # signalling rate and FS signalling polarity (USB 2.0 spec, 11.8.4: "For
    # both upstream and downstream low-speed data, the hub is responsible for
    # inverting the polarity of the data before transmitting to/from a
    # low-speed port.").
    'low-speed-rp': {
        # (<dp>, <dm>): <symbol/state>
        (0, 0): 'SE0',
        (1, 0): 'J',
        (0, 1): 'K',
        (1, 1): 'SE1',
    },
}

bitrates = {
    'low-speed': 1500000, # 1.5Mb/s (+/- 1.5%)
    'low-speed-rp': 1500000, # 1.5Mb/s (+/- 1.5%)
    'full-speed': 12000000, # 12Mb/s (+/- 0.25%)
    'automatic': None
}

sym_annotation = {
    'J': [0, ['J']],
    'K': [1, ['K']],
    'SE0': [2, ['SE0', '0']],
    'SE1': [3, ['SE1', '1']],
}

class SamplerateError(Exception):
    pass

class Decoder(srd.Decoder):
    api_version = 3
    id = 'usb_signalling'
    name = 'USB signalling'
    longname = 'Universal Serial Bus (LS/FS) signalling'
    desc = 'USB (low-speed and full-speed) signalling protocol.'
    license = 'gplv2+'
    inputs = ['logic']
    outputs = ['usb_signalling']
    channels = (
        {'id': 'dp', 'name': 'D+', 'desc': 'USB D+ signal'},
        {'id': 'dm', 'name': 'D-', 'desc': 'USB D- signal'},
    )
    options = (
        {'id': 'signalling', 'desc': 'Signalling',
            'default': 'automatic', 'values': ('automatic', 'full-speed', 'low-speed')},
    )
    annotations = (
        ('sym-j', 'J symbol'),
        ('sym-k', 'K symbol'),
        ('sym-se0', 'SE0 symbol'),
        ('sym-se1', 'SE1 symbol'),
        ('sop', 'Start of packet (SOP)'),
        ('eop', 'End of packet (EOP)'),
        ('bit', 'Bit'),
        ('stuffbit', 'Stuff bit'),
        ('error', 'Error'),
        ('keep-alive', 'Low-speed keep-alive'),
        ('reset', 'Reset'),
    )
    annotation_rows = (
        ('bits', 'Bits', (4, 5, 6, 7, 8, 9, 10)),
        ('symbols', 'Symbols', (0, 1, 2, 3)),
    )

    def __init__(self):
        self.samplerate = None
        self.oldsym = 'J' # The "idle" state is J.
        self.ss_block = None
        self.samplenum = 0
        self.bitrate = None
        self.bitwidth = None
        self.samplepos = None
        self.samplenum_target = None
        self.samplenum_edge = None
        self.samplenum_lastedge = 0
        self.edgepins = None
        self.consecutive_ones = 0
        self.bits = None
        self.state = 'IDLE'

    def start(self):
        self.out_python = self.register(srd.OUTPUT_PYTHON)
        self.out_ann = self.register(srd.OUTPUT_ANN)

    def metadata(self, key, value):
        if key == srd.SRD_CONF_SAMPLERATE:
            self.samplerate = value
            self.signalling = self.options['signalling']
            if self.signalling != 'automatic':
                self.update_bitrate()

    def update_bitrate(self):
        self.bitrate = bitrates[self.signalling]
        self.bitwidth = float(self.samplerate) / float(self.bitrate)

    def putpx(self, data):
        s = self.samplenum_edge
        self.put(s, s, self.out_python, data)

    def putx(self, data):
        s = self.samplenum_edge
        self.put(s, s, self.out_ann, data)

    def putpm(self, data):
        e = self.samplenum_edge
        self.put(self.ss_block, e, self.out_python, data)

    def putm(self, data):
        e = self.samplenum_edge
        self.put(self.ss_block, e, self.out_ann, data)

    def putpb(self, data):
        s, e = self.samplenum_lastedge, self.samplenum_edge
        self.put(s, e, self.out_python, data)

    def putb(self, data):
        s, e = self.samplenum_lastedge, self.samplenum_edge
        self.put(s, e, self.out_ann, data)

    def set_new_target_samplenum(self):
        self.samplepos += self.bitwidth;
        self.samplenum_target = int(self.samplepos)
        self.samplenum_lastedge = self.samplenum_edge
        self.samplenum_edge = int(self.samplepos - (self.bitwidth / 2))

    def wait_for_sop(self, sym):
        # Wait for a Start of Packet (SOP), i.e. a J->K symbol change.
        if sym != 'K' or self.oldsym != 'J':
            return
        self.consecutive_ones = 0
        self.bits = ''
        self.update_bitrate()
        self.samplepos = self.samplenum - (self.bitwidth / 2) + 0.5
        self.set_new_target_samplenum()
        self.putpx(['SOP', None])
        self.putx([4, ['SOP', 'S']])
        self.state = 'GET BIT'

    def handle_bit(self, b):
        if self.consecutive_ones == 6:
            if b == '0':
                # Stuff bit.
                self.putpb(['STUFF BIT', None])
                self.putb([7, ['Stuff bit: 0', 'SB: 0', '0']])
                self.consecutive_ones = 0
            else:
                self.putpb(['ERR', None])
                self.putb([8, ['Bit stuff error', 'BS ERR', 'B']])
                self.state = 'IDLE'
        else:
            # Normal bit (not a stuff bit).
            self.putpb(['BIT', b])
            self.putb([6, ['%s' % b]])
            if b == '1':
                self.consecutive_ones += 1
            else:
                self.consecutive_ones = 0

    def get_eop(self, sym):
        # EOP: SE0 for >= 1 bittime (usually 2 bittimes), then J.
        self.set_new_target_samplenum()
        self.putpb(['SYM', sym])
        self.putb(sym_annotation[sym])
        self.oldsym = sym
        if sym == 'SE0':
            pass
        elif sym == 'J':
            # Got an EOP.
            self.putpm(['EOP', None])
            self.putm([5, ['EOP', 'E']])
            self.state = 'WAIT IDLE'
        else:
            self.putpm(['ERR', None])
            self.putm([8, ['EOP Error', 'EErr', 'E']])
            self.state = 'IDLE'

    def get_bit(self, sym):
        self.set_new_target_samplenum()
        b = '0' if self.oldsym != sym else '1'
        self.oldsym = sym
        if sym == 'SE0':
            # Start of an EOP. Change state, save edge
            self.state = 'GET EOP'
            self.ss_block = self.samplenum_lastedge
        else:
            self.handle_bit(b)
        self.putpb(['SYM', sym])
        self.putb(sym_annotation[sym])
        if len(self.bits) <= 16:
            self.bits += b
        if len(self.bits) == 16 and self.bits == '0000000100111100':
            # Sync and low-speed PREamble seen
            self.putpx(['EOP', None])
            self.state = 'IDLE'
            self.signalling = 'low-speed-rp'
            self.update_bitrate()
            self.oldsym = 'J'
        if b == '0':
            edgesym = symbols[self.signalling][tuple(self.edgepins)]
            if edgesym not in ('SE0', 'SE1'):
                if edgesym == sym:
                    self.bitwidth = self.bitwidth - (0.001 * self.bitwidth)
                    self.samplepos = self.samplepos - (0.01 * self.bitwidth)
                else:
                    self.bitwidth = self.bitwidth + (0.001 * self.bitwidth)
                    self.samplepos = self.samplepos + (0.01 * self.bitwidth)

    def handle_idle(self, sym):
        self.samplenum_edge = self.samplenum
        se0_length = float(self.samplenum - self.samplenum_lastedge) / self.samplerate
        if se0_length > 2.5e-6: # 2.5us
            self.putpb(['RESET', None])
            self.putb([10, ['Reset', 'Res', 'R']])
            self.signalling = self.options['signalling']
        elif se0_length > 1.2e-6 and self.signalling == 'low-speed':
            self.putpb(['KEEP ALIVE', None])
            self.putb([9, ['Keep-alive', 'KA', 'A']])

        if sym == 'FS_J':
            self.signalling = 'full-speed'
            self.update_bitrate()
        elif sym == 'LS_J':
            self.signalling = 'low-speed'
            self.update_bitrate()
        self.oldsym = 'J'
        self.state = 'IDLE'

    def decode(self):
        if not self.samplerate:
            raise SamplerateError('Cannot decode without samplerate.')

        # Seed internal state from the very first sample.
        pins = self.wait({'skip': 1})
        sym = symbols[self.options['signalling']][pins]
        self.handle_idle(sym)

        while True:
            # State machine.
            if self.state == 'IDLE':
                # Wait for any edge on either DP and/or DM.
                pins = self.wait([{0: 'e'}, {1: 'e'}])
                sym = symbols[self.signalling][pins]
                if sym == 'SE0':
                    self.samplenum_lastedge = self.samplenum
                    self.state = 'WAIT IDLE'
                else:
                    self.wait_for_sop(sym)
                self.edgepins = pins
            elif self.state in ('GET BIT', 'GET EOP'):
                # Wait until we're in the middle of the desired bit.
                self.edgepins = self.wait([{'skip': self.samplenum_edge - self.samplenum}])
                pins = self.wait([{'skip': self.samplenum_target - self.samplenum}])

                sym = symbols[self.signalling][pins]
                if self.state == 'GET BIT':
                    self.get_bit(sym)
                elif self.state == 'GET EOP':
                    self.get_eop(sym)
            elif self.state == 'WAIT IDLE':
                pins = self.wait({'skip': 1})
                if pins == (0, 0):
                    continue
                if self.samplenum - self.samplenum_lastedge > 1:
                    sym = symbols[self.options['signalling']][pins]
                    self.handle_idle(sym)
                else:
                    sym = symbols[self.signalling][pins]
                    self.wait_for_sop(sym)
                self.edgepins = pins