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