How to create a FAT image consumable by micropython #10880
-
I tried to use from pyfatfs.PyFat import PyFat
image = PyFat()
with open("fat.img", 'wb') as f:
image.mkfs(f.name, 12, 32 * 1024)
image.close() |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
In general system-level drivers written for CPython are unlikely to work in MicroPython. MP does support FAT on a disk-type device, but does not support creating a FAT partition in a file. |
Beta Was this translation helpful? Give feedback.
-
You can do this using the unix port of MicroPython. For example: import uos
TRACE = 1
class RAMBlockDevice:
def __init__(self, block_size, num_blocks):
self.block_size = block_size
self.data = bytearray(num_blocks * self.block_size)
def readblocks(self, block, buf, off=0):
if TRACE:
print(f"readblocks({block=}, {len(buf)=}, {off=}")
addr = block * self.block_size + off
for i in range(len(buf)):
buf[i] = self.data[addr + i]
def writeblocks(self, block, buf, off=0):
if TRACE:
print(f"writeblocks({block=}, {len(buf)=}, {off=}", buf[:100] if any(buf) else "ZERO")
addr = block * self.block_size + off
for i in range(len(buf)):
self.data[addr + i] = buf[i]
def ioctl(self, op, arg):
if op == 4: # block count
return len(self.data) // self.block_size
if op == 5: # block size
return self.block_size
if op == 6: # erase block
return 0
# Create and mount a user filesystem.
bdev = RAMBlockDevice(512, 128)
print("mkfs")
uos.VfsFat.mkfs(bdev)
print("mkfs done")
print("mount")
uos.mount(uos.VfsFat(bdev), "/userfs")
print(uos.statvfs("/userfs"))
print("mount done")
# Test open for writing.
print("create file")
with open("/userfs/test.txt", "w") as f:
print(f.write("".join(f"hello{i}" for i in range(100))))
print("create file done") Then the filesystem's raw data is in |
Beta Was this translation helpful? Give feedback.
-
Increased image size and it did help Full story: I mount this image using the built-in |
Beta Was this translation helpful? Give feedback.
Increased image size and it did help
image.mkfs(f.name, 12, 1024 * 1024)
.Full story: I mount this image using the built-in
os.mount
on ESP32. I use/develop a small library to serve the image as a network block device.