drivers/sdcard: Fix address calculation on v2 SDSC cards.

For v2 cards that are standard capacity the read/write/erase commands take
byte address values.  Use the result of CMD58 to distinguish SDSC from
SDHC/SDXC.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-06-10 12:51:51 +10:00
parent 203b98c42b
commit ee9feacc00

View File

@ -120,6 +120,7 @@ class SDCard:
for i in range(_CMD_TIMEOUT):
self.cmd(55, 0, 0)
if self.cmd(41, 0, 0) == 0:
# SDSC card, uses byte addressing in read/write/erase commands
self.cdv = 512
# print("[SDCard] v1 card")
return
@ -131,8 +132,14 @@ class SDCard:
self.cmd(58, 0, 0, 4)
self.cmd(55, 0, 0)
if self.cmd(41, 0x40000000, 0) == 0:
self.cmd(58, 0, 0, 4)
self.cdv = 1
self.cmd(58, 0, 0, -4) # 4-byte response, negative means keep the first byte
ocr = self.tokenbuf[0] # get first byte of response, which is OCR
if not ocr & 0x40:
# SDSC card, uses byte addressing in read/write/erase commands
self.cdv = 512
else:
# SDHC/SDXC card, uses block addressing in read/write/erase commands
self.cdv = 1
# print("[SDCard] v2 card")
return
raise OSError("timeout waiting for v2 card")
@ -159,6 +166,10 @@ class SDCard:
response = self.tokenbuf[0]
if not (response & 0x80):
# this could be a big-endian integer that we are getting here
# if final<0 then store the first byte to tokenbuf and discard the rest
if final < 0:
self.spi.readinto(self.tokenbuf, 0xFF)
final = -1 - final
for j in range(final):
self.spi.write(b"\xff")
if release: