]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/spiflash/pd.py
decoders: Add/update tags for each PD.
[libsigrokdecode.git] / decoders / spiflash / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2011-2016 Uwe Hermann <uwe@hermann-uwe.de>
5##
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.
10##
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.
15##
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/>.
18##
19
20import sigrokdecode as srd
21from .lists import *
22
23L = len(cmds)
24
25# Don't forget to keep this in sync with 'cmds' is lists.py.
26class Ann:
27 WRSR, PP, READ, WRDI, RDSR, WREN, FAST_READ, SE, RDSCUR, WRSCUR, \
28 RDSR2, CE, ESRY, DSRY, WRITE1, WRITE2, REMS, RDID, RDP_RES, CP, ENSO, DP, \
29 READ2X, EXSO, CE2, STATUS, BE, REMS2, \
30 BIT, FIELD, WARN = range(L + 3)
31
32def cmd_annotation_classes():
33 return tuple([tuple([cmd[0].lower(), cmd[1]]) for cmd in cmds.values()])
34
35def decode_dual_bytes(sio0, sio1):
36 # Given a byte in SIO0 (MOSI) of even bits and a byte in
37 # SIO1 (MISO) of odd bits, return a tuple of two bytes.
38 def combine_byte(even, odd):
39 result = 0
40 for bit in range(4):
41 if even & (1 << bit):
42 result |= 1 << (bit*2)
43 if odd & (1 << bit):
44 result |= 1 << ((bit*2) + 1)
45 return result
46 return (combine_byte(sio0 >> 4, sio1 >> 4), combine_byte(sio0, sio1))
47
48def decode_status_reg(data):
49 # TODO: Additional per-bit(s) self.put() calls with correct start/end.
50
51 # Bits[0:0]: WIP (write in progress)
52 s = 'W' if (data & (1 << 0)) else 'No w'
53 ret = '%srite operation in progress.\n' % s
54
55 # Bits[1:1]: WEL (write enable latch)
56 s = '' if (data & (1 << 1)) else 'not '
57 ret += 'Internal write enable latch is %sset.\n' % s
58
59 # Bits[5:2]: Block protect bits
60 # TODO: More detailed decoding (chip-dependent).
61 ret += 'Block protection bits (BP3-BP0): 0x%x.\n' % ((data & 0x3c) >> 2)
62
63 # Bits[6:6]: Continuously program mode (CP mode)
64 s = '' if (data & (1 << 6)) else 'not '
65 ret += 'Device is %sin continuously program mode (CP mode).\n' % s
66
67 # Bits[7:7]: SRWD (status register write disable)
68 s = 'not ' if (data & (1 << 7)) else ''
69 ret += 'Status register writes are %sallowed.\n' % s
70
71 return ret
72
73class Decoder(srd.Decoder):
74 api_version = 3
75 id = 'spiflash'
76 name = 'SPI flash'
77 longname = 'SPI flash chips'
78 desc = 'xx25 series SPI (NOR) flash chip protocol.'
79 license = 'gplv2+'
80 inputs = ['spi']
81 outputs = ['spiflash']
82 tags = ['IC', 'Memory']
83 annotations = cmd_annotation_classes() + (
84 ('bit', 'Bit'),
85 ('field', 'Field'),
86 ('warning', 'Warning'),
87 )
88 annotation_rows = (
89 ('bits', 'Bits', (L + 0,)),
90 ('fields', 'Fields', (L + 1,)),
91 ('commands', 'Commands', tuple(range(len(cmds)))),
92 ('warnings', 'Warnings', (L + 2,)),
93 )
94 options = (
95 {'id': 'chip', 'desc': 'Chip', 'default': tuple(chips.keys())[0],
96 'values': tuple(chips.keys())},
97 {'id': 'format', 'desc': 'Data format', 'default': 'hex',
98 'values': ('hex', 'ascii')},
99 )
100
101 def __init__(self):
102 self.reset()
103
104 def reset(self):
105 self.device_id = -1
106 self.on_end_transaction = None
107 self.end_current_transaction()
108 self.writestate = 0
109
110 # Build dict mapping command keys to handler functions. Each
111 # command in 'cmds' (defined in lists.py) has a matching
112 # handler self.handle_<shortname>.
113 def get_handler(cmd):
114 s = 'handle_%s' % cmds[cmd][0].lower().replace('/', '_')
115 return getattr(self, s)
116 self.cmd_handlers = dict((cmd, get_handler(cmd)) for cmd in cmds.keys())
117
118 def end_current_transaction(self):
119 if self.on_end_transaction is not None: # Callback for CS# transition.
120 self.on_end_transaction()
121 self.on_end_transaction = None
122 self.state = None
123 self.cmdstate = 1
124 self.addr = 0
125 self.data = []
126
127 def start(self):
128 self.out_ann = self.register(srd.OUTPUT_ANN)
129 self.chip = chips[self.options['chip']]
130 self.vendor = self.options['chip'].split('_')[0]
131
132 def putx(self, data):
133 # Simplification, most annotations span exactly one SPI byte/packet.
134 self.put(self.ss, self.es, self.out_ann, data)
135
136 def putf(self, data):
137 self.put(self.ss_field, self.es_field, self.out_ann, data)
138
139 def putc(self, data):
140 self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
141
142 def device(self):
143 return device_name[self.vendor].get(self.device_id, 'Unknown')
144
145 def vendor_device(self):
146 return '%s %s' % (self.chip['vendor'], self.device())
147
148 def cmd_ann_list(self):
149 x, s = cmds[self.state][0], cmds[self.state][1]
150 return ['Command: %s (%s)' % (s, x), 'Command: %s' % s,
151 'Cmd: %s' % s, 'Cmd: %s' % x, x]
152
153 def cmd_vendor_dev_list(self):
154 c, d = cmds[self.state], 'Device = %s' % self.vendor_device()
155 return ['%s (%s): %s' % (c[1], c[0], d), '%s: %s' % (c[1], d),
156 '%s: %s' % (c[0], d), d, self.vendor_device()]
157
158 def emit_cmd_byte(self):
159 self.ss_cmd = self.ss
160 self.putx([Ann.FIELD, self.cmd_ann_list()])
161 self.addr = 0
162
163 def emit_addr_bytes(self, mosi):
164 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
165 b = ((3 - (self.cmdstate - 2)) * 8) - 1
166 self.putx([Ann.BIT,
167 ['Address bits %d..%d: 0x%02x' % (b, b - 7, mosi),
168 'Addr bits %d..%d: 0x%02x' % (b, b - 7, mosi),
169 'Addr bits %d..%d' % (b, b - 7), 'A%d..A%d' % (b, b - 7)]])
170 if self.cmdstate == 2:
171 self.ss_field = self.ss
172 if self.cmdstate == 4:
173 self.es_field = self.es
174 self.putf([Ann.FIELD, ['Address: 0x%06x' % self.addr,
175 'Addr: 0x%06x' % self.addr, '0x%06x' % self.addr]])
176
177 def handle_wren(self, mosi, miso):
178 self.putx([Ann.WREN, self.cmd_ann_list()])
179 self.writestate = 1
180
181 def handle_wrdi(self, mosi, miso):
182 self.putx([Ann.WRDI, self.cmd_ann_list()])
183 self.writestate = 0
184
185 def handle_rdid(self, mosi, miso):
186 if self.cmdstate == 1:
187 # Byte 1: Master sends command ID.
188 self.emit_cmd_byte()
189 elif self.cmdstate == 2:
190 # Byte 2: Slave sends the JEDEC manufacturer ID.
191 self.putx([Ann.FIELD, ['Manufacturer ID: 0x%02x' % miso]])
192 elif self.cmdstate == 3:
193 # Byte 3: Slave sends the memory type.
194 self.putx([Ann.FIELD, ['Memory type: 0x%02x' % miso]])
195 elif self.cmdstate == 4:
196 # Byte 4: Slave sends the device ID.
197 self.device_id = miso
198 self.putx([Ann.FIELD, ['Device ID: 0x%02x' % miso]])
199
200 if self.cmdstate == 4:
201 self.es_cmd = self.es
202 self.putc([Ann.RDID, self.cmd_vendor_dev_list()])
203 self.state = None
204 else:
205 self.cmdstate += 1
206
207 def handle_rdsr(self, mosi, miso):
208 # Read status register: Master asserts CS#, sends RDSR command,
209 # reads status register byte. If CS# is kept asserted, the status
210 # register can be read continuously / multiple times in a row.
211 # When done, the master de-asserts CS# again.
212 if self.cmdstate == 1:
213 # Byte 1: Master sends command ID.
214 self.emit_cmd_byte()
215 elif self.cmdstate >= 2:
216 # Bytes 2-x: Slave sends status register as long as master clocks.
217 self.es_cmd = self.es
218 self.putx([Ann.BIT, [decode_status_reg(miso)]])
219 self.putx([Ann.FIELD, ['Status register']])
220 self.putc([Ann.RDSR, self.cmd_ann_list()])
221 # Set write latch state.
222 self.writestate = 1 if (miso & (1 << 1)) else 0
223 self.cmdstate += 1
224
225 def handle_rdsr2(self, mosi, miso):
226 # Read status register 2: Master asserts CS#, sends RDSR2 command,
227 # reads status register 2 byte. If CS# is kept asserted, the status
228 # register 2 can be read continuously / multiple times in a row.
229 # When done, the master de-asserts CS# again.
230 if self.cmdstate == 1:
231 # Byte 1: Master sends command ID.
232 self.emit_cmd_byte()
233 elif self.cmdstate >= 2:
234 # Bytes 2-x: Slave sends status register 2 as long as master clocks.
235 self.es_cmd = self.es
236 # TODO: Decode status register 2 correctly.
237 self.putx([Ann.BIT, [decode_status_reg(miso)]])
238 self.putx([Ann.FIELD, ['Status register 2']])
239 self.putc([Ann.RDSR2, self.cmd_ann_list()])
240 self.cmdstate += 1
241
242 def handle_wrsr(self, mosi, miso):
243 # Write status register: Master asserts CS#, sends WRSR command,
244 # writes 1 or 2 status register byte(s).
245 # When done, the master de-asserts CS# again. If this doesn't happen
246 # the WRSR command will not be executed.
247 if self.cmdstate == 1:
248 # Byte 1: Master sends command ID.
249 self.emit_cmd_byte()
250 elif self.cmdstate == 2:
251 # Byte 2: Master sends status register 1.
252 self.putx([Ann.BIT, [decode_status_reg(mosi)]])
253 self.putx([Ann.FIELD, ['Status register 1']])
254 # Set write latch state.
255 self.writestate = 1 if (miso & (1 << 1)) else 0
256 elif self.cmdstate == 3:
257 # Byte 3: Master sends status register 2.
258 # TODO: Decode status register 2 correctly.
259 self.putx([Ann.BIT, [decode_status_reg(mosi)]])
260 self.putx([Ann.FIELD, ['Status register 2']])
261 self.es_cmd = self.es
262 self.putc([Ann.WRSR, self.cmd_ann_list()])
263 self.cmdstate += 1
264
265 def handle_read(self, mosi, miso):
266 # Read data bytes: Master asserts CS#, sends READ command, sends
267 # 3-byte address, reads >= 1 data bytes, de-asserts CS#.
268 if self.cmdstate == 1:
269 # Byte 1: Master sends command ID.
270 self.emit_cmd_byte()
271 elif self.cmdstate in (2, 3, 4):
272 # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
273 self.emit_addr_bytes(mosi)
274 elif self.cmdstate >= 5:
275 # Bytes 5-x: Master reads data bytes (until CS# de-asserted).
276 self.es_field = self.es # Will be overwritten for each byte.
277 if self.cmdstate == 5:
278 self.ss_field = self.ss
279 self.on_end_transaction = lambda: self.output_data_block('Data', Ann.READ)
280 self.data.append(miso)
281 self.cmdstate += 1
282
283 def handle_write_common(self, mosi, miso, ann):
284 # Write data bytes: Master asserts CS#, sends WRITE command, sends
285 # 3-byte address, writes >= 1 data bytes, de-asserts CS#.
286 if self.cmdstate == 1:
287 # Byte 1: Master sends command ID.
288 self.emit_cmd_byte()
289 if self.writestate == 0:
290 self.putc([Ann.WARN, ['Warning: WREN might be missing']])
291 elif self.cmdstate in (2, 3, 4):
292 # Bytes 2/3/4: Master sends write address (24bits, MSB-first).
293 self.emit_addr_bytes(mosi)
294 elif self.cmdstate >= 5:
295 # Bytes 5-x: Master writes data bytes (until CS# de-asserted).
296 self.es_field = self.es # Will be overwritten for each byte.
297 if self.cmdstate == 5:
298 self.ss_field = self.ss
299 self.on_end_transaction = lambda: self.output_data_block('Data', ann)
300 self.data.append(mosi)
301 self.cmdstate += 1
302
303 def handle_write1(self, mosi, miso):
304 self.handle_write_common(mosi, miso, Ann.WRITE1)
305
306 def handle_write2(self, mosi, miso):
307 self.handle_write_common(mosi, miso, Ann.WRITE2)
308
309 def handle_fast_read(self, mosi, miso):
310 # Fast read: Master asserts CS#, sends FAST READ command, sends
311 # 3-byte address + 1 dummy byte, reads >= 1 data bytes, de-asserts CS#.
312 if self.cmdstate == 1:
313 # Byte 1: Master sends command ID.
314 self.emit_cmd_byte()
315 elif self.cmdstate in (2, 3, 4):
316 # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
317 self.emit_addr_bytes(mosi)
318 elif self.cmdstate == 5:
319 self.putx([Ann.BIT, ['Dummy byte: 0x%02x' % mosi]])
320 elif self.cmdstate >= 6:
321 # Bytes 6-x: Master reads data bytes (until CS# de-asserted).
322 self.es_field = self.es # Will be overwritten for each byte.
323 if self.cmdstate == 6:
324 self.ss_field = self.ss
325 self.on_end_transaction = lambda: self.output_data_block('Data', Ann.FAST_READ)
326 self.data.append(miso)
327 self.cmdstate += 1
328
329 def handle_2read(self, mosi, miso):
330 # 2x I/O read (fast read dual I/O): Master asserts CS#, sends 2READ
331 # command, sends 3-byte address + 1 dummy byte, reads >= 1 data bytes,
332 # de-asserts CS#. All data after the command is sent via two I/O pins.
333 # MOSI = SIO0 = even bits, MISO = SIO1 = odd bits.
334 if self.cmdstate != 1:
335 b1, b2 = decode_dual_bytes(mosi, miso)
336 if self.cmdstate == 1:
337 # Byte 1: Master sends command ID.
338 self.emit_cmd_byte()
339 elif self.cmdstate == 2:
340 # Bytes 2/3(/4): Master sends read address (24bits, MSB-first).
341 # Handle bytes 2 and 3 here.
342 self.emit_addr_bytes(b1)
343 self.cmdstate = 3
344 self.emit_addr_bytes(b2)
345 elif self.cmdstate == 4:
346 # Byte 5: Dummy byte. Also handle byte 4 (address LSB) here.
347 self.emit_addr_bytes(b1)
348 self.cmdstate = 5
349 self.putx([Ann.BIT, ['Dummy byte: 0x%02x' % b2]])
350 elif self.cmdstate >= 6:
351 # Bytes 6-x: Master reads data bytes (until CS# de-asserted).
352 self.es_field = self.es # Will be overwritten for each byte.
353 if self.cmdstate == 6:
354 self.ss_field = self.ss
355 self.on_end_transaction = lambda: self.output_data_block('Data', Ann.READ2X)
356 self.data.append(b1)
357 self.data.append(b2)
358 self.cmdstate += 1
359
360 def handle_status(self, mosi, miso):
361 if self.cmdstate == 1:
362 # Byte 1: Master sends command ID.
363 self.emit_cmd_byte()
364 self.on_end_transaction = lambda: self.putc([Ann.STATUS, [cmds[self.state][1]]])
365 else:
366 # Will be overwritten for each byte.
367 self.es_cmd = self.es
368 self.es_field = self.es
369 if self.cmdstate == 2:
370 self.ss_field = self.ss
371 self.putx([Ann.BIT, ['Status register byte %d: 0x%02x' % ((self.cmdstate % 2) + 1, miso)]])
372 self.cmdstate += 1
373
374 # TODO: Warn/abort if we don't see the necessary amount of bytes.
375 def handle_se(self, mosi, miso):
376 if self.cmdstate == 1:
377 # Byte 1: Master sends command ID.
378 self.emit_cmd_byte()
379 if self.writestate == 0:
380 self.putx([Ann.WARN, ['Warning: WREN might be missing']])
381 elif self.cmdstate in (2, 3, 4):
382 # Bytes 2/3/4: Master sends sector address (24bits, MSB-first).
383 self.emit_addr_bytes(mosi)
384
385 if self.cmdstate == 4:
386 self.es_cmd = self.es
387 d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr)
388 self.putc([Ann.SE, [d]])
389 # TODO: Max. size depends on chip, check that too if possible.
390 if self.addr % 4096 != 0:
391 # Sector addresses must be 4K-aligned (same for all 3 chips).
392 self.putc([Ann.WARN, ['Warning: Invalid sector address!']])
393 self.state = None
394 else:
395 self.cmdstate += 1
396
397 def handle_be(self, mosi, miso):
398 pass # TODO
399
400 def handle_ce(self, mosi, miso):
401 self.putx([Ann.CE, self.cmd_ann_list()])
402 if self.writestate == 0:
403 self.putx([Ann.WARN, ['Warning: WREN might be missing']])
404
405 def handle_ce2(self, mosi, miso):
406 self.putx([Ann.CE2, self.cmd_ann_list()])
407 if self.writestate == 0:
408 self.putx([Ann.WARN, ['Warning: WREN might be missing']])
409
410 def handle_pp(self, mosi, miso):
411 # Page program: Master asserts CS#, sends PP command, sends 3-byte
412 # page address, sends >= 1 data bytes, de-asserts CS#.
413 if self.cmdstate == 1:
414 # Byte 1: Master sends command ID.
415 self.emit_cmd_byte()
416 elif self.cmdstate in (2, 3, 4):
417 # Bytes 2/3/4: Master sends page address (24bits, MSB-first).
418 self.emit_addr_bytes(mosi)
419 elif self.cmdstate >= 5:
420 # Bytes 5-x: Master sends data bytes (until CS# de-asserted).
421 self.es_field = self.es # Will be overwritten for each byte.
422 if self.cmdstate == 5:
423 self.ss_field = self.ss
424 self.on_end_transaction = lambda: self.output_data_block('Data', Ann.PP)
425 self.data.append(mosi)
426 self.cmdstate += 1
427
428 def handle_cp(self, mosi, miso):
429 pass # TODO
430
431 def handle_dp(self, mosi, miso):
432 pass # TODO
433
434 def handle_rdp_res(self, mosi, miso):
435 if self.cmdstate == 1:
436 # Byte 1: Master sends command ID.
437 self.emit_cmd_byte()
438 elif self.cmdstate in (2, 3, 4):
439 # Bytes 2/3/4: Master sends three dummy bytes.
440 self.putx([Ann.FIELD, ['Dummy byte: %02x' % mosi]])
441 elif self.cmdstate == 5:
442 # Byte 5: Slave sends device ID.
443 self.es_cmd = self.es
444 self.device_id = miso
445 self.putx([Ann.FIELD, ['Device ID: %s' % self.device()]])
446 d = 'Device = %s' % self.vendor_device()
447 self.putc([Ann.RDP_RES, self.cmd_vendor_dev_list()])
448 self.state = None
449 self.cmdstate += 1
450
451 def handle_rems(self, mosi, miso):
452 if self.cmdstate == 1:
453 # Byte 1: Master sends command ID.
454 self.emit_cmd_byte()
455 elif self.cmdstate in (2, 3):
456 # Bytes 2/3: Master sends two dummy bytes.
457 self.putx([Ann.FIELD, ['Dummy byte: 0x%02x' % mosi]])
458 elif self.cmdstate == 4:
459 # Byte 4: Master sends 0x00 or 0x01.
460 # 0x00: Master wants manufacturer ID as first reply byte.
461 # 0x01: Master wants device ID as first reply byte.
462 self.manufacturer_id_first = True if (mosi == 0x00) else False
463 d = 'manufacturer' if (mosi == 0x00) else 'device'
464 self.putx([Ann.FIELD, ['Master wants %s ID first' % d]])
465 elif self.cmdstate == 5:
466 # Byte 5: Slave sends manufacturer ID (or device ID).
467 self.ids = [miso]
468 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
469 self.putx([Ann.FIELD, ['%s ID: 0x%02x' % (d, miso)]])
470 elif self.cmdstate == 6:
471 # Byte 6: Slave sends device ID (or manufacturer ID).
472 self.ids.append(miso)
473 d = 'Device' if self.manufacturer_id_first else 'Manufacturer'
474 self.putx([Ann.FIELD, ['%s ID: 0x%02x' % (d, miso)]])
475
476 if self.cmdstate == 6:
477 id_ = self.ids[1] if self.manufacturer_id_first else self.ids[0]
478 self.device_id = id_
479 self.es_cmd = self.es
480 self.putc([Ann.REMS, self.cmd_vendor_dev_list()])
481 self.state = None
482 else:
483 self.cmdstate += 1
484
485 def handle_rems2(self, mosi, miso):
486 pass # TODO
487
488 def handle_enso(self, mosi, miso):
489 pass # TODO
490
491 def handle_exso(self, mosi, miso):
492 pass # TODO
493
494 def handle_rdscur(self, mosi, miso):
495 pass # TODO
496
497 def handle_wrscur(self, mosi, miso):
498 pass # TODO
499
500 def handle_esry(self, mosi, miso):
501 pass # TODO
502
503 def handle_dsry(self, mosi, miso):
504 pass # TODO
505
506 def output_data_block(self, label, idx):
507 # Print accumulated block of data
508 # (called on CS# de-assert via self.on_end_transaction callback).
509 self.es_cmd = self.es # End on the CS# de-assert sample.
510 if self.options['format'] == 'hex':
511 s = ' '.join([('%02x' % b) for b in self.data])
512 else:
513 s = ''.join(map(chr, self.data))
514 self.putf([Ann.FIELD, ['%s (%d bytes)' % (label, len(self.data))]])
515 self.putc([idx, ['%s (addr 0x%06x, %d bytes): %s' % \
516 (cmds[self.state][1], self.addr, len(self.data), s)]])
517
518 def decode(self, ss, es, data):
519 ptype, mosi, miso = data
520
521 self.ss, self.es = ss, es
522
523 if ptype == 'CS-CHANGE':
524 self.end_current_transaction()
525
526 if ptype != 'DATA':
527 return
528
529 # If we encountered a known chip command, enter the resp. state.
530 if self.state is None:
531 self.state = mosi
532 self.cmdstate = 1
533
534 # Handle commands.
535 try:
536 self.cmd_handlers[self.state](mosi, miso)
537 except KeyError:
538 self.putx([Ann.BIT, ['Unknown command: 0x%02x' % mosi]])
539 self.state = None