Skip to content

Commit ee9feac

Browse files
committed
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>
1 parent 203b98c commit ee9feac

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

drivers/sdcard/sdcard.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def init_card_v1(self):
120120
for i in range(_CMD_TIMEOUT):
121121
self.cmd(55, 0, 0)
122122
if self.cmd(41, 0, 0) == 0:
123+
# SDSC card, uses byte addressing in read/write/erase commands
123124
self.cdv = 512
124125
# print("[SDCard] v1 card")
125126
return
@@ -131,8 +132,14 @@ def init_card_v2(self):
131132
self.cmd(58, 0, 0, 4)
132133
self.cmd(55, 0, 0)
133134
if self.cmd(41, 0x40000000, 0) == 0:
134-
self.cmd(58, 0, 0, 4)
135-
self.cdv = 1
135+
self.cmd(58, 0, 0, -4) # 4-byte response, negative means keep the first byte
136+
ocr = self.tokenbuf[0] # get first byte of response, which is OCR
137+
if not ocr & 0x40:
138+
# SDSC card, uses byte addressing in read/write/erase commands
139+
self.cdv = 512
140+
else:
141+
# SDHC/SDXC card, uses block addressing in read/write/erase commands
142+
self.cdv = 1
136143
# print("[SDCard] v2 card")
137144
return
138145
raise OSError("timeout waiting for v2 card")
@@ -159,6 +166,10 @@ def cmd(self, cmd, arg, crc, final=0, release=True, skip1=False):
159166
response = self.tokenbuf[0]
160167
if not (response & 0x80):
161168
# this could be a big-endian integer that we are getting here
169+
# if final<0 then store the first byte to tokenbuf and discard the rest
170+
if final < 0:
171+
self.spi.readinto(self.tokenbuf, 0xFF)
172+
final = -1 - final
162173
for j in range(final):
163174
self.spi.write(b"\xff")
164175
if release:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy