From ae06265de050b20e7d9cad2363b824c0b5c047fc Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 4 Dec 2021 16:28:54 +0100 Subject: [PATCH] pkg/fatfs: VFS: internally handle fatfs_mtd_devs This makes FAT behave more like the other file systems supported by VFS. The `fatfs_mtd_devs` array is populated internally so the application does not have to handle this. --- examples/filesystem/main.c | 13 +++++-------- pkg/fatfs/fatfs_vfs/fatfs_vfs.c | 26 ++++++++++++++++++++++++++ sys/include/fs/fatfs.h | 1 + tests/pkg_fatfs_vfs/main.c | 12 +++--------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/examples/filesystem/main.c b/examples/filesystem/main.c index add2dda4ec..bedfe131c8 100644 --- a/examples/filesystem/main.c +++ b/examples/filesystem/main.c @@ -45,8 +45,8 @@ static mtd_sdcard_t mtd_sdcard_dev = { .sd_card = &sdcard_spi_devs[0], .params = &sdcard_spi_params[0], }; -static mtd_dev_t *mtd0 = (mtd_dev_t*)&mtd_sdcard_dev; -#define MTD_0 mtd0 +static mtd_dev_t *mtd_sdcard = (mtd_dev_t*)&mtd_sdcard_dev; +#define MTD_0 mtd_sdcard #endif /* Flash mount point */ @@ -111,9 +111,6 @@ static spiffs_desc_t fs_desc = { * this example focus on basic usage, i.e. entire memory used */ static fatfs_desc_t fs_desc; -/* provide mtd devices for use within diskio layer of fatfs */ -mtd_dev_t *fatfs_mtd_devs[FF_VOLUMES]; - /* fatfs driver will be used */ #define FS_DRIVER fatfs_file_system #endif @@ -311,12 +308,12 @@ static const shell_command_t shell_commands[] = { int main(void) { -#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || defined(MODULE_LITTLEFS2)) +#if defined(MTD_0) && \ + (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || \ + defined(MODULE_LITTLEFS2) || defined(MODULE_FATFS_VFS)) /* spiffs and littlefs need a mtd pointer * by default the whole memory is used */ fs_desc.dev = MTD_0; -#elif defined(MTD_0) && defined(MODULE_FATFS_VFS) - fatfs_mtd_devs[fs_desc.vol_idx] = MTD_0; #endif int res = vfs_mount(&const_mount); if (res < 0) { diff --git a/pkg/fatfs/fatfs_vfs/fatfs_vfs.c b/pkg/fatfs/fatfs_vfs/fatfs_vfs.c index 2ce45fd84e..f19a976055 100644 --- a/pkg/fatfs/fatfs_vfs/fatfs_vfs.c +++ b/pkg/fatfs/fatfs_vfs/fatfs_vfs.c @@ -35,6 +35,8 @@ static int fatfs_err_to_errno(int32_t err); static void _fatfs_time_to_timespec(WORD fdate, WORD ftime, time_t *time); +mtd_dev_t *fatfs_mtd_devs[FF_VOLUMES]; + /** * @brief Concatenate drive number and path into the buffer provided by fs_desc * @@ -46,6 +48,25 @@ static void _build_abs_path(fatfs_desc_t *fs_desc, const char *name) fs_desc->vol_idx, name); } +static int _init(vfs_mount_t *mountp) +{ + fatfs_desc_t *fs_desc = mountp->private_data; + + for (unsigned i = 0; i < ARRAY_SIZE(fatfs_mtd_devs); ++i) { + if (fatfs_mtd_devs[i] == fs_desc->dev) { + /* already initialized */ + return 0; + } + if (fatfs_mtd_devs[i] == NULL) { + fatfs_mtd_devs[i] = fs_desc->dev; + fs_desc->vol_idx = i; + return 0; + } + } + + return -1; +} + static int _mount(vfs_mount_t *mountp) { /* if one of the lines below fail to compile you probably need to adjust @@ -57,6 +78,11 @@ static int _mount(vfs_mount_t *mountp) fatfs_desc_t *fs_desc = (fatfs_desc_t *)mountp->private_data; + if (_init(mountp)) { + DEBUG("can't find free slot in fatfs_mtd_devs\n"); + return -ENOMEM; + } + _build_abs_path(fs_desc, ""); memset(&fs_desc->fat_fs, 0, sizeof(fs_desc->fat_fs)); diff --git a/sys/include/fs/fatfs.h b/sys/include/fs/fatfs.h index bedae9472a..c0b6945cb1 100644 --- a/sys/include/fs/fatfs.h +++ b/sys/include/fs/fatfs.h @@ -54,6 +54,7 @@ extern "C" { */ typedef struct fatfs_desc { FATFS fat_fs; /**< FatFs work area needed for each volume */ + mtd_dev_t *dev; /**< MTD device to use */ uint8_t vol_idx; /**< low level device that is used by FatFs */ /** most FatFs file operations need an absolute path. This buffer provides diff --git a/tests/pkg_fatfs_vfs/main.c b/tests/pkg_fatfs_vfs/main.c index c98786a12d..d9b63ce36d 100644 --- a/tests/pkg_fatfs_vfs/main.c +++ b/tests/pkg_fatfs_vfs/main.c @@ -54,9 +54,7 @@ static const char test_txt[] = "the test file content 123 abc"; static const char test_txt2[] = "another text"; static const char test_txt3[] = "hello world for vfs"; -static fatfs_desc_t fatfs = { - .vol_idx = 0 -}; +static fatfs_desc_t fatfs; static vfs_mount_t _test_vfs_mount = { .mount_point = MNT_PATH, @@ -64,9 +62,6 @@ static vfs_mount_t _test_vfs_mount = { .private_data = (void *)&fatfs, }; -/* provide mtd devices for use within diskio layer of fatfs */ -mtd_dev_t *fatfs_mtd_devs[FF_VOLUMES]; - #if defined(MODULE_MTD_NATIVE) || defined(MODULE_MTD_MCI) /* mtd devices are provided in the board's board_init.c*/ extern mtd_dev_t *mtd0; @@ -402,17 +397,16 @@ int main(void) mtd_sdcard_devs[i].base.driver = &mtd_sdcard_driver; mtd_sdcard_devs[i].sd_card = &sdcard_spi_devs[i]; mtd_sdcard_devs[i].params = &sdcard_spi_params[i]; - fatfs_mtd_devs[i] = &mtd_sdcard_devs[i].base; mtd_init(&mtd_sdcard_devs[i].base); } #endif #if defined(MODULE_MTD_NATIVE) || defined(MODULE_MTD_MCI) - fatfs_mtd_devs[fatfs.vol_idx] = mtd0; + fatfs.dev = mtd0; #endif #if defined(MODULE_MTD_SDCARD) - fatfs_mtd_devs[fatfs.vol_idx] = mtd_sdcard; + fatfs.dev = mtd_sdcard; #endif printf("Tests for FatFs over VFS - test results will be printed "