-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Description
Currently RP2's rp2.Flash()
returns a block device that affects the entire, compile-time determined user flash region of the Pico/RP2040 board.
This- insofar as I'm aware- precludes multiple filesystems from existing alongside each other in flash, since there's no way to tell os.VfsLfs2.mkfs()
where specifically to put its filesystem. I'd like to "fix" this.
Right now rp2.Flash()
is a singleton class: https://github.com/micropython/micropython/blob/master/ports/rp2/rp2_flash.c
I propose upgrading it to a regular, instanced class with two constructor arguments: base
(or offset
probably) and size
.
The default behavior for rp2.Flash()
would be to use the entire storage region, matching its current behavior.
The class already has flash_base
and flash_size
to store the settings, and observes these values. I think the changes would be pretty minimal.
The endgame would be to make something like the following _boot.py
work:
import os
import machine, rp2
# Try to mount the filesystem, and format the flash if it doesn't exist.
# Note: the flash requires the programming size to be aligned to 256 bytes.
USER_FLASH_SIZE = rp2.Flash().ioctl(4, 0) * rp2.Flash().ioctl(5, 0) # Extremely cursed
CONFIG_FS_SIZE = 48 * 1024
bdev_root = rp2.Flash(0, USER_FLASH_SIZE - CONFIG_FS_SIZE)
try:
vfs_root = os.VfsLfs2(bdev_root, progsize=256)
except:
os.VfsLfs2.mkfs(bdev_root, progsize=256)
vfs_root = os.VfsLfs2(bdev_root, progsize=256)
os.mount(vfs_root, "/")
bdev_config = rp2.Flash(USER_FLASH_SIZE - CONFIG_FS_SIZE, CONFIG_FS_SIZE)
try:
vfs_config = os.VfsLfs2(bdev_config, progsize=256)
except:
os.VfsLfs2.mkfs(bdev_config, progsize=256)
vfs_config = os.VfsLfs2(bdev_config, progsize=256)
os.mount(vfs_config, "/config")
del os, bdev_root, bdev_config, vfs_root, vfs_config, CONFIG_FS_SIZE
The above example shows the use of an additional "config" filesystem right at the end of user flash. The intention here is to provide a region in flash that firmware upgrade .uf2 files with pre-baked filesystems do not touch so that user config can be preserved.
See: https://github.com/pimoroni/enviro/releases/tag/v0.0.7 for an example of a firmware that includes a fully initialized and populated LittleFS filesystem in the .uf2 file.
Other uses for this might be a split "user" and "logging" filesystem using FatFS for mass storage mountable user files, and LittleFS for highly robust and fault tolerant logging.
I intend to try and make and test the above changes, but I'm raising this as an issue to invite discussion about:
- Whether this is a good idea
- If there's a better way (some secret VFS sauce I don't understand)
- If removing the singleton approach in
rp2.Flash()
would be dangerous/breaking somehow - Other!