flashbdev.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import esp
  2. class FlashBdev:
  3. SEC_SIZE = 4096
  4. START_SEC = esp.flash_user_start() // SEC_SIZE
  5. def __init__(self, blocks):
  6. self.blocks = blocks
  7. def readblocks(self, n, buf):
  8. #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
  9. esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf)
  10. def writeblocks(self, n, buf):
  11. #print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
  12. #assert len(buf) <= self.SEC_SIZE, len(buf)
  13. esp.flash_erase(n + self.START_SEC)
  14. esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf)
  15. def ioctl(self, op, arg):
  16. #print("ioctl(%d, %r)" % (op, arg))
  17. if op == 4: # BP_IOCTL_SEC_COUNT
  18. return self.blocks
  19. if op == 5: # BP_IOCTL_SEC_SIZE
  20. return self.SEC_SIZE
  21. size = esp.flash_size()
  22. if size < 1024*1024:
  23. # flash too small for a filesystem
  24. bdev = None
  25. else:
  26. # for now we use a fixed size for the filesystem
  27. bdev = FlashBdev(2048 * 1024 // FlashBdev.SEC_SIZE)