]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2cdemux/pd.py
dd773b54359155ceaf2fc13750e9de7774e8499c
[libsigrokdecode.git] / decoders / i2cdemux / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
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
21 import sigrokdecode as srd
22
23 class Decoder(srd.Decoder):
24     api_version = 1
25     id = 'i2cdemux'
26     name = 'I²C demux'
27     longname = 'I²C demultiplexer'
28     desc = 'Demux I²C packets into per-slave-address streams.'
29     license = 'gplv2+'
30     inputs = ['i2c']
31     outputs = [] # TODO: Only known at run-time.
32     annotations = []
33
34     def __init__(self, **kwargs):
35         self.packets = [] # Local cache of I²C packets
36         self.slaves = [] # List of known slave addresses
37         self.stream = -1 # Current output stream
38         self.streamcount = 0 # Number of created output streams
39
40     def start(self):
41         self.out_python = []
42
43     # Grab I²C packets into a local cache, until an I²C STOP condition
44     # packet comes along. At some point before that STOP condition, there
45     # will have been an ADDRESS READ or ADDRESS WRITE which contains the
46     # I²C address of the slave that the master wants to talk to.
47     # We use this slave address to figure out which output stream should
48     # get the whole chunk of packets (from START to STOP).
49     def decode(self, ss, es, data):
50
51         cmd, databyte = data
52
53         # Add the I²C packet to our local cache.
54         self.packets.append([ss, es, data])
55
56         if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
57             if databyte in self.slaves:
58                 self.stream = self.slaves.index(databyte)
59                 return
60
61             # We're never seen this slave, add a new stream.
62             self.slaves.append(databyte)
63             self.out_python.append(self.register(srd.OUTPUT_PYTHON,
64                                    proto_id='i2c-%s' % hex(databyte)))
65             self.stream = self.streamcount
66             self.streamcount += 1
67         elif cmd == 'STOP':
68             if self.stream == -1:
69                 raise Exception('Invalid stream!') # FIXME?
70
71             # Send the whole chunk of I²C packets to the correct stream.
72             for p in self.packets:
73                 self.put(p[0], p[1], self.out_python[self.stream], p[2])
74
75             self.packets = []
76             self.stream = -1
77         else:
78             pass # Do nothing, only add the I²C packet to our cache.
79