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