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,
44 TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
47 static void test_teardown(void)
49 TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
52 static const char* test_filename = "/sdcard/hello.txt";
54 TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][ignore]")
57 HEAP_SIZE_CAPTURE(heap_size);
58 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
59 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
60 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
61 .format_if_mount_failed = false,
65 for (int i = 0; i < 3; ++i) {
66 printf("Initializing card, attempt %d ", i);
67 esp_err_t err = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL);
68 printf(" err=%d\n", err);
69 TEST_ESP_ERR(ESP_FAIL, err);
71 HEAP_SIZE_CHECK(heap_size, 0);
74 TEST_CASE("(SD) can create and write file", "[fatfs][sdcard][ignore]")
77 test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
81 TEST_CASE("(SD) can read file", "[fatfs][ignore]")
84 test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
85 test_fatfs_read_file(test_filename);
90 TEST_CASE("(SD) overwrite and append file", "[fatfs][sdcard][ignore]")
93 test_fatfs_overwrite_append(test_filename);
98 TEST_CASE("(SD) can lseek", "[fatfs][sdcard][ignore]")
101 test_fatfs_lseek("/sdcard/seek.txt");
105 TEST_CASE("(SD) stat returns correct values", "[fatfs][ignore]")
108 test_fatfs_stat("/sdcard/stat.txt", "/sdcard");
112 TEST_CASE("(SD) unlink removes a file", "[fatfs][ignore]")
115 test_fatfs_unlink("/sdcard/unlink.txt");
119 TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][ignore]")
122 test_fatfs_link_rename("/sdcard/link");
126 TEST_CASE("(SD) can create and remove directories", "[fatfs][ignore]")
129 test_fatfs_mkdir_rmdir("/sdcard/dir");
133 TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][ignore]")
136 test_fatfs_can_opendir("/sdcard");
140 TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][ignore]")
143 test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
147 TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][ignore]")
150 test_fatfs_concurrent("/sdcard/f");
154 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write);
156 TEST_CASE("(SD) write/read speed test", "[fatfs][sdcard][ignore]")
159 HEAP_SIZE_CAPTURE(heap_size);
161 const size_t buf_size = 16 * 1024;
162 uint32_t* buf = (uint32_t*) calloc(1, buf_size);
163 for (size_t i = 0; i < buf_size / 4; ++i) {
164 buf[i] = esp_random();
166 const size_t file_size = 4 * 1024 * 1024;
168 speed_test(buf, 4 * 1024, file_size, true);
169 speed_test(buf, 8 * 1024, file_size, true);
170 speed_test(buf, 16 * 1024, file_size, true);
172 speed_test(buf, 4 * 1024, file_size, false);
173 speed_test(buf, 8 * 1024, file_size, false);
174 speed_test(buf, 16 * 1024, file_size, false);
178 HEAP_SIZE_CHECK(heap_size, 0);
181 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write)
183 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
184 host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
185 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
186 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
187 .format_if_mount_failed = write,
189 .allocation_unit_size = 64 * 1024
191 TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
193 test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
195 TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
198 TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sdcard][ignore]")
200 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
201 .format_if_mount_failed = true,
205 const char* filename_sd = "/sdcard/sd.txt";
206 const char* filename_wl = "/spiflash/wl.txt";
207 const char* str_sd = "this is sd\n";
208 const char* str_wl = "this is spiflash\n";
210 /* Mount FATFS in SD can WL at the same time. Create a file on each FS */
211 wl_handle_t wl_handle = WL_INVALID_HANDLE;
213 TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
216 test_fatfs_create_file_with_text(filename_sd, str_sd);
217 test_fatfs_create_file_with_text(filename_wl, str_wl);
218 TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
221 /* Check that the file "sd.txt" was created on FS in SD, and has the right data */
223 TEST_ASSERT_NULL(fopen(filename_wl, "r"));
224 FILE* f = fopen(filename_sd, "r");
225 TEST_ASSERT_NOT_NULL(f);
227 TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
228 TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
232 /* Check that the file "wl.txt" was created on FS in WL, and has the right data */
233 TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
234 TEST_ASSERT_NULL(fopen(filename_sd, "r"));
235 f = fopen(filename_wl, "r");
236 TEST_ASSERT_NOT_NULL(f);
237 TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
238 TEST_ASSERT_EQUAL(0, strcmp(buf, str_wl));
240 TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));