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