]> sigrok.org Git - libsigrokdecode.git/blame - decoders/mx25lxx05d/pd.py
s/out_proto/out_python/.
[libsigrokdecode.git] / decoders / mx25lxx05d / pd.py
CommitLineData
1b1c914f 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
1b1c914f 3##
9b4d8a57 4## Copyright (C) 2011-2012 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
1b1c914f 22
4772a846 23# Dict which maps command IDs to their names and descriptions.
1b1c914f 24cmds = {
4772a846
UH
25 0x06: ('WREN', 'Write enable'),
26 0x04: ('WRDI', 'Write disable'),
27 0x9f: ('RDID', 'Read identification'),
28 0x05: ('RDSR', 'Read status register'),
29 0x01: ('WRSR', 'Write status register'),
30 0x03: ('READ', 'Read data'),
781ef945 31 0x0b: ('FAST/READ', 'Fast read data'),
4772a846
UH
32 0xbb: ('2READ', '2x I/O read'),
33 0x20: ('SE', 'Sector erase'),
34 0xd8: ('BE', 'Block erase'),
35 0x60: ('CE', 'Chip erase'),
36 0xc7: ('CE2', 'Chip erase'), # Alternative command ID
37 0x02: ('PP', 'Page program'),
38 0xad: ('CP', 'Continuously program mode'),
39 0xb9: ('DP', 'Deep power down'),
781ef945 40 0xab: ('RDP/RES', 'Release from deep powerdown / Read electronic ID'),
4772a846
UH
41 0x90: ('REMS', 'Read electronic manufacturer & device ID'),
42 0xef: ('REMS2', 'Read ID for 2x I/O mode'),
43 0xb1: ('ENSO', 'Enter secured OTP'),
44 0xc1: ('EXSO', 'Exit secured OTP'),
45 0x2b: ('RDSCUR', 'Read security register'),
46 0x2f: ('WRSCUR', 'Write security register'),
47 0x70: ('ESRY', 'Enable SO to output RY/BY#'),
48 0x80: ('DSRY', 'Disable SO to output RY/BY#'),
1b1c914f
UH
49}
50
51device_name = {
52 0x14: 'MX25L1605D',
53 0x15: 'MX25L3205D',
54 0x16: 'MX25L6405D',
55}
56
7cfbf663
UH
57def decode_status_reg(data):
58 # TODO: Additional per-bit(s) self.put() calls with correct start/end.
59
60 # Bits[0:0]: WIP (write in progress)
61 s = 'W' if (data & (1 << 0)) else 'No w'
62 ret = '%srite operation in progress.\n' % s
63
64 # Bits[1:1]: WEL (write enable latch)
65 s = '' if (data & (1 << 1)) else 'not '
66 ret += 'Internal write enable latch is %sset.\n' % s
67
68 # Bits[5:2]: Block protect bits
69 # TODO: More detailed decoding (chip-dependent).
70 ret += 'Block protection bits (BP3-BP0): 0x%x.\n' % ((data & 0x3c) >> 2)
71
72 # Bits[6:6]: Continuously program mode (CP mode)
73 s = '' if (data & (1 << 6)) else 'not '
74 ret += 'Device is %sin continuously program mode (CP mode).\n' % s
75
76 # Bits[7:7]: SRWD (status register write disable)
cd287c56 77 s = 'not ' if (data & (1 << 7)) else ''
7cfbf663
UH
78 ret += 'Status register writes are %sallowed.\n' % s
79
80 return ret
81
677d597b 82class Decoder(srd.Decoder):
a2c2afd9 83 api_version = 1
1b1c914f 84 id = 'mx25lxx05d'
9a12a6e7 85 name = 'MX25Lxx05D'
3d3da57d 86 longname = 'Macronix MX25Lxx05D'
a465436e 87 desc = 'SPI (NOR) flash chip protocol.'
1b1c914f 88 license = 'gplv2+'
385508e9 89 inputs = ['spi', 'logic']
1b1c914f 90 outputs = ['mx25lxx05d']
385508e9 91 probes = []
b77614bc 92 optional_probes = [
385508e9
UH
93 {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'},
94 {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'},
95 ]
781ef945 96 options = {}
9b4d8a57 97 annotations = [
9f2f42c0
UH
98 ['text', 'Human-readable text'],
99 ['verbose-decode', 'Decoded register bits, read/write data'],
100 ['warnings', 'Human-readable warnings'],
9b4d8a57 101 ]
1b1c914f
UH
102
103 def __init__(self, **kwargs):
4772a846 104 self.state = None
781ef945 105 self.cmdstate = 1
e4022299
UH
106 self.addr = 0
107 self.data = []
1b1c914f 108
8915b346 109 def start(self):
c515eed7 110 # self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 111 self.out_ann = self.register(srd.OUTPUT_ANN)
1b1c914f 112
385508e9 113 def putx(self, data):
ee3e279c 114 # Simplification, most annotations span exactly one SPI byte/packet.
9b4d8a57
UH
115 self.put(self.ss, self.es, self.out_ann, data)
116
117 def handle_wren(self, mosi, miso):
781ef945 118 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
4772a846 119 self.state = None
1b1c914f 120
b54936a9
UH
121 def handle_wrdi(self, mosi, miso):
122 pass # TODO
123
1b1c914f 124 # TODO: Check/display device ID / name
9b4d8a57 125 def handle_rdid(self, mosi, miso):
1b1c914f
UH
126 if self.cmdstate == 1:
127 # Byte 1: Master sends command ID.
9b4d8a57 128 self.start_sample = self.ss
781ef945 129 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
1b1c914f
UH
130 elif self.cmdstate == 2:
131 # Byte 2: Slave sends the JEDEC manufacturer ID.
385508e9 132 self.putx([0, ['Manufacturer ID: 0x%02x' % miso]])
1b1c914f
UH
133 elif self.cmdstate == 3:
134 # Byte 3: Slave sends the memory type (0x20 for this chip).
385508e9 135 self.putx([0, ['Memory type: 0x%02x' % miso]])
1b1c914f
UH
136 elif self.cmdstate == 4:
137 # Byte 4: Slave sends the device ID.
9b4d8a57 138 self.device_id = miso
385508e9 139 self.putx([0, ['Device ID: 0x%02x' % miso]])
1b1c914f
UH
140
141 if self.cmdstate == 4:
142 # TODO: Check self.device_id is valid & exists in device_names.
143 # TODO: Same device ID? Check!
9b4d8a57
UH
144 d = 'Device: Macronix %s' % device_name[self.device_id]
145 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
4772a846 146 self.state = None
1b1c914f
UH
147 else:
148 self.cmdstate += 1
149
b54936a9
UH
150 def handle_rdsr(self, mosi, miso):
151 # Read status register: Master asserts CS#, sends RDSR command,
152 # reads status register byte. If CS# is kept asserted, the status
153 # register can be read continuously / multiple times in a row.
154 # When done, the master de-asserts CS# again.
155 if self.cmdstate == 1:
156 # Byte 1: Master sends command ID.
157 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
158 elif self.cmdstate >= 2:
159 # Bytes 2-x: Slave sends status register as long as master clocks.
160 if self.cmdstate <= 3: # TODO: While CS# asserted.
161 self.putx([0, ['Status register: 0x%02x' % miso]])
173c919c 162 self.putx([1, [decode_status_reg(miso)]])
b54936a9
UH
163
164 if self.cmdstate == 3: # TODO: If CS# got de-asserted.
165 self.state = None
166 return
167
168 self.cmdstate += 1
169
170 def handle_wrsr(self, mosi, miso):
171 pass # TODO
172
173 def handle_read(self, mosi, miso):
174 # Read data bytes: Master asserts CS#, sends READ command, sends
175 # 3-byte address, reads >= 1 data bytes, de-asserts CS#.
176 if self.cmdstate == 1:
177 # Byte 1: Master sends command ID.
178 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
179 elif self.cmdstate in (2, 3, 4):
180 # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
181 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
182 # self.putx([0, ['Read address, byte %d: 0x%02x' % \
183 # (4 - self.cmdstate, mosi)]])
184 if self.cmdstate == 4:
185 self.putx([0, ['Read address: 0x%06x' % self.addr]])
186 self.addr = 0
187 elif self.cmdstate >= 5:
188 # Bytes 5-x: Master reads data bytes (until CS# de-asserted).
189 # TODO: For now we hardcode 256 bytes per READ command.
190 if self.cmdstate <= 256 + 4: # TODO: While CS# asserted.
191 self.data.append(miso)
192 # self.putx([0, ['New read byte: 0x%02x' % miso]])
193
194 if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted.
195 # s = ', '.join(map(hex, self.data))
196 s = ''.join(map(chr, self.data))
173c919c
UH
197 self.putx([0, ['Read data']])
198 self.putx([1, ['Read data: %s' % s]])
b54936a9
UH
199 self.data = []
200 self.state = None
201 return
202
203 self.cmdstate += 1
204
205 def handle_fast_read(self, mosi, miso):
206 pass # TODO
207
208 def handle_2read(self, mosi, miso):
209 pass # TODO
210
1b1c914f
UH
211 # TODO: Warn/abort if we don't see the necessary amount of bytes.
212 # TODO: Warn if WREN was not seen before.
9b4d8a57 213 def handle_se(self, mosi, miso):
1b1c914f
UH
214 if self.cmdstate == 1:
215 # Byte 1: Master sends command ID.
216 self.addr = 0
9b4d8a57 217 self.start_sample = self.ss
781ef945 218 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
1b1c914f 219 elif self.cmdstate in (2, 3, 4):
87e574b7
UH
220 # Bytes 2/3/4: Master sends sectror address (24bits, MSB-first).
221 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
222 # self.putx([0, ['Sector address, byte %d: 0x%02x' % \
223 # (4 - self.cmdstate, mosi)]])
1b1c914f
UH
224
225 if self.cmdstate == 4:
87e574b7 226 d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr)
9b4d8a57 227 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
1b1c914f
UH
228 # TODO: Max. size depends on chip, check that too if possible.
229 if self.addr % 4096 != 0:
230 # Sector addresses must be 4K-aligned (same for all 3 chips).
173c919c
UH
231 d = 'Warning: Invalid sector address!'
232 self.put(self.start_sample, self.es, self.out_ann, [2, [d]])
4772a846 233 self.state = None
1b1c914f
UH
234 else:
235 self.cmdstate += 1
236
b54936a9
UH
237 def handle_be(self, mosi, miso):
238 pass # TODO
239
240 def handle_ce(self, mosi, miso):
241 pass # TODO
242
243 def handle_ce2(self, mosi, miso):
244 pass # TODO
245
246 def handle_pp(self, mosi, miso):
247 # Page program: Master asserts CS#, sends PP command, sends 3-byte
248 # page address, sends >= 1 data bytes, de-asserts CS#.
249 if self.cmdstate == 1:
250 # Byte 1: Master sends command ID.
251 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
252 elif self.cmdstate in (2, 3, 4):
253 # Bytes 2/3/4: Master sends page address (24bits, MSB-first).
254 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
255 # self.putx([0, ['Page address, byte %d: 0x%02x' % \
256 # (4 - self.cmdstate, mosi)]])
257 if self.cmdstate == 4:
258 self.putx([0, ['Page address: 0x%06x' % self.addr]])
259 self.addr = 0
260 elif self.cmdstate >= 5:
261 # Bytes 5-x: Master sends data bytes (until CS# de-asserted).
262 # TODO: For now we hardcode 256 bytes per page / PP command.
263 if self.cmdstate <= 256 + 4: # TODO: While CS# asserted.
264 self.data.append(mosi)
265 # self.putx([0, ['New data byte: 0x%02x' % mosi]])
266
267 if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted.
268 # s = ', '.join(map(hex, self.data))
269 s = ''.join(map(chr, self.data))
173c919c
UH
270 self.putx([0, ['Page data']])
271 self.putx([1, ['Page data: %s' % s]])
b54936a9
UH
272 self.data = []
273 self.state = None
274 return
275
276 self.cmdstate += 1
277
278 def handle_cp(self, mosi, miso):
279 pass # TODO
280
281 def handle_dp(self, mosi, miso):
282 pass # TODO
283
284 def handle_rdp_res(self, mosi, miso):
285 pass # TODO
286
9b4d8a57 287 def handle_rems(self, mosi, miso):
1b1c914f
UH
288 if self.cmdstate == 1:
289 # Byte 1: Master sends command ID.
9b4d8a57 290 self.start_sample = self.ss
781ef945 291 self.putx([0, ['Command: %s' % cmds[self.state][1]]])
1b1c914f
UH
292 elif self.cmdstate in (2, 3):
293 # Bytes 2/3: Master sends two dummy bytes.
294 # TODO: Check dummy bytes? Check reply from device?
385508e9 295 self.putx([0, ['Dummy byte: %s' % mosi]])
1b1c914f
UH
296 elif self.cmdstate == 4:
297 # Byte 4: Master sends 0x00 or 0x01.
298 # 0x00: Master wants manufacturer ID as first reply byte.
299 # 0x01: Master wants device ID as first reply byte.
9b4d8a57
UH
300 self.manufacturer_id_first = True if (mosi == 0x00) else False
301 d = 'manufacturer' if (mosi == 0x00) else 'device'
385508e9 302 self.putx([0, ['Master wants %s ID first' % d]])
1b1c914f
UH
303 elif self.cmdstate == 5:
304 # Byte 5: Slave sends manufacturer ID (or device ID).
9b4d8a57
UH
305 self.ids = [miso]
306 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
385508e9 307 self.putx([0, ['%s ID' % d]])
9b4d8a57 308 elif self.cmdstate == 6:
1b1c914f 309 # Byte 6: Slave sends device ID (or manufacturer ID).
7f7ea759 310 self.ids.append(miso)
9b4d8a57 311 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
385508e9 312 self.putx([0, ['%s ID' % d]])
1b1c914f
UH
313
314 if self.cmdstate == 6:
9b4d8a57 315 self.end_sample = self.es
1b1c914f 316 id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
385508e9 317 self.putx([0, ['Device: Macronix %s' % device_name[id]]])
4772a846 318 self.state = None
1b1c914f
UH
319 else:
320 self.cmdstate += 1
321
b54936a9
UH
322 def handle_rems2(self, mosi, miso):
323 pass # TODO
e4022299 324
b54936a9
UH
325 def handle_enso(self, mosi, miso):
326 pass # TODO
e4022299 327
b54936a9
UH
328 def handle_exso(self, mosi, miso):
329 pass # TODO
e4022299 330
b54936a9
UH
331 def handle_rdscur(self, mosi, miso):
332 pass # TODO
e4022299 333
b54936a9
UH
334 def handle_wrscur(self, mosi, miso):
335 pass # TODO
e4022299 336
b54936a9
UH
337 def handle_esry(self, mosi, miso):
338 pass # TODO
1b1c914f 339
b54936a9
UH
340 def handle_dsry(self, mosi, miso):
341 pass # TODO
5ebb76fe 342
2b9837d9 343 def decode(self, ss, es, data):
1b1c914f 344
9b4d8a57 345 ptype, mosi, miso = data
1b1c914f 346
e4022299 347 # if ptype == 'DATA':
781ef945 348 # self.putx([0, ['MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)]])
e4022299
UH
349
350 # if ptype == 'CS-CHANGE':
351 # if mosi == 1 and miso == 0:
781ef945 352 # self.putx([0, ['Asserting CS#']])
e4022299 353 # elif mosi == 0 and miso == 1:
781ef945 354 # self.putx([0, ['De-asserting CS#']])
e4022299 355
3e3c0330 356 if ptype != 'DATA':
9b4d8a57 357 return
1b1c914f 358
e4022299 359 self.ss, self.es = ss, es
1b1c914f 360
9b4d8a57 361 # If we encountered a known chip command, enter the resp. state.
4772a846 362 if self.state == None:
781ef945
UH
363 self.state = mosi
364 self.cmdstate = 1
1b1c914f 365
9b4d8a57 366 # Handle commands.
781ef945
UH
367 if self.state in cmds:
368 s = 'handle_%s' % cmds[self.state][0].lower().replace('/', '_')
369 handle_reg = getattr(self, s)
4772a846 370 handle_reg(mosi, miso)
9b4d8a57 371 else:
781ef945 372 self.putx([0, ['Unknown command: 0x%02x' % mosi]])
4772a846 373 self.state = None
1b1c914f 374