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'),
80 # Bytes for function command.
82 self.family_code = None
84 self.commands = commands_2432 # Use max command set until we know better.
87 self.out_ann = self.register(srd.OUTPUT_ANN)
88 self.out_binary = self.register(srd.OUTPUT_BINARY)
91 self.put(self.ss, self.es, self.out_ann, data)
93 def decode(self, ss, es, data):
96 if code == 'RESET/PRESENCE':
97 self.ss, self.es = ss, es
98 self.putx([0, ['Reset/presence: %s'
99 % ('true' if val else 'false')]])
102 self.ss, self.es = ss, es
103 self.family_code = val & 0xff
106 if self.family_code in family_codes:
107 self.family, self.commands = family_codes[val & 0xff]
108 s = 'is 0x%02x, %s detected' % (self.family_code, self.family)
110 s = '%x%02x unknown' % (self.family_code)
112 self.putx([0, ['ROM: 0x%016x (%s)' % (val, 'family code ' + s),
113 'ROM: 0x%016x (%s)' % (val, self.family)]])
116 self.bytes.append(val)
117 if 1 == len(self.bytes):
118 self.ss, self.es = ss, es
119 if val not in self.commands:
120 self.putx([0, ['Unrecognized command: 0x%02x' % val]])
122 self.putx([0, ['Function command: %s (0x%02x)'
123 % (self.commands[val], val)]])
124 elif 0x0f == self.bytes[0]: # Write scratchpad
125 if 2 == len(self.bytes):
127 elif 3 == len(self.bytes):
129 self.putx([0, ['Target address: 0x%04x'
130 % ((self.bytes[2] << 8) + self.bytes[1])]])
131 elif 4 == len(self.bytes):
133 elif 11 == len(self.bytes):
135 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
136 for n in self.bytes[3:11]))]])
137 elif 12 == len(self.bytes):
139 elif 13 == len(self.bytes):
141 self.putx([0, ['CRC: '
142 + ('ok' if crc16(self.bytes[0:11]) == (self.bytes[11]
143 + (self.bytes[12] << 8)) else 'error')]])
144 elif 0xaa == self.bytes[0]: # Read scratchpad
145 if 2 == len(self.bytes):
147 elif 3 == len(self.bytes):
149 self.putx([0, ['Target address: 0x%04x'
150 % ((self.bytes[2] << 8) + self.bytes[1])]])
151 elif 4 == len(self.bytes):
152 self.ss, self.es = ss, es
153 self.putx([0, ['Data status (E/S): 0x%02x'
155 elif 5 == len(self.bytes):
157 elif 12 == len(self.bytes):
159 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
160 for n in self.bytes[4:12]))]])
161 elif 13 == len(self.bytes):
163 elif 14 == len(self.bytes):
165 self.putx([0, ['CRC: '
166 + ('ok' if crc16(self.bytes[0:12]) == (self.bytes[12]
167 + (self.bytes[13] << 8)) else 'error')]])
168 elif 0x5a == self.bytes[0]: # Load first secret
169 if 2 == len(self.bytes):
171 elif 4 == len(self.bytes):
173 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
174 + (','.join(format(n, '#04x')
175 for n in self.bytes[1:4]))]])
176 elif 4 < len(self.bytes):
177 self.ss, self.es = ss, es
178 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
179 self.putx([0, ['End of operation']])
180 elif 0x33 == self.bytes[0]: # Compute next secret
181 if 2 == len(self.bytes):
183 elif 3 == len(self.bytes):
185 self.putx([0, ['Target address: 0x%04x'
186 % ((self.bytes[2] << 8) + self.bytes[1])]])
187 elif 3 < len(self.bytes):
188 self.ss, self.es = ss, es
189 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
190 self.putx([0, ['End of operation']])
191 elif 0x55 == self.bytes[0]: # Copy scratchpad
192 if 2 == len(self.bytes):
194 elif 4 == len(self.bytes):
196 self.putx([0, ['Authorization pattern (TA1, TA2, E/S): '
197 + (','.join(format(n, '#04x')
198 for n in self.bytes[1:4]))]])
199 elif 5 == len(self.bytes):
201 elif 24 == len(self.bytes):
203 mac = ','.join(format(n, '#04x') for n in self.bytes[4:24])
204 self.putx([0, ['Message authentication code: ' + mac,
206 elif 24 < len(self.bytes):
207 self.ss, self.es = ss, es
208 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
209 self.putx([0, ['Operation succeeded']])
210 elif (0 == self.bytes[-1]):
211 self.putx([0, ['Operation failed']])
212 elif 0xa5 == self.bytes[0]: # Read authenticated page
213 if 2 == len(self.bytes):
215 elif 3 == len(self.bytes):
217 self.putx([0, ['Target address: 0x%04x'
218 % ((self.bytes[2] << 8) + self.bytes[1])]])
219 elif 4 == len(self.bytes):
221 elif 35 == len(self.bytes):
223 self.putx([0, ['Data: ' + (','.join(format(n, '#04x')
224 for n in self.bytes[3:35]))]])
225 elif 36 == len(self.bytes):
226 self.ss, self.es = ss, es
227 self.putx([0, ['Padding: '
228 + ('ok' if 0xff == self.bytes[-1] else 'error')]])
229 elif 37 == len(self.bytes):
231 elif 38 == len(self.bytes):
233 self.putx([0, ['CRC: '
234 + ('ok' if crc16(self.bytes[0:36]) == (self.bytes[36]
235 + (self.bytes[37] << 8)) else 'error')]])
236 elif 39 == len(self.bytes):
238 elif 58 == len(self.bytes):
240 mac = ','.join(format(n, '#04x') for n in self.bytes[38:58])
241 self.putx([0, ['Message authentication code: ' + mac,
243 elif 59 == len(self.bytes):
245 elif 60 == len(self.bytes):
247 self.putx([0, ['MAC CRC: '
248 + ('ok' if crc16(self.bytes[38:58]) == (self.bytes[58]
249 + (self.bytes[59] << 8)) else 'error')]])
250 elif 60 < len(self.bytes):
251 self.ss, self.es = ss, es
252 if (0xaa == self.bytes[-1] or 0x55 == self.bytes[-1]):
253 self.putx([0, ['Operation completed']])
254 elif 0xf0 == self.bytes[0]: # Read memory
255 if 2 == len(self.bytes):
257 elif 3 == len(self.bytes):
259 self.putx([0, ['Target address: 0x%04x'
260 % ((self.bytes[2] << 8) + self.bytes[1])]])
261 elif 3 < len(self.bytes):
262 self.ss, self.es = ss, es
263 self.putx([0, ['Data: 0x%02x' % (self.bytes[-1])]])
265 bdata = self.bytes[-1].to_bytes(1, byteorder='big')
266 self.put(ss, es, self.out_binary, [0, bdata])