]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tca6408a/pd.py
a794547c3b772b3d0dafafb5a654ea32dd355980
[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, write to the Free Software
20 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 ##
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 2
27     id = 'tca6408a'
28     name = 'TI TCA6408A'
29     longname = 'Texas Instruments TCA6408A'
30     desc = 'Texas Instruments TCA6408A 8-bit I²C I/O expander.'
31     license = 'gplv2+'
32     inputs = ['i2c']
33     outputs = ['tca6408a']
34     annotations = (
35         ('register', 'Register type'),
36         ('value', 'Register value'),
37         ('warnings', 'Warning messages'),
38     )
39     annotation_rows = (
40         ('regs', 'Registers', (0, 1)),
41         ('warnings', 'Warnings', (2,)),
42     )
43
44     def __init__(self):
45         self.state = 'IDLE'
46         self.chip = -1
47
48     def start(self):
49         self.out_ann = self.register(srd.OUTPUT_ANN)
50
51     def putx(self, data):
52         self.put(self.ss, self.es, self.out_ann, data)
53
54     def handle_reg_0x00(self, b):
55         self.putx([1, ['State of inputs: %02X' % b]])
56
57     def handle_reg_0x01(self, b):
58         self.putx([1, ['Outputs set: %02X' % b ]])
59
60     def handle_reg_0x02(self, b):
61         self.putx([1, ['Polarity inverted: %02X' % b]])
62
63     def handle_reg_0x03(self, b):
64         self.putx([1, ['Configuration: %02X' % b]])
65
66     def handle_write_reg(self, b):
67         if b == 0:
68             self.putx([0, ['Input port', 'In', 'I']])
69         elif b == 1:
70             self.putx([0, ['Output port', 'Out', 'O']])
71         elif b == 2:
72             self.putx([0, ['Polarity inversion register', 'Pol', 'P']])
73         elif b == 3:
74             self.putx([0, ['Configuration register', 'Conf', 'C']])
75
76     def check_correct_chip(self, addr):
77         if addr not in (0x20, 0x21):
78             self.putx([2, ['Warning: I²C slave 0x%02X not a TCA6408A '
79                            'compatible chip.' % addr]])
80             self.state = 'IDLE'
81
82     def decode(self, ss, es, data):
83         cmd, databyte = data
84
85         # Store the start/end samples of this I²C packet.
86         self.ss, self.es = ss, es
87
88         # State machine.
89         if self.state == 'IDLE':
90             # Wait for an I²C START condition.
91             if cmd != 'START':
92                 return
93             self.state = 'GET SLAVE ADDR'
94         elif self.state == 'GET SLAVE ADDR':
95             self.chip = databyte  
96             self.state = 'GET REG ADDR'
97         elif self.state == 'GET REG ADDR':
98             # Wait for a data write (master selects the slave register).
99             if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
100                 self.check_correct_chip(databyte)
101             if cmd != 'DATA WRITE':
102                 return
103             self.reg = databyte
104             self.handle_write_reg(self.reg)
105             self.state = 'WRITE IO REGS'
106         elif self.state == 'WRITE IO REGS':
107             # If we see a Repeated Start here, the master wants to read.
108             if cmd == 'START REPEAT':
109                 self.state = 'READ IO REGS'
110                 return
111             # Otherwise: Get data bytes until a STOP condition occurs.
112             if cmd == 'DATA WRITE':
113                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
114                 handle_reg(databyte)
115             elif cmd == 'STOP':
116                 self.state = 'IDLE'
117                 self.chip = -1
118         elif self.state == 'READ IO REGS':
119             # Wait for an address read operation.
120             if cmd == 'ADDRESS READ':
121                 self.state = 'READ IO REGS2'
122                 self.chip = databyte
123                 return
124         elif self.state == 'READ IO REGS2':
125             if cmd == 'DATA READ':
126                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
127                 handle_reg(databyte)
128             elif cmd == 'STOP':
129                 self.state = 'IDLE'