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