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