2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012-2014 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 import sigrokdecode as srd
24 # Normal commands (CMD)
31 12: 'STOP_TRANSMISSION',
34 17: 'READ_SINGLE_BLOCK',
35 18: 'READ_MULTIPLE_BLOCK',
37 25: 'WRITE_MULTIPLE_BLOCK',
41 30: 'SEND_WRITE_PROT',
42 32: 'ERASE_WR_BLK_START_ADDR',
43 33: 'ERASE_WR_BLK_END_ADDR',
50 # CMD60-63: Reserved for manufacturer
52 # Application-specific commands (ACMD)
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',
74 for i in range(63 + 1):
75 l.append(('cmd%d' % i, 'CMD%d' % i))
79 class Decoder(srd.Decoder):
82 name = 'SD card (SPI mode)'
83 longname = 'Secure Digital card (SPI mode)'
84 desc = 'Secure Digital card (SPI mode) low-level protocol.'
87 outputs = ['sdcard_spi']
88 annotations = cmd_list() + (
89 ('cmd-desc', 'Command description'),
96 ('bit-warnings', 'Bit warnings'),
99 ('bits', 'Bits', (70, 71)),
100 ('cmd-reply', 'Commands/replies',
101 tuple(range(0, 63 + 1)) + tuple(range(65, 69 + 1))),
102 ('cmd-token', 'Command tokens', (64,)),
105 def __init__(self, **kwargs):
108 self.ss, self.es = 0, 0
109 self.bit_ss, self.bit_es = 0, 0
110 self.cmd_ss, self.cmd_es = 0, 0
112 self.cmd_token_bits = []
113 self.is_acmd = False # Indicates CMD vs. ACMD
119 # self.out_python = self.register(srd.OUTPUT_PYTHON)
120 self.out_ann = self.register(srd.OUTPUT_ANN)
122 def putx(self, data):
123 self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
125 def putc(self, cmd, desc):
126 self.putx([cmd, ['%s: %s' % (self.cmd_str, desc)]])
128 def putb(self, data):
129 self.put(self.bit_ss, self.bit_es, self.out_ann, data)
131 def handle_command_token(self, mosi, miso):
132 # Command tokens (6 bytes) are sent (MSB-first) by the host.
135 # - CMD[47:47]: Start bit (always 0)
136 # - CMD[46:46]: Transmitter bit (1 == host)
137 # - CMD[45:40]: Command index (BCD; valid: 0-63)
138 # - CMD[39:08]: Argument
140 # - CMD[00:00]: End bit (always 1)
142 if len(self.cmd_token) == 0:
143 self.cmd_ss = self.ss
145 self.cmd_token.append(mosi)
146 self.cmd_token_bits.append(self.mosi_bits)
148 # All command tokens are 6 bytes long.
149 if len(self.cmd_token) < 6:
152 self.cmd_es = self.es
157 s = 'ACMD' if self.is_acmd else 'CMD'
160 return self.cmd_token_bits[5 - byte][bit]
162 # Bits[47:47]: Start bit (always 0)
163 bit, self.bit_ss, self.bit_es = tb(5, 7)[0], tb(5, 7)[1], tb(5, 7)[2]
165 self.putb([70, ['Start bit: %d' % bit]])
167 self.putb([71, ['Start bit: %s (Warning: Must be 0!)' % bit]])
169 # Bits[46:46]: Transmitter bit (1 == host)
170 bit, self.bit_ss, self.bit_es = tb(5, 6)[0], tb(5, 6)[1], tb(5, 6)[2]
172 self.putb([70, ['Transmitter bit: %d' % bit]])
174 self.putb([71, ['Transmitter bit: %d (Warning: Must be 1!)' % bit]])
176 # Bits[45:40]: Command index (BCD; valid: 0-63)
177 cmd = self.cmd_index = t[0] & 0x3f
178 self.bit_ss, self.bit_es = tb(5, 5)[1], tb(5, 0)[2]
179 self.putb([70, ['Command: %s%d (%s)' % (s, cmd, cmd_name[cmd])]])
181 # Bits[39:8]: Argument
182 self.arg = (t[1] << 24) | (t[2] << 16) | (t[3] << 8) | t[4]
183 self.bit_ss, self.bit_es = tb(4, 7)[1], tb(1, 0)[2]
184 self.putb([70, ['Argument: 0x%04x' % self.arg]])
189 self.bit_ss, self.bit_es = tb(0, 7)[1], tb(0, 1)[2]
190 self.putb([70, ['CRC7: 0x%01x' % crc]])
192 # Bits[0:0]: End bit (always 1)
193 bit, self.bit_ss, self.bit_es = tb(0, 0)[0], tb(0, 0)[1], tb(0, 0)[2]
194 self.putb([70, ['End bit: %d' % bit]])
196 self.putb([70, ['End bit: %d' % bit]])
198 self.putb([71, ['End bit: %d (Warning: Must be 1!)' % bit]])
201 if cmd in (0, 1, 9, 16, 17, 41, 49, 55, 59):
202 self.state = 'HANDLE CMD%d' % cmd
203 self.cmd_str = '%s%d (%s)' % (s, cmd, cmd_name[cmd])
205 self.state = 'HANDLE CMD999'
206 a = '%s%d: %02x %02x %02x %02x %02x %02x' % ((s, cmd) + tuple(t))
207 self.putx([cmd, [a]])
210 if self.is_acmd and cmd != 55:
213 def handle_cmd0(self):
214 # CMD0: GO_IDLE_STATE
215 self.putc(0, 'Reset the SD card')
216 self.state = 'GET RESPONSE R1'
218 def handle_cmd1(self):
220 self.putc(1, 'Send HCS info and activate the card init process')
221 hcs = (self.arg & (1 << 30)) >> 30
222 self.bit_ss = self.cmd_token_bits[5 - 4][6][1]
223 self.bit_es = self.cmd_token_bits[5 - 4][6][2]
224 self.putb([70, ['HCS: %d' % hcs]])
225 self.state = 'GET RESPONSE R1'
227 def handle_cmd9(self):
228 # CMD9: SEND_CSD (128 bits / 16 bytes)
229 self.putc(9, 'Ask card to send its card specific data (CSD)')
230 if len(self.read_buf) == 0:
231 self.cmd_ss = self.ss
232 self.read_buf.append(self.miso)
234 ### if len(self.read_buf) < 16:
235 if len(self.read_buf) < 16 + 4:
237 self.cmd_es = self.es
238 self.read_buf = self.read_buf[4:] ### TODO: Document or redo.
239 self.putx([9, ['CSD: %s' % self.read_buf]])
240 # TODO: Decode all bits.
242 ### self.state = 'GET RESPONSE R1'
245 def handle_cmd10(self):
246 # CMD10: SEND_CID (128 bits / 16 bytes)
247 self.putc(10, 'Ask card to send its card identification (CID)')
248 self.read_buf.append(self.miso)
249 if len(self.read_buf) < 16:
251 self.putx([10, ['CID: %s' % self.read_buf]])
252 # TODO: Decode all bits.
254 self.state = 'GET RESPONSE R1'
256 def handle_cmd16(self):
257 # CMD16: SET_BLOCKLEN
258 self.blocklen = self.arg
259 # TODO: Sanity check on block length.
260 self.putc(16, 'Set the block length to %d bytes' % self.blocklen)
261 self.state = 'GET RESPONSE R1'
263 def handle_cmd17(self):
264 # CMD17: READ_SINGLE_BLOCK
265 self.putc(17, 'Read a block from address 0x%04x' % self.arg)
266 if len(self.read_buf) == 0:
267 self.cmd_ss = self.ss
268 self.read_buf.append(self.miso)
269 if len(self.read_buf) < self.blocklen + 2: # FIXME
271 self.cmd_es = self.es
272 self.read_buf = self.read_buf[2:] # FIXME
273 self.putx([17, ['Block data: %s' % self.read_buf]])
275 self.state = 'GET RESPONSE R1'
277 def handle_cmd41(self):
278 # ACMD41: SD_SEND_OP_COND
279 self.putc(41, 'Send HCS info and activate the card init process')
280 self.state = 'GET RESPONSE R1'
282 def handle_cmd49(self):
283 self.state = 'GET RESPONSE R1'
285 def handle_cmd55(self):
287 self.putc(55, 'Next command is an application-specific command')
289 self.state = 'GET RESPONSE R1'
291 def handle_cmd59(self):
293 crc_on_off = self.arg & (1 << 0)
294 s = 'on' if crc_on_off == 1 else 'off'
295 self.putc(59, 'Turn the SD card CRC option %s' % s)
296 self.state = 'GET RESPONSE R1'
298 def handle_cmd999(self):
299 self.state = 'GET RESPONSE R1'
301 def handle_cid_register(self):
302 # Card Identification (CID) register, 128bits
306 # Manufacturer ID: CID[127:120] (8 bits)
309 # OEM/Application ID: CID[119:104] (16 bits)
310 oid = (cid[14] << 8) | cid[13]
312 # Product name: CID[103:64] (40 bits)
314 for i in range(12, 8 - 1, -1):
318 # Product revision: CID[63:56] (8 bits)
321 # Product serial number: CID[55:24] (32 bits)
323 for i in range(6, 3 - 1, -1):
327 # RESERVED: CID[23:20] (4 bits)
329 # Manufacturing date: CID[19:8] (12 bits)
332 # CRC7 checksum: CID[7:1] (7 bits)
335 # Not used, always 1: CID[0:0] (1 bit)
338 def handle_response_r1(self, res):
339 # The R1 response token format (1 byte).
340 # Sent by the card after every command except for SEND_STATUS.
342 self.cmd_ss, self.cmd_es = self.miso_bits[7][1], self.miso_bits[0][2]
343 self.putx([65, ['R1: 0x%02x' % res]])
345 def putbit(bit, data):
346 b = self.miso_bits[bit]
347 self.bit_ss, self.bit_es = b[1], b[2]
348 self.putb([70, data])
350 # Bit 0: 'In idle state' bit
351 s = '' if (res & (1 << 0)) else 'not '
352 putbit(0, ['Card is %sin idle state' % s])
354 # Bit 1: 'Erase reset' bit
355 s = '' if (res & (1 << 1)) else 'not '
356 putbit(1, ['Erase sequence %scleared' % s])
358 # Bit 2: 'Illegal command' bit
359 s = 'I' if (res & (1 << 2)) else 'No i'
360 putbit(2, ['%sllegal command detected' % s])
362 # Bit 3: 'Communication CRC error' bit
363 s = 'failed' if (res & (1 << 3)) else 'was successful'
364 putbit(3, ['CRC check of last command %s' % s])
366 # Bit 4: 'Erase sequence error' bit
367 s = 'E' if (res & (1 << 4)) else 'No e'
368 putbit(4, ['%srror in the sequence of erase commands' % s])
370 # Bit 5: 'Address error' bit
371 s = 'M' if (res & (1 << 4)) else 'No m'
372 putbit(5, ['%sisaligned address used in command' % s])
374 # Bit 6: 'Parameter error' bit
375 s = '' if (res & (1 << 4)) else 'not '
376 putbit(6, ['Command argument %soutside allowed range' % s])
378 # Bit 7: Always set to 0
379 putbit(7, ['Bit 7 (always 0)'])
383 def handle_response_r1b(self, res):
387 def handle_response_r2(self, res):
391 def handle_response_r3(self, res):
395 # Note: Response token formats R4 and R5 are reserved for SDIO.
399 def handle_response_r7(self, res):
403 def decode(self, ss, es, data):
404 ptype, mosi, miso = data
406 # For now, only use DATA and BITS packets.
407 if ptype not in ('DATA', 'BITS'):
410 # Store the individual bit values and ss/es numbers. The next packet
411 # is guaranteed to be a 'DATA' packet belonging to this 'BITS' one.
413 self.miso_bits, self.mosi_bits = miso, mosi
416 self.ss, self.es = ss, es
419 if self.state == 'IDLE':
420 # Ignore stray 0xff bytes, some devices seem to send those!?
421 if mosi == 0xff: # TODO?
423 self.state = 'GET COMMAND TOKEN'
424 self.handle_command_token(mosi, miso)
425 elif self.state == 'GET COMMAND TOKEN':
426 self.handle_command_token(mosi, miso)
427 elif self.state.startswith('HANDLE CMD'):
428 self.miso, self.mosi = miso, mosi
429 # Call the respective handler method for the command.
430 s = 'handle_cmd%s' % self.state[10:].lower()
431 handle_cmd = getattr(self, s)
434 self.cmd_token_bits = []
435 elif self.state.startswith('GET RESPONSE'):
436 # Ignore stray 0xff bytes, some devices seem to send those!?
437 if miso == 0xff: # TODO?
440 # Call the respective handler method for the response.
441 s = 'handle_response_%s' % self.state[13:].lower()
442 handle_response = getattr(self, s)
443 handle_response(miso)
447 raise Exception('Invalid state: %s' % self.state)