]> sigrok.org Git - libsigrokdecode.git/blame - decoders/mx25lxx05d/mx25lxx05d.py
srd: MX25Lxx05D: Implement RDSR and PP commands.
[libsigrokdecode.git] / decoders / mx25lxx05d / mx25lxx05d.py
CommitLineData
1b1c914f
UH
1##
2## This file is part of the sigrok project.
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
156509ca 21# Macronix MX25Lxx05D SPI (NOR) flash chip protocol decoder
1b1c914f 22
156509ca 23# Note: Works for MX25L1605D/MX25L3205D/MX25L6405D.
1b1c914f 24
677d597b 25import sigrokdecode as srd
1b1c914f
UH
26
27# States
28IDLE = -1
29
30# Chip commands (also used as additional decoder states).
31CMD_WREN = 0x06
32CMD_WRDI = 0x04
33CMD_RDID = 0x9f
34CMD_RDSR = 0x05
35CMD_WRSR = 0x01
36CMD_READ = 0x03
37CMD_FAST_READ = 0x0b
38CMD_2READ = 0xbb
39CMD_SE = 0x20
40CMD_BE = 0xd8
41CMD_CE = 0x60
42CMD_CE2 = 0xc7
43CMD_PP = 0x02
44CMD_CP = 0xad
45CMD_DP = 0xb9
46# CMD_RDP = 0xab
47# CMD_RES = 0xab
48CMD_RDP_RES = 0xab # Note: RDP/RES have the same ID.
49CMD_REMS = 0x90
50CMD_REMS2 = 0xef
51CMD_ENSO = 0xb1
52CMD_EXSO = 0xc1
53CMD_RDSCUR = 0x2b
54CMD_WRSCUR = 0x2f
55CMD_ESRY = 0x70
56CMD_DSRY = 0x80
57
58# TODO: (Short) command names as strings in a dict, too?
59
60# Dict which maps command IDs to their description.
61cmds = {
62 CMD_WREN: 'Write enable',
63 CMD_WRDI: 'Write disable',
64 CMD_RDID: 'Read identification',
65 CMD_RDSR: 'Read status register',
66 CMD_WRSR: 'Write status register',
67 CMD_READ: 'Read data',
68 CMD_FAST_READ: 'Fast read data',
69 CMD_2READ: '2x I/O read',
70 CMD_SE: 'Sector erase',
71 CMD_BE: 'Block erase',
72 CMD_CE: 'Chip erase',
73 CMD_CE2: 'Chip erase', # Alternative command ID
74 CMD_PP: 'Page program',
75 CMD_CP: 'Continuously program mode',
76 CMD_DP: 'Deep power down',
77 # CMD_RDP: 'Release from deep powerdown',
78 # CMD_RES: 'Read electronic ID',
79 CMD_RDP_RES: 'Release from deep powerdown / Read electronic ID',
80 CMD_REMS: 'Read electronic manufacturer & device ID',
81 CMD_REMS2: 'Read ID for 2x I/O mode',
82 CMD_ENSO: 'Enter secured OTP',
83 CMD_EXSO: 'Exit secured OTP',
84 CMD_RDSCUR: 'Read security register',
85 CMD_WRSCUR: 'Write security register',
86 CMD_ESRY: 'Enable SO to output RY/BY#',
87 CMD_DSRY: 'Disable SO to output RY/BY#',
88}
89
90device_name = {
91 0x14: 'MX25L1605D',
92 0x15: 'MX25L3205D',
93 0x16: 'MX25L6405D',
94}
95
677d597b 96class Decoder(srd.Decoder):
a2c2afd9 97 api_version = 1
1b1c914f 98 id = 'mx25lxx05d'
9a12a6e7 99 name = 'MX25Lxx05D'
3d3da57d 100 longname = 'Macronix MX25Lxx05D'
a465436e 101 desc = 'SPI (NOR) flash chip protocol.'
1b1c914f 102 license = 'gplv2+'
385508e9 103 inputs = ['spi', 'logic']
1b1c914f 104 outputs = ['mx25lxx05d']
385508e9 105 probes = []
b77614bc 106 optional_probes = [
385508e9
UH
107 {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'},
108 {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'},
109 ]
1b1c914f 110 options = {} # TODO
9b4d8a57 111 annotations = [
ee3e279c 112 ['Text', 'Human-readable text'],
9b4d8a57 113 ]
1b1c914f
UH
114
115 def __init__(self, **kwargs):
1b1c914f
UH
116 self.state = IDLE
117 self.cmdstate = 1 # TODO
e4022299
UH
118 self.addr = 0
119 self.data = []
1b1c914f
UH
120
121 def start(self, metadata):
56202222
UH
122 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d')
123 self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d')
1b1c914f
UH
124
125 def report(self):
126 pass
127
385508e9 128 def putx(self, data):
ee3e279c 129 # Simplification, most annotations span exactly one SPI byte/packet.
9b4d8a57
UH
130 self.put(self.ss, self.es, self.out_ann, data)
131
132 def handle_wren(self, mosi, miso):
385508e9 133 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
1b1c914f
UH
134 self.state = IDLE
135
136 # TODO: Check/display device ID / name
9b4d8a57 137 def handle_rdid(self, mosi, miso):
1b1c914f
UH
138 if self.cmdstate == 1:
139 # Byte 1: Master sends command ID.
9b4d8a57 140 self.start_sample = self.ss
385508e9 141 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
1b1c914f
UH
142 elif self.cmdstate == 2:
143 # Byte 2: Slave sends the JEDEC manufacturer ID.
385508e9 144 self.putx([0, ['Manufacturer ID: 0x%02x' % miso]])
1b1c914f
UH
145 elif self.cmdstate == 3:
146 # Byte 3: Slave sends the memory type (0x20 for this chip).
385508e9 147 self.putx([0, ['Memory type: 0x%02x' % miso]])
1b1c914f
UH
148 elif self.cmdstate == 4:
149 # Byte 4: Slave sends the device ID.
9b4d8a57 150 self.device_id = miso
385508e9 151 self.putx([0, ['Device ID: 0x%02x' % miso]])
1b1c914f
UH
152
153 if self.cmdstate == 4:
154 # TODO: Check self.device_id is valid & exists in device_names.
155 # TODO: Same device ID? Check!
9b4d8a57
UH
156 d = 'Device: Macronix %s' % device_name[self.device_id]
157 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
1b1c914f
UH
158 self.state = IDLE
159 else:
160 self.cmdstate += 1
161
1b1c914f
UH
162 # TODO: Warn/abort if we don't see the necessary amount of bytes.
163 # TODO: Warn if WREN was not seen before.
9b4d8a57 164 def handle_se(self, mosi, miso):
1b1c914f
UH
165 if self.cmdstate == 1:
166 # Byte 1: Master sends command ID.
167 self.addr = 0
9b4d8a57 168 self.start_sample = self.ss
385508e9 169 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
1b1c914f
UH
170 elif self.cmdstate in (2, 3, 4):
171 # Bytes 2/3/4: Master sends address of the sector to erase.
172 # Note: Assumes SPI data is 8 bits wide (it is for MX25Lxx05D).
173 # TODO: LSB-first of MSB-first?
174 self.addr <<= 8
9b4d8a57 175 self.addr |= mosi
385508e9 176 self.putx([0, ['Address byte %d: 0x%02x' % (self.cmdstate - 1,
9b4d8a57 177 miso)]]) # TODO: Count from 0 or 1?
1b1c914f
UH
178
179 if self.cmdstate == 4:
9b4d8a57
UH
180 d = 'Erase sector %d' % self.addr
181 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
1b1c914f
UH
182 # TODO: Max. size depends on chip, check that too if possible.
183 if self.addr % 4096 != 0:
184 # Sector addresses must be 4K-aligned (same for all 3 chips).
9b4d8a57
UH
185 d = 'Warning: Invalid sector address!' # TODO: type == WARN?
186 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
1b1c914f
UH
187 self.state = IDLE
188 else:
189 self.cmdstate += 1
190
9b4d8a57 191 def handle_rems(self, mosi, miso):
1b1c914f
UH
192 if self.cmdstate == 1:
193 # Byte 1: Master sends command ID.
9b4d8a57 194 self.start_sample = self.ss
385508e9 195 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
1b1c914f
UH
196 elif self.cmdstate in (2, 3):
197 # Bytes 2/3: Master sends two dummy bytes.
198 # TODO: Check dummy bytes? Check reply from device?
385508e9 199 self.putx([0, ['Dummy byte: %s' % mosi]])
1b1c914f
UH
200 elif self.cmdstate == 4:
201 # Byte 4: Master sends 0x00 or 0x01.
202 # 0x00: Master wants manufacturer ID as first reply byte.
203 # 0x01: Master wants device ID as first reply byte.
9b4d8a57
UH
204 self.manufacturer_id_first = True if (mosi == 0x00) else False
205 d = 'manufacturer' if (mosi == 0x00) else 'device'
385508e9 206 self.putx([0, ['Master wants %s ID first' % d]])
1b1c914f
UH
207 elif self.cmdstate == 5:
208 # Byte 5: Slave sends manufacturer ID (or device ID).
9b4d8a57
UH
209 self.ids = [miso]
210 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
385508e9 211 self.putx([0, ['%s ID' % d]])
9b4d8a57 212 elif self.cmdstate == 6:
1b1c914f 213 # Byte 6: Slave sends device ID (or manufacturer ID).
7f7ea759 214 self.ids.append(miso)
9b4d8a57 215 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
385508e9 216 self.putx([0, ['%s ID' % d]])
1b1c914f
UH
217 else:
218 # TODO: Error?
219 pass
220
221 if self.cmdstate == 6:
9b4d8a57 222 self.end_sample = self.es
1b1c914f 223 id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
385508e9 224 self.putx([0, ['Device: Macronix %s' % device_name[id]]])
1b1c914f
UH
225 self.state = IDLE
226 else:
227 self.cmdstate += 1
228
9b4d8a57 229 def handle_rdsr(self, mosi, miso):
e4022299
UH
230 # Read status register: Master asserts CS#, sends RDSR command,
231 # reads status register byte. If CS# is kept asserted, the status
232 # register can be read continuously / multiple times in a row.
233 # When done, the master de-asserts CS# again.
234 if self.cmdstate == 1:
235 # Byte 1: Master sends command ID.
236 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
237 elif self.cmdstate >= 2:
238 # Bytes 2-x: Slave sends status register as long as master clocks.
239 if self.cmdstate <= 3: # TODO: While CS# asserted.
240 self.putx([0, ['Status register: 0x%02x' % miso]])
241 # TODO: Decode status register bits.
242
243 if self.cmdstate == 3: # TODO: If CS# got de-asserted.
244 self.state = IDLE
245 return
246
247 self.cmdstate += 1
248
249 def handle_pp(self, mosi, miso):
250 # Page program: Master asserts CS#, sends PP command, sends 3-byte
251 # page address, sends >= 1 data bytes, de-asserts CS#.
252 if self.cmdstate == 1:
253 # Byte 1: Master sends command ID.
254 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
255 elif self.cmdstate in (2, 3, 4):
256 # Bytes 2/3/4: Master sends page address (24bits, MSB-first).
257 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
258 # self.putx([0, ['Page address, byte %d: 0x%02x' % \
259 # (4 - self.cmdstate, mosi)]])
260 if self.cmdstate == 4:
261 self.putx([0, ['Page address: 0x%06x' % self.addr]])
262 self.addr = 0
263 elif self.cmdstate >= 5:
264 # Bytes 5-x: Master sends data bytes (until CS# de-asserted).
265 # TODO: For now we hardcode 256 bytes per page / PP command.
266 if self.cmdstate <= 256 + 4: # TODO: While CS# asserted.
267 self.data.append(mosi)
268 # self.putx([0, ['New data byte: 0x%02x' % mosi]])
269
270 if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted.
271 # s = ', '.join(map(hex, self.data))
272 s = ''.join(map(chr, self.data))
273 self.putx([0, ['Page data: %s' % s]])
274 self.data = []
275 self.state = IDLE
276 return
277
278 self.cmdstate += 1
1b1c914f 279
2b9837d9 280 def decode(self, ss, es, data):
1b1c914f 281
9b4d8a57 282 ptype, mosi, miso = data
1b1c914f 283
e4022299
UH
284 # if ptype == 'DATA':
285 # s = 'MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)
286 # self.put(0, 0, self.out_ann, [0, [s]])
287 # pass
288
289 # if ptype == 'CS-CHANGE':
290 # if mosi == 1 and miso == 0:
291 # self.put(0, 0, self.out_ann, [0, ['Asserting CS#']])
292 # elif mosi == 0 and miso == 1:
293 # self.put(0, 0, self.out_ann, [0, ['De-asserting CS#']])
294 # return
295
3e3c0330 296 if ptype != 'DATA':
9b4d8a57 297 return
1b1c914f 298
9b4d8a57 299 cmd = mosi
e4022299 300 self.ss, self.es = ss, es
1b1c914f 301
9b4d8a57
UH
302 # If we encountered a known chip command, enter the resp. state.
303 if self.state == IDLE:
304 if cmd in cmds:
305 self.state = cmd
306 self.cmd = cmd # TODO: Eliminate?
307 self.cmdstate = 1
1b1c914f 308 else:
9b4d8a57 309 pass # TODO
1b1c914f 310
9b4d8a57
UH
311 # Handle commands.
312 # TODO: Use some generic way to invoke the resp. method.
313 if self.state == CMD_WREN:
314 self.handle_wren(mosi, miso)
315 elif self.state == CMD_SE:
316 self.handle_se(mosi, miso)
317 elif self.state == CMD_RDID:
318 self.handle_rdid(mosi, miso)
e4022299 319 elif self.state == CMD_REMS:
9b4d8a57 320 self.handle_rems(mosi, miso)
e4022299 321 elif self.state == CMD_RDSR:
9b4d8a57 322 self.handle_rdsr(mosi, miso)
e4022299
UH
323 elif self.state == CMD_PP:
324 self.handle_pp(mosi, miso)
9b4d8a57
UH
325 else:
326 self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]])
327 self.state = IDLE
1b1c914f 328