Skip to content

Commit

Permalink
Merge branch 'feature/fatfs_format_api' into 'master'
Browse files Browse the repository at this point in the history
fatfs: added APIs to format a FAT filesystem

Closes IDF-6046

See merge request espressif/esp-idf!21845
  • Loading branch information
pacucha42 committed Feb 16, 2023
2 parents 045163a + d59fd3e commit c58a84f
Show file tree
Hide file tree
Showing 17 changed files with 818 additions and 270 deletions.
15 changes: 11 additions & 4 deletions components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c
Expand Up @@ -33,7 +33,7 @@ static void test_setup(void)
{
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5
.max_files = 5,
};

TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &s_test_wl_handle));
Expand All @@ -46,10 +46,17 @@ static void test_teardown(void)

TEST_CASE("(WL) can format partition", "[fatfs][wear_levelling]")
{
const esp_partition_t* part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
esp_partition_erase_range(part, 0, part->size);
TEST_ESP_OK(esp_vfs_fat_spiflash_format_rw_wl("/spiflash", NULL));
test_setup();
test_teardown();
}

TEST_CASE("(WL) can format when the FAT is mounted already", "[fatfs][wear_levelling]")
{
test_setup();
TEST_ESP_OK(esp_vfs_fat_spiflash_format_rw_wl("/spiflash", NULL));
test_fatfs_create_file_with_text("/spiflash/hello.txt", fatfs_test_hello_str);
test_fatfs_pread_file("/spiflash/hello.txt");
test_teardown();
}

Expand Down
129 changes: 81 additions & 48 deletions components/fatfs/test_apps/sdcard/main/test_fatfs_sdmmc.c
Expand Up @@ -51,21 +51,23 @@
// No runner
#include "driver/sdmmc_host.h"

static void test_setup_sdmmc(void)
static void test_setup_sdmmc(sdmmc_card_t **out_card)
{
sdmmc_card_t *card = NULL;
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));
*out_card = card;
}

static void test_teardown_sdmmc(void)
static void test_teardown_sdmmc(sdmmc_card_t *card)
{
TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
}

static const char* test_filename = "/sdcard/hello.txt";
Expand All @@ -90,118 +92,144 @@ TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][ignore]")
HEAP_SIZE_CHECK(heap_size, 0);
}

TEST_CASE("(SD) can format partition", "[fatfs][sdmmc]")
{
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_fatfs_read_file(test_filename);
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can create and write file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can read file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_fatfs_read_file(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can read file with pread()", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_fatfs_pread_file(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) pwrite() works well", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_pwrite_file(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) overwrite and append file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_overwrite_append(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can lseek", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_lseek("/sdcard/seek.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can truncate", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_truncate_file("/sdcard/truncate.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can ftruncate", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_ftruncate_file("/sdcard/ftrunc.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) stat returns correct values", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_stat("/sdcard/stat.txt", "/sdcard");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) utime sets modification time", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_utime("/sdcard/utime.txt", "/sdcard");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) unlink removes a file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_unlink("/sdcard/unlink.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_link_rename("/sdcard/link");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can create and remove directories", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_mkdir_rmdir("/sdcard/dir");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_can_opendir("/sdcard");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_concurrent("/sdcard/f");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool write);
Expand Down Expand Up @@ -231,6 +259,7 @@ TEST_CASE("(SD) write/read speed test", "[fatfs][sdmmc]")

static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool write)
{
sdmmc_card_t *card = NULL;
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
Expand All @@ -239,11 +268,11 @@ static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool
.max_files = 5,
.allocation_unit_size = 64 * 1024
};
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));

test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);

TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
}

TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sdmmc]")
Expand All @@ -265,25 +294,26 @@ TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fat

/* Mount FATFS in SD can WL at the same time. Create a file on each FS */
wl_handle_t wl_handle = WL_INVALID_HANDLE;
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
unlink(filename_sd);
unlink(filename_wl);
test_fatfs_create_file_with_text(filename_sd, str_sd);
test_fatfs_create_file_with_text(filename_wl, str_wl);
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle));
test_teardown_sdmmc();
test_teardown_sdmmc(card);

/* Check that the file "sd.txt" was created on FS in SD, and has the right data */
test_setup_sdmmc();
test_setup_sdmmc(&card);
TEST_ASSERT_NULL(fopen(filename_wl, "r"));
FILE* f = fopen(filename_sd, "r");
TEST_ASSERT_NOT_NULL(f);
char buf[64];
TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
fclose(f);
test_teardown_sdmmc();
test_teardown_sdmmc(card);

/* Check that the file "wl.txt" was created on FS in WL, and has the right data */
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
Expand All @@ -307,25 +337,28 @@ static const char* test_filename_utf_8 = "/sdcard/测试文件.txt";

TEST_CASE("(SD) can read file using UTF-8 encoded strings", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename_utf_8, fatfs_test_hello_str_utf);
test_fatfs_read_file_utf_8(test_filename_utf_8);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][ignore]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_opendir_readdir_rewinddir_utf_8("/sdcard/目录");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
#endif // CONFIG_FATFS_API_ENCODING_UTF_8 && CONFIG_FATFS_CODEPAGE == 936

TEST_CASE("(SD) can get partition info", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_info("/sdcard", "/sdcard/test.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}

#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
32 changes: 32 additions & 0 deletions components/fatfs/test_apps/sdcard/main/test_fatfs_sdspi.c
Expand Up @@ -53,6 +53,7 @@ typedef struct sdspi_mem {
uint32_t* buf;
} sdspi_mem_t;

static const char* s_test_filename = "/sdcard/hello.txt";
static void sdspi_speed_test(void *buf, size_t buf_size, size_t file_size, bool write);

static void test_setup_sdspi(sdspi_mem_t* mem)
Expand Down Expand Up @@ -159,3 +160,34 @@ TEST_CASE("(SDSPI) can get partition info", "[fatfs][sdspi]")

test_teardown_sdspi(&mem);
}

TEST_CASE("(SDSPI) can format card", "[fatfs][sdspi]")
{
sdspi_mem_t mem;
test_setup_sdspi(&mem);

const char path[] = "/sdcard";
sdmmc_card_t *card;
card = NULL;
sdspi_device_config_t device_cfg = {
.gpio_cs = SDSPI_CS_PIN,
.host_id = SDSPI_HOST_ID,
.gpio_cd = SDSPI_SLOT_NO_CD,
.gpio_wp = SDSPI_SLOT_NO_WP,
.gpio_int = SDSPI_SLOT_NO_INT,
};

sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.slot = SDSPI_HOST_ID;
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = 64 * 1024
};
TEST_ESP_OK(esp_vfs_fat_sdspi_mount(path, &host, &device_cfg, &mount_config, &card));
TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
test_fatfs_create_file_with_text(s_test_filename, fatfs_test_hello_str);
test_fatfs_read_file(s_test_filename);
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
test_teardown_sdspi(&mem);
}

0 comments on commit c58a84f

Please sign in to comment.