2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 of the License, or
9 ## (at your option) any later version.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 # Generic I2C filtering protocol decoder
22 import sigrokdecode as srd
24 class Decoder(srd.Decoder):
28 longname = 'I2C filter'
29 desc = 'Filter out addresses/directions in an I2C stream.'
36 'address': ['Address to filter out of the I2C stream', 0],
37 'direction': ['Direction to filter (read/write)', '']
41 def __init__(self, **kwargs):
44 def start(self, metadata):
45 self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2cdata')
46 if self.options['direction'] not in ('', 'read', 'write'):
47 raise Exception('Invalid direction: expected "read" or "write"')
52 def decode(self, ss, es, data):
55 except Exception as e:
56 raise Exception('Malformed I2C input: %s' % str(e)) from e
58 # Whichever state we're in, these always reset the state machine.
59 # This should make it easier to deal with corrupt data etc.
60 if cmd in ('START', 'START REPEAT'):
66 if cmd in ('ACK', 'NACK'):
67 # Don't care, we just want data.
70 if self.state == 'start':
71 # Start of a transfer, see if we want this one.
72 if cmd == 'ADDRESS READ' and self.options['direction'] == 'write':
74 elif cmd == 'ADDRESS WRITE' and self.options['direction'] == 'read':
76 elif cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
77 if self.options['address'] in (0, data):
78 # We want this tranfer.
79 self.state = 'transfer'
80 elif self.state == 'transfer':
81 if cmd in ('DATA READ', 'DATA WRITE'):
82 self.put(ss, es, self.out_proto, data)
84 raise Exception('Invalid state: %s' % self.state)