]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ds2408/pd.py
05fcbff3d7def1f767ac48a19c438e50f6662549
[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 = ['ds2408']
41     annotations = (
42         ('text', 'Human-readable text'),
43     )
44
45     def __init__(self):
46         self.reset()
47
48     def reset(self):
49         # Bytes for function command.
50         self.bytes = []
51
52     def start(self):
53         self.out_ann = self.register(srd.OUTPUT_ANN)
54
55     def putx(self, data):
56         self.put(self.ss, self.es, self.out_ann, data)
57
58     def decode(self, ss, es, data):
59         code, val = data
60
61         if code == 'RESET/PRESENCE':
62             self.ss, self.es = ss, es
63             self.putx([0, ['Reset/presence: %s'
64                            % ('true' if val else 'false')]])
65             self.bytes = []
66         elif code == 'ROM':
67             self.ss, self.es = ss, es
68             family_code = val & 0xff
69             self.putx([0, ['ROM: 0x%016x (family code 0x%02x)' % (val, family_code)]])
70             self.bytes = []
71         elif code == 'DATA':
72             self.bytes.append(val)
73             if 1 == len(self.bytes):
74                 self.ss, self.es = ss, es
75                 if val not in command:
76                     self.putx([0, ['Unrecognized command: 0x%02x' % val]])
77                 else:
78                     self.putx([0, ['%s (0x%02x)' % (command[val], val)]])
79             elif 0xf0 == self.bytes[0]: # Read PIO Registers
80                 if 2 == len(self.bytes):
81                     self.ss = ss
82                 elif 3 == len(self.bytes):
83                     self.es = es
84                     self.putx([0, ['Target address: 0x%04x'
85                                    % ((self.bytes[2] << 8) + self.bytes[1])]])
86                 elif 3 < len(self.bytes):
87                     self.ss, self.es = ss, es
88                     self.putx([0, ['Data: 0x%02x' % self.bytes[-1]]])
89             elif 0xf5 == self.bytes[0]: # Channel Access Read
90                 if 2 == len(self.bytes):
91                     self.ss = ss
92                 elif 2 < len(self.bytes):
93                     self.ss, self.es = ss, es
94                     self.putx([0, ['PIO sample: 0x%02x' % self.bytes[-1]]])
95             elif 0x5a == self.bytes[0]: # Channel Access Write
96                 if 2 == len(self.bytes):
97                     self.ss = ss
98                 elif 3 == len(self.bytes):
99                     self.es = es
100                     if (self.bytes[-1] == (self.bytes[-2] ^ 0xff)):
101                       self.putx([0, ['Data: 0x%02x (bit-inversion correct: 0x%02x)' % (self.bytes[-2], self.bytes[-1])]])
102                     else:
103                       self.putx([0, ['Data error: second byte (0x%02x) is not bit-inverse of first (0x%02x)' % (self.bytes[-1], self.bytes[-2])]])
104                 elif 3 < len(self.bytes):
105                     self.ss, self.es = ss, es
106                     if 0xaa == self.bytes[-1]:
107                       self.putx([0, ['Success']])
108                     elif 0xff == self.bytes[-1]:
109                       self.putx([0, ['Fail New State']])
110             elif 0xcc == self.bytes[0]: # Write Conditional Search Register
111                 if 2 == len(self.bytes):
112                     self.ss = ss
113                 elif 3 == len(self.bytes):
114                     self.es = es
115                     self.putx([0, ['Target address: 0x%04x'
116                                    % ((self.bytes[2] << 8) + self.bytes[1])]])
117                 elif 3 < len(self.bytes):
118                     self.ss, self.es = ss, es
119                     self.putx([0, ['Data: 0x%02x' % self.bytes[-1]]])
120             elif 0xc3 == self.bytes[0]: # Reset Activity Latches
121                 if 2 == len(self.bytes):
122                     self.ss = ss
123                 elif 2 < len(self.bytes):
124                     self.ss, self.es = ss, es
125                     if 0xaa == self.bytes[-1]:
126                       self.putx([0, ['Success']])
127                     else:
128                       self.putx([0, ['Invalid byte']])