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'),
76 ('mem_read', 'Data read from memory'),
83 # Bytes for function command.
85 self.family_code = None
87 self.commands = commands_2432 # Use max command set until we know better.
90 self.out_ann = self.register(srd.OUTPUT_ANN)
91 self.out_binary = self.register(srd.OUTPUT_BINARY)
94 self.put(self.ss, self.es, self.out_ann, data)
96 def decode(self, ss, es, data):
99 if code == 'RESET/PRESENCE':
100 self.ss, self.es = ss, es
101 self.putx([0, ['Reset/presence: %s'
102 % ('true' if val else 'false')]])
105 self.ss, self.es = ss, es
106 self.family_code = val & 0xff
109 if self.family_code in family_codes:
110 self.family, self.commands = family_codes[val & 0xff]
111 s = 'is 0x%02x, %s detected' % (self.family_code, self.family)
113 s = '%x%02x unknown' % (self.family_code)
115 self.putx([0, ['ROM: 0x%016x (%s)' % (val, 'family code ' + s),
116 'ROM: 0x%016x (%s)' % (val, self.family)]])
119 self.bytes.append(val)
120 if 1 == len(self.bytes):
121 self.ss, self.es = ss, es
122 if val not in self.commands:
123 self.putx([0, ['Unrecognized command: 0x%02x' % val]])
125 self.putx([0, ['Function command: %s (0x%02x)'
126 % (self.commands[val], val)]])
127 elif 0x0f == self.bytes[0]: # Write scratchpad
128 if 2 == len(self.bytes):
130 elif 3 == len(self.bytes):
132 self.putx([0, ['Target address: 0x%04x'
133 % ((self.bytes[2] << 8) + self.bytes[1])]])
134 elif 4 == len(self.bytes):
136 elif 11 == len(self.bytes):
138 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
139 for n in self.bytes[3:11]))]])
140 elif 12 == len(self.bytes):
142 elif 13 == len(self.bytes):
144 self.putx([0, ['CRC: '
145 + ('ok' if crc16(self.bytes[0:11]) == (self.bytes[11]
146 + (self.bytes[12] << 8)) else 'error')]])
147 elif 0xaa == self.bytes[0]: # Read scratchpad
148 if 2 == len(self.bytes):
150 elif 3 == len(self.bytes):
152 self.putx([0, ['Target address: 0x%04x'
153 % ((self.bytes[2] << 8) + self.bytes[1])]])
154 elif 4 == len(self.bytes):
155 self.ss, self.es = ss, es
156 self.putx([0, ['Data status (E/S): 0x%02x'
158 elif 5 == len(self.bytes):
160 elif 12 == len(self.bytes):
162 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
163 for n in self.bytes[4:12]))]])
164 elif 13 == len(self.bytes):
166 elif 14 == len(self.bytes):
168 self.putx([0, ['CRC: '
169 + ('ok' if crc16(self.bytes[0:12]) == (self.bytes[12]
170 + (self.bytes[13] << 8)) else 'error')]])
171 elif 0x5a == self.bytes[0]: # Load first secret
172 if 2 == len(self.bytes):
174 elif 4 == len(self.bytes):
176 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
177 + (','.join(format(n, '#04x')
178 for n in self.bytes[1:4]))]])
179 elif 4 < len(self.bytes):
180 self.ss, self.es = ss, es
181 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
182 self.putx([0, ['End of operation']])
183 elif 0x33 == self.bytes[0]: # Compute next secret
184 if 2 == len(self.bytes):
186 elif 3 == len(self.bytes):
188 self.putx([0, ['Target address: 0x%04x'
189 % ((self.bytes[2] << 8) + self.bytes[1])]])
190 elif 3 < len(self.bytes):
191 self.ss, self.es = ss, es
192 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
193 self.putx([0, ['End of operation']])
194 elif 0x55 == self.bytes[0]: # Copy scratchpad
195 if 2 == len(self.bytes):
197 elif 4 == len(self.bytes):
199 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
200 + (','.join(format(n, '#04x')
201 for n in self.bytes[1:4]))]])
202 elif 5 == len(self.bytes):
204 elif 24 == len(self.bytes):
206 mac = ','.join(format(n, '#04x') for n in self.bytes[4:24])
207 self.putx([0, ['Message authentication code: ' + mac,
209 elif 24 < len(self.bytes):
210 self.ss, self.es = ss, es
211 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
212 self.putx([0, ['Operation succeeded']])
213 elif (0 == self.bytes[-1]):
214 self.putx([0, ['Operation failed']])
215 elif 0xa5 == self.bytes[0]: # Read authenticated page
216 if 2 == len(self.bytes):
218 elif 3 == len(self.bytes):
220 self.putx([0, ['Target address: 0x%04x'
221 % ((self.bytes[2] << 8) + self.bytes[1])]])
222 elif 4 == len(self.bytes):
224 elif 35 == len(self.bytes):
226 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
227 for n in self.bytes[3:35]))]])
228 elif 36 == len(self.bytes):
229 self.ss, self.es = ss, es
230 self.putx([0, ['Padding: '
231 + ('ok' if 0xff == self.bytes[-1] else 'error')]])
232 elif 37 == len(self.bytes):
234 elif 38 == len(self.bytes):
236 self.putx([0, ['CRC: '
237 + ('ok' if crc16(self.bytes[0:36]) == (self.bytes[36]
238 + (self.bytes[37] << 8)) else 'error')]])
239 elif 39 == len(self.bytes):
241 elif 58 == len(self.bytes):
243 mac = ','.join(format(n, '#04x') for n in self.bytes[38:58])
244 self.putx([0, ['Message authentication code: ' + mac,
246 elif 59 == len(self.bytes):
248 elif 60 == len(self.bytes):
250 self.putx([0, ['MAC CRC: '
251 + ('ok' if crc16(self.bytes[38:58]) == (self.bytes[58]
252 + (self.bytes[59] << 8)) else 'error')]])
253 elif 60 < len(self.bytes):
254 self.ss, self.es = ss, es
255 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
256 self.putx([0, ['Operation completed']])
257 elif 0xf0 == self.bytes[0]: # Read memory
258 if 2 == len(self.bytes):
260 elif 3 == len(self.bytes):
262 self.putx([0, ['Target address: 0x%04x'
263 % ((self.bytes[2] << 8) + self.bytes[1])]])
264 elif 3 < len(self.bytes):
265 self.ss, self.es = ss, es
266 self.putx([0, ['Data: 0x%02x' % (self.bytes[-1])]])
268 bdata = self.bytes[-1].to_bytes(1, byteorder='big')
269 self.put(ss, es, self.out_binary, [0, bdata])