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