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 = 'Macronix MX25Lxx05D SPI flash chip decoder'
103 inputs = ['spi', 'logic']
104 outputs = ['mx25lxx05d']
107 {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'},
108 {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'},
115 def __init__(self, **kwargs):
117 self.cmdstate = 1 # TODO
119 def start(self, metadata):
120 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d')
121 self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d')
126 def putx(self, data):
127 # Simplification, most annotations span extactly one SPI byte/packet.
128 self.put(self.ss, self.es, self.out_ann, data)
130 def handle_wren(self, mosi, miso):
131 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
134 # TODO: Check/display device ID / name
135 def handle_rdid(self, mosi, miso):
136 if self.cmdstate == 1:
137 # Byte 1: Master sends command ID.
138 self.start_sample = self.ss
139 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
140 elif self.cmdstate == 2:
141 # Byte 2: Slave sends the JEDEC manufacturer ID.
142 self.putx([0, ['Manufacturer ID: 0x%02x' % miso]])
143 elif self.cmdstate == 3:
144 # Byte 3: Slave sends the memory type (0x20 for this chip).
145 self.putx([0, ['Memory type: 0x%02x' % miso]])
146 elif self.cmdstate == 4:
147 # Byte 4: Slave sends the device ID.
148 self.device_id = miso
149 self.putx([0, ['Device ID: 0x%02x' % miso]])
151 if self.cmdstate == 4:
152 # TODO: Check self.device_id is valid & exists in device_names.
153 # TODO: Same device ID? Check!
154 d = 'Device: Macronix %s' % device_name[self.device_id]
155 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
160 # TODO: Warn/abort if we don't see the necessary amount of bytes.
161 # TODO: Warn if WREN was not seen before.
162 def handle_se(self, mosi, miso):
163 if self.cmdstate == 1:
164 # Byte 1: Master sends command ID.
166 self.start_sample = self.ss
167 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
168 elif self.cmdstate in (2, 3, 4):
169 # Bytes 2/3/4: Master sends address of the sector to erase.
170 # Note: Assumes SPI data is 8 bits wide (it is for MX25Lxx05D).
171 # TODO: LSB-first of MSB-first?
174 self.putx([0, ['Address byte %d: 0x%02x' % (self.cmdstate - 1,
175 miso)]]) # TODO: Count from 0 or 1?
177 if self.cmdstate == 4:
178 d = 'Erase sector %d' % self.addr
179 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
180 # TODO: Max. size depends on chip, check that too if possible.
181 if self.addr % 4096 != 0:
182 # Sector addresses must be 4K-aligned (same for all 3 chips).
183 d = 'Warning: Invalid sector address!' # TODO: type == WARN?
184 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
189 def handle_rems(self, mosi, miso):
190 if self.cmdstate == 1:
191 # Byte 1: Master sends command ID.
192 self.start_sample = self.ss
193 self.putx([0, ['Command: %s' % cmds[self.cmd]]])
194 elif self.cmdstate in (2, 3):
195 # Bytes 2/3: Master sends two dummy bytes.
196 # TODO: Check dummy bytes? Check reply from device?
197 self.putx([0, ['Dummy byte: %s' % mosi]])
198 elif self.cmdstate == 4:
199 # Byte 4: Master sends 0x00 or 0x01.
200 # 0x00: Master wants manufacturer ID as first reply byte.
201 # 0x01: Master wants device ID as first reply byte.
202 self.manufacturer_id_first = True if (mosi == 0x00) else False
203 d = 'manufacturer' if (mosi == 0x00) else 'device'
204 self.putx([0, ['Master wants %s ID first' % d]])
205 elif self.cmdstate == 5:
206 # Byte 5: Slave sends manufacturer ID (or device ID).
208 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
209 self.putx([0, ['%s ID' % d]])
210 elif self.cmdstate == 6:
211 # Byte 6: Slave sends device ID (or manufacturer ID).
212 self.ids.append(miso)
213 d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
214 self.putx([0, ['%s ID' % d]])
219 if self.cmdstate == 6:
220 self.end_sample = self.es
221 id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
222 self.putx([0, ['Device: Macronix %s' % device_name[id]]])
227 def handle_rdsr(self, mosi, miso):
228 self.putx([0, ['Command: %s (0x%02x)' % (cmds[self.cmd], miso)]])
231 def decode(self, ss, es, data):
233 ptype, mosi, miso = data
242 # If we encountered a known chip command, enter the resp. state.
243 if self.state == IDLE:
246 self.cmd = cmd # TODO: Eliminate?
252 # TODO: Use some generic way to invoke the resp. method.
253 if self.state == CMD_WREN:
254 self.handle_wren(mosi, miso)
255 elif self.state == CMD_SE:
256 self.handle_se(mosi, miso)
257 elif self.state == CMD_RDID:
258 self.handle_rdid(mosi, miso)
259 if self.state == CMD_REMS:
260 self.handle_rems(mosi, miso)
261 if self.state == CMD_RDSR:
262 self.handle_rdsr(mosi, miso)
264 self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]])