]> sigrok.org Git - libsigrokdecode.git/blob - decoders/mx25lxx05d/mx25lxx05d.py
srd: Properly use append() for appending to lists.
[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 # States
28 IDLE = -1
29
30 # Chip commands (also used as additional decoder states).
31 CMD_WREN      = 0x06
32 CMD_WRDI      = 0x04
33 CMD_RDID      = 0x9f
34 CMD_RDSR      = 0x05
35 CMD_WRSR      = 0x01
36 CMD_READ      = 0x03
37 CMD_FAST_READ = 0x0b
38 CMD_2READ     = 0xbb
39 CMD_SE        = 0x20
40 CMD_BE        = 0xd8
41 CMD_CE        = 0x60
42 CMD_CE2       = 0xc7
43 CMD_PP        = 0x02
44 CMD_CP        = 0xad
45 CMD_DP        = 0xb9
46 # CMD_RDP       = 0xab
47 # CMD_RES       = 0xab
48 CMD_RDP_RES   = 0xab # Note: RDP/RES have the same ID.
49 CMD_REMS      = 0x90
50 CMD_REMS2     = 0xef
51 CMD_ENSO      = 0xb1
52 CMD_EXSO      = 0xc1
53 CMD_RDSCUR    = 0x2b
54 CMD_WRSCUR    = 0x2f
55 CMD_ESRY      = 0x70
56 CMD_DSRY      = 0x80
57
58 # TODO: (Short) command names as strings in a dict, too?
59
60 # Dict which maps command IDs to their description.
61 cmds = {
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',
72     CMD_CE: 'Chip 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#',
88 }
89
90 device_name = {
91     0x14: 'MX25L1605D',
92     0x15: 'MX25L3205D',
93     0x16: 'MX25L6405D',
94 }
95
96 class Decoder(srd.Decoder):
97     api_version = 1
98     id = 'mx25lxx05d'
99     name = 'MX25Lxx05D'
100     longname = 'Macronix MX25Lxx05D'
101     desc = 'Macronix MX25Lxx05D SPI flash chip decoder'
102     longdesc = 'TODO'
103     license = 'gplv2+'
104     inputs = ['spi', 'logic']
105     outputs = ['mx25lxx05d']
106     probes = []
107     optional_probes = [
108         {'id': 'hold', 'name': 'HOLD#', 'desc': 'TODO.'},
109         {'id': 'wp_acc', 'name': 'WP#/ACC', 'desc': 'TODO.'},
110     ]
111     options = {} # TODO
112     annotations = [
113         ['TODO', 'TODO'],
114     ]
115
116     def __init__(self, **kwargs):
117         self.state = IDLE
118         self.cmdstate = 1 # TODO
119
120     def start(self, metadata):
121         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'mx25lxx05d')
122         self.out_ann = self.add(srd.OUTPUT_ANN, 'mx25lxx05d')
123
124     def report(self):
125         pass
126
127     def putx(self, data):
128         # Simplification, most annotations span extactly one SPI byte/packet.
129         self.put(self.ss, self.es, self.out_ann, data)
130
131     def handle_wren(self, mosi, miso):
132         self.putx([0, ['Command: %s' % cmds[self.cmd]]])
133         self.state = IDLE
134
135     # TODO: Check/display device ID / name
136     def handle_rdid(self, mosi, miso):
137         if self.cmdstate == 1:
138             # Byte 1: Master sends command ID.
139             self.start_sample = self.ss
140             self.putx([0, ['Command: %s' % cmds[self.cmd]]])
141         elif self.cmdstate == 2:
142             # Byte 2: Slave sends the JEDEC manufacturer ID.
143             self.putx([0, ['Manufacturer ID: 0x%02x' % miso]])
144         elif self.cmdstate == 3:
145             # Byte 3: Slave sends the memory type (0x20 for this chip).
146             self.putx([0, ['Memory type: 0x%02x' % miso]])
147         elif self.cmdstate == 4:
148             # Byte 4: Slave sends the device ID.
149             self.device_id = miso
150             self.putx([0, ['Device ID: 0x%02x' % miso]])
151
152         if self.cmdstate == 4:
153             # TODO: Check self.device_id is valid & exists in device_names.
154             # TODO: Same device ID? Check!
155             d = 'Device: Macronix %s' % device_name[self.device_id]
156             self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
157             self.state = IDLE
158         else:
159             self.cmdstate += 1
160
161     # TODO: Warn/abort if we don't see the necessary amount of bytes.
162     # TODO: Warn if WREN was not seen before.
163     def handle_se(self, mosi, miso):
164         if self.cmdstate == 1:
165             # Byte 1: Master sends command ID.
166             self.addr = 0
167             self.start_sample = self.ss
168             self.putx([0, ['Command: %s' % cmds[self.cmd]]])
169         elif self.cmdstate in (2, 3, 4):
170             # Bytes 2/3/4: Master sends address of the sector to erase.
171             # Note: Assumes SPI data is 8 bits wide (it is for MX25Lxx05D).
172             # TODO: LSB-first of MSB-first?
173             self.addr <<= 8
174             self.addr |= mosi
175             self.putx([0, ['Address byte %d: 0x%02x' % (self.cmdstate - 1,
176                         miso)]]) # TODO: Count from 0 or 1?
177
178         if self.cmdstate == 4:
179             d = 'Erase sector %d' % self.addr
180             self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
181             # TODO: Max. size depends on chip, check that too if possible.
182             if self.addr % 4096 != 0:
183                 # Sector addresses must be 4K-aligned (same for all 3 chips).
184                 d = 'Warning: Invalid sector address!' # TODO: type == WARN?
185                 self.put(self.start_sample, self.es, self.out_ann, [0, [d]])
186             self.state = IDLE
187         else:
188             self.cmdstate += 1
189
190     def handle_rems(self, mosi, miso):
191         if self.cmdstate == 1:
192             # Byte 1: Master sends command ID.
193             self.start_sample = self.ss
194             self.putx([0, ['Command: %s' % cmds[self.cmd]]])
195         elif self.cmdstate in (2, 3):
196             # Bytes 2/3: Master sends two dummy bytes.
197             # TODO: Check dummy bytes? Check reply from device?
198             self.putx([0, ['Dummy byte: %s' % mosi]])
199         elif self.cmdstate == 4:
200             # Byte 4: Master sends 0x00 or 0x01.
201             # 0x00: Master wants manufacturer ID as first reply byte.
202             # 0x01: Master wants device ID as first reply byte.
203             self.manufacturer_id_first = True if (mosi == 0x00) else False
204             d = 'manufacturer' if (mosi == 0x00) else 'device'
205             self.putx([0, ['Master wants %s ID first' % d]])
206         elif self.cmdstate == 5:
207             # Byte 5: Slave sends manufacturer ID (or device ID).
208             self.ids = [miso]
209             d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
210             self.putx([0, ['%s ID' % d]])
211         elif self.cmdstate == 6:
212             # Byte 6: Slave sends device ID (or manufacturer ID).
213             self.ids.append(miso)
214             d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
215             self.putx([0, ['%s ID' % d]])
216         else:
217             # TODO: Error?
218             pass
219
220         if self.cmdstate == 6:
221             self.end_sample = self.es
222             id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
223             self.putx([0, ['Device: Macronix %s' % device_name[id]]])
224             self.state = IDLE
225         else:
226             self.cmdstate += 1
227
228     def handle_rdsr(self, mosi, miso):
229         self.putx([0, ['Command: %s (0x%02x)' % (cmds[self.cmd], miso)]])
230         self.state = IDLE
231
232     def decode(self, ss, es, data):
233
234         ptype, mosi, miso = data
235
236         if ptype != 'data':
237             return
238
239         cmd = mosi
240         self.ss = ss
241         self.es = es
242
243         # If we encountered a known chip command, enter the resp. state.
244         if self.state == IDLE:
245             if cmd in cmds:
246                 self.state = cmd
247                 self.cmd = cmd # TODO: Eliminate?
248                 self.cmdstate = 1
249             else:
250                 pass # TODO
251
252         # Handle commands.
253         # TODO: Use some generic way to invoke the resp. method.
254         if self.state == CMD_WREN:
255             self.handle_wren(mosi, miso)
256         elif self.state == CMD_SE:
257             self.handle_se(mosi, miso)
258         elif self.state == CMD_RDID:
259             self.handle_rdid(mosi, miso)
260         if self.state == CMD_REMS:
261             self.handle_rems(mosi, miso)
262         if self.state == CMD_RDSR:
263             self.handle_rdsr(mosi, miso)
264         else:
265             self.put(0, 0, self.out_ann, [0, ['Unknown command: 0x%02x' % cmd]])
266             self.state = IDLE
267