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