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