]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/sdcard_spi/pd.py
avr_isp: Add more parts
[libsigrokdecode.git] / decoders / sdcard_spi / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2012-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
21import sigrokdecode as srd
22
23# Normal commands (CMD)
24cmd_names = {
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
53# Application-specific commands (ACMD)
54acmd_names = {
55 13: 'SD_STATUS',
56 18: 'Reserved for SD security applications',
57 22: 'SEND_NUM_WR_BLOCKS',
58 23: 'SET_WR_BLK_ERASE_COUNT',
59 25: 'Reserved for SD security applications',
60 26: 'Reserved for SD security applications',
61 38: 'Reserved for SD security applications',
62 41: 'SD_SEND_OP_COND',
63 42: 'SET_CLR_CARD_DETECT',
64 43: 'Reserved for SD security applications',
65 44: 'Reserved for SD security applications',
66 45: 'Reserved for SD security applications',
67 46: 'Reserved for SD security applications',
68 47: 'Reserved for SD security applications',
69 48: 'Reserved for SD security applications',
70 49: 'Reserved for SD security applications',
71 51: 'SEND_SCR',
72}
73
74class Decoder(srd.Decoder):
75 api_version = 2
76 id = 'sdcard_spi'
77 name = 'SD card (SPI mode)'
78 longname = 'Secure Digital card (SPI mode)'
79 desc = 'Secure Digital card (SPI mode) low-level protocol.'
80 license = 'gplv2+'
81 inputs = ['spi']
82 outputs = ['sdcard_spi']
83 annotations = \
84 tuple(('cmd%d' % i, 'CMD%d' % i) for i in range(64)) + \
85 tuple(('acmd%d' % i, 'ACMD%d' % i) for i in range(64)) + ( \
86 ('r1', 'R1 reply'),
87 ('r1b', 'R1B reply'),
88 ('r2', 'R2 reply'),
89 ('r3', 'R3 reply'),
90 ('r7', 'R7 reply'),
91 ('bits', 'Bits'),
92 ('bit-warnings', 'Bit warnings'),
93 )
94 annotation_rows = (
95 ('bits', 'Bits', (134, 135)),
96 ('cmd-reply', 'Commands/replies', tuple(range(134))),
97 )
98
99 def __init__(self, **kwargs):
100 self.state = 'IDLE'
101 self.samplenum = 0
102 self.ss, self.es = 0, 0
103 self.bit_ss, self.bit_es = 0, 0
104 self.cmd_ss, self.cmd_es = 0, 0
105 self.cmd_token = []
106 self.cmd_token_bits = []
107 self.is_acmd = False # Indicates CMD vs. ACMD
108 self.blocklen = 0
109 self.read_buf = []
110 self.cmd_str = ''
111
112 def start(self):
113 self.out_ann = self.register(srd.OUTPUT_ANN)
114
115 def putx(self, data):
116 self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
117
118 def putc(self, cmd, desc):
119 self.putx([cmd, ['%s: %s' % (self.cmd_str, desc)]])
120
121 def putb(self, data):
122 self.put(self.bit_ss, self.bit_es, self.out_ann, data)
123
124 def cmd_name(self, cmd):
125 c = acmd_names if self.is_acmd else cmd_names
126 return c.get(cmd, 'Unknown')
127
128 def handle_command_token(self, mosi, miso):
129 # Command tokens (6 bytes) are sent (MSB-first) by the host.
130 #
131 # Format:
132 # - CMD[47:47]: Start bit (always 0)
133 # - CMD[46:46]: Transmitter bit (1 == host)
134 # - CMD[45:40]: Command index (BCD; valid: 0-63)
135 # - CMD[39:08]: Argument
136 # - CMD[07:01]: CRC7
137 # - CMD[00:00]: End bit (always 1)
138
139 if len(self.cmd_token) == 0:
140 self.cmd_ss = self.ss
141
142 self.cmd_token.append(mosi)
143 self.cmd_token_bits.append(self.mosi_bits)
144
145 # All command tokens are 6 bytes long.
146 if len(self.cmd_token) < 6:
147 return
148
149 self.cmd_es = self.es
150
151 t = self.cmd_token
152
153 # CMD or ACMD?
154 s = 'ACMD' if self.is_acmd else 'CMD'
155
156 def tb(byte, bit):
157 return self.cmd_token_bits[5 - byte][bit]
158
159 # Bits[47:47]: Start bit (always 0)
160 bit, self.bit_ss, self.bit_es = tb(5, 7)[0], tb(5, 7)[1], tb(5, 7)[2]
161 if bit == 0:
162 self.putb([134, ['Start bit: %d' % bit]])
163 else:
164 self.putb([135, ['Start bit: %s (Warning: Must be 0!)' % bit]])
165
166 # Bits[46:46]: Transmitter bit (1 == host)
167 bit, self.bit_ss, self.bit_es = tb(5, 6)[0], tb(5, 6)[1], tb(5, 6)[2]
168 if bit == 1:
169 self.putb([134, ['Transmitter bit: %d' % bit]])
170 else:
171 self.putb([135, ['Transmitter bit: %d (Warning: Must be 1!)' % bit]])
172
173 # Bits[45:40]: Command index (BCD; valid: 0-63)
174 cmd = self.cmd_index = t[0] & 0x3f
175 self.bit_ss, self.bit_es = tb(5, 5)[1], tb(5, 0)[2]
176 self.putb([134, ['Command: %s%d (%s)' % (s, cmd, self.cmd_name(cmd))]])
177
178 # Bits[39:8]: Argument
179 self.arg = (t[1] << 24) | (t[2] << 16) | (t[3] << 8) | t[4]
180 self.bit_ss, self.bit_es = tb(4, 7)[1], tb(1, 0)[2]
181 self.putb([134, ['Argument: 0x%04x' % self.arg]])
182
183 # Bits[7:1]: CRC7
184 # TODO: Check CRC7.
185 crc = t[5] >> 1
186 self.bit_ss, self.bit_es = tb(0, 7)[1], tb(0, 1)[2]
187 self.putb([134, ['CRC7: 0x%01x' % crc]])
188
189 # Bits[0:0]: End bit (always 1)
190 bit, self.bit_ss, self.bit_es = tb(0, 0)[0], tb(0, 0)[1], tb(0, 0)[2]
191 self.putb([134, ['End bit: %d' % bit]])
192 if bit == 1:
193 self.putb([134, ['End bit: %d' % bit]])
194 else:
195 self.putb([135, ['End bit: %d (Warning: Must be 1!)' % bit]])
196
197 # Handle command.
198 if cmd in (0, 1, 9, 16, 17, 41, 49, 55, 59):
199 self.state = 'HANDLE CMD%d' % cmd
200 self.cmd_str = '%s%d (%s)' % (s, cmd, self.cmd_name(cmd))
201 else:
202 self.state = 'HANDLE CMD999'
203 a = '%s%d: %02x %02x %02x %02x %02x %02x' % ((s, cmd) + tuple(t))
204 self.putx([cmd, [a]])
205
206 def handle_cmd0(self):
207 # CMD0: GO_IDLE_STATE
208 self.putc(0, 'Reset the SD card')
209 self.state = 'GET RESPONSE R1'
210
211 def handle_cmd1(self):
212 # CMD1: SEND_OP_COND
213 self.putc(1, 'Send HCS info and activate the card init process')
214 hcs = (self.arg & (1 << 30)) >> 30
215 self.bit_ss = self.cmd_token_bits[5 - 4][6][1]
216 self.bit_es = self.cmd_token_bits[5 - 4][6][2]
217 self.putb([134, ['HCS: %d' % hcs]])
218 self.state = 'GET RESPONSE R1'
219
220 def handle_cmd9(self):
221 # CMD9: SEND_CSD (128 bits / 16 bytes)
222 self.putc(9, 'Ask card to send its card specific data (CSD)')
223 if len(self.read_buf) == 0:
224 self.cmd_ss = self.ss
225 self.read_buf.append(self.miso)
226 # FIXME
227 ### if len(self.read_buf) < 16:
228 if len(self.read_buf) < 16 + 4:
229 return
230 self.cmd_es = self.es
231 self.read_buf = self.read_buf[4:] ### TODO: Document or redo.
232 self.putx([9, ['CSD: %s' % self.read_buf]])
233 # TODO: Decode all bits.
234 self.read_buf = []
235 ### self.state = 'GET RESPONSE R1'
236 self.state = 'IDLE'
237
238 def handle_cmd10(self):
239 # CMD10: SEND_CID (128 bits / 16 bytes)
240 self.putc(10, 'Ask card to send its card identification (CID)')
241 self.read_buf.append(self.miso)
242 if len(self.read_buf) < 16:
243 return
244 self.putx([10, ['CID: %s' % self.read_buf]])
245 # TODO: Decode all bits.
246 self.read_buf = []
247 self.state = 'GET RESPONSE R1'
248
249 def handle_cmd16(self):
250 # CMD16: SET_BLOCKLEN
251 self.blocklen = self.arg
252 # TODO: Sanity check on block length.
253 self.putc(16, 'Set the block length to %d bytes' % self.blocklen)
254 self.state = 'GET RESPONSE R1'
255
256 def handle_cmd17(self):
257 # CMD17: READ_SINGLE_BLOCK
258 self.putc(17, 'Read a block from address 0x%04x' % self.arg)
259 if len(self.read_buf) == 0:
260 self.cmd_ss = self.ss
261 self.read_buf.append(self.miso)
262 if len(self.read_buf) < self.blocklen + 2: # FIXME
263 return
264 self.cmd_es = self.es
265 self.read_buf = self.read_buf[2:] # FIXME
266 self.putx([17, ['Block data: %s' % self.read_buf]])
267 self.read_buf = []
268 self.state = 'GET RESPONSE R1'
269
270 def handle_cmd49(self):
271 self.state = 'GET RESPONSE R1'
272
273 def handle_cmd55(self):
274 # CMD55: APP_CMD
275 self.putc(55, 'Next command is an application-specific command')
276 self.is_acmd = True
277 self.state = 'GET RESPONSE R1'
278
279 def handle_cmd59(self):
280 # CMD59: CRC_ON_OFF
281 crc_on_off = self.arg & (1 << 0)
282 s = 'on' if crc_on_off == 1 else 'off'
283 self.putc(59, 'Turn the SD card CRC option %s' % s)
284 self.state = 'GET RESPONSE R1'
285
286 def handle_acmd41(self):
287 # ACMD41: SD_SEND_OP_COND
288 self.putc(64 + 41, 'Send HCS info and activate the card init process')
289 self.state = 'GET RESPONSE R1'
290
291 def handle_cmd999(self):
292 self.state = 'GET RESPONSE R1'
293
294 def handle_cid_register(self):
295 # Card Identification (CID) register, 128bits
296
297 cid = self.cid
298
299 # Manufacturer ID: CID[127:120] (8 bits)
300 mid = cid[15]
301
302 # OEM/Application ID: CID[119:104] (16 bits)
303 oid = (cid[14] << 8) | cid[13]
304
305 # Product name: CID[103:64] (40 bits)
306 pnm = 0
307 for i in range(12, 8 - 1, -1):
308 pnm <<= 8
309 pnm |= cid[i]
310
311 # Product revision: CID[63:56] (8 bits)
312 prv = cid[7]
313
314 # Product serial number: CID[55:24] (32 bits)
315 psn = 0
316 for i in range(6, 3 - 1, -1):
317 psn <<= 8
318 psn |= cid[i]
319
320 # RESERVED: CID[23:20] (4 bits)
321
322 # Manufacturing date: CID[19:8] (12 bits)
323 # TODO
324
325 # CRC7 checksum: CID[7:1] (7 bits)
326 # TODO
327
328 # Not used, always 1: CID[0:0] (1 bit)
329 # TODO
330
331 def handle_response_r1(self, res):
332 # The R1 response token format (1 byte).
333 # Sent by the card after every command except for SEND_STATUS.
334
335 self.cmd_ss, self.cmd_es = self.miso_bits[7][1], self.miso_bits[0][2]
336 self.putx([65, ['R1: 0x%02x' % res]])
337
338 def putbit(bit, data):
339 b = self.miso_bits[bit]
340 self.bit_ss, self.bit_es = b[1], b[2]
341 self.putb([134, data])
342
343 # Bit 0: 'In idle state' bit
344 s = '' if (res & (1 << 0)) else 'not '
345 putbit(0, ['Card is %sin idle state' % s])
346
347 # Bit 1: 'Erase reset' bit
348 s = '' if (res & (1 << 1)) else 'not '
349 putbit(1, ['Erase sequence %scleared' % s])
350
351 # Bit 2: 'Illegal command' bit
352 s = 'I' if (res & (1 << 2)) else 'No i'
353 putbit(2, ['%sllegal command detected' % s])
354
355 # Bit 3: 'Communication CRC error' bit
356 s = 'failed' if (res & (1 << 3)) else 'was successful'
357 putbit(3, ['CRC check of last command %s' % s])
358
359 # Bit 4: 'Erase sequence error' bit
360 s = 'E' if (res & (1 << 4)) else 'No e'
361 putbit(4, ['%srror in the sequence of erase commands' % s])
362
363 # Bit 5: 'Address error' bit
364 s = 'M' if (res & (1 << 4)) else 'No m'
365 putbit(5, ['%sisaligned address used in command' % s])
366
367 # Bit 6: 'Parameter error' bit
368 s = '' if (res & (1 << 4)) else 'not '
369 putbit(6, ['Command argument %soutside allowed range' % s])
370
371 # Bit 7: Always set to 0
372 putbit(7, ['Bit 7 (always 0)'])
373
374 self.state = 'IDLE'
375
376 def handle_response_r1b(self, res):
377 # TODO
378 pass
379
380 def handle_response_r2(self, res):
381 # TODO
382 pass
383
384 def handle_response_r3(self, res):
385 # TODO
386 pass
387
388 # Note: Response token formats R4 and R5 are reserved for SDIO.
389
390 # TODO: R6?
391
392 def handle_response_r7(self, res):
393 # TODO
394 pass
395
396 def decode(self, ss, es, data):
397 ptype, mosi, miso = data
398
399 # For now, only use DATA and BITS packets.
400 if ptype not in ('DATA', 'BITS'):
401 return
402
403 # Store the individual bit values and ss/es numbers. The next packet
404 # is guaranteed to be a 'DATA' packet belonging to this 'BITS' one.
405 if ptype == 'BITS':
406 self.miso_bits, self.mosi_bits = miso, mosi
407 return
408
409 self.ss, self.es = ss, es
410
411 # State machine.
412 if self.state == 'IDLE':
413 # Ignore stray 0xff bytes, some devices seem to send those!?
414 if mosi == 0xff: # TODO?
415 return
416 self.state = 'GET COMMAND TOKEN'
417 self.handle_command_token(mosi, miso)
418 elif self.state == 'GET COMMAND TOKEN':
419 self.handle_command_token(mosi, miso)
420 elif self.state.startswith('HANDLE CMD'):
421 self.miso, self.mosi = miso, mosi
422 # Call the respective handler method for the command.
423 a, cmdstr = 'a' if self.is_acmd else '', self.state[10:].lower()
424 handle_cmd = getattr(self, 'handle_%scmd%s' % (a, cmdstr))
425 handle_cmd()
426 self.cmd_token = []
427 self.cmd_token_bits = []
428 # Leave ACMD mode again after the first command after CMD55.
429 if self.is_acmd and cmdstr != '55':
430 self.is_acmd = False
431 elif self.state.startswith('GET RESPONSE'):
432 # Ignore stray 0xff bytes, some devices seem to send those!?
433 if miso == 0xff: # TODO?
434 return
435
436 # Call the respective handler method for the response.
437 s = 'handle_response_%s' % self.state[13:].lower()
438 handle_response = getattr(self, s)
439 handle_response(miso)
440
441 self.state = 'IDLE'
442 else:
443 raise Exception('Invalid state: %s' % self.state)
444