2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Mariusz Bialonczyk <manio@skyboo.net>
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.
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.
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/>.
20 import sigrokdecode as srd
22 # Dictionary of FUNCTION commands and their names.
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',
32 class Decoder(srd.Decoder):
36 longname = 'Maxim DS2408'
37 desc = '1-Wire 8-channel addressable switch.'
39 inputs = ['onewire_network']
41 tags = ['Embedded/industrial', 'IC']
43 ('text', 'Human-readable text'),
50 # Bytes for function command.
54 self.out_ann = self.register(srd.OUTPUT_ANN)
57 self.put(self.ss, self.es, self.out_ann, data)
59 def decode(self, ss, es, data):
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')]])
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)]])
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]])
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):
83 elif 3 == len(self.bytes):
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):
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):
99 elif 3 == len(self.bytes):
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])]])
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):
114 elif 3 == len(self.bytes):
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):
124 elif 2 < len(self.bytes):
125 self.ss, self.es = ss, es
126 if 0xaa == self.bytes[-1]:
127 self.putx([0, ['Success']])
129 self.putx([0, ['Invalid byte']])