]> sigrok.org Git - libsigrokdecode.git/blame - decoders/sdcard_spi/pd.py
s/out_proto/out_python/.
[libsigrokdecode.git] / decoders / sdcard_spi / pd.py
CommitLineData
47ec18f5 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
47ec18f5
UH
3##
4## Copyright (C) 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
47ec18f5
UH
21import sigrokdecode as srd
22
23cmd_name = {
24 # Normal commands (CMD)
25 0: 'GO_IDLE_STATE',
26 1: 'SEND_OP_COND',
27 6: 'SWITCH_FUNC',
28 8: 'SEND_IF_COND',
29 9: 'SEND_CSD',
30 10: 'SEND_CID',
31 12: 'STOP_TRANSMISSION',
32 13: 'SEND_STATUS',
33 16: 'SET_BLOCKLEN',
34 17: 'READ_SINGLE_BLOCK',
35 18: 'READ_MULTIPLE_BLOCK',
36 24: 'WRITE_BLOCK',
37 25: 'WRITE_MULTIPLE_BLOCK',
38 27: 'PROGRAM_CSD',
39 28: 'SET_WRITE_PROT',
40 29: 'CLR_WRITE_PROT',
41 30: 'SEND_WRITE_PROT',
42 32: 'ERASE_WR_BLK_START_ADDR',
43 33: 'ERASE_WR_BLK_END_ADDR',
44 38: 'ERASE',
45 42: 'LOCK_UNLOCK',
46 55: 'APP_CMD',
47 56: 'GEN_CMD',
48 58: 'READ_OCR',
49 59: 'CRC_ON_OFF',
50 # CMD60-63: Reserved for manufacturer
51
52 # Application-specific commands (ACMD)
53 13: 'SD_STATUS',
54 18: 'Reserved for SD security applications',
55 22: 'SEND_NUM_WR_BLOCKS',
56 23: 'SET_WR_BLK_ERASE_COUNT',
57 25: 'Reserved for SD security applications',
58 26: 'Reserved for SD security applications',
59 38: 'Reserved for SD security applications',
60 41: 'SD_SEND_OP_COND',
61 42: 'SET_CLR_CARD_DETECT',
62 43: 'Reserved for SD security applications',
63 44: 'Reserved for SD security applications',
64 45: 'Reserved for SD security applications',
65 46: 'Reserved for SD security applications',
66 47: 'Reserved for SD security applications',
67 48: 'Reserved for SD security applications',
68 49: 'Reserved for SD security applications',
69 51: 'SEND_SCR',
70}
71
72class Decoder(srd.Decoder):
73 api_version = 1
74 id = 'sdcard_spi'
75 name = 'SD card (SPI mode)'
76 longname = 'Secure Digital card (SPI mode)'
77 desc = 'Secure Digital card (SPI mode) low-level protocol.'
78 license = 'gplv2+'
79 inputs = ['spi']
80 outputs = ['sdcard_spi']
81 probes = []
82 optional_probes = []
83 options = {}
84 annotations = [
9f2f42c0
UH
85 ['text', 'Human-readable text'],
86 ['warnings', 'Human-readable warnings'],
47ec18f5
UH
87 ]
88
89 def __init__(self, **kwargs):
90 self.state = 'IDLE'
91 self.samplenum = 0
92 self.cmd_ss, self.cmd_es = 0, 0
93 self.cmd_token = []
94 self.is_acmd = False # Indicates CMD vs. ACMD
95 self.blocklen = 0
96 self.read_buf = []
97
8915b346 98 def start(self):
c515eed7 99 # self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 100 self.out_ann = self.register(srd.OUTPUT_ANN)
47ec18f5 101
47ec18f5
UH
102 def putx(self, data):
103 self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
104
105 def handle_command_token(self, mosi, miso):
106 # Command tokens (6 bytes) are sent (MSB-first) by the host.
107 #
108 # Format:
109 # - CMD[47:47]: Start bit (always 0)
110 # - CMD[46:46]: Transmitter bit (1 == host)
111 # - CMD[45:40]: Command index (BCD; valid: 0-63)
112 # - CMD[39:08]: Argument
113 # - CMD[07:01]: CRC7
114 # - CMD[00:00]: End bit (always 1)
115
116 self.cmd_token.append(mosi)
117 # TODO: Record MISO too?
118
119 # All command tokens are 6 bytes long.
120 if len(self.cmd_token) < 6:
121 return
122
123 # Received all 6 bytes of the command token. Now decode it.
124
125 t = self.cmd_token
126
127 # CMD or ACMD?
128 s = 'ACMD' if self.is_acmd else 'CMD'
129 # TODO
130 self.put(0, 0, self.out_ann,
131 [0, [s + ': %02x %02x %02x %02x %02x %02x' % tuple(t)]])
132
133 # Start bit
134 self.startbit = (t[0] & (1 << 7)) >> 7
135 self.put(0, 0, self.out_ann,
136 [0, ['Start bit: %d' % self.startbit]])
137 if self.startbit != 0:
138 # TODO
139 self.put(0, 0, self.out_ann, [1, ['Warning: Start bit != 0']])
140
141 # Transmitter bit
142 self.transmitterbit = (t[0] & (1 << 6)) >> 6
143 self.put(0, 0, self.out_ann,
144 [0, ['Transmitter bit: %d' % self.transmitterbit]])
145 if self.transmitterbit != 0:
146 # TODO
147 self.put(0, 0, self.out_ann, [1, ['Warning: Transmitter bit != 1']])
148
149 # Command index
150 cmd = self.cmd_index = t[0] & 0x3f
151 # TODO
152 self.put(0, 0, self.out_ann,
153 [0, ['Command: %s%d (%s)' % (s, cmd, cmd_name[cmd])]])
154
155 # Argument
156 self.arg = (t[1] << 24) | (t[2] << 16) | (t[3] << 8) | t[4]
157 self.put(0, 0, self.out_ann, [0, ['Argument: 0x%04x' % self.arg]])
158 # TODO: Sanity check on argument? Must be per-cmd?
159
160 # CRC
161 # TODO: Check CRC.
162 self.crc = t[5] >> 1
163 self.put(0, 0, self.out_ann, [0, ['CRC: 0x%01x' % self.crc]])
164
165 # End bit
166 self.endbit = t[5] & (1 << 0)
167 self.put(0, 0, self.out_ann, [0, ['End bit: %d' % self.endbit]])
168 if self.endbit != 1:
169 # TODO
170 self.put(0, 0, self.out_ann, [1, ['Warning: End bit != 1']])
171
172 # Handle command.
173 if cmd in (0, 1, 9, 16, 17, 41, 49, 55, 59):
174 self.state = 'HANDLE CMD%d' % cmd
175
176 # ...
177 if self.is_acmd and cmd != 55:
178 self.is_acmd = False
179
180 self.cmd_token = []
181
182 def handle_cmd0(self, ):
183 # CMD0: GO_IDLE_STATE
184 # TODO
185 self.put(0, 0, self.out_ann, [0, ['CMD0: Card reset / idle state']])
186 self.state = 'GET RESPONSE R1'
187
188 def handle_cmd1(self):
189 # CMD1: SEND_OP_COND
190 # TODO
191 hcs = (self.arg & (1 << 30)) >> 30
192 self.put(0, 0, self.out_ann, [0, ['HCS bit = %d' % hcs]])
193 self.state = 'GET RESPONSE R1'
194
195 def handle_cmd9(self):
196 # CMD9: SEND_CSD (128 bits / 16 bytes)
197 self.read_buf.append(self.miso)
198 # FIXME
199 ### if len(self.read_buf) < 16:
200 if len(self.read_buf) < 16 + 4:
201 return
202 self.read_buf = self.read_buf[4:] ### TODO: Document or redo.
203 self.put(0, 0, self.out_ann, [0, ['CSD: %s' % self.read_buf]])
204 # TODO: Decode all bits.
205 self.read_buf = []
206 ### self.state = 'GET RESPONSE R1'
207 self.state = 'IDLE'
208
209 def handle_cmd10(self):
210 # CMD10: SEND_CID (128 bits / 16 bytes)
211 self.read_buf.append(self.miso)
212 if len(self.read_buf) < 16:
213 return
214 self.put(0, 0, self.out_ann, [0, ['CID: %s' % self.read_buf]])
215 # TODO: Decode all bits.
216 self.read_buf = []
217 self.state = 'GET RESPONSE R1'
218
219 def handle_cmd16(self):
220 # CMD16: SET_BLOCKLEN
221 self.blocklen = self.arg # TODO
222 # TODO: Sanity check on block length.
223 self.put(0, 0, self.out_ann, [0, ['Block length: %d' % self.blocklen]])
224 self.state = 'GET RESPONSE R1'
225
226 def handle_cmd17(self):
227 # CMD17: READ_SINGLE_BLOCK
228 self.read_buf.append(self.miso)
229 if len(self.read_buf) == 1:
230 self.put(0, 0, self.out_ann,
231 [0, ['Read block at address: 0x%04x' % self.arg]])
232 if len(self.read_buf) < self.blocklen + 2: # FIXME
233 return
234 self.read_buf = self.read_buf[2:] # FIXME
235 self.put(0, 0, self.out_ann, [0, ['Block data: %s' % self.read_buf]])
236 self.read_buf = []
237 self.state = 'GET RESPONSE R1'
238
239 def handle_cmd41(self):
240 # ACMD41: SD_SEND_OP_COND
241 self.state = 'GET RESPONSE R1'
242
243 def handle_cmd49(self):
244 self.state = 'GET RESPONSE R1'
245
246 def handle_cmd55(self):
247 # CMD55: APP_CMD
248 self.is_acmd = True
249 self.state = 'GET RESPONSE R1'
250
251 def handle_cmd59(self):
252 # CMD59: CRC_ON_OFF
253 crc_on_off = self.arg & (1 << 0)
254 s = 'on' if crc_on_off == 1 else 'off'
255 self.put(0, 0, self.out_ann, [0, ['SD card CRC option: %s' % s]])
256 self.state = 'GET RESPONSE R1'
257
258 def handle_cid_register(self):
259 # Card Identification (CID) register, 128bits
260
261 cid = self.cid
262
263 # Manufacturer ID: CID[127:120] (8 bits)
264 mid = cid[15]
265
266 # OEM/Application ID: CID[119:104] (16 bits)
267 oid = (cid[14] << 8) | cid[13]
268
269 # Product name: CID[103:64] (40 bits)
270 pnm = 0
271 for i in range(12, 8 - 1, -1):
272 pnm <<= 8
273 pnm |= cid[i]
274
275 # Product revision: CID[63:56] (8 bits)
276 prv = cid[7]
277
278 # Product serial number: CID[55:24] (32 bits)
279 psn = 0
280 for i in range(6, 3 - 1, -1):
281 psn <<= 8
282 psn |= cid[i]
283
284 # RESERVED: CID[23:20] (4 bits)
285
286 # Manufacturing date: CID[19:8] (12 bits)
287 # TODO
288
289 # CRC7 checksum: CID[7:1] (7 bits)
290 # TODO
291
292 # Not used, always 1: CID[0:0] (1 bit)
293 # TODO
294
295 def handle_response_r1(self, res):
296 # The R1 response token format (1 byte).
297 # Sent by the card after every command except for SEND_STATUS.
298
299 self.put(0, 0, self.out_ann, [0, ['R1: 0x%02x' % res]])
300
301 # TODO: Configurable whether all bits are decoded.
302
303 # 'In idle state' bit
304 s = '' if (res & (1 << 0)) else 'not '
305 self.put(0, 0, self.out_ann, [0, ['Card is %sin idle state' % s]])
306
307 # 'Erase reset' bit
308 s = '' if (res & (1 << 1)) else 'not '
309 self.put(0, 0, self.out_ann, [0, ['Erase sequence %scleared' % s]])
310
311 # 'Illegal command' bit
312 s = 'I' if (res & (1 << 2)) else 'No i'
313 self.put(0, 0, self.out_ann, [0, ['%sllegal command detected' % s]])
314
315 # 'Communication CRC error' bit
316 s = 'failed' if (res & (1 << 3)) else 'was successful'
317 self.put(0, 0, self.out_ann,
318 [0, ['CRC check of last command %s' % s]])
319
320 # 'Erase sequence error' bit
321 s = 'E' if (res & (1 << 4)) else 'No e'
322 self.put(0, 0, self.out_ann,
323 [0, ['%srror in the sequence of erase commands' % s]])
324
325 # 'Address error' bit
326 s = 'M' if (res & (1 << 4)) else 'No m'
327 self.put(0, 0, self.out_ann,
328 [0, ['%sisaligned address used in command' % s]])
329
330 # 'Parameter error' bit
331 s = '' if (res & (1 << 4)) else 'not '
332 self.put(0, 0, self.out_ann,
333 [0, ['Command argument %soutside allowed range' % s]])
334
335 self.state = 'IDLE'
336
337 def handle_response_r1b(self, res):
338 # TODO
339 pass
340
341 def handle_response_r2(self, res):
342 # TODO
343 pass
344
345 def handle_response_r3(self, res):
346 # TODO
347 pass
348
349 # Note: Response token formats R4 and R5 are reserved for SDIO.
350
351 # TODO: R6?
352
353 def handle_response_r7(self, res):
354 # TODO
355 pass
356
357 def decode(self, ss, es, data):
358 ptype, mosi, miso = data
359
360 # For now, ignore non-data packets.
361 if ptype != 'DATA':
362 return
363
364 self.put(0, 0, self.out_ann, [0, ['0x%02x 0x%02x' % (mosi, miso)]])
365
366 # State machine.
367 if self.state == 'IDLE':
368 # Ignore stray 0xff bytes, some devices seem to send those!?
369 if mosi == 0xff: # TODO?
370 return
371 self.state = 'GET COMMAND TOKEN'
372 self.handle_command_token(mosi, miso)
373 elif self.state == 'GET COMMAND TOKEN':
374 self.handle_command_token(mosi, miso)
375 elif self.state.startswith('HANDLE CMD'):
376 self.miso, self.mosi = miso, mosi
377 # Call the respective handler method for the command.
378 s = 'handle_cmd%s' % self.state[10:].lower()
379 handle_cmd = getattr(self, s)
380 handle_cmd()
381 elif self.state.startswith('GET RESPONSE'):
382 # Ignore stray 0xff bytes, some devices seem to send those!?
383 if miso == 0xff: # TODO?
384 return
385
386 # Call the respective handler method for the response.
387 s = 'handle_response_%s' % self.state[13:].lower()
388 # self.put(0, 0, self.out_ann, [0, [s]]) # TODO
389 handle_response = getattr(self, s)
390 handle_response(miso)
391
392 self.state = 'IDLE'
393 else:
394 raise Exception('Invalid state: %s' % self.state)
395