]> sigrok.org Git - libsigrokdecode.git/blame - decoders/sdcard_sd/pd.py
sdcard_sd: Put card status fields in their own annotation classes.
[libsigrokdecode.git] / decoders / sdcard_sd / pd.py
CommitLineData
08c41387 1##
ada4c3a3 2## This file is part of the libsigrokdecode project.
08c41387 3##
624710c9 4## Copyright (C) 2015-2020 Uwe Hermann <uwe@hermann-uwe.de>
08c41387
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
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
08c41387
UH
18##
19
20import sigrokdecode as srd
9f08e757 21from common.srdhelper import SrdIntEnum, SrdStrEnum
2729ea3e 22from common.sdcard import (cmd_names, acmd_names, accepted_voltages, sd_status)
08c41387 23
6266715a 24responses = '1 1b 2 3 6 7'.split()
2729ea3e
UH
25reg_card_status = 'OUT_OF_RANGE ADDRESS_ERROR BLOCK_LEN_ERROR ERASE_SEQ_ERROR \
26 ERASE_PARAM WP_VIOLATION CARD_IS_LOCKED LOCK_UNLOCK_FAILED COM_CRC_ERROR \
27 ILLEGAL_COMMAND CARD_ECC_FAILED CC_ERROR ERROR RSVD_DEFERRED_RESPONSE \
28 CSD_OVERWRITE WP_ERASE_SKIP CARD_ECC_DISABLED ERASE_RESET CURRENT_STATE \
29 READY_FOR_DATA RSVD FX_EVENT APP_CMD RSVD_SDIO AKE_SEQ_ERROR RSVD_APP_CMD \
30 RSVD_TESTMODE'.split()
b13f3595 31reg_cid = 'MID OID PNM PRV PSN RSVD MDT CRC ONE'.split()
73957cba
UH
32reg_csd = 'CSD_STRUCTURE RSVD TAAC NSAC TRAN_SPEED CCC READ_BL_LEN \
33 READ_BL_PARTIAL WRITE_BLK_MISALIGN READ_BLK_MISALIGN DSR_IMP C_SIZE \
34 VDD_R_CURR_MIN VDD_R_CURR_MAX VDD_W_CURR_MIN VDD_W_CURR_MAX C_SIZE_MULT \
35 ERASE_BLK_EN SECTOR_SIZE WP_GRP_SIZE WP_GRP_ENABLE R2W_FACTOR \
36 WRITE_BL_LEN WRITE_BL_PARTIAL FILE_FORMAT_GRP COPY PERM_WRITE_PROTECT \
37 TMP_WRITE_PROTECT FILE_FORMAT CRC ONE'.split()
6266715a 38
a32575cd
UH
39Pin = SrdIntEnum.from_str('Pin', 'CMD CLK DAT0 DAT1 DAT2 DAT3')
40
9f08e757 41a = ['CMD%d' % i for i in range(64)] + ['ACMD%d' % i for i in range(64)] + \
604b5f9d 42 ['RESPONSE_R' + r.upper() for r in responses] + \
2729ea3e 43 ['R_STATUS_' + r for r in reg_card_status] + \
b13f3595 44 ['R_CID_' + r for r in reg_cid] + \
73957cba 45 ['R_CSD_' + r for r in reg_csd] + \
9f08e757
UH
46 ['F_' + f for f in 'START TRANSM CMD ARG CRC END'.split()] + \
47 ['BIT', 'DECODED_BIT', 'DECODED_F']
48Ann = SrdIntEnum.from_list('Ann', a)
49
624710c9
UH
50s = ['GET_COMMAND_TOKEN', 'HANDLE_CMD999'] + \
51 ['HANDLE_CMD%d' % i for i in range(64)] + \
52 ['HANDLE_ACMD%d' % i for i in range(64)] + \
6266715a 53 ['GET_RESPONSE_R%s' % r.upper() for r in responses]
624710c9
UH
54St = SrdStrEnum.from_list('St', s)
55
07da8a9b
UH
56class Bit:
57 def __init__(self, s, e, b):
58 self.ss, self.es, self.bit = s, e ,b
59
08c41387 60class Decoder(srd.Decoder):
11535843 61 api_version = 3
08c41387
UH
62 id = 'sdcard_sd'
63 name = 'SD card (SD mode)'
64 longname = 'Secure Digital card (SD mode)'
65 desc = 'Secure Digital card (SD mode) low-level protocol.'
66 license = 'gplv2+'
67 inputs = ['logic']
6cbba91f 68 outputs = []
d6d8a8a4 69 tags = ['Memory']
08c41387
UH
70 channels = (
71 {'id': 'cmd', 'name': 'CMD', 'desc': 'Command'},
72 {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'},
73 )
74 optional_channels = (
75 {'id': 'dat0', 'name': 'DAT0', 'desc': 'Data pin 0'},
76 {'id': 'dat1', 'name': 'DAT1', 'desc': 'Data pin 1'},
77 {'id': 'dat2', 'name': 'DAT2', 'desc': 'Data pin 2'},
78 {'id': 'dat3', 'name': 'DAT3', 'desc': 'Data pin 3'},
79 )
80 annotations = \
81 tuple(('cmd%d' % i, 'CMD%d' % i) for i in range(64)) + \
6266715a 82 tuple(('acmd%d' % i, 'ACMD%d' % i) for i in range(64)) + \
b13f3595 83 tuple(('response_r%s' % r, 'R%s' % r) for r in responses) + \
2729ea3e 84 tuple(('reg_status_' + r.lower(), 'Status: ' + r) for r in reg_card_status) + \
b13f3595 85 tuple(('reg_cid_' + r.lower(), 'CID: ' + r) for r in reg_cid) + \
73957cba 86 tuple(('reg_csd_' + r.lower(), 'CSD: ' + r) for r in reg_csd) + \
b13f3595 87 ( \
08c41387
UH
88 ('field-start', 'Start bit'),
89 ('field-transmission', 'Transmission bit'),
90 ('field-cmd', 'Command'),
91 ('field-arg', 'Argument'),
92 ('field-crc', 'CRC'),
93 ('field-end', 'End bit'),
9f08e757 94 ('bit', 'Bit'),
e144452b
UH
95 ('decoded-bit', 'Decoded bit'),
96 ('decoded-field', 'Decoded field'),
08c41387
UH
97 )
98 annotation_rows = (
9f08e757 99 ('raw-bits', 'Raw bits', (Ann.BIT,)),
b13f3595 100 ('decoded-bits', 'Decoded bits', (Ann.DECODED_BIT,) + Ann.prefixes('R_')),
9f08e757
UH
101 ('decoded-fields', 'Decoded fields', (Ann.DECODED_F,)),
102 ('fields', 'Fields', Ann.prefixes('F_')),
604b5f9d 103 ('commands', 'Commands', Ann.prefixes('CMD ACMD RESPONSE_')),
08c41387
UH
104 )
105
92b7b49f 106 def __init__(self):
10aeb8ea
GS
107 self.reset()
108
109 def reset(self):
624710c9 110 self.state = St.GET_COMMAND_TOKEN
08c41387 111 self.token = []
08c41387
UH
112 self.is_acmd = False # Indicates CMD vs. ACMD
113 self.cmd = None
f8eb6c3f 114 self.last_cmd = None
08c41387
UH
115 self.arg = None
116
117 def start(self):
118 self.out_ann = self.register(srd.OUTPUT_ANN)
119
08c41387 120 def putt(self, data):
07da8a9b 121 self.put(self.token[0].ss, self.token[47].es, self.out_ann, data)
08c41387 122
08c41387 123 def putf(self, s, e, data):
07da8a9b 124 self.put(self.token[s].ss, self.token[e].es, self.out_ann, data)
08c41387
UH
125
126 def puta(self, s, e, data):
07da8a9b 127 self.put(self.token[47 - 8 - e].ss, self.token[47 - 8 - s].es,
08c41387
UH
128 self.out_ann, data)
129
0671a9de 130 def putc(self, desc):
608b9c03 131 cmd = Ann.ACMD0 + self.cmd if self.is_acmd else self.cmd
f8eb6c3f 132 self.last_cmd = cmd
08c41387
UH
133 self.putt([cmd, ['%s: %s' % (self.cmd_str, desc), self.cmd_str,
134 self.cmd_str.split(' ')[0]]])
135
6266715a 136 def putr(self, r):
604b5f9d 137 self.putt([r, ['Response: %s' % r.name.split('_')[1]]])
08c41387 138
08c41387
UH
139 def cmd_name(self, cmd):
140 c = acmd_names if self.is_acmd else cmd_names
141 return c.get(cmd, 'Unknown')
142
4dfde5a5 143 def get_token_bits(self, cmd_pin, n):
08c41387 144 # Get a bit, return True if we already got 'n' bits, False otherwise.
07da8a9b 145 self.token.append(Bit(self.samplenum, self.samplenum, cmd_pin))
08c41387 146 if len(self.token) > 0:
07da8a9b 147 self.token[len(self.token) - 2].es = self.samplenum
08c41387
UH
148 if len(self.token) < n:
149 return False
07da8a9b 150 self.token[n - 1].es += self.token[n - 1].ss - self.token[n - 2].ss
08c41387
UH
151 return True
152
153 def handle_common_token_fields(self):
154 s = self.token
155
156 # Annotations for each individual bit.
157 for bit in range(len(self.token)):
07da8a9b 158 self.putf(bit, bit, [Ann.BIT, ['%d' % s[bit].bit]])
08c41387
UH
159
160 # CMD[47:47]: Start bit (always 0)
9f08e757 161 self.putf(0, 0, [Ann.F_START, ['Start bit', 'Start', 'S']])
08c41387
UH
162
163 # CMD[46:46]: Transmission bit (1 == host)
07da8a9b 164 t = 'host' if s[1].bit == 1 else 'card'
9f08e757 165 self.putf(1, 1, [Ann.F_TRANSM, ['Transmission: ' + t, 'T: ' + t, 'T']])
08c41387
UH
166
167 # CMD[45:40]: Command index (BCD; valid: 0-63)
07da8a9b 168 self.cmd = int('0b' + ''.join([str(s[i].bit) for i in range(2, 8)]), 2)
08c41387 169 c = '%s (%d)' % (self.cmd_name(self.cmd), self.cmd)
9f08e757 170 self.putf(2, 7, [Ann.F_CMD, ['Command: ' + c, 'Cmd: ' + c,
08c41387
UH
171 'CMD%d' % self.cmd, 'Cmd', 'C']])
172
173 # CMD[39:08]: Argument
07da8a9b 174 self.arg = int('0b' + ''.join([str(s[i].bit) for i in range(8, 40)]), 2)
9f08e757 175 self.putf(8, 39, [Ann.F_ARG, ['Argument: 0x%08x' % self.arg, 'Arg', 'A']])
08c41387
UH
176
177 # CMD[07:01]: CRC7
07da8a9b 178 self.crc = int('0b' + ''.join([str(s[i].bit) for i in range(40, 47)]), 2)
9f08e757 179 self.putf(40, 46, [Ann.F_CRC, ['CRC: 0x%x' % self.crc, 'CRC', 'C']])
08c41387
UH
180
181 # CMD[00:00]: End bit (always 1)
9f08e757 182 self.putf(47, 47, [Ann.F_END, ['End bit', 'End', 'E']])
08c41387 183
4dfde5a5 184 def get_command_token(self, cmd_pin):
08c41387
UH
185 # Command tokens (48 bits) are sent serially (MSB-first) by the host
186 # (over the CMD line), either to one SD card or to multiple ones.
187 #
188 # Format:
189 # - Bits[47:47]: Start bit (always 0)
190 # - Bits[46:46]: Transmission bit (1 == host)
191 # - Bits[45:40]: Command index (BCD; valid: 0-63)
192 # - Bits[39:08]: Argument
193 # - Bits[07:01]: CRC7
194 # - Bits[00:00]: End bit (always 1)
195
4dfde5a5 196 if not self.get_token_bits(cmd_pin, 48):
08c41387
UH
197 return
198
199 self.handle_common_token_fields()
200
201 # Handle command.
202 s = 'ACMD' if self.is_acmd else 'CMD'
203 self.cmd_str = '%s%d (%s)' % (s, self.cmd, self.cmd_name(self.cmd))
1e9dfe60 204 if hasattr(self, 'handle_%s%d' % (s.lower(), self.cmd)):
624710c9 205 self.state = St['HANDLE_CMD%d' % self.cmd]
08c41387 206 else:
624710c9 207 self.state = St.HANDLE_CMD999
0671a9de 208 self.putc('%s%d' % (s, self.cmd))
08c41387
UH
209
210 def handle_cmd0(self):
211 # CMD0 (GO_IDLE_STATE) -> no response
9f08e757 212 self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 213 self.putc('Reset all SD cards')
624710c9 214 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387
UH
215
216 def handle_cmd2(self):
217 # CMD2 (ALL_SEND_CID) -> R2
9f08e757 218 self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 219 self.putc('Ask card for CID number')
624710c9 220 self.token, self.state = [], St.GET_RESPONSE_R2
08c41387
UH
221
222 def handle_cmd3(self):
223 # CMD3 (SEND_RELATIVE_ADDR) -> R6
9f08e757 224 self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 225 self.putc('Ask card for new relative card address (RCA)')
624710c9 226 self.token, self.state = [], St.GET_RESPONSE_R6
08c41387
UH
227
228 def handle_cmd6(self):
229 # CMD6 (SWITCH_FUNC) -> R1
0671a9de 230 self.putc('Switch/check card function')
624710c9 231 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
232
233 def handle_cmd7(self):
234 # CMD7 (SELECT/DESELECT_CARD) -> R1b
0671a9de 235 self.putc('Select / deselect card')
624710c9 236 self.token, self.state = [], St.GET_RESPONSE_R6
08c41387
UH
237
238 def handle_cmd8(self):
239 # CMD8 (SEND_IF_COND) -> R7
9f08e757
UH
240 self.puta(12, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']])
241 self.puta(8, 11, [Ann.DECODED_F, ['Supply voltage', 'Voltage', 'VHS', 'V']])
242 self.puta(0, 7, [Ann.DECODED_F, ['Check pattern', 'Check pat', 'Check', 'C']])
0671a9de 243 self.putc('Send interface condition to card')
624710c9 244 self.token, self.state = [], St.GET_RESPONSE_R7
08c41387
UH
245 # TODO: Handle case when card doesn't reply with R7 (no reply at all).
246
247 def handle_cmd9(self):
248 # CMD9 (SEND_CSD) -> R2
9f08e757
UH
249 self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']])
250 self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 251 self.putc('Send card-specific data (CSD)')
624710c9 252 self.token, self.state = [], St.GET_RESPONSE_R2
08c41387
UH
253
254 def handle_cmd10(self):
255 # CMD10 (SEND_CID) -> R2
9f08e757
UH
256 self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']])
257 self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 258 self.putc('Send card identification data (CID)')
624710c9 259 self.token, self.state = [], St.GET_RESPONSE_R2
08c41387
UH
260
261 def handle_cmd13(self):
262 # CMD13 (SEND_STATUS) -> R1
9f08e757
UH
263 self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']])
264 self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 265 self.putc('Send card status register')
624710c9 266 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
267
268 def handle_cmd16(self):
269 # CMD16 (SET_BLOCKLEN) -> R1
9f08e757 270 self.puta(0, 31, [Ann.DECODED_F, ['Block length', 'Blocklen', 'BL', 'B']])
0671a9de 271 self.putc('Set the block length to %d bytes' % self.arg)
624710c9 272 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
273
274 def handle_cmd55(self):
275 # CMD55 (APP_CMD) -> R1
9f08e757
UH
276 self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']])
277 self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 278 self.putc('Next command is an application-specific command')
08c41387 279 self.is_acmd = True
624710c9 280 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
281
282 def handle_acmd6(self):
283 # ACMD6 (SET_BUS_WIDTH) -> R1
0671a9de 284 self.putc('Read SD config register (SCR)')
624710c9 285 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
286
287 def handle_acmd13(self):
288 # ACMD13 (SD_STATUS) -> R1
9f08e757 289 self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']])
0671a9de 290 self.putc('Set SD status')
624710c9 291 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
292
293 def handle_acmd41(self):
294 # ACMD41 (SD_SEND_OP_COND) -> R3
9f08e757
UH
295 self.puta(0, 23, [Ann.DECODED_F,
296 ['VDD voltage window', 'VDD volt', 'VDD', 'V']])
297 self.puta(24, 24, [Ann.DECODED_F, ['S18R']])
298 self.puta(25, 27, [Ann.DECODED_F, ['Reserved', 'Res', 'R']])
299 self.puta(28, 28, [Ann.DECODED_F, ['XPC']])
300 self.puta(29, 29, [Ann.DECODED_F,
301 ['Reserved for eSD', 'Reserved', 'Res', 'R']])
302 self.puta(30, 30, [Ann.DECODED_F,
303 ['Host capacity support info', 'Host capacity', 'HCS', 'H']])
304 self.puta(31, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']])
0671a9de 305 self.putc('Send HCS info and activate the card init process')
624710c9 306 self.token, self.state = [], St.GET_RESPONSE_R3
08c41387
UH
307
308 def handle_acmd51(self):
309 # ACMD51 (SEND_SCR) -> R1
0671a9de 310 self.putc('Read SD config register (SCR)')
624710c9 311 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
312
313 def handle_cmd999(self):
624710c9 314 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387
UH
315
316 def handle_acmd999(self):
624710c9 317 self.token, self.state = [], St.GET_RESPONSE_R1
08c41387 318
2729ea3e
UH
319 def handle_reg_status(self):
320 self.putf(8, 8, [Ann.R_STATUS_OUT_OF_RANGE, ['OUT_OF_RANGE']])
321 self.putf(9, 9, [Ann.R_STATUS_ADDRESS_ERROR, ['ADDRESS_ERROR']])
322 self.putf(10, 10, [Ann.R_STATUS_BLOCK_LEN_ERROR, ['BLOCK_LEN_ERROR']])
323 self.putf(11, 11, [Ann.R_STATUS_ERASE_SEQ_ERROR, ['ERASE_SEQ_ERROR']])
324 self.putf(12, 12, [Ann.R_STATUS_ERASE_PARAM, ['ERASE_PARAM']])
325 self.putf(13, 13, [Ann.R_STATUS_WP_VIOLATION, ['WP_VIOLATION']])
326 self.putf(14, 14, [Ann.R_STATUS_CARD_IS_LOCKED, ['CARD_IS_LOCKED']])
327 self.putf(15, 15, [Ann.R_STATUS_LOCK_UNLOCK_FAILED, ['LOCK_UNLOCK_FAILED']])
328 self.putf(16, 16, [Ann.R_STATUS_COM_CRC_ERROR, ['COM_CRC_ERROR']])
329 self.putf(17, 17, [Ann.R_STATUS_ILLEGAL_COMMAND, ['ILLEGAL_COMMAND']])
330 self.putf(18, 18, [Ann.R_STATUS_CARD_ECC_FAILED, ['CARD_ECC_FAILED']])
331 self.putf(19, 19, [Ann.R_STATUS_CC_ERROR, ['CC_ERROR']])
332 self.putf(20, 20, [Ann.R_STATUS_ERROR, ['ERROR']])
333 self.putf(21, 21, [Ann.R_STATUS_RSVD, ['Reserved', 'RSVD', 'R']])
334 self.putf(22, 22, [Ann.R_STATUS_RSVD_DEFERRED_RESPONSE, ['Reserved for DEFERRED_RESPONSE', 'RSVD_DEFERRED_RESPONSE']])
335 self.putf(23, 23, [Ann.R_STATUS_CSD_OVERWRITE, ['CSD_OVERWRITE']])
336 self.putf(24, 24, [Ann.R_STATUS_WP_ERASE_SKIP, ['WP_ERASE_SKIP']])
337 self.putf(25, 25, [Ann.R_STATUS_CARD_ECC_DISABLED, ['CARD_ECC_DISABLED']])
338 self.putf(26, 26, [Ann.R_STATUS_ERASE_RESET, ['ERASE_RESET']])
339 self.putf(27, 30, [Ann.R_STATUS_CURRENT_STATE, ['CURRENT_STATE']])
340 self.putf(31, 31, [Ann.R_STATUS_READY_FOR_DATA, ['READY_FOR_DATA']])
341 self.putf(32, 32, [Ann.R_STATUS_RSVD, ['RSVD']])
342 self.putf(33, 33, [Ann.R_STATUS_FX_EVENT, ['FX_EVENT']])
343 self.putf(34, 34, [Ann.R_STATUS_APP_CMD, ['APP_CMD']])
344 self.putf(35, 35, [Ann.R_STATUS_RSVD_SDIO, ['Reserved for SDIO card', 'RSVD_SDIO']])
345 self.putf(36, 36, [Ann.R_STATUS_AKE_SEQ_ERROR, ['AKE_SEQ_ERROR']])
346 self.putf(37, 37, [Ann.R_STATUS_RSVD_APP_CMD, ['Reserved for application specific commands', 'RSVD_APP_CMD']])
347 self.putf(38, 39, [Ann.R_STATUS_RSVD_TESTMODE, ['Reserved for manufacturer test mode', 'RSVD_TESTMODE']])
348
b13f3595
UH
349 def handle_reg_cid(self):
350 self.putf(8, 15, [Ann.R_CID_MID, ['Manufacturer ID', 'MID']])
351 self.putf(16, 31, [Ann.R_CID_OID, ['OEM/application ID', 'OID']])
352 self.putf(32, 71, [Ann.R_CID_PNM, ['Product name', 'PNM']])
353 self.putf(72, 79, [Ann.R_CID_PRV, ['Product revision', 'PRV']])
354 self.putf(80, 111, [Ann.R_CID_PSN, ['Product serial number', 'PSN']])
355 self.putf(112, 115, [Ann.R_CID_RSVD, ['Reserved', 'RSVD', 'R']])
356 self.putf(116, 127, [Ann.R_CID_MDT, ['Manufacturing date', 'MDT']])
357 self.putf(128, 134, [Ann.R_CID_CRC, ['CRC7 checksum', 'CRC']])
358 self.putf(135, 135, [Ann.R_CID_ONE, ['Always 1', '1']])
359
73957cba
UH
360 def handle_reg_csd(self):
361 self.putf(8, 9, [Ann.R_CSD_CSD_STRUCTURE, ['CSD structure', 'CSD_STRUCTURE']])
362 self.putf(10, 15, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
363 self.putf(16, 23, [Ann.R_CSD_TAAC, ['Data read access-time - 1', 'TAAC']])
364 self.putf(24, 31, [Ann.R_CSD_NSAC, ['Data read access-time - 2 in CLK cycles (NSAC * 100)', 'NSAC']])
365 self.putf(32, 39, [Ann.R_CSD_TRAN_SPEED, ['Max. data transfer rate', 'TRAN_SPEED']])
366 self.putf(40, 51, [Ann.R_CSD_CCC, ['Card command classes', 'CCC']])
367 self.putf(52, 55, [Ann.R_CSD_READ_BL_LEN, ['Max. read data block length', 'READ_BL_LEN']])
368 self.putf(56, 56, [Ann.R_CSD_READ_BL_PARTIAL, ['Partial blocks for read allowed', 'READ_BL_PARTIAL']])
369 self.putf(57, 57, [Ann.R_CSD_WRITE_BLK_MISALIGN, ['Write block misalignment', 'WRITE_BLK_MISALIGN']])
370 self.putf(58, 58, [Ann.R_CSD_READ_BLK_MISALIGN, ['Read block misalignment', 'READ_BLK_MISALIGN']])
371 self.putf(59, 59, [Ann.R_CSD_DSR_IMP, ['DSR implemented', 'DSR_IMP']])
372 self.putf(60, 61, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
373 self.putf(62, 73, [Ann.R_CSD_C_SIZE, ['Device size', 'C_SIZE']])
374 self.putf(74, 76, [Ann.R_CSD_VDD_R_CURR_MIN, ['Max. read current @VDD min', 'VDD_R_CURR_MIN']])
375 self.putf(77, 79, [Ann.R_CSD_VDD_R_CURR_MAX, ['Max. read current @VDD max', 'VDD_R_CURR_MAX']])
376 self.putf(80, 82, [Ann.R_CSD_VDD_W_CURR_MIN, ['Max. write current @VDD min', 'VDD_W_CURR_MIN']])
377 self.putf(83, 85, [Ann.R_CSD_VDD_W_CURR_MAX, ['Max. write current @VDD max', 'VDD_W_CURR_MAX']])
378 self.putf(86, 88, [Ann.R_CSD_C_SIZE_MULT, ['Device size multiplier', 'C_SIZE_MULT']])
379 self.putf(89, 89, [Ann.R_CSD_ERASE_BLK_EN, ['Erase single block enable', 'ERASE_BLK_EN']])
380 self.putf(90, 96, [Ann.R_CSD_SECTOR_SIZE, ['Erase sector size', 'SECTOR_SIZE']])
381 self.putf(97, 103, [Ann.R_CSD_WP_GRP_SIZE, ['Write protect group size', 'WP_GRP_SIZE']])
382 self.putf(104, 104, [Ann.R_CSD_WP_GRP_ENABLE, ['Write protect group enable', 'WP_GRP_ENABLE']])
383 self.putf(105, 106, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
384 self.putf(107, 109, [Ann.R_CSD_R2W_FACTOR, ['Write speed factor', 'R2W_FACTOR']])
385 self.putf(110, 113, [Ann.R_CSD_WRITE_BL_LEN, ['Max. write data block length', 'WRITE_BL_LEN']])
386 self.putf(114, 114, [Ann.R_CSD_WRITE_BL_PARTIAL, ['Partial blocks for write allowed', 'WRITE_BL_PARTIAL']])
387 self.putf(115, 119, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD']])
388 self.putf(120, 120, [Ann.R_CSD_FILE_FORMAT_GRP, ['File format group', 'FILE_FORMAT_GRP']])
389 self.putf(121, 121, [Ann.R_CSD_COPY, ['Copy flag', 'COPY']])
390 self.putf(122, 122, [Ann.R_CSD_PERM_WRITE_PROTECT, ['Permanent write protection', 'PERM_WRITE_PROTECT']])
391 self.putf(123, 123, [Ann.R_CSD_TMP_WRITE_PROTECT, ['Temporary write protection', 'TMP_WRITE_PROTECT']])
392 self.putf(124, 125, [Ann.R_CSD_FILE_FORMAT, ['File format', 'FILE_FORMAT']])
393 self.putf(126, 127, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
394 self.putf(128, 134, [Ann.R_CSD_CRC, ['CRC', 'CRC', 'C']])
395 self.putf(135, 135, [Ann.R_CSD_ONE, ['Always 1', '1']])
396
08c41387
UH
397 # Response tokens can have one of four formats (depends on content).
398 # They can have a total length of 48 or 136 bits.
399 # They're sent serially (MSB-first) by the card that the host
400 # addressed previously, or (synchronously) by all connected cards.
401
4dfde5a5 402 def handle_response_r1(self, cmd_pin):
08c41387
UH
403 # R1: Normal response command
404 # - Bits[47:47]: Start bit (always 0)
405 # - Bits[46:46]: Transmission bit (0 == card)
406 # - Bits[45:40]: Command index (BCD; valid: 0-63)
407 # - Bits[39:08]: Card status
408 # - Bits[07:01]: CRC7
409 # - Bits[00:00]: End bit (always 1)
4dfde5a5 410 if not self.get_token_bits(cmd_pin, 48):
08c41387
UH
411 return
412 self.handle_common_token_fields()
604b5f9d 413 self.putr(Ann.RESPONSE_R1)
9f08e757 414 self.puta(0, 31, [Ann.DECODED_F, ['Card status', 'Status', 'S']])
2729ea3e
UH
415 self.handle_reg_status()
416
624710c9 417 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387 418
4dfde5a5 419 def handle_response_r1b(self, cmd_pin):
08c41387 420 # R1b: Same as R1 with an optional busy signal (on the data line)
4dfde5a5 421 if not self.get_token_bits(cmd_pin, 48):
08c41387
UH
422 return
423 self.handle_common_token_fields()
9f08e757 424 self.puta(0, 31, [Ann.DECODED_F, ['Card status', 'Status', 'S']])
604b5f9d 425 self.putr(Ann.RESPONSE_R1B)
624710c9 426 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387 427
4dfde5a5 428 def handle_response_r2(self, cmd_pin):
08c41387
UH
429 # R2: CID/CSD register
430 # - Bits[135:135]: Start bit (always 0)
431 # - Bits[134:134]: Transmission bit (0 == card)
432 # - Bits[133:128]: Reserved (always 0b111111)
433 # - Bits[127:001]: CID or CSD register including internal CRC7
434 # - Bits[000:000]: End bit (always 1)
4dfde5a5 435 if not self.get_token_bits(cmd_pin, 136):
08c41387
UH
436 return
437 # Annotations for each individual bit.
438 for bit in range(len(self.token)):
07da8a9b 439 self.putf(bit, bit, [Ann.BIT, ['%d' % self.token[bit].bit]])
9f08e757 440 self.putf(0, 0, [Ann.F_START, ['Start bit', 'Start', 'S']])
07da8a9b 441 t = 'host' if self.token[1].bit == 1 else 'card'
9f08e757
UH
442 self.putf(1, 1, [Ann.F_TRANSM, ['Transmission: ' + t, 'T: ' + t, 'T']])
443 self.putf(2, 7, [Ann.F_CMD, ['Reserved', 'Res', 'R']])
444 self.putf(8, 134, [Ann.F_ARG, ['Argument', 'Arg', 'A']])
445 self.putf(135, 135, [Ann.F_END, ['End bit', 'End', 'E']])
446 self.putf(8, 134, [Ann.DECODED_F, ['CID/CSD register', 'CID/CSD', 'C']])
604b5f9d 447 self.putf(0, 135, [Ann.RESPONSE_R2, ['Response: R2']])
b13f3595
UH
448
449 if self.last_cmd in (Ann.CMD2, Ann.CMD10):
450 self.handle_reg_cid()
451
73957cba
UH
452 if self.last_cmd == Ann.CMD9:
453 self.handle_reg_csd()
454
624710c9 455 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387 456
4dfde5a5 457 def handle_response_r3(self, cmd_pin):
08c41387
UH
458 # R3: OCR register
459 # - Bits[47:47]: Start bit (always 0)
460 # - Bits[46:46]: Transmission bit (0 == card)
461 # - Bits[45:40]: Reserved (always 0b111111)
462 # - Bits[39:08]: OCR register
463 # - Bits[07:01]: Reserved (always 0b111111)
464 # - Bits[00:00]: End bit (always 1)
4dfde5a5 465 if not self.get_token_bits(cmd_pin, 48):
08c41387 466 return
604b5f9d 467 self.putr(Ann.RESPONSE_R3)
08c41387
UH
468 # Annotations for each individual bit.
469 for bit in range(len(self.token)):
07da8a9b 470 self.putf(bit, bit, [Ann.BIT, ['%d' % self.token[bit].bit]])
9f08e757 471 self.putf(0, 0, [Ann.F_START, ['Start bit', 'Start', 'S']])
07da8a9b 472 t = 'host' if self.token[1].bit == 1 else 'card'
9f08e757
UH
473 self.putf(1, 1, [Ann.F_TRANSM, ['Transmission: ' + t, 'T: ' + t, 'T']])
474 self.putf(2, 7, [Ann.F_CMD, ['Reserved', 'Res', 'R']])
475 self.putf(8, 39, [Ann.F_ARG, ['Argument', 'Arg', 'A']])
476 self.putf(40, 46, [Ann.F_CRC, ['Reserved', 'Res', 'R']])
477 self.putf(47, 47, [Ann.F_END, ['End bit', 'End', 'E']])
478 self.puta(0, 31, [Ann.DECODED_F, ['OCR register', 'OCR reg', 'OCR', 'O']])
624710c9 479 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387 480
4dfde5a5 481 def handle_response_r6(self, cmd_pin):
08c41387
UH
482 # R6: Published RCA response
483 # - Bits[47:47]: Start bit (always 0)
484 # - Bits[46:46]: Transmission bit (0 == card)
485 # - Bits[45:40]: Command index (always 0b000011)
486 # - Bits[39:24]: Argument[31:16]: New published RCA of the card
487 # - Bits[23:08]: Argument[15:0]: Card status bits
488 # - Bits[07:01]: CRC7
489 # - Bits[00:00]: End bit (always 1)
4dfde5a5 490 if not self.get_token_bits(cmd_pin, 48):
08c41387
UH
491 return
492 self.handle_common_token_fields()
9f08e757
UH
493 self.puta(0, 15, [Ann.DECODED_F, ['Card status bits', 'Status', 'S']])
494 self.puta(16, 31, [Ann.DECODED_F, ['Relative card address', 'RCA', 'R']])
604b5f9d 495 self.putr(Ann.RESPONSE_R6)
624710c9 496 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387 497
4dfde5a5 498 def handle_response_r7(self, cmd_pin):
08c41387
UH
499 # R7: Card interface condition
500 # - Bits[47:47]: Start bit (always 0)
501 # - Bits[46:46]: Transmission bit (0 == card)
502 # - Bits[45:40]: Command index (always 0b001000)
503 # - Bits[39:20]: Reserved bits (all-zero)
504 # - Bits[19:16]: Voltage accepted
505 # - Bits[15:08]: Echo-back of check pattern
506 # - Bits[07:01]: CRC7
507 # - Bits[00:00]: End bit (always 1)
4dfde5a5 508 if not self.get_token_bits(cmd_pin, 48):
08c41387
UH
509 return
510 self.handle_common_token_fields()
511
604b5f9d 512 self.putr(Ann.RESPONSE_R7)
08c41387
UH
513
514 # Arg[31:12]: Reserved bits (all-zero)
9f08e757 515 self.puta(12, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']])
08c41387
UH
516
517 # Arg[11:08]: Voltage accepted
07da8a9b 518 v = ''.join(str(i.bit) for i in self.token[28:32])
08c41387 519 av = accepted_voltages.get(int('0b' + v, 2), 'Unknown')
9f08e757
UH
520 self.puta(8, 11, [Ann.DECODED_F,
521 ['Voltage accepted: ' + av, 'Voltage', 'Volt', 'V']])
08c41387
UH
522
523 # Arg[07:00]: Echo-back of check pattern
9f08e757
UH
524 self.puta(0, 7, [Ann.DECODED_F,
525 ['Echo-back of check pattern', 'Echo', 'E']])
08c41387 526
624710c9 527 self.token, self.state = [], St.GET_COMMAND_TOKEN
08c41387 528
11535843
GS
529 def decode(self):
530 while True:
08c41387 531 # Wait for a rising CLK edge.
4dfde5a5 532 (cmd_pin, clk, dat0, dat1, dat2, dat3) = self.wait({Pin.CLK: 'r'})
08c41387
UH
533
534 # State machine.
624710c9 535 if self.state == St.GET_COMMAND_TOKEN:
08c41387
UH
536 if len(self.token) == 0:
537 # Wait for start bit (CMD = 0).
4dfde5a5 538 if cmd_pin != 0:
08c41387 539 continue
4dfde5a5 540 self.get_command_token(cmd_pin)
624710c9 541 elif self.state.value.startswith('HANDLE_CMD'):
08c41387 542 # Call the respective handler method for the command.
624710c9 543 a, cmdstr = 'a' if self.is_acmd else '', self.state.value[10:].lower()
08c41387
UH
544 handle_cmd = getattr(self, 'handle_%scmd%s' % (a, cmdstr))
545 handle_cmd()
546 # Leave ACMD mode again after the first command after CMD55.
547 if self.is_acmd and cmdstr not in ('55', '63'):
548 self.is_acmd = False
624710c9 549 elif self.state.value.startswith('GET_RESPONSE'):
08c41387
UH
550 if len(self.token) == 0:
551 # Wait for start bit (CMD = 0).
4dfde5a5 552 if cmd_pin != 0:
08c41387
UH
553 continue
554 # Call the respective handler method for the response.
624710c9 555 s = 'handle_response_%s' % self.state.value[13:].lower()
08c41387 556 handle_response = getattr(self, s)
4dfde5a5 557 handle_response(cmd_pin)