2022-01-12 01:53:47 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-05-04 04:48:44 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include "unity.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
#include "esp_log.h"
|
2022-01-12 01:53:47 -05:00
|
|
|
#include "esp_random.h"
|
2017-05-04 04:48:44 -04:00
|
|
|
#include "esp_vfs.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "test_fatfs_common.h"
|
|
|
|
#include "wear_levelling.h"
|
|
|
|
#include "esp_partition.h"
|
2022-07-21 07:14:41 -04:00
|
|
|
#include "esp_memory_utils.h"
|
2017-05-04 04:48:44 -04:00
|
|
|
|
|
|
|
|
2022-05-20 06:16:47 -04:00
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
|
|
|
//IDF-5136
|
2017-05-04 04:48:44 -04:00
|
|
|
static wl_handle_t s_test_wl_handle;
|
2019-07-16 05:33:30 -04:00
|
|
|
static void test_setup(void)
|
2017-05-04 04:48:44 -04:00
|
|
|
{
|
|
|
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
|
|
|
.format_if_mount_failed = true,
|
|
|
|
.max_files = 5
|
|
|
|
};
|
|
|
|
|
2022-03-04 14:15:49 -05:00
|
|
|
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &s_test_wl_handle));
|
2017-05-04 04:48:44 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void test_teardown(void)
|
2017-05-04 04:48:44 -04:00
|
|
|
{
|
2022-03-04 14:15:49 -05:00
|
|
|
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", s_test_wl_handle));
|
2017-05-04 04:48:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) can format partition", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
const esp_partition_t* part = get_test_data_partition();
|
|
|
|
esp_partition_erase_range(part, 0, part->size);
|
|
|
|
test_setup();
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) can create and write file", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_create_file_with_text("/spiflash/hello.txt", fatfs_test_hello_str);
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) can read file", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_create_file_with_text("/spiflash/hello.txt", fatfs_test_hello_str);
|
|
|
|
test_fatfs_read_file("/spiflash/hello.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2019-07-01 05:08:57 -04:00
|
|
|
TEST_CASE("(WL) can read file with pread", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_create_file_with_text("/spiflash/hello.txt", fatfs_test_hello_str);
|
|
|
|
test_fatfs_pread_file("/spiflash/hello.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) pwrite() works well", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_pwrite_file("/spiflash/hello.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:07:38 -04:00
|
|
|
TEST_CASE("(WL) can open maximum number of files", "[fatfs][wear_levelling]")
|
2017-05-04 04:48:44 -04:00
|
|
|
{
|
|
|
|
size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
|
|
|
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
|
|
|
.format_if_mount_failed = true,
|
|
|
|
.max_files = max_files
|
|
|
|
};
|
2022-03-04 14:15:49 -05:00
|
|
|
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &s_test_wl_handle));
|
2017-05-04 04:48:44 -04:00
|
|
|
test_fatfs_open_max_files("/spiflash/f", max_files);
|
2022-03-04 14:15:49 -05:00
|
|
|
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", s_test_wl_handle));
|
2017-05-04 04:48:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) overwrite and append file", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_overwrite_append("/spiflash/hello.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) can lseek", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_lseek("/spiflash/seek.txt");
|
|
|
|
test_teardown();
|
2018-06-12 06:29:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) can truncate", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_truncate_file("/spiflash/truncate.txt");
|
|
|
|
test_teardown();
|
2017-05-04 04:48:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) stat returns correct values", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
2017-12-08 06:58:39 -05:00
|
|
|
test_fatfs_stat("/spiflash/stat.txt", "/spiflash");
|
2017-05-04 04:48:44 -04:00
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:34:29 -04:00
|
|
|
TEST_CASE("(WL) stat returns correct mtime if DST is enabled", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_mtime_dst("/spiflash/statdst.txt", "/spiflash");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2018-10-25 05:53:52 -04:00
|
|
|
TEST_CASE("(WL) utime sets modification time", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_utime("/spiflash/utime.txt", "/spiflash");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2017-05-04 04:48:44 -04:00
|
|
|
TEST_CASE("(WL) unlink removes a file", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_unlink("/spiflash/unlink.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) link copies a file, rename moves a file", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_link_rename("/spiflash/link");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) can create and remove directories", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_mkdir_rmdir("/spiflash/dir");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2017-06-20 13:21:14 -04:00
|
|
|
TEST_CASE("(WL) can opendir root directory of FS", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_can_opendir("/spiflash");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2017-05-04 04:48:44 -04:00
|
|
|
TEST_CASE("(WL) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_opendir_readdir_rewinddir("/spiflash/dir");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:07:38 -04:00
|
|
|
TEST_CASE("(WL) multiple tasks can use same volume", "[fatfs][wear_levelling]")
|
2017-05-04 04:48:44 -04:00
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_concurrent("/spiflash/f");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:07:38 -04:00
|
|
|
TEST_CASE("(WL) write/read speed test", "[fatfs][wear_levelling][timeout=60]")
|
2017-05-04 04:48:44 -04:00
|
|
|
{
|
|
|
|
/* Erase partition before running the test to get consistent results */
|
|
|
|
const esp_partition_t* part = get_test_data_partition();
|
|
|
|
esp_partition_erase_range(part, 0, part->size);
|
|
|
|
|
|
|
|
test_setup();
|
|
|
|
|
|
|
|
const size_t buf_size = 16 * 1024;
|
|
|
|
uint32_t* buf = (uint32_t*) calloc(1, buf_size);
|
2018-08-15 04:20:16 -04:00
|
|
|
esp_fill_random(buf, buf_size);
|
2017-05-04 04:48:44 -04:00
|
|
|
const size_t file_size = 256 * 1024;
|
|
|
|
const char* file = "/spiflash/256k.bin";
|
|
|
|
|
|
|
|
test_fatfs_rw_speed(file, buf, 4 * 1024, file_size, true);
|
|
|
|
test_fatfs_rw_speed(file, buf, 8 * 1024, file_size, true);
|
|
|
|
test_fatfs_rw_speed(file, buf, 16 * 1024, file_size, true);
|
|
|
|
|
|
|
|
test_fatfs_rw_speed(file, buf, 4 * 1024, file_size, false);
|
|
|
|
test_fatfs_rw_speed(file, buf, 8 * 1024, file_size, false);
|
|
|
|
test_fatfs_rw_speed(file, buf, 16 * 1024, file_size, false);
|
|
|
|
|
|
|
|
unlink(file);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
test_teardown();
|
|
|
|
}
|
2017-11-30 07:57:37 -05:00
|
|
|
|
2022-04-13 08:11:58 -04:00
|
|
|
TEST_CASE("(WL) can get partition info", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_info("/spiflash", "/spiflash/test.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
2022-05-20 06:16:47 -04:00
|
|
|
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
2022-04-13 08:11:58 -04:00
|
|
|
|
2017-11-30 07:57:37 -05:00
|
|
|
/*
|
|
|
|
* In FatFs menuconfig, set CONFIG_FATFS_API_ENCODING to UTF-8 and set the
|
|
|
|
* Codepage to CP936 (Simplified Chinese) in order to run the following tests.
|
|
|
|
* Ensure that the text editor is UTF-8 compatible when compiling these tests.
|
|
|
|
*/
|
|
|
|
#if defined(CONFIG_FATFS_API_ENCODING_UTF_8) && (CONFIG_FATFS_CODEPAGE == 936)
|
2022-05-20 06:16:47 -04:00
|
|
|
|
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
|
|
|
//IDF-5136
|
2017-11-30 07:57:37 -05:00
|
|
|
TEST_CASE("(WL) can read file with UTF-8 encoded strings", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_create_file_with_text("/spiflash/测试文件.txt", fatfs_test_hello_str_utf);
|
|
|
|
test_fatfs_read_file_utf_8("/spiflash/测试文件.txt");
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("(WL) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][wear_levelling]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
test_fatfs_opendir_readdir_rewinddir_utf_8("/spiflash/目录");
|
|
|
|
test_teardown();
|
|
|
|
}
|
2022-05-20 06:16:47 -04:00
|
|
|
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
|
|
|
#endif //defined(CONFIG_FATFS_API_ENCODING_UTF_8) && (CONFIG_FATFS_CODEPAGE == 936)
|
2020-09-09 19:04:32 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_SPIRAM
|
|
|
|
TEST_CASE("FATFS prefers SPI RAM for allocations", "[fatfs]")
|
|
|
|
{
|
|
|
|
test_setup();
|
|
|
|
DIR* dir = opendir("/spiflash");
|
|
|
|
TEST_ASSERT_NOT_NULL(dir);
|
|
|
|
TEST_ASSERT(esp_ptr_external_ram(dir));
|
|
|
|
closedir(dir);
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
#endif // CONFIG_SPIRAM
|