]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | import sigrokdecode as srd | |
9f08e757 | 21 | from common.srdhelper import SrdIntEnum, SrdStrEnum |
135b790c | 22 | from common.sdcard import (cmd_names, acmd_names, accepted_voltages, card_status, sd_status) |
08c41387 | 23 | |
9f08e757 UH |
24 | a = ['CMD%d' % i for i in range(64)] + ['ACMD%d' % i for i in range(64)] + \ |
25 | ['F_' + f for f in 'START TRANSM CMD ARG CRC END'.split()] + \ | |
26 | ['BIT', 'DECODED_BIT', 'DECODED_F'] | |
27 | Ann = SrdIntEnum.from_list('Ann', a) | |
28 | ||
624710c9 UH |
29 | s = ['GET_COMMAND_TOKEN', 'HANDLE_CMD999'] + \ |
30 | ['HANDLE_CMD%d' % i for i in range(64)] + \ | |
31 | ['HANDLE_ACMD%d' % i for i in range(64)] + \ | |
32 | ['GET_RESPONSE_R%s' % r for r in ['1', '2', '3', '6', '7']] | |
33 | St = SrdStrEnum.from_list('St', s) | |
34 | ||
08c41387 | 35 | class Decoder(srd.Decoder): |
11535843 | 36 | api_version = 3 |
08c41387 UH |
37 | id = 'sdcard_sd' |
38 | name = 'SD card (SD mode)' | |
39 | longname = 'Secure Digital card (SD mode)' | |
40 | desc = 'Secure Digital card (SD mode) low-level protocol.' | |
41 | license = 'gplv2+' | |
42 | inputs = ['logic'] | |
6cbba91f | 43 | outputs = [] |
d6d8a8a4 | 44 | tags = ['Memory'] |
08c41387 UH |
45 | channels = ( |
46 | {'id': 'cmd', 'name': 'CMD', 'desc': 'Command'}, | |
47 | {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'}, | |
48 | ) | |
49 | optional_channels = ( | |
50 | {'id': 'dat0', 'name': 'DAT0', 'desc': 'Data pin 0'}, | |
51 | {'id': 'dat1', 'name': 'DAT1', 'desc': 'Data pin 1'}, | |
52 | {'id': 'dat2', 'name': 'DAT2', 'desc': 'Data pin 2'}, | |
53 | {'id': 'dat3', 'name': 'DAT3', 'desc': 'Data pin 3'}, | |
54 | ) | |
55 | annotations = \ | |
56 | tuple(('cmd%d' % i, 'CMD%d' % i) for i in range(64)) + \ | |
57 | tuple(('acmd%d' % i, 'ACMD%d' % i) for i in range(64)) + ( \ | |
08c41387 UH |
58 | ('field-start', 'Start bit'), |
59 | ('field-transmission', 'Transmission bit'), | |
60 | ('field-cmd', 'Command'), | |
61 | ('field-arg', 'Argument'), | |
62 | ('field-crc', 'CRC'), | |
63 | ('field-end', 'End bit'), | |
9f08e757 | 64 | ('bit', 'Bit'), |
e144452b UH |
65 | ('decoded-bit', 'Decoded bit'), |
66 | ('decoded-field', 'Decoded field'), | |
08c41387 UH |
67 | ) |
68 | annotation_rows = ( | |
9f08e757 UH |
69 | ('raw-bits', 'Raw bits', (Ann.BIT,)), |
70 | ('decoded-bits', 'Decoded bits', (Ann.DECODED_BIT,)), | |
71 | ('decoded-fields', 'Decoded fields', (Ann.DECODED_F,)), | |
72 | ('fields', 'Fields', Ann.prefixes('F_')), | |
73 | ('commands', 'Commands', Ann.prefixes('CMD ACMD')), | |
08c41387 UH |
74 | ) |
75 | ||
92b7b49f | 76 | def __init__(self): |
10aeb8ea GS |
77 | self.reset() |
78 | ||
79 | def reset(self): | |
624710c9 | 80 | self.state = St.GET_COMMAND_TOKEN |
08c41387 | 81 | self.token = [] |
08c41387 UH |
82 | self.is_acmd = False # Indicates CMD vs. ACMD |
83 | self.cmd = None | |
f8eb6c3f | 84 | self.last_cmd = None |
08c41387 UH |
85 | self.arg = None |
86 | ||
87 | def start(self): | |
88 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
89 | ||
90 | def putbit(self, b, data): | |
9f08e757 UH |
91 | self.put(self.token[b][0], self.token[b][1], self.out_ann, |
92 | [Ann.DECODED_BIT, data]) | |
08c41387 UH |
93 | |
94 | def putt(self, data): | |
95 | self.put(self.token[0][0], self.token[47][1], self.out_ann, data) | |
96 | ||
08c41387 UH |
97 | def putf(self, s, e, data): |
98 | self.put(self.token[s][0], self.token[e][1], self.out_ann, data) | |
99 | ||
100 | def puta(self, s, e, data): | |
101 | self.put(self.token[47 - 8 - e][0], self.token[47 - 8 - s][1], | |
102 | self.out_ann, data) | |
103 | ||
104 | def putc(self, cmd, desc): | |
f8eb6c3f | 105 | self.last_cmd = cmd |
08c41387 UH |
106 | self.putt([cmd, ['%s: %s' % (self.cmd_str, desc), self.cmd_str, |
107 | self.cmd_str.split(' ')[0]]]) | |
108 | ||
f8eb6c3f WS |
109 | def putr(self, desc): |
110 | self.putt([self.last_cmd, ['Reply: %s' % desc]]) | |
08c41387 | 111 | |
08c41387 UH |
112 | def cmd_name(self, cmd): |
113 | c = acmd_names if self.is_acmd else cmd_names | |
114 | return c.get(cmd, 'Unknown') | |
115 | ||
116 | def get_token_bits(self, cmd, n): | |
117 | # Get a bit, return True if we already got 'n' bits, False otherwise. | |
118 | self.token.append([self.samplenum, self.samplenum, cmd]) | |
119 | if len(self.token) > 0: | |
120 | self.token[len(self.token) - 2][1] = self.samplenum | |
121 | if len(self.token) < n: | |
122 | return False | |
123 | self.token[n - 1][1] += self.token[n - 1][0] - self.token[n - 2][0] | |
124 | return True | |
125 | ||
126 | def handle_common_token_fields(self): | |
127 | s = self.token | |
128 | ||
129 | # Annotations for each individual bit. | |
130 | for bit in range(len(self.token)): | |
9f08e757 | 131 | self.putf(bit, bit, [Ann.BIT, ['%d' % s[bit][2]]]) |
08c41387 UH |
132 | |
133 | # CMD[47:47]: Start bit (always 0) | |
9f08e757 | 134 | self.putf(0, 0, [Ann.F_START, ['Start bit', 'Start', 'S']]) |
08c41387 UH |
135 | |
136 | # CMD[46:46]: Transmission bit (1 == host) | |
137 | t = 'host' if s[1][2] == 1 else 'card' | |
9f08e757 | 138 | self.putf(1, 1, [Ann.F_TRANSM, ['Transmission: ' + t, 'T: ' + t, 'T']]) |
08c41387 UH |
139 | |
140 | # CMD[45:40]: Command index (BCD; valid: 0-63) | |
141 | self.cmd = int('0b' + ''.join([str(s[i][2]) for i in range(2, 8)]), 2) | |
142 | c = '%s (%d)' % (self.cmd_name(self.cmd), self.cmd) | |
9f08e757 | 143 | self.putf(2, 7, [Ann.F_CMD, ['Command: ' + c, 'Cmd: ' + c, |
08c41387 UH |
144 | 'CMD%d' % self.cmd, 'Cmd', 'C']]) |
145 | ||
146 | # CMD[39:08]: Argument | |
619ed6b4 | 147 | self.arg = int('0b' + ''.join([str(s[i][2]) for i in range(8, 40)]), 2) |
9f08e757 | 148 | self.putf(8, 39, [Ann.F_ARG, ['Argument: 0x%08x' % self.arg, 'Arg', 'A']]) |
08c41387 UH |
149 | |
150 | # CMD[07:01]: CRC7 | |
151 | self.crc = int('0b' + ''.join([str(s[i][2]) for i in range(40, 47)]), 2) | |
9f08e757 | 152 | self.putf(40, 46, [Ann.F_CRC, ['CRC: 0x%x' % self.crc, 'CRC', 'C']]) |
08c41387 UH |
153 | |
154 | # CMD[00:00]: End bit (always 1) | |
9f08e757 | 155 | self.putf(47, 47, [Ann.F_END, ['End bit', 'End', 'E']]) |
08c41387 UH |
156 | |
157 | def get_command_token(self, cmd): | |
158 | # Command tokens (48 bits) are sent serially (MSB-first) by the host | |
159 | # (over the CMD line), either to one SD card or to multiple ones. | |
160 | # | |
161 | # Format: | |
162 | # - Bits[47:47]: Start bit (always 0) | |
163 | # - Bits[46:46]: Transmission bit (1 == host) | |
164 | # - Bits[45:40]: Command index (BCD; valid: 0-63) | |
165 | # - Bits[39:08]: Argument | |
166 | # - Bits[07:01]: CRC7 | |
167 | # - Bits[00:00]: End bit (always 1) | |
168 | ||
169 | if not self.get_token_bits(cmd, 48): | |
170 | return | |
171 | ||
172 | self.handle_common_token_fields() | |
173 | ||
174 | # Handle command. | |
175 | s = 'ACMD' if self.is_acmd else 'CMD' | |
176 | self.cmd_str = '%s%d (%s)' % (s, self.cmd, self.cmd_name(self.cmd)) | |
ee390f67 | 177 | if self.cmd in (0, 2, 3, 6, 7, 8, 9, 10, 13, 41, 51, 55): |
624710c9 | 178 | self.state = St['HANDLE_CMD%d' % self.cmd] |
08c41387 | 179 | else: |
624710c9 | 180 | self.state = St.HANDLE_CMD999 |
08c41387 UH |
181 | self.putc(self.cmd, '%s%d' % (s, self.cmd)) |
182 | ||
183 | def handle_cmd0(self): | |
184 | # CMD0 (GO_IDLE_STATE) -> no response | |
9f08e757 UH |
185 | self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) |
186 | self.putc(Ann.CMD0, 'Reset all SD cards') | |
624710c9 | 187 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 UH |
188 | |
189 | def handle_cmd2(self): | |
190 | # CMD2 (ALL_SEND_CID) -> R2 | |
9f08e757 UH |
191 | self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) |
192 | self.putc(Ann.CMD2, 'Ask card for CID number') | |
624710c9 | 193 | self.token, self.state = [], St.GET_RESPONSE_R2 |
08c41387 UH |
194 | |
195 | def handle_cmd3(self): | |
196 | # CMD3 (SEND_RELATIVE_ADDR) -> R6 | |
9f08e757 UH |
197 | self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) |
198 | self.putc(Ann.CMD3, 'Ask card for new relative card address (RCA)') | |
624710c9 | 199 | self.token, self.state = [], St.GET_RESPONSE_R6 |
08c41387 UH |
200 | |
201 | def handle_cmd6(self): | |
202 | # CMD6 (SWITCH_FUNC) -> R1 | |
9f08e757 | 203 | self.putc(Ann.CMD6, 'Switch/check card function') |
624710c9 | 204 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
205 | |
206 | def handle_cmd7(self): | |
207 | # CMD7 (SELECT/DESELECT_CARD) -> R1b | |
9f08e757 | 208 | self.putc(Ann.CMD7, 'Select / deselect card') |
624710c9 | 209 | self.token, self.state = [], St.GET_RESPONSE_R6 |
08c41387 UH |
210 | |
211 | def handle_cmd8(self): | |
212 | # CMD8 (SEND_IF_COND) -> R7 | |
9f08e757 UH |
213 | self.puta(12, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']]) |
214 | self.puta(8, 11, [Ann.DECODED_F, ['Supply voltage', 'Voltage', 'VHS', 'V']]) | |
215 | self.puta(0, 7, [Ann.DECODED_F, ['Check pattern', 'Check pat', 'Check', 'C']]) | |
216 | self.putc(Ann.CMD8, 'Send interface condition to card') | |
624710c9 | 217 | self.token, self.state = [], St.GET_RESPONSE_R7 |
08c41387 UH |
218 | # TODO: Handle case when card doesn't reply with R7 (no reply at all). |
219 | ||
220 | def handle_cmd9(self): | |
221 | # CMD9 (SEND_CSD) -> R2 | |
9f08e757 UH |
222 | self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']]) |
223 | self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) | |
224 | self.putc(Ann.CMD9, 'Send card-specific data (CSD)') | |
624710c9 | 225 | self.token, self.state = [], St.GET_RESPONSE_R2 |
08c41387 UH |
226 | |
227 | def handle_cmd10(self): | |
228 | # CMD10 (SEND_CID) -> R2 | |
9f08e757 UH |
229 | self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']]) |
230 | self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) | |
231 | self.putc(Ann.CMD10, 'Send card identification data (CID)') | |
624710c9 | 232 | self.token, self.state = [], St.GET_RESPONSE_R2 |
08c41387 UH |
233 | |
234 | def handle_cmd13(self): | |
235 | # CMD13 (SEND_STATUS) -> R1 | |
9f08e757 UH |
236 | self.puta(16, 31, [Ann.DECODED_F, ['RCA', 'R']]) |
237 | self.puta(0, 15, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) | |
238 | self.putc(Ann.CMD13, 'Send card status register') | |
624710c9 | 239 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
240 | |
241 | def handle_cmd16(self): | |
242 | # CMD16 (SET_BLOCKLEN) -> R1 | |
9f08e757 UH |
243 | self.puta(0, 31, [Ann.DECODED_F, ['Block length', 'Blocklen', 'BL', 'B']]) |
244 | self.putc(Ann.CMD16, 'Set the block length to %d bytes' % self.arg) | |
624710c9 | 245 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
246 | |
247 | def handle_cmd55(self): | |
248 | # CMD55 (APP_CMD) -> R1 | |
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']]) | |
251 | self.putc(Ann.CMD55, 'Next command is an application-specific command') | |
08c41387 | 252 | self.is_acmd = True |
624710c9 | 253 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
254 | |
255 | def handle_acmd6(self): | |
256 | # ACMD6 (SET_BUS_WIDTH) -> R1 | |
9f08e757 | 257 | self.putc(Ann.ACMD6, 'Read SD config register (SCR)') |
624710c9 | 258 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
259 | |
260 | def handle_acmd13(self): | |
261 | # ACMD13 (SD_STATUS) -> R1 | |
9f08e757 UH |
262 | self.puta(0, 31, [Ann.DECODED_F, ['Stuff bits', 'Stuff', 'SB', 'S']]) |
263 | self.putc(Ann.ACMD13, 'Set SD status') | |
624710c9 | 264 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
265 | |
266 | def handle_acmd41(self): | |
267 | # ACMD41 (SD_SEND_OP_COND) -> R3 | |
9f08e757 UH |
268 | self.puta(0, 23, [Ann.DECODED_F, |
269 | ['VDD voltage window', 'VDD volt', 'VDD', 'V']]) | |
270 | self.puta(24, 24, [Ann.DECODED_F, ['S18R']]) | |
271 | self.puta(25, 27, [Ann.DECODED_F, ['Reserved', 'Res', 'R']]) | |
272 | self.puta(28, 28, [Ann.DECODED_F, ['XPC']]) | |
273 | self.puta(29, 29, [Ann.DECODED_F, | |
274 | ['Reserved for eSD', 'Reserved', 'Res', 'R']]) | |
275 | self.puta(30, 30, [Ann.DECODED_F, | |
276 | ['Host capacity support info', 'Host capacity', 'HCS', 'H']]) | |
277 | self.puta(31, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']]) | |
278 | self.putc(Ann.ACMD41, 'Send HCS info and activate the card init process') | |
624710c9 | 279 | self.token, self.state = [], St.GET_RESPONSE_R3 |
08c41387 UH |
280 | |
281 | def handle_acmd51(self): | |
282 | # ACMD51 (SEND_SCR) -> R1 | |
9f08e757 | 283 | self.putc(Ann.ACMD51, 'Read SD config register (SCR)') |
624710c9 | 284 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
285 | |
286 | def handle_cmd999(self): | |
624710c9 | 287 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
288 | |
289 | def handle_acmd999(self): | |
624710c9 | 290 | self.token, self.state = [], St.GET_RESPONSE_R1 |
08c41387 UH |
291 | |
292 | # Response tokens can have one of four formats (depends on content). | |
293 | # They can have a total length of 48 or 136 bits. | |
294 | # They're sent serially (MSB-first) by the card that the host | |
295 | # addressed previously, or (synchronously) by all connected cards. | |
296 | ||
297 | def handle_response_r1(self, cmd): | |
298 | # R1: Normal response command | |
299 | # - Bits[47:47]: Start bit (always 0) | |
300 | # - Bits[46:46]: Transmission bit (0 == card) | |
301 | # - Bits[45:40]: Command index (BCD; valid: 0-63) | |
302 | # - Bits[39:08]: Card status | |
303 | # - Bits[07:01]: CRC7 | |
304 | # - Bits[00:00]: End bit (always 1) | |
305 | if not self.get_token_bits(cmd, 48): | |
306 | return | |
307 | self.handle_common_token_fields() | |
f8eb6c3f | 308 | self.putr('R1') |
9f08e757 | 309 | self.puta(0, 31, [Ann.DECODED_F, ['Card status', 'Status', 'S']]) |
08c41387 UH |
310 | for i in range(32): |
311 | self.putbit(8 + i, [card_status[31 - i]]) | |
624710c9 | 312 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 UH |
313 | |
314 | def handle_response_r1b(self, cmd): | |
315 | # R1b: Same as R1 with an optional busy signal (on the data line) | |
316 | if not self.get_token_bits(cmd, 48): | |
317 | return | |
318 | self.handle_common_token_fields() | |
9f08e757 | 319 | self.puta(0, 31, [Ann.DECODED_F, ['Card status', 'Status', 'S']]) |
f8eb6c3f | 320 | self.putr('R1b') |
624710c9 | 321 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 UH |
322 | |
323 | def handle_response_r2(self, cmd): | |
324 | # R2: CID/CSD register | |
325 | # - Bits[135:135]: Start bit (always 0) | |
326 | # - Bits[134:134]: Transmission bit (0 == card) | |
327 | # - Bits[133:128]: Reserved (always 0b111111) | |
328 | # - Bits[127:001]: CID or CSD register including internal CRC7 | |
329 | # - Bits[000:000]: End bit (always 1) | |
330 | if not self.get_token_bits(cmd, 136): | |
331 | return | |
332 | # Annotations for each individual bit. | |
333 | for bit in range(len(self.token)): | |
9f08e757 UH |
334 | self.putf(bit, bit, [Ann.BIT, ['%d' % self.token[bit][2]]]) |
335 | self.putf(0, 0, [Ann.F_START, ['Start bit', 'Start', 'S']]) | |
08c41387 | 336 | t = 'host' if self.token[1][2] == 1 else 'card' |
9f08e757 UH |
337 | self.putf(1, 1, [Ann.F_TRANSM, ['Transmission: ' + t, 'T: ' + t, 'T']]) |
338 | self.putf(2, 7, [Ann.F_CMD, ['Reserved', 'Res', 'R']]) | |
339 | self.putf(8, 134, [Ann.F_ARG, ['Argument', 'Arg', 'A']]) | |
340 | self.putf(135, 135, [Ann.F_END, ['End bit', 'End', 'E']]) | |
341 | self.putf(8, 134, [Ann.DECODED_F, ['CID/CSD register', 'CID/CSD', 'C']]) | |
08c41387 | 342 | self.putf(0, 135, [55, ['R2']]) |
624710c9 | 343 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 UH |
344 | |
345 | def handle_response_r3(self, cmd): | |
346 | # R3: OCR register | |
347 | # - Bits[47:47]: Start bit (always 0) | |
348 | # - Bits[46:46]: Transmission bit (0 == card) | |
349 | # - Bits[45:40]: Reserved (always 0b111111) | |
350 | # - Bits[39:08]: OCR register | |
351 | # - Bits[07:01]: Reserved (always 0b111111) | |
352 | # - Bits[00:00]: End bit (always 1) | |
353 | if not self.get_token_bits(cmd, 48): | |
354 | return | |
f8eb6c3f | 355 | self.putr('R3') |
08c41387 UH |
356 | # Annotations for each individual bit. |
357 | for bit in range(len(self.token)): | |
9f08e757 UH |
358 | self.putf(bit, bit, [Ann.BIT, ['%d' % self.token[bit][2]]]) |
359 | self.putf(0, 0, [Ann.F_START, ['Start bit', 'Start', 'S']]) | |
08c41387 | 360 | t = 'host' if self.token[1][2] == 1 else 'card' |
9f08e757 UH |
361 | self.putf(1, 1, [Ann.F_TRANSM, ['Transmission: ' + t, 'T: ' + t, 'T']]) |
362 | self.putf(2, 7, [Ann.F_CMD, ['Reserved', 'Res', 'R']]) | |
363 | self.putf(8, 39, [Ann.F_ARG, ['Argument', 'Arg', 'A']]) | |
364 | self.putf(40, 46, [Ann.F_CRC, ['Reserved', 'Res', 'R']]) | |
365 | self.putf(47, 47, [Ann.F_END, ['End bit', 'End', 'E']]) | |
366 | self.puta(0, 31, [Ann.DECODED_F, ['OCR register', 'OCR reg', 'OCR', 'O']]) | |
624710c9 | 367 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 UH |
368 | |
369 | def handle_response_r6(self, cmd): | |
370 | # R6: Published RCA response | |
371 | # - Bits[47:47]: Start bit (always 0) | |
372 | # - Bits[46:46]: Transmission bit (0 == card) | |
373 | # - Bits[45:40]: Command index (always 0b000011) | |
374 | # - Bits[39:24]: Argument[31:16]: New published RCA of the card | |
375 | # - Bits[23:08]: Argument[15:0]: Card status bits | |
376 | # - Bits[07:01]: CRC7 | |
377 | # - Bits[00:00]: End bit (always 1) | |
378 | if not self.get_token_bits(cmd, 48): | |
379 | return | |
380 | self.handle_common_token_fields() | |
9f08e757 UH |
381 | self.puta(0, 15, [Ann.DECODED_F, ['Card status bits', 'Status', 'S']]) |
382 | self.puta(16, 31, [Ann.DECODED_F, ['Relative card address', 'RCA', 'R']]) | |
f8eb6c3f | 383 | self.putr('R6') |
624710c9 | 384 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 UH |
385 | |
386 | def handle_response_r7(self, cmd): | |
387 | # R7: Card interface condition | |
388 | # - Bits[47:47]: Start bit (always 0) | |
389 | # - Bits[46:46]: Transmission bit (0 == card) | |
390 | # - Bits[45:40]: Command index (always 0b001000) | |
391 | # - Bits[39:20]: Reserved bits (all-zero) | |
392 | # - Bits[19:16]: Voltage accepted | |
393 | # - Bits[15:08]: Echo-back of check pattern | |
394 | # - Bits[07:01]: CRC7 | |
395 | # - Bits[00:00]: End bit (always 1) | |
396 | if not self.get_token_bits(cmd, 48): | |
397 | return | |
398 | self.handle_common_token_fields() | |
399 | ||
f8eb6c3f | 400 | self.putr('R7') |
08c41387 UH |
401 | |
402 | # Arg[31:12]: Reserved bits (all-zero) | |
9f08e757 | 403 | self.puta(12, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']]) |
08c41387 UH |
404 | |
405 | # Arg[11:08]: Voltage accepted | |
406 | v = ''.join(str(i[2]) for i in self.token[28:32]) | |
407 | av = accepted_voltages.get(int('0b' + v, 2), 'Unknown') | |
9f08e757 UH |
408 | self.puta(8, 11, [Ann.DECODED_F, |
409 | ['Voltage accepted: ' + av, 'Voltage', 'Volt', 'V']]) | |
08c41387 UH |
410 | |
411 | # Arg[07:00]: Echo-back of check pattern | |
9f08e757 UH |
412 | self.puta(0, 7, [Ann.DECODED_F, |
413 | ['Echo-back of check pattern', 'Echo', 'E']]) | |
08c41387 | 414 | |
624710c9 | 415 | self.token, self.state = [], St.GET_COMMAND_TOKEN |
08c41387 | 416 | |
11535843 GS |
417 | def decode(self): |
418 | while True: | |
08c41387 | 419 | # Wait for a rising CLK edge. |
11535843 | 420 | (cmd, clk, dat0, dat1, dat2, dat3) = self.wait({1: 'r'}) |
08c41387 UH |
421 | |
422 | # State machine. | |
624710c9 | 423 | if self.state == St.GET_COMMAND_TOKEN: |
08c41387 UH |
424 | if len(self.token) == 0: |
425 | # Wait for start bit (CMD = 0). | |
426 | if cmd != 0: | |
427 | continue | |
428 | self.get_command_token(cmd) | |
624710c9 | 429 | elif self.state.value.startswith('HANDLE_CMD'): |
08c41387 | 430 | # Call the respective handler method for the command. |
624710c9 | 431 | a, cmdstr = 'a' if self.is_acmd else '', self.state.value[10:].lower() |
08c41387 UH |
432 | handle_cmd = getattr(self, 'handle_%scmd%s' % (a, cmdstr)) |
433 | handle_cmd() | |
434 | # Leave ACMD mode again after the first command after CMD55. | |
435 | if self.is_acmd and cmdstr not in ('55', '63'): | |
436 | self.is_acmd = False | |
624710c9 | 437 | elif self.state.value.startswith('GET_RESPONSE'): |
08c41387 UH |
438 | if len(self.token) == 0: |
439 | # Wait for start bit (CMD = 0). | |
440 | if cmd != 0: | |
441 | continue | |
442 | # Call the respective handler method for the response. | |
624710c9 | 443 | s = 'handle_response_%s' % self.state.value[13:].lower() |
08c41387 UH |
444 | handle_response = getattr(self, s) |
445 | handle_response(cmd) |