]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tca6408a/pd.py
Remove samplerate from srd_decoder_logic_output_channel
[libsigrokdecode.git] / decoders / tca6408a / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 ## Copyright (C) 2013 Matt Ranostay <mranostay@gmail.com>
6 ## Copyright (C) 2014 alberink <alberink@stampfini.org>
7 ##
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 ##
21
22 import sigrokdecode as srd
23
24 NUM_OUTPUT_CHANNELS = 8
25
26 def logic_channels(num_channels):
27     l = []
28     for i in range(num_channels):
29         l.append(tuple(['p%d' % i, 'P-port input/output %d' % i]))
30     return tuple(l)
31
32 class Decoder(srd.Decoder):
33     api_version = 3
34     id = 'tca6408a'
35     name = 'TI TCA6408A'
36     longname = 'Texas Instruments TCA6408A'
37     desc = 'Texas Instruments TCA6408A 8-bit I²C I/O expander.'
38     license = 'gplv2+'
39     inputs = ['i2c']
40     outputs = []
41     tags = ['Embedded/industrial', 'IC']
42     annotations = (
43         ('register', 'Register type'),
44         ('value', 'Register value'),
45         ('warning', 'Warning'),
46     )
47     logic_output_channels = logic_channels(NUM_OUTPUT_CHANNELS)
48     annotation_rows = (
49         ('regs', 'Registers', (0, 1)),
50         ('warnings', 'Warnings', (2,)),
51     )
52
53     def __init__(self):
54         self.reset()
55
56     def reset(self):
57         self.state = 'IDLE'
58         self.chip = -1
59         self.ss_logic = -1
60
61     def start(self):
62         self.out_ann = self.register(srd.OUTPUT_ANN)
63         self.out_logic = self.register(srd.OUTPUT_LOGIC)
64
65     def putx(self, data):
66         self.put(self.ss, self.es, self.out_ann, data)
67
68     def putl(self, data):
69         self.put(self.ss_logic, self.ss_logic, self.out_logic, data)
70
71     def handle_reg_0x00(self, b):
72         self.putx([1, ['State of inputs: %02X' % b]])
73         # TODO
74
75     def handle_reg_0x01(self, b):
76         self.putx([1, ['Outputs set: %02X' % b]])
77         self.ss_logic = self.ss
78         for i in range(NUM_OUTPUT_CHANNELS):
79             bit = (b & (1 << i)) != 0
80             self.putl([i, bytes([bit])])
81
82     def handle_reg_0x02(self, b):
83         self.putx([1, ['Polarity inverted: %02X' % b]])
84
85     def handle_reg_0x03(self, b):
86         self.putx([1, ['Configuration: %02X' % b]])
87
88     def handle_write_reg(self, b):
89         if b == 0:
90             self.putx([0, ['Input port', 'In', 'I']])
91         elif b == 1:
92             self.putx([0, ['Output port', 'Out', 'O']])
93         elif b == 2:
94             self.putx([0, ['Polarity inversion register', 'Pol', 'P']])
95         elif b == 3:
96             self.putx([0, ['Configuration register', 'Conf', 'C']])
97
98     def check_correct_chip(self, addr):
99         if addr not in (0x20, 0x21):
100             self.putx([2, ['Warning: I²C slave 0x%02X not a TCA6408A '
101                            'compatible chip.' % addr]])
102             self.state = 'IDLE'
103
104     def decode(self, ss, es, data):
105         cmd, databyte = data
106
107         # Store the start/end samples of this I²C packet.
108         self.ss, self.es = ss, es
109
110         # State machine.
111         if self.state == 'IDLE':
112             # Wait for an I²C START condition.
113             if cmd != 'START':
114                 return
115             self.state = 'GET SLAVE ADDR'
116         elif self.state == 'GET SLAVE ADDR':
117             self.chip = databyte
118             self.state = 'GET REG ADDR'
119         elif self.state == 'GET REG ADDR':
120             # Wait for a data write (master selects the slave register).
121             if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
122                 self.check_correct_chip(databyte)
123             if cmd != 'DATA WRITE':
124                 return
125             self.reg = databyte
126             self.handle_write_reg(self.reg)
127             self.state = 'WRITE IO REGS'
128         elif self.state == 'WRITE IO REGS':
129             # If we see a Repeated Start here, the master wants to read.
130             if cmd == 'START REPEAT':
131                 self.state = 'READ IO REGS'
132                 return
133             # Otherwise: Get data bytes until a STOP condition occurs.
134             if cmd == 'DATA WRITE':
135                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
136                 handle_reg(databyte)
137             elif cmd == 'STOP':
138                 self.state = 'IDLE'
139                 self.chip = -1
140         elif self.state == 'READ IO REGS':
141             # Wait for an address read operation.
142             if cmd == 'ADDRESS READ':
143                 self.state = 'READ IO REGS2'
144                 self.chip = databyte
145                 return
146         elif self.state == 'READ IO REGS2':
147             if cmd == 'DATA READ':
148                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
149                 handle_reg(databyte)
150             elif cmd == 'STOP':
151                 self.state = 'IDLE'