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