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']
42 ('text', 'Human-readable text'),
49 # Bytes for function command.
53 self.out_ann = self.register(srd.OUTPUT_ANN)
56 self.put(self.ss, self.es, self.out_ann, data)
58 def decode(self, ss, es, data):
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')]])
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)]])
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]])
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):
82 elif 3 == len(self.bytes):
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):
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):
98 elif 3 == len(self.bytes):
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])]])
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):
113 elif 3 == len(self.bytes):
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):
123 elif 2 < len(self.bytes):
124 self.ss, self.es = ss, es
125 if 0xaa == self.bytes[-1]:
126 self.putx([0, ['Success']])
128 self.putx([0, ['Invalid byte']])