]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 | ||
21 | # TODO: Support for filtering out multiple slave/direction pairs? | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
25 | class Decoder(srd.Decoder): | |
26 | api_version = 1 | |
27 | id = 'i2cfilter' | |
28 | name = 'I²C filter' | |
29 | longname = 'I²C filter' | |
30 | desc = 'Filter out addresses/directions in an I²C stream.' | |
31 | license = 'gplv3+' | |
32 | inputs = ['i2c'] | |
33 | outputs = ['i2c'] | |
34 | probes = [] | |
35 | optional_probes = [] | |
36 | options = { | |
37 | 'address': ['Address to filter out of the I²C stream', 0], | |
38 | 'direction': ['Direction to filter (read/write/both)', 'both'] | |
39 | } | |
40 | annotations = [] | |
41 | ||
42 | def __init__(self, **kwargs): | |
43 | self.state = None | |
44 | self.curslave = -1 | |
45 | self.curdirection = None | |
46 | self.packets = [] # Local cache of I²C packets | |
47 | ||
48 | def start(self): | |
49 | self.out_proto = self.register(srd.OUTPUT_PYTHON, proto_id='i2c') | |
50 | if self.options['address'] not in range(0, 127 + 1): | |
51 | raise Exception('Invalid slave (must be 0..127).') | |
52 | if self.options['direction'] not in ('both', 'read', 'write'): | |
53 | raise Exception('Invalid direction (valid: read/write/both).') | |
54 | ||
55 | # Grab I²C packets into a local cache, until an I²C STOP condition | |
56 | # packet comes along. At some point before that STOP condition, there | |
57 | # will have been an ADDRESS READ or ADDRESS WRITE which contains the | |
58 | # I²C address of the slave that the master wants to talk to. | |
59 | # If that slave shall be filtered, output the cache (all packets from | |
60 | # START to STOP) as proto 'i2c', otherwise drop it. | |
61 | def decode(self, ss, es, data): | |
62 | ||
63 | cmd, databyte = data | |
64 | ||
65 | # Add the I²C packet to our local cache. | |
66 | self.packets.append([ss, es, data]) | |
67 | ||
68 | if cmd in ('ADDRESS READ', 'ADDRESS WRITE'): | |
69 | self.curslave = databyte | |
70 | self.curdirection = cmd[8:].lower() | |
71 | elif cmd in ('STOP', 'START REPEAT'): | |
72 | # If this chunk was not for the correct slave, drop it. | |
73 | if self.options['address'] == 0: | |
74 | pass | |
75 | elif self.curslave != self.options['address']: | |
76 | self.packets = [] | |
77 | return | |
78 | ||
79 | # If this chunk was not in the right direction, drop it. | |
80 | if self.options['direction'] == 'both': | |
81 | pass | |
82 | elif self.options['direction'] != self.curdirection: | |
83 | self.packets = [] | |
84 | return | |
85 | ||
86 | # TODO: START->STOP chunks with both read and write (Repeat START) | |
87 | # Otherwise, send out the whole chunk of I²C packets. | |
88 | for p in self.packets: | |
89 | self.put(p[0], p[1], self.out_proto, p[2]) | |
90 | ||
91 | self.packets = [] | |
92 | else: | |
93 | pass # Do nothing, only add the I²C packet to our cache. | |
94 |