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