]>
Commit | Line | Data |
---|---|---|
1b1c914f | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
1b1c914f | 3 | ## |
3ca1f1b3 | 4 | ## Copyright (C) 2011-2015 Uwe Hermann <uwe@hermann-uwe.de> |
1b1c914f UH |
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, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
677d597b | 21 | import sigrokdecode as srd |
c2446117 | 22 | from .lists import * |
1b1c914f | 23 | |
9389f2c1 | 24 | def cmd_annotation_classes(): |
da9bcbd9 | 25 | return tuple([tuple([cmd[0].lower(), cmd[1]]) for cmd in cmds.values()]) |
9389f2c1 | 26 | |
10d3c8dc AG |
27 | def decode_dual_bytes(sio0, sio1): |
28 | # Given a byte in SIO0 (MOSI) of even bits and a byte in | |
29 | # SIO1 (MISO) of odd bits, return a tuple of two bytes. | |
30 | def combine_byte(even, odd): | |
31 | result = 0 | |
32 | for bit in range(4): | |
33 | if even & (1 << bit): | |
34 | result |= 1 << (bit*2) | |
35 | if odd & (1 << bit): | |
36 | result |= 1 << ((bit*2) + 1) | |
37 | return result | |
38 | return (combine_byte(sio0 >> 4, sio1 >> 4), combine_byte(sio0, sio1)) | |
39 | ||
7cfbf663 UH |
40 | def decode_status_reg(data): |
41 | # TODO: Additional per-bit(s) self.put() calls with correct start/end. | |
42 | ||
43 | # Bits[0:0]: WIP (write in progress) | |
44 | s = 'W' if (data & (1 << 0)) else 'No w' | |
45 | ret = '%srite operation in progress.\n' % s | |
46 | ||
47 | # Bits[1:1]: WEL (write enable latch) | |
48 | s = '' if (data & (1 << 1)) else 'not ' | |
49 | ret += 'Internal write enable latch is %sset.\n' % s | |
50 | ||
51 | # Bits[5:2]: Block protect bits | |
52 | # TODO: More detailed decoding (chip-dependent). | |
53 | ret += 'Block protection bits (BP3-BP0): 0x%x.\n' % ((data & 0x3c) >> 2) | |
54 | ||
55 | # Bits[6:6]: Continuously program mode (CP mode) | |
56 | s = '' if (data & (1 << 6)) else 'not ' | |
57 | ret += 'Device is %sin continuously program mode (CP mode).\n' % s | |
58 | ||
59 | # Bits[7:7]: SRWD (status register write disable) | |
cd287c56 | 60 | s = 'not ' if (data & (1 << 7)) else '' |
7cfbf663 UH |
61 | ret += 'Status register writes are %sallowed.\n' % s |
62 | ||
63 | return ret | |
64 | ||
677d597b | 65 | class Decoder(srd.Decoder): |
12851357 | 66 | api_version = 2 |
3ca1f1b3 UH |
67 | id = 'spiflash' |
68 | name = 'SPI flash' | |
69 | longname = 'SPI flash chips' | |
70 | desc = 'xx25 series SPI (NOR) flash chip protocol.' | |
1b1c914f | 71 | license = 'gplv2+' |
a1497fa3 | 72 | inputs = ['spi'] |
3ca1f1b3 | 73 | outputs = ['spiflash'] |
da9bcbd9 BV |
74 | annotations = cmd_annotation_classes() + ( |
75 | ('bits', 'Bits'), | |
76 | ('bits2', 'Bits2'), | |
77 | ('warnings', 'Warnings'), | |
78 | ) | |
9ed42038 UH |
79 | annotation_rows = ( |
80 | ('bits', 'Bits', (24, 25)), | |
81 | ('commands', 'Commands', tuple(range(23 + 1))), | |
82 | ('warnings', 'Warnings', (26,)), | |
83 | ) | |
c2446117 UH |
84 | options = ( |
85 | {'id': 'chip', 'desc': 'Chip', 'default': tuple(chips.keys())[0], | |
86 | 'values': tuple(chips.keys())}, | |
b33a73bc UH |
87 | {'id': 'format', 'desc': 'Data format', 'default': 'hex', |
88 | 'values': ('hex', 'ascii')}, | |
c2446117 | 89 | ) |
1b1c914f | 90 | |
92b7b49f | 91 | def __init__(self): |
2c920167 AG |
92 | self.on_end_transaction = None |
93 | self.end_current_transaction() | |
94 | ||
1ec9c5e2 AG |
95 | # Build dict mapping command keys to handler functions. Each |
96 | # command in 'cmds' (defined in lists.py) has a matching | |
97 | # handler self.handle_<shortname>. | |
98 | def get_handler(cmd): | |
99 | s = 'handle_%s' % cmds[cmd][0].lower().replace('/', '_') | |
100 | return getattr(self, s) | |
101 | self.cmd_handlers = dict((cmd, get_handler(cmd)) for cmd in cmds.keys()) | |
102 | ||
2c920167 AG |
103 | def end_current_transaction(self): |
104 | if self.on_end_transaction is not None: # Callback for CS# transition. | |
105 | self.on_end_transaction() | |
106 | self.on_end_transaction = None | |
4772a846 | 107 | self.state = None |
781ef945 | 108 | self.cmdstate = 1 |
e4022299 UH |
109 | self.addr = 0 |
110 | self.data = [] | |
1b1c914f | 111 | |
8915b346 | 112 | def start(self): |
be465111 | 113 | self.out_ann = self.register(srd.OUTPUT_ANN) |
c2446117 | 114 | self.chip = chips[self.options['chip']] |
1b1c914f | 115 | |
385508e9 | 116 | def putx(self, data): |
ee3e279c | 117 | # Simplification, most annotations span exactly one SPI byte/packet. |
9b4d8a57 UH |
118 | self.put(self.ss, self.es, self.out_ann, data) |
119 | ||
7c139a54 | 120 | def putb(self, data): |
5b0b88ce | 121 | self.put(self.ss_block, self.es_block, self.out_ann, data) |
7c139a54 | 122 | |
9b4d8a57 | 123 | def handle_wren(self, mosi, miso): |
781ef945 | 124 | self.putx([0, ['Command: %s' % cmds[self.state][1]]]) |
4772a846 | 125 | self.state = None |
1b1c914f | 126 | |
b54936a9 UH |
127 | def handle_wrdi(self, mosi, miso): |
128 | pass # TODO | |
129 | ||
1b1c914f | 130 | # TODO: Check/display device ID / name |
9b4d8a57 | 131 | def handle_rdid(self, mosi, miso): |
1b1c914f UH |
132 | if self.cmdstate == 1: |
133 | # Byte 1: Master sends command ID. | |
486b19ce | 134 | self.ss_block = self.ss |
9389f2c1 | 135 | self.putx([2, ['Command: %s' % cmds[self.state][1]]]) |
1b1c914f UH |
136 | elif self.cmdstate == 2: |
137 | # Byte 2: Slave sends the JEDEC manufacturer ID. | |
9389f2c1 | 138 | self.putx([2, ['Manufacturer ID: 0x%02x' % miso]]) |
1b1c914f UH |
139 | elif self.cmdstate == 3: |
140 | # Byte 3: Slave sends the memory type (0x20 for this chip). | |
9389f2c1 | 141 | self.putx([2, ['Memory type: 0x%02x' % miso]]) |
1b1c914f UH |
142 | elif self.cmdstate == 4: |
143 | # Byte 4: Slave sends the device ID. | |
9b4d8a57 | 144 | self.device_id = miso |
9389f2c1 | 145 | self.putx([2, ['Device ID: 0x%02x' % miso]]) |
1b1c914f UH |
146 | |
147 | if self.cmdstate == 4: | |
148 | # TODO: Check self.device_id is valid & exists in device_names. | |
149 | # TODO: Same device ID? Check! | |
9b4d8a57 | 150 | d = 'Device: Macronix %s' % device_name[self.device_id] |
486b19ce | 151 | self.put(self.ss_block, self.es, self.out_ann, [0, [d]]) |
4772a846 | 152 | self.state = None |
1b1c914f UH |
153 | else: |
154 | self.cmdstate += 1 | |
155 | ||
b54936a9 UH |
156 | def handle_rdsr(self, mosi, miso): |
157 | # Read status register: Master asserts CS#, sends RDSR command, | |
158 | # reads status register byte. If CS# is kept asserted, the status | |
159 | # register can be read continuously / multiple times in a row. | |
160 | # When done, the master de-asserts CS# again. | |
161 | if self.cmdstate == 1: | |
162 | # Byte 1: Master sends command ID. | |
9389f2c1 | 163 | self.putx([3, ['Command: %s' % cmds[self.state][1]]]) |
b54936a9 UH |
164 | elif self.cmdstate >= 2: |
165 | # Bytes 2-x: Slave sends status register as long as master clocks. | |
2c920167 AG |
166 | self.putx([24, ['Status register: 0x%02x' % miso]]) |
167 | self.putx([25, [decode_status_reg(miso)]]) | |
b54936a9 UH |
168 | |
169 | self.cmdstate += 1 | |
170 | ||
171 | def handle_wrsr(self, mosi, miso): | |
172 | pass # TODO | |
173 | ||
174 | def handle_read(self, mosi, miso): | |
175 | # Read data bytes: Master asserts CS#, sends READ command, sends | |
176 | # 3-byte address, reads >= 1 data bytes, de-asserts CS#. | |
177 | if self.cmdstate == 1: | |
178 | # Byte 1: Master sends command ID. | |
9389f2c1 | 179 | self.putx([5, ['Command: %s' % cmds[self.state][1]]]) |
b54936a9 UH |
180 | elif self.cmdstate in (2, 3, 4): |
181 | # Bytes 2/3/4: Master sends read address (24bits, MSB-first). | |
182 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
183 | # self.putx([0, ['Read address, byte %d: 0x%02x' % \ | |
184 | # (4 - self.cmdstate, mosi)]]) | |
185 | if self.cmdstate == 4: | |
9389f2c1 | 186 | self.putx([24, ['Read address: 0x%06x' % self.addr]]) |
b54936a9 UH |
187 | self.addr = 0 |
188 | elif self.cmdstate >= 5: | |
189 | # Bytes 5-x: Master reads data bytes (until CS# de-asserted). | |
2c920167 AG |
190 | if self.cmdstate == 5: |
191 | self.ss_block = self.ss | |
192 | self.on_end_transaction = lambda: self.output_data_block('Read') | |
193 | self.data.append(miso) | |
b54936a9 UH |
194 | |
195 | self.cmdstate += 1 | |
196 | ||
197 | def handle_fast_read(self, mosi, miso): | |
7c139a54 UH |
198 | # Fast read: Master asserts CS#, sends FAST READ command, sends |
199 | # 3-byte address + 1 dummy byte, reads >= 1 data bytes, de-asserts CS#. | |
200 | if self.cmdstate == 1: | |
201 | # Byte 1: Master sends command ID. | |
202 | self.putx([5, ['Command: %s' % cmds[self.state][1]]]) | |
203 | elif self.cmdstate in (2, 3, 4): | |
204 | # Bytes 2/3/4: Master sends read address (24bits, MSB-first). | |
205 | self.putx([24, ['AD%d: 0x%02x' % (self.cmdstate - 1, mosi)]]) | |
206 | if self.cmdstate == 2: | |
5b0b88ce | 207 | self.ss_block = self.ss |
7c139a54 UH |
208 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) |
209 | elif self.cmdstate == 5: | |
210 | self.putx([24, ['Dummy byte: 0x%02x' % mosi]]) | |
5b0b88ce | 211 | self.es_block = self.es |
7c139a54 UH |
212 | self.putb([5, ['Read address: 0x%06x' % self.addr]]) |
213 | self.addr = 0 | |
214 | elif self.cmdstate >= 6: | |
215 | # Bytes 6-x: Master reads data bytes (until CS# de-asserted). | |
7c139a54 | 216 | if self.cmdstate == 6: |
5b0b88ce | 217 | self.ss_block = self.ss |
10d3c8dc | 218 | self.on_end_transaction = lambda: self.output_data_block('Read') |
2c920167 | 219 | self.data.append(miso) |
7c139a54 UH |
220 | |
221 | self.cmdstate += 1 | |
b54936a9 UH |
222 | |
223 | def handle_2read(self, mosi, miso): | |
10d3c8dc AG |
224 | # Fast read dual I/O: Same as fast read, but all data |
225 | # after the command is sent via two I/O pins. | |
226 | # MOSI = SIO0 = even bits, MISO = SIO1 = odd bits. | |
227 | # Recombine the bytes and pass them up to the handle_fast_read command. | |
228 | if self.cmdstate == 1: | |
229 | # Byte 1: Master sends command ID. | |
230 | self.putx([5, ['Command: %s' % cmds[self.state][1]]]) | |
231 | self.cmdstate = 2 | |
232 | else: | |
233 | # Dual I/O mode. | |
234 | a, b = decode_dual_bytes(mosi, miso) | |
235 | # Pass same byte in as both MISO & MOSI, parser state determines | |
236 | # which one it cares about. | |
237 | self.handle_fast_read(a, a) | |
238 | self.handle_fast_read(b, b) | |
b54936a9 | 239 | |
1b1c914f UH |
240 | # TODO: Warn/abort if we don't see the necessary amount of bytes. |
241 | # TODO: Warn if WREN was not seen before. | |
9b4d8a57 | 242 | def handle_se(self, mosi, miso): |
1b1c914f UH |
243 | if self.cmdstate == 1: |
244 | # Byte 1: Master sends command ID. | |
245 | self.addr = 0 | |
486b19ce | 246 | self.ss_block = self.ss |
9389f2c1 | 247 | self.putx([8, ['Command: %s' % cmds[self.state][1]]]) |
1b1c914f | 248 | elif self.cmdstate in (2, 3, 4): |
868fd207 | 249 | # Bytes 2/3/4: Master sends sector address (24bits, MSB-first). |
87e574b7 UH |
250 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) |
251 | # self.putx([0, ['Sector address, byte %d: 0x%02x' % \ | |
252 | # (4 - self.cmdstate, mosi)]]) | |
1b1c914f UH |
253 | |
254 | if self.cmdstate == 4: | |
87e574b7 | 255 | d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr) |
486b19ce | 256 | self.put(self.ss_block, self.es, self.out_ann, [24, [d]]) |
1b1c914f UH |
257 | # TODO: Max. size depends on chip, check that too if possible. |
258 | if self.addr % 4096 != 0: | |
259 | # Sector addresses must be 4K-aligned (same for all 3 chips). | |
173c919c | 260 | d = 'Warning: Invalid sector address!' |
486b19ce | 261 | self.put(self.ss_block, self.es, self.out_ann, [101, [d]]) |
4772a846 | 262 | self.state = None |
1b1c914f UH |
263 | else: |
264 | self.cmdstate += 1 | |
265 | ||
b54936a9 UH |
266 | def handle_be(self, mosi, miso): |
267 | pass # TODO | |
268 | ||
269 | def handle_ce(self, mosi, miso): | |
270 | pass # TODO | |
271 | ||
272 | def handle_ce2(self, mosi, miso): | |
273 | pass # TODO | |
274 | ||
275 | def handle_pp(self, mosi, miso): | |
276 | # Page program: Master asserts CS#, sends PP command, sends 3-byte | |
277 | # page address, sends >= 1 data bytes, de-asserts CS#. | |
278 | if self.cmdstate == 1: | |
279 | # Byte 1: Master sends command ID. | |
9389f2c1 | 280 | self.putx([12, ['Command: %s' % cmds[self.state][1]]]) |
b54936a9 UH |
281 | elif self.cmdstate in (2, 3, 4): |
282 | # Bytes 2/3/4: Master sends page address (24bits, MSB-first). | |
283 | self.addr |= (mosi << ((4 - self.cmdstate) * 8)) | |
284 | # self.putx([0, ['Page address, byte %d: 0x%02x' % \ | |
285 | # (4 - self.cmdstate, mosi)]]) | |
286 | if self.cmdstate == 4: | |
9389f2c1 | 287 | self.putx([24, ['Page address: 0x%06x' % self.addr]]) |
b54936a9 UH |
288 | self.addr = 0 |
289 | elif self.cmdstate >= 5: | |
290 | # Bytes 5-x: Master sends data bytes (until CS# de-asserted). | |
2c920167 AG |
291 | if self.cmdstate == 5: |
292 | self.ss_block = self.ss | |
293 | self.on_end_transaction = lambda: self.output_data_block('Page data') | |
294 | self.data.append(mosi) | |
b54936a9 UH |
295 | |
296 | self.cmdstate += 1 | |
297 | ||
298 | def handle_cp(self, mosi, miso): | |
299 | pass # TODO | |
300 | ||
301 | def handle_dp(self, mosi, miso): | |
302 | pass # TODO | |
303 | ||
304 | def handle_rdp_res(self, mosi, miso): | |
305 | pass # TODO | |
306 | ||
9b4d8a57 | 307 | def handle_rems(self, mosi, miso): |
1b1c914f UH |
308 | if self.cmdstate == 1: |
309 | # Byte 1: Master sends command ID. | |
486b19ce | 310 | self.ss_block = self.ss |
9389f2c1 | 311 | self.putx([16, ['Command: %s' % cmds[self.state][1]]]) |
1b1c914f UH |
312 | elif self.cmdstate in (2, 3): |
313 | # Bytes 2/3: Master sends two dummy bytes. | |
314 | # TODO: Check dummy bytes? Check reply from device? | |
9389f2c1 | 315 | self.putx([24, ['Dummy byte: %s' % mosi]]) |
1b1c914f UH |
316 | elif self.cmdstate == 4: |
317 | # Byte 4: Master sends 0x00 or 0x01. | |
318 | # 0x00: Master wants manufacturer ID as first reply byte. | |
319 | # 0x01: Master wants device ID as first reply byte. | |
9b4d8a57 UH |
320 | self.manufacturer_id_first = True if (mosi == 0x00) else False |
321 | d = 'manufacturer' if (mosi == 0x00) else 'device' | |
9389f2c1 | 322 | self.putx([24, ['Master wants %s ID first' % d]]) |
1b1c914f UH |
323 | elif self.cmdstate == 5: |
324 | # Byte 5: Slave sends manufacturer ID (or device ID). | |
9b4d8a57 UH |
325 | self.ids = [miso] |
326 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' | |
9389f2c1 | 327 | self.putx([24, ['%s ID' % d]]) |
9b4d8a57 | 328 | elif self.cmdstate == 6: |
1b1c914f | 329 | # Byte 6: Slave sends device ID (or manufacturer ID). |
7f7ea759 | 330 | self.ids.append(miso) |
9b4d8a57 | 331 | d = 'Manufacturer' if self.manufacturer_id_first else 'Device' |
9389f2c1 | 332 | self.putx([24, ['%s ID' % d]]) |
1b1c914f UH |
333 | |
334 | if self.cmdstate == 6: | |
1b1c914f | 335 | id = self.ids[1] if self.manufacturer_id_first else self.ids[0] |
9389f2c1 | 336 | self.putx([24, ['Device: Macronix %s' % device_name[id]]]) |
4772a846 | 337 | self.state = None |
1b1c914f UH |
338 | else: |
339 | self.cmdstate += 1 | |
340 | ||
b54936a9 UH |
341 | def handle_rems2(self, mosi, miso): |
342 | pass # TODO | |
e4022299 | 343 | |
b54936a9 UH |
344 | def handle_enso(self, mosi, miso): |
345 | pass # TODO | |
e4022299 | 346 | |
b54936a9 UH |
347 | def handle_exso(self, mosi, miso): |
348 | pass # TODO | |
e4022299 | 349 | |
b54936a9 UH |
350 | def handle_rdscur(self, mosi, miso): |
351 | pass # TODO | |
e4022299 | 352 | |
b54936a9 UH |
353 | def handle_wrscur(self, mosi, miso): |
354 | pass # TODO | |
e4022299 | 355 | |
b54936a9 UH |
356 | def handle_esry(self, mosi, miso): |
357 | pass # TODO | |
1b1c914f | 358 | |
b54936a9 UH |
359 | def handle_dsry(self, mosi, miso): |
360 | pass # TODO | |
5ebb76fe | 361 | |
2c920167 AG |
362 | def output_data_block(self, label): |
363 | # Print accumulated block of data | |
364 | # (called on CS# de-assert via self.on_end_transaction callback). | |
365 | self.es_block = self.es # Ends on the CS# de-assert sample. | |
b33a73bc UH |
366 | if self.options['format'] == 'hex': |
367 | s = ' '.join([('%02x' % b) for b in self.data]) | |
368 | else: | |
369 | s = ''.join(map(chr, self.data)) | |
2c920167 | 370 | self.putb([25, ['%s %d bytes: %s' % (label, len(self.data), s)]]) |
1b1c914f | 371 | |
2c920167 | 372 | def decode(self, ss, es, data): |
9b4d8a57 | 373 | ptype, mosi, miso = data |
1b1c914f | 374 | |
2c920167 | 375 | self.ss, self.es = ss, es |
e4022299 | 376 | |
2c920167 AG |
377 | if ptype == 'CS-CHANGE': |
378 | self.end_current_transaction() | |
e4022299 | 379 | |
3e3c0330 | 380 | if ptype != 'DATA': |
9b4d8a57 | 381 | return |
1b1c914f | 382 | |
9b4d8a57 | 383 | # If we encountered a known chip command, enter the resp. state. |
35b380b1 | 384 | if self.state is None: |
781ef945 UH |
385 | self.state = mosi |
386 | self.cmdstate = 1 | |
1b1c914f | 387 | |
9b4d8a57 | 388 | # Handle commands. |
1ec9c5e2 AG |
389 | try: |
390 | self.cmd_handlers[self.state](mosi, miso) | |
391 | except KeyError: | |
9389f2c1 | 392 | self.putx([24, ['Unknown command: 0x%02x' % mosi]]) |
4772a846 | 393 | self.state = None |