2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
5 ## Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
21 import sigrokdecode as srd
23 # Dictionary of FUNCTION commands and their names.
25 0x0f: 'Write scratchpad',
26 0xaa: 'Read scratchpad',
27 0x55: 'Copy scratchpad',
29 0x5a: 'Load first secret',
30 0x33: 'Compute next secret',
31 0xa5: 'Read authenticated page',
35 0x0f: 'Write scratchpad',
36 0xaa: 'Read scratchpad',
37 0x55: 'Copy scratchpad',
41 # Maxim DS243x family code, present at the end of the ROM code.
43 0x33: ('DS2432', commands_2432),
44 0x23: ('DS2433', commands_2433),
47 # Calculate the CRC-16 checksum.
48 # Initial value: 0x0000, xor-in: 0x0000, polynom 0x8005, xor-out: 0xffff.
49 def crc16(byte_array):
50 reverse = 0xa001 # Use the reverse polynom to make algo simpler.
51 crc = 0x0000 # Initial value.
52 # Reverse CRC calculation.
53 for byte in byte_array:
56 crc = (crc >> 1) ^ reverse
60 crc ^= 0xffff # Invert CRC.
63 class Decoder(srd.Decoder):
67 longname = 'Maxim DS2432/2433'
68 desc = 'Maxim DS243x series 1-Wire EEPROM protocol.'
70 inputs = ['onewire_network']
73 ('text', 'Human-readable text'),
77 # Bytes for function command.
79 self.family_code = None
81 self.commands = commands_2432 # Use max command set until we know better.
84 self.out_ann = self.register(srd.OUTPUT_ANN)
87 self.put(self.ss, self.es, self.out_ann, data)
89 def decode(self, ss, es, data):
92 if code == 'RESET/PRESENCE':
93 self.ss, self.es = ss, es
94 self.putx([0, ['Reset/presence: %s'
95 % ('true' if val else 'false')]])
98 self.ss, self.es = ss, es
99 self.family_code = val & 0xff
102 if self.family_code in family_codes:
103 self.family, self.commands = family_codes[val & 0xff]
104 s = 'is 0x%02x, %s detected' % (self.family_code, self.family)
106 s = '%x%02x unknown' % (self.family_code)
108 self.putx([0, ['ROM: 0x%016x (%s)' % (val, 'family code ' + s),
109 'ROM: 0x%016x (%s)' % (val, self.family)]])
112 self.bytes.append(val)
113 if 1 == len(self.bytes):
114 self.ss, self.es = ss, es
115 if val not in self.commands:
116 self.putx([0, ['Unrecognized command: 0x%02x' % val]])
118 self.putx([0, ['Function command: %s (0x%02x)'
119 % (self.commands[val], val)]])
120 elif 0x0f == self.bytes[0]: # Write scratchpad
121 if 2 == len(self.bytes):
123 elif 3 == len(self.bytes):
125 self.putx([0, ['Target address: 0x%04x'
126 % ((self.bytes[2] << 8) + self.bytes[1])]])
127 elif 4 == len(self.bytes):
129 elif 11 == len(self.bytes):
131 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
132 for n in self.bytes[3:11]))]])
133 elif 12 == len(self.bytes):
135 elif 13 == len(self.bytes):
137 self.putx([0, ['CRC: '
138 + ('ok' if crc16(self.bytes[0:11]) == (self.bytes[11]
139 + (self.bytes[12] << 8)) else 'error')]])
140 elif 0xaa == self.bytes[0]: # Read scratchpad
141 if 2 == len(self.bytes):
143 elif 3 == len(self.bytes):
145 self.putx([0, ['Target address: 0x%04x'
146 % ((self.bytes[2] << 8) + self.bytes[1])]])
147 elif 4 == len(self.bytes):
148 self.ss, self.es = ss, es
149 self.putx([0, ['Data status (E/S): 0x%02x'
151 elif 5 == len(self.bytes):
153 elif 12 == len(self.bytes):
155 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
156 for n in self.bytes[4:12]))]])
157 elif 13 == len(self.bytes):
159 elif 14 == len(self.bytes):
161 self.putx([0, ['CRC: '
162 + ('ok' if crc16(self.bytes[0:12]) == (self.bytes[12]
163 + (self.bytes[13] << 8)) else 'error')]])
164 elif 0x5a == self.bytes[0]: # Load first secret
165 if 2 == len(self.bytes):
167 elif 4 == len(self.bytes):
169 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
170 + (','.join(format(n, '#04x')
171 for n in self.bytes[1:4]))]])
172 elif 4 < len(self.bytes):
173 self.ss, self.es = ss, es
174 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
175 self.putx([0, ['End of operation']])
176 elif 0x33 == self.bytes[0]: # Compute next secret
177 if 2 == len(self.bytes):
179 elif 3 == len(self.bytes):
181 self.putx([0, ['Target address: 0x%04x'
182 % ((self.bytes[2] << 8) + self.bytes[1])]])
183 elif 3 < len(self.bytes):
184 self.ss, self.es = ss, es
185 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
186 self.putx([0, ['End of operation']])
187 elif 0x55 == self.bytes[0]: # Copy scratchpad
188 if 2 == len(self.bytes):
190 elif 4 == len(self.bytes):
192 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
193 + (','.join(format(n, '#04x')
194 for n in self.bytes[1:4]))]])
195 elif 5 == len(self.bytes):
197 elif 24 == len(self.bytes):
199 mac = ','.join(format(n, '#04x') for n in self.bytes[4:24])
200 self.putx([0, ['Message authentication code: ' + mac,
202 elif 24 < len(self.bytes):
203 self.ss, self.es = ss, es
204 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
205 self.putx([0, ['Operation succeeded']])
206 elif (0 == self.bytes[-1]):
207 self.putx([0, ['Operation failed']])
208 elif 0xa5 == self.bytes[0]: # Read authenticated page
209 if 2 == len(self.bytes):
211 elif 3 == len(self.bytes):
213 self.putx([0, ['Target address: 0x%04x'
214 % ((self.bytes[2] << 8) + self.bytes[1])]])
215 elif 4 == len(self.bytes):
217 elif 35 == len(self.bytes):
219 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
220 for n in self.bytes[3:35]))]])
221 elif 36 == len(self.bytes):
222 self.ss, self.es = ss, es
223 self.putx([0, ['Padding: '
224 + ('ok' if 0xff == self.bytes[-1] else 'error')]])
225 elif 37 == len(self.bytes):
227 elif 38 == len(self.bytes):
229 self.putx([0, ['CRC: '
230 + ('ok' if crc16(self.bytes[0:36]) == (self.bytes[36]
231 + (self.bytes[37] << 8)) else 'error')]])
232 elif 39 == len(self.bytes):
234 elif 58 == len(self.bytes):
236 mac = ','.join(format(n, '#04x') for n in self.bytes[38:58])
237 self.putx([0, ['Message authentication code: ' + mac,
239 elif 59 == len(self.bytes):
241 elif 60 == len(self.bytes):
243 self.putx([0, ['MAC CRC: '
244 + ('ok' if crc16(self.bytes[38:58]) == (self.bytes[58]
245 + (self.bytes[59] << 8)) else 'error')]])
246 elif 60 < len(self.bytes):
247 self.ss, self.es = ss, es
248 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
249 self.putx([0, ['Operation completed']])
250 elif 0xf0 == self.bytes[0]: # Read memory
251 if 2 == len(self.bytes):
253 elif 3 == len(self.bytes):
255 self.putx([0, ['Target address: 0x%04x'
256 % ((self.bytes[2] << 8) + self.bytes[1])]])
257 elif 3 < len(self.bytes):
258 self.ss, self.es = ss, es
259 self.putx([0, ['Data: 0x%02x' % (self.bytes[-1])]])