-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
stm32: littlefs support #5330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stm32: littlefs support #5330
Changes from all commits
c169094
7897f5d
715e4fc
5634a31
120368b
d8057c3
6b3404f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,13 @@ | |
#include "lib/mp-readline/readline.h" | ||
#include "lib/utils/pyexec.h" | ||
#include "lib/oofatfs/ff.h" | ||
#include "lib/littlefs/lfs1.h" | ||
#include "lib/littlefs/lfs1_util.h" | ||
#include "lib/littlefs/lfs2.h" | ||
#include "lib/littlefs/lfs2_util.h" | ||
#include "extmod/vfs.h" | ||
#include "extmod/vfs_fat.h" | ||
#include "extmod/vfs_lfs.h" | ||
|
||
#if MICROPY_PY_LWIP | ||
#include "lwip/init.h" | ||
|
@@ -183,13 +188,53 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { | |
factory_reset_create_filesystem(); | ||
} | ||
|
||
// Try to mount the flash on "/flash" and chdir to it for the boot-up directory. | ||
// Default block device to entire flash storage | ||
mp_obj_t bdev = MP_OBJ_FROM_PTR(&pyb_flash_obj); | ||
|
||
#if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2 | ||
|
||
// Try to detect the block device used for the main filesystem, based on the first block | ||
|
||
uint8_t buf[FLASH_BLOCK_SIZE]; | ||
storage_read_blocks(buf, FLASH_PART1_START_BLOCK, 1); | ||
|
||
mp_int_t len = -1; | ||
|
||
#if MICROPY_VFS_LFS1 | ||
if (memcmp(&buf[40], "littlefs", 8) == 0) { | ||
// LFS1 | ||
lfs1_superblock_t *superblock = (void*)&buf[12]; | ||
uint32_t block_size = lfs1_fromle32(superblock->d.block_size); | ||
uint32_t block_count = lfs1_fromle32(superblock->d.block_count); | ||
len = block_count * block_size; | ||
} | ||
#endif | ||
|
||
#if MICROPY_VFS_LFS2 | ||
if (memcmp(&buf[8], "littlefs", 8) == 0) { | ||
// LFS2 | ||
lfs2_superblock_t *superblock = (void*)&buf[20]; | ||
uint32_t block_size = lfs2_fromle32(superblock->block_size); | ||
uint32_t block_count = lfs2_fromle32(superblock->block_count); | ||
len = block_count * block_size; | ||
} | ||
#endif | ||
|
||
if (len != -1) { | ||
// Detected a littlefs filesystem so create correct block device for it | ||
mp_obj_t args[] = { MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_NEW_SMALL_INT(len) }; | ||
bdev = pyb_flash_type.make_new(&pyb_flash_type, 2, 0, args); | ||
} | ||
|
||
#endif | ||
|
||
// Try to mount the flash on "/flash" and chdir to it for the boot-up directory. | ||
mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash); | ||
int ret = vfs_mount_and_chdir(bdev, mount_point); | ||
|
||
if (ret == -MP_ENODEV && reset_mode != 3) { | ||
// No filesystem (and didn't already create one), try to create a fresh one | ||
if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) && reset_mode != 3) { | ||
// No filesystem, bdev is still the default (so didn't detect a possibly corrupt littlefs), | ||
// and didn't already create a filesystem, so try to create a fresh one now. | ||
ret = factory_reset_create_filesystem(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a case here where the above code can detect LFS, but mount fails. In which case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed: this code will now only run if bdev==pyb_flash_obj, which reduces the chance that it will accidentally wipe a littlefs filesystem |
||
if (ret == 0) { | ||
ret = vfs_mount_and_chdir(bdev, mount_point); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these strings at magic locations part of the official lfs1/2 header formats?
If so, this chunk of code the check the format of the block device would be a good candidate to move to common place for all the ports to use!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found them out by inspecting the littlefs code, the superblocks.
Can certainly move it to a common location for general use.