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