]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ds2408/pd.py
33f2873fe9e092f276140cf501d85cf917a0cbec
[libsigrokdecode.git] / decoders / ds2408 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Mariusz Bialonczyk <manio@skyboo.net>
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 # Dictionary of FUNCTION commands and their names.
23 command = {
24     0xf0: 'Read PIO Registers',
25     0xf5: 'Channel Access Read',
26     0x5a: 'Channel Access Write',
27     0xcc: 'Write Conditional Search Register',
28     0xc3: 'Reset Activity Latches',
29     0x3c: 'Disable Test Mode',
30 }
31
32 class Decoder(srd.Decoder):
33     api_version = 3
34     id = 'ds2408'
35     name = 'DS2408'
36     longname = 'Maxim DS2408'
37     desc = '1-Wire 8-channel addressable switch.'
38     license = 'gplv2+'
39     inputs = ['onewire_network']
40     outputs = []
41     tags = ['Embedded/industrial', 'IC']
42     annotations = (
43         ('text', 'Human-readable text'),
44     )
45
46     def __init__(self):
47         self.reset()
48
49     def reset(self):
50         # Bytes for function command.
51         self.bytes = []
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 decode(self, ss, es, data):
60         code, val = data
61
62         if code == 'RESET/PRESENCE':
63             self.ss, self.es = ss, es
64             self.putx([0, ['Reset/presence: %s'
65                            % ('true' if val else 'false')]])
66             self.bytes = []
67         elif code == 'ROM':
68             self.ss, self.es = ss, es
69             family_code = val & 0xff
70             self.putx([0, ['ROM: 0x%016x (family code 0x%02x)' % (val, family_code)]])
71             self.bytes = []
72         elif code == 'DATA':
73             self.bytes.append(val)
74             if 1 == len(self.bytes):
75                 self.ss, self.es = ss, es
76                 if val not in command:
77                     self.putx([0, ['Unrecognized command: 0x%02x' % val]])
78                 else:
79                     self.putx([0, ['%s (0x%02x)' % (command[val], val)]])
80             elif 0xf0 == self.bytes[0]: # Read PIO Registers
81                 if 2 == len(self.bytes):
82                     self.ss = ss
83                 elif 3 == len(self.bytes):
84                     self.es = es
85                     self.putx([0, ['Target address: 0x%04x'
86                                    % ((self.bytes[2] << 8) + self.bytes[1])]])
87                 elif 3 < len(self.bytes):
88                     self.ss, self.es = ss, es
89                     self.putx([0, ['Data: 0x%02x' % self.bytes[-1]]])
90             elif 0xf5 == self.bytes[0]: # Channel Access Read
91                 if 2 == len(self.bytes):
92                     self.ss = ss
93                 elif 2 < len(self.bytes):
94                     self.ss, self.es = ss, es
95                     self.putx([0, ['PIO sample: 0x%02x' % self.bytes[-1]]])
96             elif 0x5a == self.bytes[0]: # Channel Access Write
97                 if 2 == len(self.bytes):
98                     self.ss = ss
99                 elif 3 == len(self.bytes):
100                     self.es = es
101                     if (self.bytes[-1] == (self.bytes[-2] ^ 0xff)):
102                       self.putx([0, ['Data: 0x%02x (bit-inversion correct: 0x%02x)' % (self.bytes[-2], self.bytes[-1])]])
103                     else:
104                       self.putx([0, ['Data error: second byte (0x%02x) is not bit-inverse of first (0x%02x)' % (self.bytes[-1], self.bytes[-2])]])
105                 elif 3 < len(self.bytes):
106                     self.ss, self.es = ss, es
107                     if 0xaa == self.bytes[-1]:
108                       self.putx([0, ['Success']])
109                     elif 0xff == self.bytes[-1]:
110                       self.putx([0, ['Fail New State']])
111             elif 0xcc == self.bytes[0]: # Write Conditional Search Register
112                 if 2 == len(self.bytes):
113                     self.ss = ss
114                 elif 3 == len(self.bytes):
115                     self.es = es
116                     self.putx([0, ['Target address: 0x%04x'
117                                    % ((self.bytes[2] << 8) + self.bytes[1])]])
118                 elif 3 < len(self.bytes):
119                     self.ss, self.es = ss, es
120                     self.putx([0, ['Data: 0x%02x' % self.bytes[-1]]])
121             elif 0xc3 == self.bytes[0]: # Reset Activity Latches
122                 if 2 == len(self.bytes):
123                     self.ss = ss
124                 elif 2 < len(self.bytes):
125                     self.ss, self.es = ss, es
126                     if 0xaa == self.bytes[-1]:
127                       self.putx([0, ['Success']])
128                     else:
129                       self.putx([0, ['Invalid byte']])