Skip to content

Commit

Permalink
drivers/sdcard: Do not release CS during the middle of read operations.
Browse files Browse the repository at this point in the history
It seems that some cards do not tolerate releasing the card (by setting CS
high) after issuing CMD17 (and 18) and raising it again before reading
data. Somehow this causes the 0xfe data start marker not being read and
SDCard.readinto() is spinning forever (or until this byte is in the data).

This seems to fix weird behviour of SDCard.readblocks() returning different
data, also solved hanging os.mount() for my case with a 16GB Infineon card.

This stackexchange answer gives more context:
https://electronics.stackexchange.com/questions/307214/sd-card-spi-interface-issue-read-operation-returns-0x3f-0xff-instead-of-0x7f-0#307268
  • Loading branch information
matikij authored and dpgeorge committed Jul 5, 2018
1 parent 14ab81e commit 1751f5a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions drivers/sdcard/sdcard.py
Expand Up @@ -174,7 +174,7 @@ def readinto(self, buf):
# read until start byte (0xff)
while True:
self.spi.readinto(self.tokenbuf, 0xff)
if self.tokenbuf[0] == 0xfe:
if self.tokenbuf[0] == _TOKEN_DATA:
break

# read data
Expand Down Expand Up @@ -228,17 +228,22 @@ def readblocks(self, block_num, buf):
assert nblocks and not len(buf) % 512, 'Buffer length is invalid'
if nblocks == 1:
# CMD17: set read address for single block
if self.cmd(17, block_num * self.cdv, 0) != 0:
if self.cmd(17, block_num * self.cdv, 0, release=False) != 0:
# release the card
self.cs(1)
raise OSError(5) # EIO
# receive the data
# receive the data and release card
self.readinto(buf)
else:
# CMD18: set read address for multiple blocks
if self.cmd(18, block_num * self.cdv, 0) != 0:
if self.cmd(18, block_num * self.cdv, 0, release=False) != 0:
# release the card
self.cs(1)
raise OSError(5) # EIO
offset = 0
mv = memoryview(buf)
while nblocks:
# receive the data and release card
self.readinto(mv[offset : offset + 512])
offset += 512
nblocks -= 1
Expand Down

0 comments on commit 1751f5a

Please sign in to comment.