MCPcopy Create free account
hub / github.com/RetiredWizard/PyDOS / readblocks

Method readblocks

mpython/lib/sdcard.py:245–274  ·  view source on GitHub ↗
(self, block_num, buf)

Source from the content-addressed store, hash-verified

243 self.spi.write(b"\xff")
244
245 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
250 nblocks = len(buf) // 512
251 assert nblocks and not len(buf) % 512, "Buffer length is invalid"
252 if nblocks == 1:
253 # CMD17: set read address for single block
254 if self.cmd(17, block_num * self.cdv, 0, release=False) != 0:
255 # release the card
256 self.cs(1)
257 raise OSError(5) # EIO
258 # receive the data and release card
259 self.readinto(buf)
260 else:
261 # CMD18: set read address for multiple blocks
262 if self.cmd(18, block_num * self.cdv, 0, release=False) != 0:
263 # release the card
264 self.cs(1)
265 raise OSError(5) # EIO
266 offset = 0
267 mv = memoryview(buf)
268 while nblocks:
269 # receive the data and release card
270 self.readinto(mv[offset : offset + 512])
271 offset += 512
272 nblocks -= 1
273 if self.cmd(12, 0, 0xFF, skip1=True):
274 raise OSError(5) # EIO
275
276 def writeblocks(self, block_num, buf):
277 # workaround for shared bus, required for (at least) some Kingston

Callers

nothing calls this directly

Calls 3

cmdMethod · 0.95
readintoMethod · 0.95
writeMethod · 0.45

Tested by

no test coverage detected