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