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