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