]>
Commit | Line | Data |
---|---|---|
d567790b | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
d567790b UH |
3 | ## |
4 | ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
d567790b UH |
21 | import sigrokdecode as srd |
22 | ||
23 | class Decoder(srd.Decoder): | |
24 | api_version = 1 | |
25 | id = 'i2cdemux' | |
25ac80b2 JH |
26 | name = 'I²C demux' |
27 | longname = 'I²C demultiplexer' | |
28 | desc = 'Demux I²C packets into per-slave-address streams.' | |
d567790b UH |
29 | license = 'gplv2+' |
30 | inputs = ['i2c'] | |
31 | outputs = [] # TODO: Only known at run-time. | |
32 | probes = [] | |
b77614bc | 33 | optional_probes = [] |
d567790b UH |
34 | options = {} |
35 | annotations = [] | |
36 | ||
37 | def __init__(self, **kwargs): | |
25ac80b2 | 38 | self.packets = [] # Local cache of I²C packets |
d567790b UH |
39 | self.slaves = [] # List of known slave addresses |
40 | self.stream = -1 # Current output stream | |
41 | self.streamcount = 0 # Number of created output streams | |
42 | ||
8915b346 | 43 | def start(self): |
c515eed7 | 44 | self.out_python = [] |
d567790b | 45 | |
25ac80b2 | 46 | # Grab I²C packets into a local cache, until an I²C STOP condition |
d567790b UH |
47 | # packet comes along. At some point before that STOP condition, there |
48 | # will have been an ADDRESS READ or ADDRESS WRITE which contains the | |
25ac80b2 | 49 | # I²C address of the slave that the master wants to talk to. |
d567790b UH |
50 | # We use this slave address to figure out which output stream should |
51 | # get the whole chunk of packets (from START to STOP). | |
52 | def decode(self, ss, es, data): | |
53 | ||
1b75abfd | 54 | cmd, databyte = data |
d567790b | 55 | |
25ac80b2 | 56 | # Add the I²C packet to our local cache. |
7f7ea759 | 57 | self.packets.append([ss, es, data]) |
d567790b UH |
58 | |
59 | if cmd in ('ADDRESS READ', 'ADDRESS WRITE'): | |
60 | if databyte in self.slaves: | |
61 | self.stream = self.slaves.index(databyte) | |
62 | return | |
63 | ||
64 | # We're never seen this slave, add a new stream. | |
7f7ea759 | 65 | self.slaves.append(databyte) |
c515eed7 UH |
66 | self.out_python.append(self.register(srd.OUTPUT_PYTHON, |
67 | proto_id='i2c-%s' % hex(databyte))) | |
d567790b UH |
68 | self.stream = self.streamcount |
69 | self.streamcount += 1 | |
70 | elif cmd == 'STOP': | |
71 | if self.stream == -1: | |
72 | raise Exception('Invalid stream!') # FIXME? | |
73 | ||
25ac80b2 | 74 | # Send the whole chunk of I²C packets to the correct stream. |
d567790b | 75 | for p in self.packets: |
c515eed7 | 76 | self.put(p[0], p[1], self.out_python[self.stream], p[2]) |
d567790b UH |
77 | |
78 | self.packets = [] | |
79 | self.stream = -1 | |
decde15e | 80 | else: |
25ac80b2 | 81 | pass # Do nothing, only add the I²C packet to our cache. |
d567790b | 82 |