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/3'
68 desc = 'Maxim DS243x series 1-Wire EEPROM protocol.'
70 inputs = ['onewire_network']
72 tags = ['IC', 'Memory']
74 ('text', 'Human-readable text'),
77 ('mem_read', 'Data read from memory'),
84 # Bytes for function command.
86 self.family_code = None
88 self.commands = commands_2432 # Use max command set until we know better.
91 self.out_ann = self.register(srd.OUTPUT_ANN)
92 self.out_binary = self.register(srd.OUTPUT_BINARY)
95 self.put(self.ss, self.es, self.out_ann, data)
97 def decode(self, ss, es, data):
100 if code == 'RESET/PRESENCE':
101 self.ss, self.es = ss, es
102 self.putx([0, ['Reset/presence: %s'
103 % ('true' if val else 'false')]])
106 self.ss, self.es = ss, es
107 self.family_code = val & 0xff
110 if self.family_code in family_codes:
111 self.family, self.commands = family_codes[val & 0xff]
112 s = 'is 0x%02x, %s detected' % (self.family_code, self.family)
114 s = '0x%02x unknown' % (self.family_code)
116 self.putx([0, ['ROM: 0x%016x (%s)' % (val, 'family code ' + s),
117 'ROM: 0x%016x (%s)' % (val, self.family)]])
120 self.bytes.append(val)
121 if 1 == len(self.bytes):
122 self.ss, self.es = ss, es
123 if val not in self.commands:
124 self.putx([0, ['Unrecognized command: 0x%02x' % val]])
126 self.putx([0, ['Function command: %s (0x%02x)'
127 % (self.commands[val], val)]])
128 elif 0x0f == self.bytes[0]: # Write scratchpad
129 if 2 == len(self.bytes):
131 elif 3 == len(self.bytes):
133 self.putx([0, ['Target address: 0x%04x'
134 % ((self.bytes[2] << 8) + self.bytes[1])]])
135 elif 4 == len(self.bytes):
137 elif 11 == len(self.bytes):
139 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
140 for n in self.bytes[3:11]))]])
141 elif 12 == len(self.bytes):
143 elif 13 == len(self.bytes):
145 self.putx([0, ['CRC: '
146 + ('ok' if crc16(self.bytes[0:11]) == (self.bytes[11]
147 + (self.bytes[12] << 8)) else 'error')]])
148 elif 0xaa == self.bytes[0]: # Read scratchpad
149 if 2 == len(self.bytes):
151 elif 3 == len(self.bytes):
153 self.putx([0, ['Target address: 0x%04x'
154 % ((self.bytes[2] << 8) + self.bytes[1])]])
155 elif 4 == len(self.bytes):
156 self.ss, self.es = ss, es
157 self.putx([0, ['Data status (E/S): 0x%02x'
159 elif 5 == len(self.bytes):
161 elif 12 == len(self.bytes):
163 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
164 for n in self.bytes[4:12]))]])
165 elif 13 == len(self.bytes):
167 elif 14 == len(self.bytes):
169 self.putx([0, ['CRC: '
170 + ('ok' if crc16(self.bytes[0:12]) == (self.bytes[12]
171 + (self.bytes[13] << 8)) else 'error')]])
172 elif 0x5a == self.bytes[0]: # Load first secret
173 if 2 == len(self.bytes):
175 elif 4 == len(self.bytes):
177 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
178 + (','.join(format(n, '#04x')
179 for n in self.bytes[1:4]))]])
180 elif 4 < len(self.bytes):
181 self.ss, self.es = ss, es
182 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
183 self.putx([0, ['End of operation']])
184 elif 0x33 == self.bytes[0]: # Compute next secret
185 if 2 == len(self.bytes):
187 elif 3 == len(self.bytes):
189 self.putx([0, ['Target address: 0x%04x'
190 % ((self.bytes[2] << 8) + self.bytes[1])]])
191 elif 3 < len(self.bytes):
192 self.ss, self.es = ss, es
193 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
194 self.putx([0, ['End of operation']])
195 elif 0x55 == self.bytes[0]: # Copy scratchpad
196 if 2 == len(self.bytes):
198 elif 4 == len(self.bytes):
200 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
201 + (','.join(format(n, '#04x')
202 for n in self.bytes[1:4]))]])
203 elif 5 == len(self.bytes):
205 elif 24 == len(self.bytes):
207 mac = ','.join(format(n, '#04x') for n in self.bytes[4:24])
208 self.putx([0, ['Message authentication code: ' + mac,
210 elif 24 < len(self.bytes):
211 self.ss, self.es = ss, es
212 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
213 self.putx([0, ['Operation succeeded']])
214 elif (0 == self.bytes[-1]):
215 self.putx([0, ['Operation failed']])
216 elif 0xa5 == self.bytes[0]: # Read authenticated page
217 if 2 == len(self.bytes):
219 elif 3 == len(self.bytes):
221 self.putx([0, ['Target address: 0x%04x'
222 % ((self.bytes[2] << 8) + self.bytes[1])]])
223 elif 4 == len(self.bytes):
225 elif 35 == len(self.bytes):
227 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
228 for n in self.bytes[3:35]))]])
229 elif 36 == len(self.bytes):
230 self.ss, self.es = ss, es
231 self.putx([0, ['Padding: '
232 + ('ok' if 0xff == self.bytes[-1] else 'error')]])
233 elif 37 == len(self.bytes):
235 elif 38 == len(self.bytes):
237 self.putx([0, ['CRC: '
238 + ('ok' if crc16(self.bytes[0:36]) == (self.bytes[36]
239 + (self.bytes[37] << 8)) else 'error')]])
240 elif 39 == len(self.bytes):
242 elif 58 == len(self.bytes):
244 mac = ','.join(format(n, '#04x') for n in self.bytes[38:58])
245 self.putx([0, ['Message authentication code: ' + mac,
247 elif 59 == len(self.bytes):
249 elif 60 == len(self.bytes):
251 self.putx([0, ['MAC CRC: '
252 + ('ok' if crc16(self.bytes[38:58]) == (self.bytes[58]
253 + (self.bytes[59] << 8)) else 'error')]])
254 elif 60 < len(self.bytes):
255 self.ss, self.es = ss, es
256 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
257 self.putx([0, ['Operation completed']])
258 elif 0xf0 == self.bytes[0]: # Read memory
259 if 2 == len(self.bytes):
261 elif 3 == len(self.bytes):
263 self.putx([0, ['Target address: 0x%04x'
264 % ((self.bytes[2] << 8) + self.bytes[1])]])
265 elif 3 < len(self.bytes):
266 self.ss, self.es = ss, es
267 self.putx([0, ['Data: 0x%02x' % (self.bytes[-1])]])
269 bdata = self.bytes[-1].to_bytes(1, byteorder='big')
270 self.put(ss, es, self.out_binary, [0, bdata])