]> granicus.if.org Git - esp-idf/blob - components/fatfs/test/test_fatfs_sdmmc.c
Merge branch 'bugfix/parallel_multi_heap_test' into 'master'
[esp-idf] / components / fatfs / test / test_fatfs_sdmmc.c
1 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <sys/time.h>
20 #include <sys/unistd.h>
21 #include "unity.h"
22 #include "esp_log.h"
23 #include "esp_system.h"
24 #include "esp_vfs.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"
31 #include "diskio.h"
32 #include "ff.h"
33 #include "test_fatfs_common.h"
34
35
36 static void test_setup(void)
37 {
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,
42         .max_files = 5,
43         .allocation_unit_size = 16 * 1024
44     };
45     TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
46 }
47
48 static void test_teardown(void)
49 {
50     TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
51 }
52
53 static const char* test_filename = "/sdcard/hello.txt";
54
55 TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][sd][ignore]")
56 {
57     size_t heap_size;
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,
63         .max_files = 5
64     };
65
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);
71     }
72     HEAP_SIZE_CHECK(heap_size, 0);
73 }
74
75 TEST_CASE("(SD) can create and write file", "[fatfs][sd][test_env=UT_T1_SDMODE]")
76 {
77     test_setup();
78     test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
79     test_teardown();
80 }
81
82 TEST_CASE("(SD) can read file", "[fatfs][test_env=UT_T1_SDMODE]")
83 {
84     test_setup();
85     test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
86     test_fatfs_read_file(test_filename);
87     test_teardown();
88 }
89
90
91 TEST_CASE("(SD) overwrite and append file", "[fatfs][sd][test_env=UT_T1_SDMODE]")
92 {
93     test_setup();
94     test_fatfs_overwrite_append(test_filename);
95     test_teardown();
96 }
97
98
99 TEST_CASE("(SD) can lseek", "[fatfs][sd][test_env=UT_T1_SDMODE]")
100 {
101     test_setup();
102     test_fatfs_lseek("/sdcard/seek.txt");
103     test_teardown();
104 }
105
106 TEST_CASE("(SD) stat returns correct values", "[fatfs][test_env=UT_T1_SDMODE]")
107 {
108     test_setup();
109     test_fatfs_stat("/sdcard/stat.txt", "/sdcard");
110     test_teardown();
111 }
112
113 TEST_CASE("(SD) unlink removes a file", "[fatfs][test_env=UT_T1_SDMODE]")
114 {
115     test_setup();
116     test_fatfs_unlink("/sdcard/unlink.txt");
117     test_teardown();
118 }
119
120 TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][test_env=UT_T1_SDMODE]")
121 {
122     test_setup();
123     test_fatfs_link_rename("/sdcard/link");
124     test_teardown();
125 }
126
127 TEST_CASE("(SD) can create and remove directories", "[fatfs][test_env=UT_T1_SDMODE]")
128 {
129     test_setup();
130     test_fatfs_mkdir_rmdir("/sdcard/dir");
131     test_teardown();
132 }
133
134 TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][test_env=UT_T1_SDMODE]")
135 {
136     test_setup();
137     test_fatfs_can_opendir("/sdcard");
138     test_teardown();
139 }
140
141 TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][test_env=UT_T1_SDMODE]")
142 {
143     test_setup();
144     test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
145     test_teardown();
146 }
147
148 TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][test_env=UT_T1_SDMODE]")
149 {
150     test_setup();
151     test_fatfs_concurrent("/sdcard/f");
152     test_teardown();
153 }
154
155 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write);
156
157 TEST_CASE("(SD) write/read speed test", "[fatfs][sd][test_env=UT_T1_SDMODE][timeout=60]")
158 {
159     size_t heap_size;
160     HEAP_SIZE_CAPTURE(heap_size);
161
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();
166     }
167     const size_t file_size = 1 * 1024 * 1024;
168
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);
172
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);
176
177     free(buf);
178
179     HEAP_SIZE_CHECK(heap_size, 0);
180 }
181
182 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write)
183 {
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,
189         .max_files = 5,
190         .allocation_unit_size = 64 * 1024
191     };
192     TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
193
194     test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
195
196     TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
197 }
198
199 TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sd][test_env=UT_T1_SDMODE]")
200 {
201     esp_vfs_fat_sdmmc_mount_config_t mount_config = {
202         .format_if_mount_failed = true,
203         .max_files = 5
204     };
205
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";
210
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;
213     test_setup();
214     TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
215     unlink(filename_sd);
216     unlink(filename_wl);
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));
220     test_teardown();
221
222     /* Check that the file "sd.txt" was created on FS in SD, and has the right data */
223     test_setup();
224     TEST_ASSERT_NULL(fopen(filename_wl, "r"));
225     FILE* f = fopen(filename_sd, "r");
226     TEST_ASSERT_NOT_NULL(f);
227     char buf[64];
228     TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
229     TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
230     fclose(f);
231     test_teardown();
232
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));
240     fclose(f);
241     TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
242 }
243
244 /*
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.
248  */
249 #if defined(CONFIG_FATFS_API_ENCODING_UTF_8) && (CONFIG_FATFS_CODEPAGE == 936)
250
251 static const char* test_filename_utf_8 = "/sdcard/测试文件.txt";
252
253 TEST_CASE("(SD) can read file using UTF-8 encoded strings", "[fatfs][sd][test_env=UT_T1_SDMODE]")
254 {
255     test_setup();
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);
258     test_teardown();
259 }
260
261 TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][ignore]")
262 {
263     test_setup();
264     test_fatfs_opendir_readdir_rewinddir_utf_8("/sdcard/目录");
265     test_teardown();
266 }
267 #endif // CONFIG_FATFS_API_ENCODING_UTF_8 && CONFIG_FATFS_CODEPAGE == 936