Skip to content

Commit 6fca45f

Browse files
committed
sdcard: Set MISO high before readblocks/writeblocks.
Originally by @peterhinch. See micropython/micropython#6007 for discussion. The summary is that on some cards (especially older Kingston ones) if the bus is shared with other SPI devices, then it seems to require that MISO is high for a few cycles before the transaction is initiated. Because CS is high, this change should otherwise be a no-op. Signed-off-by: Jim Mussared <[email protected]>
1 parent 56dc65b commit 6fca45f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

micropython/drivers/storage/sdcard/sdcard.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ def write_token(self, token):
243243
self.spi.write(b"\xff")
244244

245245
def readblocks(self, block_num, buf):
246+
# workaround for shared bus, required for (at least) some Kingston
247+
# devices, ensure MOSI is high before starting transaction
248+
self.spi.write(b"\xff")
249+
246250
nblocks = len(buf) // 512
247251
assert nblocks and not len(buf) % 512, "Buffer length is invalid"
248252
if nblocks == 1:
@@ -270,6 +274,10 @@ def readblocks(self, block_num, buf):
270274
raise OSError(5) # EIO
271275

272276
def writeblocks(self, block_num, buf):
277+
# workaround for shared bus, required for (at least) some Kingston
278+
# devices, ensure MOSI is high before starting transaction
279+
self.spi.write(b"\xff")
280+
273281
nblocks, err = divmod(len(buf), 512)
274282
assert nblocks and not err, "Buffer length is invalid"
275283
if nblocks == 1:

0 commit comments

Comments
 (0)