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
22 # Macronix MX25Lxx05D SPI (NOR) flash chip decoder.
23 # Works for MX25L1605D/MX25L3205D/MX25L6405D.
30 # http://www.macronix.com/QuickPlace/hq/PageLibrary4825740B00298A3B.nsf/h_Index/3F21BAC2E121E17848257639003A3146/$File/MX25L1605D-3205D-6405D-1.5.pdf
33 import sigrokdecode as srd
38 # Chip commands (also used as additional decoder states).
56 CMD_RDP_RES = 0xab # Note: RDP/RES have the same ID.
66 # TODO: (Short) command names as strings in a dict, too?
68 # Dict which maps command IDs to their description.
70 CMD_WREN: 'Write enable',
71 CMD_WRDI: 'Write disable',
72 CMD_RDID: 'Read identification',
73 CMD_RDSR: 'Read status register',
74 CMD_WRSR: 'Write status register',
75 CMD_READ: 'Read data',
76 CMD_FAST_READ: 'Fast read data',
77 CMD_2READ: '2x I/O read',
78 CMD_SE: 'Sector erase',
79 CMD_BE: 'Block erase',
81 CMD_CE2: 'Chip erase', # Alternative command ID
82 CMD_PP: 'Page program',
83 CMD_CP: 'Continuously program mode',
84 CMD_DP: 'Deep power down',
85 # CMD_RDP: 'Release from deep powerdown',
86 # CMD_RES: 'Read electronic ID',
87 CMD_RDP_RES: 'Release from deep powerdown / Read electronic ID',
88 CMD_REMS: 'Read electronic manufacturer & device ID',
89 CMD_REMS2: 'Read ID for 2x I/O mode',
90 CMD_ENSO: 'Enter secured OTP',
91 CMD_EXSO: 'Exit secured OTP',
92 CMD_RDSCUR: 'Read security register',
93 CMD_WRSCUR: 'Write security register',
94 CMD_ESRY: 'Enable SO to output RY/BY#',
95 CMD_DSRY: 'Disable SO to output RY/BY#',
104 class Decoder(srd.Decoder):
107 longname = 'Macronix MX25Lxx05D'
108 desc = 'Macronix MX25Lxx05D SPI flash chip decoder'
111 inputs = ['spi', 'spi', 'logic']
112 outputs = ['mx25lxx05d']
113 probes = [] # TODO: HOLD#, WP#/ACC
119 def __init__(self, **kwargs):
121 self.cmdstate = 1 # TODO
123 def start(self, metadata):
124 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d')
125 self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d')
130 def putann(self, data):
131 # Simplification, most annotations span extactly one SPI byte/packet.
132 self.put(self.ss, self.es, self.out_ann, data)
134 def handle_wren(self, mosi, miso):
135 self.putann([0, ['Command: %s' % cmds[self.cmd]]])
138 # TODO: Check/display device ID / name
139 def handle_rdid(self, mosi, miso):
140 if self.cmdstate == 1:
141 # Byte 1: Master sends command ID.
142 self.start_sample = self.ss
143 self.putann([0, ['Command: %s' % cmds[self.cmd]]])
144 elif self.cmdstate == 2:
145 # Byte 2: Slave sends the JEDEC manufacturer ID.
146 self.putann([0, ['Manufacturer ID: 0x%02x' % miso]])
147 elif self.cmdstate == 3:
148 # Byte 3: Slave sends the memory type (0x20 for this chip).
149 self.putann([0, ['Memory type: 0x%02x' % miso]])
150 elif self.cmdstate == 4:
151 # Byte 4: Slave sends the device ID.
152 self.device_id = miso
153 self.putann([0, ['Device ID: 0x%02x' % miso]])
155 if self.cmdstate == 4:
156 # TODO: Check self.device_id is valid & exists in device_names.
157 # TODO: Same device ID? Check!
158 d = 'Device: Macronix %s' % device_name[self.device_id]
159 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
164 # TODO: Warn/abort if we don't see the necessary amount of bytes.
165 # TODO: Warn if WREN was not seen before.
166 def handle_se(self, mosi, miso):
167 if self.cmdstate == 1:
168 # Byte 1: Master sends command ID.
170 self.start_sample = self.ss
171 self.putann([0, ['Command: %s' % cmds[self.cmd]]])
172 elif self.cmdstate in (2, 3, 4):
173 # Bytes 2/3/4: Master sends address of the sector to erase.
174 # Note: Assumes SPI data is 8 bits wide (it is for MX25Lxx05D).
175 # TODO: LSB-first of MSB-first?
178 self.putann([0, ['Address byte %d: 0x%02x' % (self.cmdstate - 1,
179 miso)]]) # TODO: Count from 0 or 1?
181 if self.cmdstate == 4:
182 d = 'Erase sector %d' % self.addr
183 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
184 # TODO: Max. size depends on chip, check that too if possible.
185 if self.addr % 4096 != 0:
186 # Sector addresses must be 4K-aligned (same for all 3 chips).
187 d = 'Warning: Invalid sector address!' # TODO: type == WARN?
188 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
193 def handle_rems(self, mosi, miso):
194 if self.cmdstate == 1:
195 # Byte 1: Master sends command ID.
196 self.start_sample = self.ss
197 self.putann([0, ['Command: %s' % cmds[self.cmd]]])
198 elif self.cmdstate in (2, 3):
199 # Bytes 2/3: Master sends two dummy bytes.
200 # TODO: Check dummy bytes? Check reply from device?
201 self.putann([0, ['Dummy byte: %s' % mosi]])
202 elif self.cmdstate == 4:
203 # Byte 4: Master sends 0x00 or 0x01.
204 # 0x00: Master wants manufacturer ID as first reply byte.
205 # 0x01: Master wants device ID as first reply byte.
206 self.manufacturer_id_first = True if (mosi == 0x00) else False
207 d = 'manufacturer' if (mosi == 0x00) else 'device'
208 self.putann([0, ['Master wants %s ID first' % d]])
209 elif self.cmdstate == 5:
210 # Byte 5: Slave sends manufacturer ID (or device ID).
212 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
213 self.putann([0, ['%s ID' % d]])
214 elif self.cmdstate == 6:
215 # Byte 6: Slave sends device ID (or manufacturer ID).
217 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
218 self.putann([0, ['%s ID' % d]])
223 if self.cmdstate == 6:
224 self.end_sample = self.es
225 id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
226 self.putann([0, ['Device: Macronix %s' % device_name[id]]])
231 def handle_rdsr(self, mosi, miso):
232 self.putann([0, ['Command: %s (0x%02x)' % (cmds[self.cmd], miso)]])
235 def decode(self, ss, es, data):
237 ptype, mosi, miso = data
246 # If we encountered a known chip command, enter the resp. state.
247 if self.state == IDLE:
250 self.cmd = cmd # TODO: Eliminate?
258 # TODO: Use some generic way to invoke the resp. method.
259 if self.state == CMD_WREN:
260 self.handle_wren(mosi, miso)
261 elif self.state == CMD_SE:
262 self.handle_se(mosi, miso)
263 elif self.state == CMD_RDID:
264 self.handle_rdid(mosi, miso)
265 if self.state == CMD_REMS:
266 self.handle_rems(mosi, miso)
267 if self.state == CMD_RDSR:
268 self.handle_rdsr(mosi, miso)
270 self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]])