]>
Commit | Line | Data |
---|---|---|
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 | ||
156509ca | 21 | # Generic I2C filtering protocol decoder |
61c2bd36 | 22 | |
48eee789 UH |
23 | # TODO: Support for filtering out multiple slave/direction pairs? |
24 | ||
156509ca | 25 | import sigrokdecode as srd |
61c2bd36 BV |
26 | |
27 | class Decoder(srd.Decoder): | |
28 | api_version = 1 | |
29 | id = 'i2cfilter' | |
30 | name = 'I2C filter' | |
31 | longname = 'I2C filter' | |
a465436e | 32 | desc = 'Filter out addresses/directions in an I2C stream.' |
61c2bd36 BV |
33 | license = 'gplv3+' |
34 | inputs = ['i2c'] | |
48eee789 | 35 | outputs = ['i2c'] |
ee3e279c UH |
36 | probes = [] |
37 | optional_probes = [] | |
61c2bd36 BV |
38 | options = { |
39 | 'address': ['Address to filter out of the I2C stream', 0], | |
48eee789 | 40 | 'direction': ['Direction to filter (read/write/both)', 'both'] |
61c2bd36 | 41 | } |
ee3e279c | 42 | annotations = [] |
61c2bd36 BV |
43 | |
44 | def __init__(self, **kwargs): | |
45 | self.state = None | |
48eee789 UH |
46 | self.curslave = -1 |
47 | self.curdirection = None | |
48 | self.packets = [] # Local cache of I2C packets | |
61c2bd36 | 49 | |
8915b346 | 50 | def start(self): |
be465111 | 51 | self.out_proto = self.register(srd.OUTPUT_PYTHON, proto_id='i2c') |
48eee789 UH |
52 | if self.options['address'] not in range(0, 127 + 1): |
53 | raise Exception('Invalid slave (must be 0..127).') | |
54 | if self.options['direction'] not in ('both', 'read', 'write'): | |
55 | raise Exception('Invalid direction (valid: read/write/both).') | |
61c2bd36 | 56 | |
48eee789 UH |
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 | # If that slave shall be filtered, output the cache (all packets from | |
62 | # START to STOP) as proto 'i2c', otherwise drop it. | |
61c2bd36 | 63 | def decode(self, ss, es, data): |
61c2bd36 | 64 | |
48eee789 UH |
65 | cmd, databyte = data |
66 | ||
67 | # Add the I2C packet to our local cache. | |
68 | self.packets.append([ss, es, data]) | |
61c2bd36 | 69 | |
48eee789 UH |
70 | if cmd in ('ADDRESS READ', 'ADDRESS WRITE'): |
71 | self.curslave = databyte | |
72 | self.curdirection = cmd[8:].lower() | |
73 | elif cmd in ('STOP', 'START REPEAT'): | |
74 | # If this chunk was not for the correct slave, drop it. | |
75 | if self.options['address'] == 0: | |
76 | pass | |
77 | elif self.curslave != self.options['address']: | |
78 | self.packets = [] | |
61c2bd36 | 79 | return |
48eee789 UH |
80 | |
81 | # If this chunk was not in the right direction, drop it. | |
82 | if self.options['direction'] == 'both': | |
83 | pass | |
84 | elif self.options['direction'] != self.curdirection: | |
85 | self.packets = [] | |
61c2bd36 | 86 | return |
48eee789 UH |
87 | |
88 | # TODO: START->STOP chunks with both read and write (Repeat START) | |
89 | # Otherwise, send out the whole chunk of I2C packets. | |
90 | for p in self.packets: | |
91 | self.put(p[0], p[1], self.out_proto, p[2]) | |
92 | ||
93 | self.packets = [] | |
61c2bd36 | 94 | else: |
48eee789 | 95 | pass # Do nothing, only add the I2C packet to our cache. |
61c2bd36 | 96 |