Skip to content

Ensure that the chip select line is low between the read command, #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions adafruit_sdcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down