1 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
20 #include <sys/unistd.h>
23 #include "esp_system.h"
25 #include "esp_vfs_fat.h"
26 #include "freertos/FreeRTOS.h"
27 #include "freertos/task.h"
28 #include "driver/sdmmc_host.h"
29 #include "driver/sdmmc_defs.h"
30 #include "sdmmc_cmd.h"
33 #include "test_fatfs_common.h"
36 static void test_setup(void)
38 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
39 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
40 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
41 .format_if_mount_failed = true,
43 .allocation_unit_size = 16 * 1024
45 TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
48 static void test_teardown(void)
50 TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
53 static const char* test_filename = "/sdcard/hello.txt";
55 TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][sd][ignore]")
58 HEAP_SIZE_CAPTURE(heap_size);
59 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
60 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
61 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
62 .format_if_mount_failed = false,
66 for (int i = 0; i < 3; ++i) {
67 printf("Initializing card, attempt %d\n", i);
68 esp_err_t err = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL);
69 printf("err=%d\n", err);
70 TEST_ESP_ERR(ESP_ERR_TIMEOUT, err);
72 HEAP_SIZE_CHECK(heap_size, 0);
75 TEST_CASE("(SD) can create and write file", "[fatfs][sd][test_env=UT_T1_SDMODE]")
78 test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
82 TEST_CASE("(SD) can read file", "[fatfs][test_env=UT_T1_SDMODE]")
85 test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
86 test_fatfs_read_file(test_filename);
91 TEST_CASE("(SD) overwrite and append file", "[fatfs][sd][test_env=UT_T1_SDMODE]")
94 test_fatfs_overwrite_append(test_filename);
99 TEST_CASE("(SD) can lseek", "[fatfs][sd][test_env=UT_T1_SDMODE]")
102 test_fatfs_lseek("/sdcard/seek.txt");
106 TEST_CASE("(SD) stat returns correct values", "[fatfs][test_env=UT_T1_SDMODE]")
109 test_fatfs_stat("/sdcard/stat.txt", "/sdcard");
113 TEST_CASE("(SD) unlink removes a file", "[fatfs][test_env=UT_T1_SDMODE]")
116 test_fatfs_unlink("/sdcard/unlink.txt");
120 TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][test_env=UT_T1_SDMODE]")
123 test_fatfs_link_rename("/sdcard/link");
127 TEST_CASE("(SD) can create and remove directories", "[fatfs][test_env=UT_T1_SDMODE]")
130 test_fatfs_mkdir_rmdir("/sdcard/dir");
134 TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][test_env=UT_T1_SDMODE]")
137 test_fatfs_can_opendir("/sdcard");
141 TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][test_env=UT_T1_SDMODE]")
144 test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
148 TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][test_env=UT_T1_SDMODE]")
151 test_fatfs_concurrent("/sdcard/f");
155 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write);
157 TEST_CASE("(SD) write/read speed test", "[fatfs][sd][test_env=UT_T1_SDMODE][timeout=60]")
160 HEAP_SIZE_CAPTURE(heap_size);
162 const size_t buf_size = 16 * 1024;
163 uint32_t* buf = (uint32_t*) calloc(1, buf_size);
164 for (size_t i = 0; i < buf_size / 4; ++i) {
165 buf[i] = esp_random();
167 const size_t file_size = 1 * 1024 * 1024;
169 speed_test(buf, 4 * 1024, file_size, true);
170 speed_test(buf, 8 * 1024, file_size, true);
171 speed_test(buf, 16 * 1024, file_size, true);
173 speed_test(buf, 4 * 1024, file_size, false);
174 speed_test(buf, 8 * 1024, file_size, false);
175 speed_test(buf, 16 * 1024, file_size, false);
179 HEAP_SIZE_CHECK(heap_size, 0);
182 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write)
184 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
185 host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
186 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
187 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
188 .format_if_mount_failed = write,
190 .allocation_unit_size = 64 * 1024
192 TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
194 test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
196 TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
199 TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sd][test_env=UT_T1_SDMODE]")
201 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
202 .format_if_mount_failed = true,
206 const char* filename_sd = "/sdcard/sd.txt";
207 const char* filename_wl = "/spiflash/wl.txt";
208 const char* str_sd = "this is sd\n";
209 const char* str_wl = "this is spiflash\n";
211 /* Mount FATFS in SD can WL at the same time. Create a file on each FS */
212 wl_handle_t wl_handle = WL_INVALID_HANDLE;
214 TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
217 test_fatfs_create_file_with_text(filename_sd, str_sd);
218 test_fatfs_create_file_with_text(filename_wl, str_wl);
219 TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
222 /* Check that the file "sd.txt" was created on FS in SD, and has the right data */
224 TEST_ASSERT_NULL(fopen(filename_wl, "r"));
225 FILE* f = fopen(filename_sd, "r");
226 TEST_ASSERT_NOT_NULL(f);
228 TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
229 TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
233 /* Check that the file "wl.txt" was created on FS in WL, and has the right data */
234 TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
235 TEST_ASSERT_NULL(fopen(filename_sd, "r"));
236 f = fopen(filename_wl, "r");
237 TEST_ASSERT_NOT_NULL(f);
238 TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
239 TEST_ASSERT_EQUAL(0, strcmp(buf, str_wl));
241 TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
245 * In FatFs menuconfig, set CONFIG_FATFS_API_ENCODING to UTF-8 and set the
246 * Codepage to CP936 (Simplified Chinese) in order to run the following tests.
247 * Ensure that the text editor is UTF-8 compatible when compiling these tests.
249 #if defined(CONFIG_FATFS_API_ENCODING_UTF_8) && (CONFIG_FATFS_CODEPAGE == 936)
251 static const char* test_filename_utf_8 = "/sdcard/测试文件.txt";
253 TEST_CASE("(SD) can read file using UTF-8 encoded strings", "[fatfs][sd][test_env=UT_T1_SDMODE]")
256 test_fatfs_create_file_with_text(test_filename_utf_8, fatfs_test_hello_str_utf);
257 test_fatfs_read_file_utf_8(test_filename_utf_8);
261 TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][ignore]")
264 test_fatfs_opendir_readdir_rewinddir_utf_8("/sdcard/目录");
267 #endif // CONFIG_FATFS_API_ENCODING_UTF_8 && CONFIG_FATFS_CODEPAGE == 936