2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 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 # SD card (SPI mode) low-level protocol decoder
23 import sigrokdecode as srd
26 # Normal commands (CMD)
33 12: 'STOP_TRANSMISSION',
36 17: 'READ_SINGLE_BLOCK',
37 18: 'READ_MULTIPLE_BLOCK',
39 25: 'WRITE_MULTIPLE_BLOCK',
43 30: 'SEND_WRITE_PROT',
44 32: 'ERASE_WR_BLK_START_ADDR',
45 33: 'ERASE_WR_BLK_END_ADDR',
52 # CMD60-63: Reserved for manufacturer
54 # Application-specific commands (ACMD)
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',
74 class Decoder(srd.Decoder):
77 name = 'SD card (SPI mode)'
78 longname = 'Secure Digital card (SPI mode)'
79 desc = 'Secure Digital card (SPI mode) low-level protocol.'
82 outputs = ['sdcard_spi']
87 ['Text', 'Human-readable text'],
88 ['Warnings', 'Human-readable warnings'],
91 def __init__(self, **kwargs):
94 self.cmd_ss, self.cmd_es = 0, 0
96 self.is_acmd = False # Indicates CMD vs. ACMD
101 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'sdcard_spi')
102 self.out_ann = self.add(srd.OUTPUT_ANN, 'sdcard_spi')
107 def putx(self, data):
108 self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
110 def handle_command_token(self, mosi, miso):
111 # Command tokens (6 bytes) are sent (MSB-first) by the host.
114 # - CMD[47:47]: Start bit (always 0)
115 # - CMD[46:46]: Transmitter bit (1 == host)
116 # - CMD[45:40]: Command index (BCD; valid: 0-63)
117 # - CMD[39:08]: Argument
119 # - CMD[00:00]: End bit (always 1)
121 self.cmd_token.append(mosi)
122 # TODO: Record MISO too?
124 # All command tokens are 6 bytes long.
125 if len(self.cmd_token) < 6:
128 # Received all 6 bytes of the command token. Now decode it.
133 s = 'ACMD' if self.is_acmd else 'CMD'
135 self.put(0, 0, self.out_ann,
136 [0, [s + ': %02x %02x %02x %02x %02x %02x' % tuple(t)]])
139 self.startbit = (t[0] & (1 << 7)) >> 7
140 self.put(0, 0, self.out_ann,
141 [0, ['Start bit: %d' % self.startbit]])
142 if self.startbit != 0:
144 self.put(0, 0, self.out_ann, [1, ['Warning: Start bit != 0']])
147 self.transmitterbit = (t[0] & (1 << 6)) >> 6
148 self.put(0, 0, self.out_ann,
149 [0, ['Transmitter bit: %d' % self.transmitterbit]])
150 if self.transmitterbit != 0:
152 self.put(0, 0, self.out_ann, [1, ['Warning: Transmitter bit != 1']])
155 cmd = self.cmd_index = t[0] & 0x3f
157 self.put(0, 0, self.out_ann,
158 [0, ['Command: %s%d (%s)' % (s, cmd, cmd_name[cmd])]])
161 self.arg = (t[1] << 24) | (t[2] << 16) | (t[3] << 8) | t[4]
162 self.put(0, 0, self.out_ann, [0, ['Argument: 0x%04x' % self.arg]])
163 # TODO: Sanity check on argument? Must be per-cmd?
168 self.put(0, 0, self.out_ann, [0, ['CRC: 0x%01x' % self.crc]])
171 self.endbit = t[5] & (1 << 0)
172 self.put(0, 0, self.out_ann, [0, ['End bit: %d' % self.endbit]])
175 self.put(0, 0, self.out_ann, [1, ['Warning: End bit != 1']])
178 if cmd in (0, 1, 9, 16, 17, 41, 49, 55, 59):
179 self.state = 'HANDLE CMD%d' % cmd
182 if self.is_acmd and cmd != 55:
187 def handle_cmd0(self, ):
188 # CMD0: GO_IDLE_STATE
190 self.put(0, 0, self.out_ann, [0, ['CMD0: Card reset / idle state']])
191 self.state = 'GET RESPONSE R1'
193 def handle_cmd1(self):
196 hcs = (self.arg & (1 << 30)) >> 30
197 self.put(0, 0, self.out_ann, [0, ['HCS bit = %d' % hcs]])
198 self.state = 'GET RESPONSE R1'
200 def handle_cmd9(self):
201 # CMD9: SEND_CSD (128 bits / 16 bytes)
202 self.read_buf.append(self.miso)
204 ### if len(self.read_buf) < 16:
205 if len(self.read_buf) < 16 + 4:
207 self.read_buf = self.read_buf[4:] ### TODO: Document or redo.
208 self.put(0, 0, self.out_ann, [0, ['CSD: %s' % self.read_buf]])
209 # TODO: Decode all bits.
211 ### self.state = 'GET RESPONSE R1'
214 def handle_cmd10(self):
215 # CMD10: SEND_CID (128 bits / 16 bytes)
216 self.read_buf.append(self.miso)
217 if len(self.read_buf) < 16:
219 self.put(0, 0, self.out_ann, [0, ['CID: %s' % self.read_buf]])
220 # TODO: Decode all bits.
222 self.state = 'GET RESPONSE R1'
224 def handle_cmd16(self):
225 # CMD16: SET_BLOCKLEN
226 self.blocklen = self.arg # TODO
227 # TODO: Sanity check on block length.
228 self.put(0, 0, self.out_ann, [0, ['Block length: %d' % self.blocklen]])
229 self.state = 'GET RESPONSE R1'
231 def handle_cmd17(self):
232 # CMD17: READ_SINGLE_BLOCK
233 self.read_buf.append(self.miso)
234 if len(self.read_buf) == 1:
235 self.put(0, 0, self.out_ann,
236 [0, ['Read block at address: 0x%04x' % self.arg]])
237 if len(self.read_buf) < self.blocklen + 2: # FIXME
239 self.read_buf = self.read_buf[2:] # FIXME
240 self.put(0, 0, self.out_ann, [0, ['Block data: %s' % self.read_buf]])
242 self.state = 'GET RESPONSE R1'
244 def handle_cmd41(self):
245 # ACMD41: SD_SEND_OP_COND
246 self.state = 'GET RESPONSE R1'
248 def handle_cmd49(self):
249 self.state = 'GET RESPONSE R1'
251 def handle_cmd55(self):
254 self.state = 'GET RESPONSE R1'
256 def handle_cmd59(self):
258 crc_on_off = self.arg & (1 << 0)
259 s = 'on' if crc_on_off == 1 else 'off'
260 self.put(0, 0, self.out_ann, [0, ['SD card CRC option: %s' % s]])
261 self.state = 'GET RESPONSE R1'
263 def handle_cid_register(self):
264 # Card Identification (CID) register, 128bits
268 # Manufacturer ID: CID[127:120] (8 bits)
271 # OEM/Application ID: CID[119:104] (16 bits)
272 oid = (cid[14] << 8) | cid[13]
274 # Product name: CID[103:64] (40 bits)
276 for i in range(12, 8 - 1, -1):
280 # Product revision: CID[63:56] (8 bits)
283 # Product serial number: CID[55:24] (32 bits)
285 for i in range(6, 3 - 1, -1):
289 # RESERVED: CID[23:20] (4 bits)
291 # Manufacturing date: CID[19:8] (12 bits)
294 # CRC7 checksum: CID[7:1] (7 bits)
297 # Not used, always 1: CID[0:0] (1 bit)
300 def handle_response_r1(self, res):
301 # The R1 response token format (1 byte).
302 # Sent by the card after every command except for SEND_STATUS.
304 self.put(0, 0, self.out_ann, [0, ['R1: 0x%02x' % res]])
306 # TODO: Configurable whether all bits are decoded.
308 # 'In idle state' bit
309 s = '' if (res & (1 << 0)) else 'not '
310 self.put(0, 0, self.out_ann, [0, ['Card is %sin idle state' % s]])
313 s = '' if (res & (1 << 1)) else 'not '
314 self.put(0, 0, self.out_ann, [0, ['Erase sequence %scleared' % s]])
316 # 'Illegal command' bit
317 s = 'I' if (res & (1 << 2)) else 'No i'
318 self.put(0, 0, self.out_ann, [0, ['%sllegal command detected' % s]])
320 # 'Communication CRC error' bit
321 s = 'failed' if (res & (1 << 3)) else 'was successful'
322 self.put(0, 0, self.out_ann,
323 [0, ['CRC check of last command %s' % s]])
325 # 'Erase sequence error' bit
326 s = 'E' if (res & (1 << 4)) else 'No e'
327 self.put(0, 0, self.out_ann,
328 [0, ['%srror in the sequence of erase commands' % s]])
330 # 'Address error' bit
331 s = 'M' if (res & (1 << 4)) else 'No m'
332 self.put(0, 0, self.out_ann,
333 [0, ['%sisaligned address used in command' % s]])
335 # 'Parameter error' bit
336 s = '' if (res & (1 << 4)) else 'not '
337 self.put(0, 0, self.out_ann,
338 [0, ['Command argument %soutside allowed range' % s]])
342 def handle_response_r1b(self, res):
346 def handle_response_r2(self, res):
350 def handle_response_r3(self, res):
354 # Note: Response token formats R4 and R5 are reserved for SDIO.
358 def handle_response_r7(self, res):
362 def decode(self, ss, es, data):
363 ptype, mosi, miso = data
365 # For now, ignore non-data packets.
369 self.put(0, 0, self.out_ann, [0, ['0x%02x 0x%02x' % (mosi, miso)]])
372 if self.state == 'IDLE':
373 # Ignore stray 0xff bytes, some devices seem to send those!?
374 if mosi == 0xff: # TODO?
376 self.state = 'GET COMMAND TOKEN'
377 self.handle_command_token(mosi, miso)
378 elif self.state == 'GET COMMAND TOKEN':
379 self.handle_command_token(mosi, miso)
380 elif self.state.startswith('HANDLE CMD'):
381 self.miso, self.mosi = miso, mosi
382 # Call the respective handler method for the command.
383 s = 'handle_cmd%s' % self.state[10:].lower()
384 handle_cmd = getattr(self, s)
386 elif self.state.startswith('GET RESPONSE'):
387 # Ignore stray 0xff bytes, some devices seem to send those!?
388 if miso == 0xff: # TODO?
391 # Call the respective handler method for the response.
392 s = 'handle_response_%s' % self.state[13:].lower()
393 # self.put(0, 0, self.out_ann, [0, [s]]) # TODO
394 handle_response = getattr(self, s)
395 handle_response(miso)
399 raise Exception('Invalid state: %s' % self.state)