2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 # Macronix MX25Lxx05D SPI (NOR) flash chip protocol decoder
23 # Note: Works for MX25L1605D/MX25L3205D/MX25L6405D.
25 import sigrokdecode as srd
30 # Chip commands (also used as additional decoder states).
48 CMD_RDP_RES = 0xab # Note: RDP/RES have the same ID.
58 # TODO: (Short) command names as strings in a dict, too?
60 # Dict which maps command IDs to their description.
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',
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#',
96 class Decoder(srd.Decoder):
100 longname = 'Macronix MX25Lxx05D'
101 desc = 'SPI (NOR) flash chip protocol.'
103 inputs = ['spi', 'logic']
104 outputs = ['mx25lxx05d']
107 {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'},
108 {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'},
112 ['Text', 'Human-readable text'],
115 def __init__(self, **kwargs):
117 self.cmdstate = 1 # TODO
121 def start(self, metadata):
122 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d')
123 self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d')
128 def putx(self, data):
129 # Simplification, most annotations span exactly one SPI byte/packet.
130 self.put(self.ss, self.es, self.out_ann, data)
132 def handle_wren(self, mosi, miso):
133 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
136 # TODO: Check/display device ID / name
137 def handle_rdid(self, mosi, miso):
138 if self.cmdstate == 1:
139 # Byte 1: Master sends command ID.
140 self.start_sample = self.ss
141 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
142 elif self.cmdstate == 2:
143 # Byte 2: Slave sends the JEDEC manufacturer ID.
144 self.putx([0, ['Manufacturer ID: 0x%02x' % miso]])
145 elif self.cmdstate == 3:
146 # Byte 3: Slave sends the memory type (0x20 for this chip).
147 self.putx([0, ['Memory type: 0x%02x' % miso]])
148 elif self.cmdstate == 4:
149 # Byte 4: Slave sends the device ID.
150 self.device_id = miso
151 self.putx([0, ['Device ID: 0x%02x' % miso]])
153 if self.cmdstate == 4:
154 # TODO: Check self.device_id is valid & exists in device_names.
155 # TODO: Same device ID? Check!
156 d = 'Device: Macronix %s' % device_name[self.device_id]
157 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
162 # TODO: Warn/abort if we don't see the necessary amount of bytes.
163 # TODO: Warn if WREN was not seen before.
164 def handle_se(self, mosi, miso):
165 if self.cmdstate == 1:
166 # Byte 1: Master sends command ID.
168 self.start_sample = self.ss
169 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
170 elif self.cmdstate in (2, 3, 4):
171 # Bytes 2/3/4: Master sends sectror address (24bits, MSB-first).
172 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
173 # self.putx([0, ['Sector address, byte %d: 0x%02x' % \
174 # (4 - self.cmdstate, mosi)]])
176 if self.cmdstate == 4:
177 d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr)
178 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
179 # TODO: Max. size depends on chip, check that too if possible.
180 if self.addr % 4096 != 0:
181 # Sector addresses must be 4K-aligned (same for all 3 chips).
182 d = 'Warning: Invalid sector address!' # TODO: type == WARN?
183 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
188 def handle_rems(self, mosi, miso):
189 if self.cmdstate == 1:
190 # Byte 1: Master sends command ID.
191 self.start_sample = self.ss
192 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
193 elif self.cmdstate in (2, 3):
194 # Bytes 2/3: Master sends two dummy bytes.
195 # TODO: Check dummy bytes? Check reply from device?
196 self.putx([0, ['Dummy byte: %s' % mosi]])
197 elif self.cmdstate == 4:
198 # Byte 4: Master sends 0x00 or 0x01.
199 # 0x00: Master wants manufacturer ID as first reply byte.
200 # 0x01: Master wants device ID as first reply byte.
201 self.manufacturer_id_first = True if (mosi == 0x00) else False
202 d = 'manufacturer' if (mosi == 0x00) else 'device'
203 self.putx([0, ['Master wants %s ID first' % d]])
204 elif self.cmdstate == 5:
205 # Byte 5: Slave sends manufacturer ID (or device ID).
207 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
208 self.putx([0, ['%s ID' % d]])
209 elif self.cmdstate == 6:
210 # Byte 6: Slave sends device ID (or manufacturer ID).
211 self.ids.append(miso)
212 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
213 self.putx([0, ['%s ID' % d]])
218 if self.cmdstate == 6:
219 self.end_sample = self.es
220 id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
221 self.putx([0, ['Device: Macronix %s' % device_name[id]]])
226 def handle_rdsr(self, mosi, miso):
227 # Read status register: Master asserts CS#, sends RDSR command,
228 # reads status register byte. If CS# is kept asserted, the status
229 # register can be read continuously / multiple times in a row.
230 # When done, the master de-asserts CS# again.
231 if self.cmdstate == 1:
232 # Byte 1: Master sends command ID.
233 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
234 elif self.cmdstate >= 2:
235 # Bytes 2-x: Slave sends status register as long as master clocks.
236 if self.cmdstate <= 3: # TODO: While CS# asserted.
237 self.putx([0, ['Status register: 0x%02x' % miso]])
238 # TODO: Decode status register bits.
240 if self.cmdstate == 3: # TODO: If CS# got de-asserted.
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.cmd]]])
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]])
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]])
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))
270 self.putx([0, ['Page data: %s' % s]])
277 def handle_read(self, mosi, miso):
278 # Read data bytes: Master asserts CS#, sends READ command, sends
279 # 3-byte address, reads >= 1 data bytes, de-asserts CS#.
280 if self.cmdstate == 1:
281 # Byte 1: Master sends command ID.
282 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
283 elif self.cmdstate in (2, 3, 4):
284 # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
285 self.addr |= (mosi << ((4 - self.cmdstate) * 8))
286 # self.putx([0, ['Read address, byte %d: 0x%02x' % \
287 # (4 - self.cmdstate, mosi)]])
288 if self.cmdstate == 4:
289 self.putx([0, ['Read address: 0x%06x' % self.addr]])
291 elif self.cmdstate >= 5:
292 # Bytes 5-x: Master reads data bytes (until CS# de-asserted).
293 # TODO: For now we hardcode 256 bytes per READ command.
294 if self.cmdstate <= 256 + 4: # TODO: While CS# asserted.
295 self.data.append(miso)
296 # self.putx([0, ['New read byte: 0x%02x' % miso]])
298 if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted.
299 # s = ', '.join(map(hex, self.data))
300 s = ''.join(map(chr, self.data))
301 self.putx([0, ['Read data: %s' % s]])
308 def decode(self, ss, es, data):
310 ptype, mosi, miso = data
312 # if ptype == 'DATA':
313 # s = 'MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)
314 # self.put(0, 0, self.out_ann, [0, [s]])
317 # if ptype == 'CS-CHANGE':
318 # if mosi == 1 and miso == 0:
319 # self.put(0, 0, self.out_ann, [0, ['Asserting CS#']])
320 # elif mosi == 0 and miso == 1:
321 # self.put(0, 0, self.out_ann, [0, ['De-asserting CS#']])
328 self.ss, self.es = ss, es
330 # If we encountered a known chip command, enter the resp. state.
331 if self.state == IDLE:
334 self.cmd = cmd # TODO: Eliminate?
340 # TODO: Use some generic way to invoke the resp. method.
341 if self.state == CMD_WREN:
342 self.handle_wren(mosi, miso)
343 elif self.state == CMD_SE:
344 self.handle_se(mosi, miso)
345 elif self.state == CMD_RDID:
346 self.handle_rdid(mosi, miso)
347 elif self.state == CMD_REMS:
348 self.handle_rems(mosi, miso)
349 elif self.state == CMD_RDSR:
350 self.handle_rdsr(mosi, miso)
351 elif self.state == CMD_PP:
352 self.handle_pp(mosi, miso)
353 elif self.state == CMD_READ:
354 self.handle_read(mosi, miso)
356 self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]])