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