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
1 changed files with 13 additions and 2 deletions

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,7 +132,13 @@ 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.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
@ -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: