]> sigrok.org Git - libsigrokdecode.git/blob - decoders/spiflash/pd.py
spiflash: Handle CS# transitions, allow variable-length transfers
[libsigrokdecode.git] / decoders / spiflash / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2011-2015 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 from .lists import *
23
24 def cmd_annotation_classes():
25     return tuple([tuple([cmd[0].lower(), cmd[1]]) for cmd in cmds.values()])
26
27 def decode_status_reg(data):
28     # TODO: Additional per-bit(s) self.put() calls with correct start/end.
29
30     # Bits[0:0]: WIP (write in progress)
31     s = 'W' if (data & (1 << 0)) else 'No w'
32     ret = '%srite operation in progress.\n' % s
33
34     # Bits[1:1]: WEL (write enable latch)
35     s = '' if (data & (1 << 1)) else 'not '
36     ret += 'Internal write enable latch is %sset.\n' % s
37
38     # Bits[5:2]: Block protect bits
39     # TODO: More detailed decoding (chip-dependent).
40     ret += 'Block protection bits (BP3-BP0): 0x%x.\n' % ((data & 0x3c) >> 2)
41
42     # Bits[6:6]: Continuously program mode (CP mode)
43     s = '' if (data & (1 << 6)) else 'not '
44     ret += 'Device is %sin continuously program mode (CP mode).\n' % s
45
46     # Bits[7:7]: SRWD (status register write disable)
47     s = 'not ' if (data & (1 << 7)) else ''
48     ret += 'Status register writes are %sallowed.\n' % s
49
50     return ret
51
52 class Decoder(srd.Decoder):
53     api_version = 2
54     id = 'spiflash'
55     name = 'SPI flash'
56     longname = 'SPI flash chips'
57     desc = 'xx25 series SPI (NOR) flash chip protocol.'
58     license = 'gplv2+'
59     inputs = ['spi']
60     outputs = ['spiflash']
61     annotations = cmd_annotation_classes() + (
62         ('bits', 'Bits'),
63         ('bits2', 'Bits2'),
64         ('warnings', 'Warnings'),
65     )
66     annotation_rows = (
67         ('bits', 'Bits', (24, 25)),
68         ('commands', 'Commands', tuple(range(23 + 1))),
69         ('warnings', 'Warnings', (26,)),
70     )
71     options = (
72         {'id': 'chip', 'desc': 'Chip', 'default': tuple(chips.keys())[0],
73             'values': tuple(chips.keys())},
74     )
75
76     def __init__(self):
77         self.on_end_transaction = None
78         self.end_current_transaction()
79
80     def end_current_transaction(self):
81         if self.on_end_transaction is not None: # Callback for CS# transition.
82             self.on_end_transaction()
83             self.on_end_transaction = None
84         self.state = None
85         self.cmdstate = 1
86         self.addr = 0
87         self.data = []
88
89     def start(self):
90         self.out_ann = self.register(srd.OUTPUT_ANN)
91         self.chip = chips[self.options['chip']]
92
93     def putx(self, data):
94         # Simplification, most annotations span exactly one SPI byte/packet.
95         self.put(self.ss, self.es, self.out_ann, data)
96
97     def putb(self, data):
98         self.put(self.ss_block, self.es_block, self.out_ann, data)
99
100     def handle_wren(self, mosi, miso):
101         self.putx([0, ['Command: %s' % cmds[self.state][1]]])
102         self.state = None
103
104     def handle_wrdi(self, mosi, miso):
105         pass # TODO
106
107     # TODO: Check/display device ID / name
108     def handle_rdid(self, mosi, miso):
109         if self.cmdstate == 1:
110             # Byte 1: Master sends command ID.
111             self.ss_block = self.ss
112             self.putx([2, ['Command: %s' % cmds[self.state][1]]])
113         elif self.cmdstate == 2:
114             # Byte 2: Slave sends the JEDEC manufacturer ID.
115             self.putx([2, ['Manufacturer ID: 0x%02x' % miso]])
116         elif self.cmdstate == 3:
117             # Byte 3: Slave sends the memory type (0x20 for this chip).
118             self.putx([2, ['Memory type: 0x%02x' % miso]])
119         elif self.cmdstate == 4:
120             # Byte 4: Slave sends the device ID.
121             self.device_id = miso
122             self.putx([2, ['Device ID: 0x%02x' % miso]])
123
124         if self.cmdstate == 4:
125             # TODO: Check self.device_id is valid & exists in device_names.
126             # TODO: Same device ID? Check!
127             d = 'Device: Macronix %s' % device_name[self.device_id]
128             self.put(self.ss_block, self.es, self.out_ann, [0, [d]])
129             self.state = None
130         else:
131             self.cmdstate += 1
132
133     def handle_rdsr(self, mosi, miso):
134         # Read status register: Master asserts CS#, sends RDSR command,
135         # reads status register byte. If CS# is kept asserted, the status
136         # register can be read continuously / multiple times in a row.
137         # When done, the master de-asserts CS# again.
138         if self.cmdstate == 1:
139             # Byte 1: Master sends command ID.
140             self.putx([3, ['Command: %s' % cmds[self.state][1]]])
141         elif self.cmdstate >= 2:
142             # Bytes 2-x: Slave sends status register as long as master clocks.
143             self.putx([24, ['Status register: 0x%02x' % miso]])
144             self.putx([25, [decode_status_reg(miso)]])
145
146         self.cmdstate += 1
147
148     def handle_wrsr(self, mosi, miso):
149         pass # TODO
150
151     def handle_read(self, mosi, miso):
152         # Read data bytes: Master asserts CS#, sends READ command, sends
153         # 3-byte address, reads >= 1 data bytes, de-asserts CS#.
154         if self.cmdstate == 1:
155             # Byte 1: Master sends command ID.
156             self.putx([5, ['Command: %s' % cmds[self.state][1]]])
157         elif self.cmdstate in (2, 3, 4):
158             # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
159             self.addr |= (mosi << ((4 - self.cmdstate) * 8))
160             # self.putx([0, ['Read address, byte %d: 0x%02x' % \
161             #                (4 - self.cmdstate, mosi)]])
162             if self.cmdstate == 4:
163                 self.putx([24, ['Read address: 0x%06x' % self.addr]])
164                 self.addr = 0
165         elif self.cmdstate >= 5:
166             # Bytes 5-x: Master reads data bytes (until CS# de-asserted).
167             if self.cmdstate == 5:
168                 self.ss_block = self.ss
169                 self.on_end_transaction = lambda: self.output_data_block('Read')
170             self.data.append(miso)
171
172         self.cmdstate += 1
173
174     def handle_fast_read(self, mosi, miso):
175         # Fast read: Master asserts CS#, sends FAST READ command, sends
176         # 3-byte address + 1 dummy byte, reads >= 1 data bytes, de-asserts CS#.
177         if self.cmdstate == 1:
178             # Byte 1: Master sends command ID.
179             self.putx([5, ['Command: %s' % cmds[self.state][1]]])
180         elif self.cmdstate in (2, 3, 4):
181             # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
182             self.putx([24, ['AD%d: 0x%02x' % (self.cmdstate - 1, mosi)]])
183             if self.cmdstate == 2:
184                 self.ss_block = self.ss
185             self.addr |= (mosi << ((4 - self.cmdstate) * 8))
186         elif self.cmdstate == 5:
187             self.putx([24, ['Dummy byte: 0x%02x' % mosi]])
188             self.es_block = self.es
189             self.putb([5, ['Read address: 0x%06x' % self.addr]])
190             self.addr = 0
191         elif self.cmdstate >= 6:
192             # Bytes 6-x: Master reads data bytes (until CS# de-asserted).
193             if self.cmdstate == 6:
194                 self.ss_block = self.ss
195                 self.on_end_transaction = lambda: self.output_block("Read")
196             self.data.append(miso)
197
198         self.cmdstate += 1
199
200     def handle_2read(self, mosi, miso):
201         pass # TODO
202
203     # TODO: Warn/abort if we don't see the necessary amount of bytes.
204     # TODO: Warn if WREN was not seen before.
205     def handle_se(self, mosi, miso):
206         if self.cmdstate == 1:
207             # Byte 1: Master sends command ID.
208             self.addr = 0
209             self.ss_block = self.ss
210             self.putx([8, ['Command: %s' % cmds[self.state][1]]])
211         elif self.cmdstate in (2, 3, 4):
212             # Bytes 2/3/4: Master sends sector address (24bits, MSB-first).
213             self.addr |= (mosi << ((4 - self.cmdstate) * 8))
214             # self.putx([0, ['Sector address, byte %d: 0x%02x' % \
215             #                (4 - self.cmdstate, mosi)]])
216
217         if self.cmdstate == 4:
218             d = 'Erase sector %d (0x%06x)' % (self.addr, self.addr)
219             self.put(self.ss_block, self.es, self.out_ann, [24, [d]])
220             # TODO: Max. size depends on chip, check that too if possible.
221             if self.addr % 4096 != 0:
222                 # Sector addresses must be 4K-aligned (same for all 3 chips).
223                 d = 'Warning: Invalid sector address!'
224                 self.put(self.ss_block, self.es, self.out_ann, [101, [d]])
225             self.state = None
226         else:
227             self.cmdstate += 1
228
229     def handle_be(self, mosi, miso):
230         pass # TODO
231
232     def handle_ce(self, mosi, miso):
233         pass # TODO
234
235     def handle_ce2(self, mosi, miso):
236         pass # TODO
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([12, ['Command: %s' % cmds[self.state][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([24, ['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             if self.cmdstate == 5:
255                 self.ss_block = self.ss
256                 self.on_end_transaction = lambda: self.output_data_block('Page data')
257             self.data.append(mosi)
258
259         self.cmdstate += 1
260
261     def handle_cp(self, mosi, miso):
262         pass # TODO
263
264     def handle_dp(self, mosi, miso):
265         pass # TODO
266
267     def handle_rdp_res(self, mosi, miso):
268         pass # TODO
269
270     def handle_rems(self, mosi, miso):
271         if self.cmdstate == 1:
272             # Byte 1: Master sends command ID.
273             self.ss_block = self.ss
274             self.putx([16, ['Command: %s' % cmds[self.state][1]]])
275         elif self.cmdstate in (2, 3):
276             # Bytes 2/3: Master sends two dummy bytes.
277             # TODO: Check dummy bytes? Check reply from device?
278             self.putx([24, ['Dummy byte: %s' % mosi]])
279         elif self.cmdstate == 4:
280             # Byte 4: Master sends 0x00 or 0x01.
281             # 0x00: Master wants manufacturer ID as first reply byte.
282             # 0x01: Master wants device ID as first reply byte.
283             self.manufacturer_id_first = True if (mosi == 0x00) else False
284             d = 'manufacturer' if (mosi == 0x00) else 'device'
285             self.putx([24, ['Master wants %s ID first' % d]])
286         elif self.cmdstate == 5:
287             # Byte 5: Slave sends manufacturer ID (or device ID).
288             self.ids = [miso]
289             d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
290             self.putx([24, ['%s ID' % d]])
291         elif self.cmdstate == 6:
292             # Byte 6: Slave sends device ID (or manufacturer ID).
293             self.ids.append(miso)
294             d = 'Manufacturer' if self.manufacturer_id_first else 'Device'
295             self.putx([24, ['%s ID' % d]])
296
297         if self.cmdstate == 6:
298             id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
299             self.putx([24, ['Device: Macronix %s' % device_name[id]]])
300             self.state = None
301         else:
302             self.cmdstate += 1
303
304     def handle_rems2(self, mosi, miso):
305         pass # TODO
306
307     def handle_enso(self, mosi, miso):
308         pass # TODO
309
310     def handle_exso(self, mosi, miso):
311         pass # TODO
312
313     def handle_rdscur(self, mosi, miso):
314         pass # TODO
315
316     def handle_wrscur(self, mosi, miso):
317         pass # TODO
318
319     def handle_esry(self, mosi, miso):
320         pass # TODO
321
322     def handle_dsry(self, mosi, miso):
323         pass # TODO
324
325     def output_data_block(self, label):
326         # Print accumulated block of data
327         # (called on CS# de-assert via self.on_end_transaction callback).
328         self.es_block = self.es # Ends on the CS# de-assert sample.
329         s = ' '.join([('%02x' % b) for b in self.data])
330         self.putb([25, ['%s %d bytes: %s' % (label, len(self.data), s)]])
331
332     def decode(self, ss, es, data):
333         ptype, mosi, miso = data
334
335         self.ss, self.es = ss, es
336
337         if ptype == 'CS-CHANGE':
338             self.end_current_transaction()
339
340         if ptype != 'DATA':
341             return
342
343         # If we encountered a known chip command, enter the resp. state.
344         if self.state is None:
345             self.state = mosi
346             self.cmdstate = 1
347
348         # Handle commands.
349         if self.state in cmds:
350             s = 'handle_%s' % cmds[self.state][0].lower().replace('/', '_')
351             handle_reg = getattr(self, s)
352             handle_reg(mosi, miso)
353         else:
354             self.putx([24, ['Unknown command: 0x%02x' % mosi]])
355             self.state = None