]> sigrok.org Git - libsigrokdecode.git/blob - decoders/pca9571/pd.py
Add an NXP PCA9571 decoder.
[libsigrokdecode.git] / decoders / pca9571 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Mickael Bosch <mickael.bosch@linux.com>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 NUM_OUTPUT_CHANNELS = 8
23
24 # TODO: Other I²C functions: general call / reset address, device ID address.
25
26 class Decoder(srd.Decoder):
27     api_version = 3
28     id = 'pca9571'
29     name = 'NXP PCA9571'
30     longname = 'NXP Semiconductors PCA9571'
31     desc = 'NXP PCA9571 8-bit I²C output expander.'
32     license = 'gplv2+'
33     inputs = ['i2c']
34     outputs = []
35     tags = ['Embedded/industrial', 'IC']
36     annotations = (
37         ('register', 'Register type'),
38         ('value', 'Register value'),
39         ('warning', 'Warning messages'),
40     )
41     annotation_rows = (
42         ('regs', 'Registers', (0, 1)),
43         ('warnings', 'Warnings', (2,)),
44     )
45
46     def __init__(self):
47         self.reset()
48
49     def reset(self):
50         self.state = 'IDLE'
51         self.last_write = 0xFF # Chip port default state is high.
52
53     def start(self):
54         self.out_ann = self.register(srd.OUTPUT_ANN)
55
56     def putx(self, data):
57         self.put(self.ss, self.es, self.out_ann, data)
58
59     def handle_io(self, b):
60         if self.state == 'READ DATA':
61             operation = ['Outputs read', 'R']
62             if b != self.last_write:
63                 self.putx([2, ['Warning: read value and last write value '
64                                '(%02X) are different' % self.last_write]])
65         else:
66             operation = ['Outputs set', 'W']
67             self.last_write = b
68         self.putx([1, [operation[0] + ': %02X' % b,
69                        operation[1] + ': %02X' % b]])
70
71     def check_correct_chip(self, addr):
72         if addr != 0x25:
73             self.putx([2, ['Warning: I²C slave 0x%02X not a PCA9571 '
74                            'compatible chip.' % addr]])
75             return False
76         return True
77
78     def decode(self, ss, es, data):
79         cmd, databyte = data
80         self.ss, self.es = ss, es
81
82         # State machine.
83         if cmd in ('ACK', 'BITS'): # Discard 'ACK' and 'BITS'.
84             pass
85         elif cmd in ('START', 'START REPEAT'): # Start a communication.
86             self.state = 'GET SLAVE ADDR'
87         elif cmd in ('NACK', 'STOP'): # Reset the state machine.
88             self.state = 'IDLE'
89         elif cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
90             if ((self.state == 'GET SLAVE ADDR') and
91                     self.check_correct_chip(databyte)):
92                 if cmd == 'ADDRESS READ':
93                     self.state = 'READ DATA'
94                 else:
95                     self.state = 'WRITE DATA'
96             else:
97                 self.state = 'IDLE'
98         elif cmd in ('DATA READ', 'DATA WRITE'):
99             if self.state in ('READ DATA', 'WRITE DATA'):
100                 self.handle_io(databyte)
101             else:
102                 self.state = 'IDLE'