@@ -221,14 +221,14 @@ def _cmd(self, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True)
221
221
return buf [0 ]
222
222
return - 1
223
223
224
- def _block_cmd (self , cmd , block , crc ):
224
+ def _block_cmd (self , cmd , block , crc , response_buf = None ):
225
225
"""Issue a command to the card with a block argument.
226
226
227
227
:param int cmd: The command number.
228
228
:param int block: The relevant block.
229
229
:param int crc: The crc to allow the card to verify the command and argument."""
230
230
if self ._cdv == 1 :
231
- return self ._cmd (cmd , block , crc )
231
+ return self ._cmd (cmd , block , crc , response_buf = response_buf )
232
232
233
233
# create and send the command
234
234
buf = self ._cmdbuf
@@ -242,6 +242,7 @@ def _block_cmd(self, cmd, block, crc):
242
242
buf [4 ] = 0
243
243
buf [5 ] = crc
244
244
245
+ result = - 1
245
246
with self ._spi as spi :
246
247
self ._wait_for_ready (spi )
247
248
@@ -251,8 +252,13 @@ def _block_cmd(self, cmd, block, crc):
251
252
for i in range (_CMD_TIMEOUT ):
252
253
spi .readinto (buf , end = 1 , write_value = 0xff )
253
254
if not (buf [0 ] & 0x80 ):
254
- return buf [0 ]
255
- return - 1
255
+ result = buf [0 ]
256
+ break
257
+
258
+ if response_buf != None and result == 0 :
259
+ self ._readinto (response_buf )
260
+
261
+ return result
256
262
257
263
def _cmd_nodata (self , cmd , response = 0xff ):
258
264
"""Issue a command to the card with no argument.
@@ -340,10 +346,10 @@ def readblocks(self, start_block, buf):
340
346
assert nblocks and not err , 'Buffer length is invalid'
341
347
if nblocks == 1 :
342
348
# CMD17: set read address for single block
343
- if self ._block_cmd (17 , start_block , 0 ) != 0 :
349
+ # We use _block_cmd to read our data so that the chip select line
350
+ # isn't toggled between the command, response and data.
351
+ if self ._block_cmd (17 , start_block , 0 , response_buf = buf ) != 0 :
344
352
return 1
345
- # receive the data
346
- self ._readinto (buf )
347
353
else :
348
354
# CMD18: set read address for multiple blocks
349
355
if self ._block_cmd (18 , start_block , 0 ) != 0 :
0 commit comments