]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2cdemux/i2cdemux.py
srd: rename extra_probes to optional_probes in all PDs
[libsigrokdecode.git] / decoders / i2cdemux / i2cdemux.py
1 ##
2 ## This file is part of the sigrok 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 #
22 # Generic I2C demultiplexing protocol decoder
23 #
24 # Takes an I2C stream as input and outputs multiple I2C streams, each stream
25 # containing only I2C packets for one specific I2C slave.
26 #
27
28 import sigrokdecode as srd
29
30 class Decoder(srd.Decoder):
31     api_version = 1
32     id = 'i2cdemux'
33     name = 'I2C demux'
34     longname = 'Generic I2C demultiplexer'
35     desc = 'TODO.'
36     longdesc = 'TODO.'
37     license = 'gplv2+'
38     inputs = ['i2c']
39     outputs = [] # TODO: Only known at run-time.
40     probes = []
41     optional_probes = []
42     options = {}
43     annotations = []
44
45     def __init__(self, **kwargs):
46         self.packets = [] # Local cache of I2C packets
47         self.slaves = [] # List of known slave addresses
48         self.stream = -1 # Current output stream
49         self.streamcount = 0 # Number of created output streams
50
51     def start(self, metadata):
52         self.out_proto = []
53
54     def report(self):
55         pass
56
57     # Grab I2C packets into a local cache, until an I2C STOP condition
58     # packet comes along. At some point before that STOP condition, there
59     # will have been an ADDRESS READ or ADDRESS WRITE which contains the
60     # I2C address of the slave that the master wants to talk to.
61     # We use this slave address to figure out which output stream should
62     # get the whole chunk of packets (from START to STOP).
63     def decode(self, ss, es, data):
64
65         cmd, databyte, ack = data
66
67         # Add the I2C packet to our local cache.
68         self.packets += [[ss, es, data]]
69
70         if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
71             if databyte in self.slaves:
72                 self.stream = self.slaves.index(databyte)
73                 return
74
75             # We're never seen this slave, add a new stream.
76             self.slaves += [databyte]
77             self.out_proto += [self.add(srd.OUTPUT_PROTO,
78                                'i2c-%s' % hex(databyte))]
79             self.stream = self.streamcount
80             self.streamcount += 1
81         elif cmd == 'STOP':
82             if self.stream == -1:
83                 raise Exception('Invalid stream!') # FIXME?
84
85             # Send the whole chunk of I2C packets to the correct stream.
86             for p in self.packets:
87                 self.put(p[0], p[1], self.out_proto[self.stream], p[2])
88
89             self.packets = []
90             self.stream = -1
91         else:
92             pass # Do nothing, only add the I2C packet to our cache.
93