From 3b2cfa64787465e04343b07c344a2e306e8f5e44 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 2 May 2020 21:45:40 +0200 Subject: [PATCH 1/3] drivers/sdcard_spi: add auto_init_storage to DEFAULT_MODULE sdcard_spi is the only driver in auto_init_storage and was previously pulled in by fatsfs (because it's used on SD cards) It does however make much more sense if the SD card driver pulls that in so other file systems can be used on SD card too. --- drivers/Makefile.dep | 1 + pkg/fatfs/Makefile.dep | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 532ad0fe3f..612b817356 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -628,6 +628,7 @@ endif ifneq (,$(filter sdcard_spi,$(USEMODULE))) FEATURES_REQUIRED += periph_gpio FEATURES_REQUIRED += periph_spi + DEFAULT_MODULE += auto_init_storage USEMODULE += checksum USEMODULE += xtimer endif diff --git a/pkg/fatfs/Makefile.dep b/pkg/fatfs/Makefile.dep index d4a91becd2..38f5ca75e9 100644 --- a/pkg/fatfs/Makefile.dep +++ b/pkg/fatfs/Makefile.dep @@ -1,5 +1,4 @@ USEMODULE += fatfs_diskio_mtd -DEFAULT_MODULE += auto_init_storage USEMODULE += mtd # Use RTC for timestamps if available From 9ada10609fa2eede5b2254d189f69804546978de Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 20 May 2020 11:58:11 +0200 Subject: [PATCH 2/3] examples/filesystem: add support for SD card as MTD backend --- examples/filesystem/Makefile | 1 + examples/filesystem/main.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/examples/filesystem/Makefile b/examples/filesystem/Makefile index 4a174ccbe5..08878353bd 100644 --- a/examples/filesystem/Makefile +++ b/examples/filesystem/Makefile @@ -26,6 +26,7 @@ USEMODULE += ps # Use MTD (flash layer) USEMODULE += mtd +# USEMODULE += mtd_sdcard # Use VFS USEMODULE += vfs diff --git a/examples/filesystem/main.c b/examples/filesystem/main.c index b496a66ecd..7179c953a2 100644 --- a/examples/filesystem/main.c +++ b/examples/filesystem/main.c @@ -26,6 +26,21 @@ #include "shell.h" #include "board.h" /* MTD_0 is defined in board.h */ +#if !defined(MTD_0) && MODULE_MTD_SDCARD +#include "mtd_sdcard.h" +#include "sdcard_spi.h" +#include "sdcard_spi_params.h" + +#define SDCARD_SPI_NUM ARRAY_SIZE(sdcard_spi_params) +extern sdcard_spi_t sdcard_spi_devs[SDCARD_SPI_NUM]; +mtd_sdcard_t mtd_sdcard_devs[SDCARD_SPI_NUM]; + +/* always default to first sdcard*/ +static mtd_dev_t *mtd0 = (mtd_dev_t*)&mtd_sdcard_devs[0]; + +#define MTD_0 mtd0 +#endif + /* Flash mount point */ #define FLASH_MOUNT_POINT "/sda" @@ -244,6 +259,14 @@ static const shell_command_t shell_commands[] = { int main(void) { +#if MODULE_MTD_SDCARD + for (unsigned int i = 0; i < SDCARD_SPI_NUM; i++){ + 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]; + } +#endif + #if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS)) /* spiffs and littlefs need a mtd pointer * by default the whole memory is used */ From ccdbbfa53c550a06a68a90c7896f654669d794eb Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 20 May 2020 11:58:46 +0200 Subject: [PATCH 3/3] examples/filesystem: add support for fatfs --- examples/filesystem/Makefile | 4 ++- examples/filesystem/main.c | 47 ++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/examples/filesystem/Makefile b/examples/filesystem/Makefile index 08878353bd..17dba14a91 100644 --- a/examples/filesystem/Makefile +++ b/examples/filesystem/Makefile @@ -34,7 +34,7 @@ USEMODULE += vfs # Use a file system USEMODULE += littlefs # USEMODULE += spiffs -# USEMODULE += fatfs +# USEMODULE += fatfs_vfs USEMODULE += constfs # USEMODULE += devfs @@ -44,6 +44,8 @@ ifneq (,$(filter littlefs, $(USEMODULE))) else ifneq (,$(filter spiffs, $(USEMODULE))) SPIFFS_NB_FD ?= 8 CFLAGS += '-DSPIFFS_FS_FD_SPACE_SIZE=(32 * $(SPIFFS_NB_FD))' +else ifneq (,$(filter fatfs_vfs, $(USEMODULE))) + CFLAGS += -DVFS_FILE_BUFFER_SIZE=72 -DVFS_DIR_BUFFER_SIZE=44 endif include $(RIOTBASE)/Makefile.include diff --git a/examples/filesystem/main.c b/examples/filesystem/main.c index 7179c953a2..25276df6e7 100644 --- a/examples/filesystem/main.c +++ b/examples/filesystem/main.c @@ -26,18 +26,26 @@ #include "shell.h" #include "board.h" /* MTD_0 is defined in board.h */ +/* Configure MTD device for SD card if none is provided */ #if !defined(MTD_0) && MODULE_MTD_SDCARD #include "mtd_sdcard.h" #include "sdcard_spi.h" #include "sdcard_spi_params.h" #define SDCARD_SPI_NUM ARRAY_SIZE(sdcard_spi_params) + +/* SD card devices are provided by auto_init_sdcard_spi */ extern sdcard_spi_t sdcard_spi_devs[SDCARD_SPI_NUM]; -mtd_sdcard_t mtd_sdcard_devs[SDCARD_SPI_NUM]; - -/* always default to first sdcard*/ -static mtd_dev_t *mtd0 = (mtd_dev_t*)&mtd_sdcard_devs[0]; +/* Configure MTD device for the first SD card */ +static mtd_sdcard_t mtd_sdcard_dev = { + .base = { + .driver = &mtd_sdcard_driver + }, + .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 #endif @@ -77,6 +85,21 @@ static spiffs_desc_t fs_desc = { /* spiffs driver will be used */ #define FS_DRIVER spiffs_file_system + +#elif defined(MODULE_FATFS_VFS) +/* include file system header */ +#include "fs/fatfs.h" + +/* file system specific descriptor + * as for littlefs, some fields can be changed if needed, + * 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 /* this structure defines the vfs mount point: @@ -130,7 +153,7 @@ static int _mount(int argc, char **argv) { (void)argc; (void)argv; -#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS)) +#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || defined(MODULE_FATFS_VFS)) int res = vfs_mount(&flash_mount); if (res < 0) { printf("Error while mounting %s...try format\n", FLASH_MOUNT_POINT); @@ -149,7 +172,7 @@ static int _format(int argc, char **argv) { (void)argc; (void)argv; -#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS)) +#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || defined(MODULE_FATFS_VFS)) int res = vfs_format(&flash_mount); if (res < 0) { printf("Error while formatting %s\n", FLASH_MOUNT_POINT); @@ -168,7 +191,7 @@ static int _umount(int argc, char **argv) { (void)argc; (void)argv; -#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS)) +#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || defined(MODULE_FATFS_VFS)) int res = vfs_umount(&flash_mount); if (res < 0) { printf("Error while unmounting %s\n", FLASH_MOUNT_POINT); @@ -259,18 +282,12 @@ static const shell_command_t shell_commands[] = { int main(void) { -#if MODULE_MTD_SDCARD - for (unsigned int i = 0; i < SDCARD_SPI_NUM; i++){ - 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]; - } -#endif - #if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS)) /* 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) {