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