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