]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2cfilter/pd.py
Probes, optional probes and annotations now take a tuple.
[libsigrokdecode.git] / decoders / i2cfilter / pd.py
CommitLineData
61c2bd36 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
61c2bd36
BV
3##
4## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
48eee789 5## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
61c2bd36
BV
6##
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 3 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, see <http://www.gnu.org/licenses/>.
19##
20
48eee789
UH
21# TODO: Support for filtering out multiple slave/direction pairs?
22
156509ca 23import sigrokdecode as srd
61c2bd36
BV
24
25class Decoder(srd.Decoder):
26 api_version = 1
27 id = 'i2cfilter'
bbe6d87f
JH
28 name = 'I²C filter'
29 longname = 'I²C filter'
30 desc = 'Filter out addresses/directions in an I²C stream.'
61c2bd36
BV
31 license = 'gplv3+'
32 inputs = ['i2c']
48eee789 33 outputs = ['i2c']
84c1c0b5
BV
34 options = (
35 {'id': 'address', 'desc': 'Address to filter out of the I²C stream',
36 'default': 0},
37 {'id': 'direction', 'desc': 'Direction to filter', 'default': 'both',
38 'values': ('read', 'write', 'both')}
39 )
61c2bd36
BV
40
41 def __init__(self, **kwargs):
42 self.state = None
48eee789
UH
43 self.curslave = -1
44 self.curdirection = None
bbe6d87f 45 self.packets = [] # Local cache of I²C packets
61c2bd36 46
8915b346 47 def start(self):
c515eed7 48 self.out_python = self.register(srd.OUTPUT_PYTHON, proto_id='i2c')
48eee789
UH
49 if self.options['address'] not in range(0, 127 + 1):
50 raise Exception('Invalid slave (must be 0..127).')
51 if self.options['direction'] not in ('both', 'read', 'write'):
52 raise Exception('Invalid direction (valid: read/write/both).')
61c2bd36 53
bbe6d87f 54 # Grab I²C packets into a local cache, until an I²C STOP condition
48eee789
UH
55 # packet comes along. At some point before that STOP condition, there
56 # will have been an ADDRESS READ or ADDRESS WRITE which contains the
bbe6d87f 57 # I²C address of the slave that the master wants to talk to.
48eee789
UH
58 # If that slave shall be filtered, output the cache (all packets from
59 # START to STOP) as proto 'i2c', otherwise drop it.
61c2bd36 60 def decode(self, ss, es, data):
61c2bd36 61
48eee789
UH
62 cmd, databyte = data
63
bbe6d87f 64 # Add the I²C packet to our local cache.
48eee789 65 self.packets.append([ss, es, data])
61c2bd36 66
48eee789
UH
67 if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
68 self.curslave = databyte
69 self.curdirection = cmd[8:].lower()
70 elif cmd in ('STOP', 'START REPEAT'):
71 # If this chunk was not for the correct slave, drop it.
72 if self.options['address'] == 0:
73 pass
74 elif self.curslave != self.options['address']:
75 self.packets = []
61c2bd36 76 return
48eee789
UH
77
78 # If this chunk was not in the right direction, drop it.
79 if self.options['direction'] == 'both':
80 pass
81 elif self.options['direction'] != self.curdirection:
82 self.packets = []
61c2bd36 83 return
48eee789
UH
84
85 # TODO: START->STOP chunks with both read and write (Repeat START)
bbe6d87f 86 # Otherwise, send out the whole chunk of I²C packets.
48eee789 87 for p in self.packets:
c515eed7 88 self.put(p[0], p[1], self.out_python, p[2])
48eee789
UH
89
90 self.packets = []
61c2bd36 91 else:
bbe6d87f 92 pass # Do nothing, only add the I²C packet to our cache.
61c2bd36 93