From 15189c1d237c18a742c688a873e89f3de7ca4754 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 8 Sep 2017 15:56:06 -0700 Subject: [PATCH] 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 --- adafruit_sdcard.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/adafruit_sdcard.py b/adafruit_sdcard.py index dd9f17c..219738c 100644 --- a/adafruit_sdcard.py +++ b/adafruit_sdcard.py @@ -221,14 +221,14 @@ def _cmd(self, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True) return buf[0] return -1 - def _block_cmd(self, cmd, block, crc): + def _block_cmd(self, cmd, block, crc, response_buf=None): """Issue a command to the card with a block argument. :param int cmd: The command number. :param int block: The relevant block. :param int crc: The crc to allow the card to verify the command and argument.""" if self._cdv == 1: - return self._cmd(cmd, block, crc) + return self._cmd(cmd, block, crc, response_buf=response_buf) # create and send the command buf = self._cmdbuf @@ -242,6 +242,7 @@ def _block_cmd(self, cmd, block, crc): buf[4] = 0 buf[5] = crc + result = -1 with self._spi as spi: self._wait_for_ready(spi) @@ -251,8 +252,13 @@ def _block_cmd(self, cmd, block, crc): for i in range(_CMD_TIMEOUT): spi.readinto(buf, end=1, write_value=0xff) if not (buf[0] & 0x80): - return buf[0] - return -1 + result = buf[0] + break + + if response_buf != None and result == 0: + self._readinto(response_buf) + + return result def _cmd_nodata(self, cmd, response=0xff): """Issue a command to the card with no argument. @@ -340,10 +346,10 @@ def readblocks(self, start_block, buf): assert nblocks and not err, 'Buffer length is invalid' if nblocks == 1: # CMD17: set read address for single block - if self._block_cmd(17, start_block, 0) != 0: + # We use _block_cmd to read our data so that the chip select line + # isn't toggled between the command, response and data. + if self._block_cmd(17, start_block, 0, response_buf=buf) != 0: return 1 - # receive the data - self._readinto(buf) else: # CMD18: set read address for multiple blocks if self._block_cmd(18, start_block, 0) != 0: