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