Skip to content

Commit 15189c1

Browse files
committed
Ensure that the chip select line is low between the read command,
response code and response data. Samsung EVO cards do not work otherwise. May fix adafruit/circuitpython#245
1 parent e23b0ea commit 15189c1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

adafruit_sdcard.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ def _cmd(self, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True)
221221
return buf[0]
222222
return -1
223223

224-
def _block_cmd(self, cmd, block, crc):
224+
def _block_cmd(self, cmd, block, crc, response_buf=None):
225225
"""Issue a command to the card with a block argument.
226226
227227
:param int cmd: The command number.
228228
:param int block: The relevant block.
229229
:param int crc: The crc to allow the card to verify the command and argument."""
230230
if self._cdv == 1:
231-
return self._cmd(cmd, block, crc)
231+
return self._cmd(cmd, block, crc, response_buf=response_buf)
232232

233233
# create and send the command
234234
buf = self._cmdbuf
@@ -242,6 +242,7 @@ def _block_cmd(self, cmd, block, crc):
242242
buf[4] = 0
243243
buf[5] = crc
244244

245+
result = -1
245246
with self._spi as spi:
246247
self._wait_for_ready(spi)
247248

@@ -251,8 +252,13 @@ def _block_cmd(self, cmd, block, crc):
251252
for i in range(_CMD_TIMEOUT):
252253
spi.readinto(buf, end=1, write_value=0xff)
253254
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
256262

257263
def _cmd_nodata(self, cmd, response=0xff):
258264
"""Issue a command to the card with no argument.
@@ -340,10 +346,10 @@ def readblocks(self, start_block, buf):
340346
assert nblocks and not err, 'Buffer length is invalid'
341347
if nblocks == 1:
342348
# 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:
344352
return 1
345-
# receive the data
346-
self._readinto(buf)
347353
else:
348354
# CMD18: set read address for multiple blocks
349355
if self._block_cmd(18, start_block, 0) != 0:

0 commit comments

Comments
 (0)