]> granicus.if.org Git - esp-idf/blob - components/fatfs/test/test_fatfs_sdmmc.c
Merge branch 'feature/sysview_via_apptrace' 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     };
44     TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
45 }
46
47 static void test_teardown(void)
48 {
49     TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
50 }
51
52 static const char* test_filename = "/sdcard/hello.txt";
53
54 TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][ignore]")
55 {
56     size_t heap_size;
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,
62         .max_files = 5
63     };
64
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);
70     }
71     HEAP_SIZE_CHECK(heap_size, 0);
72 }
73
74 TEST_CASE("(SD) can create and write file", "[fatfs][sdcard][ignore]")
75 {
76     test_setup();
77     test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
78     test_teardown();
79 }
80
81 TEST_CASE("(SD) can read file", "[fatfs][ignore]")
82 {
83     test_setup();
84     test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
85     test_fatfs_read_file(test_filename);
86     test_teardown();
87 }
88
89
90 TEST_CASE("(SD) overwrite and append file", "[fatfs][sdcard][ignore]")
91 {
92     test_setup();
93     test_fatfs_overwrite_append(test_filename);
94     test_teardown();
95 }
96
97
98 TEST_CASE("(SD) can lseek", "[fatfs][sdcard][ignore]")
99 {
100     test_setup();
101     test_fatfs_lseek("/sdcard/seek.txt");
102     test_teardown();
103 }
104
105 TEST_CASE("(SD) stat returns correct values", "[fatfs][ignore]")
106 {
107     test_setup();
108     test_fatfs_stat("/sdcard/stat.txt");
109     test_teardown();
110 }
111
112 TEST_CASE("(SD) unlink removes a file", "[fatfs][ignore]")
113 {
114     test_setup();
115     test_fatfs_unlink("/sdcard/unlink.txt");
116     test_teardown();
117 }
118
119 TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][ignore]")
120 {
121     test_setup();
122     test_fatfs_link_rename("/sdcard/link");
123     test_teardown();
124 }
125
126 TEST_CASE("(SD) can create and remove directories", "[fatfs][ignore]")
127 {
128     test_setup();
129     test_fatfs_mkdir_rmdir("/sdcard/dir");
130     test_teardown();
131 }
132
133 TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][ignore]")
134 {
135     test_setup();
136     test_fatfs_can_opendir("/sdcard");
137     test_teardown();
138 }
139
140 TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][ignore]")
141 {
142     test_setup();
143     test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
144     test_teardown();
145 }
146
147 TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][ignore]")
148 {
149     test_setup();
150     test_fatfs_concurrent("/sdcard/f");
151     test_teardown();
152 }
153
154 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write);
155
156 TEST_CASE("(SD) write/read speed test", "[fatfs][sdcard][ignore]")
157 {
158     size_t heap_size;
159     HEAP_SIZE_CAPTURE(heap_size);
160
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();
165     }
166     const size_t file_size = 4 * 1024 * 1024;
167
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);
171
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);
175
176     free(buf);
177
178     HEAP_SIZE_CHECK(heap_size, 0);
179 }
180
181 static void speed_test(void* buf, size_t buf_size, size_t file_size, bool write)
182 {
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,
188         .max_files = 5
189     };
190     TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
191
192     test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
193
194     TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
195 }
196
197 TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sdcard][ignore]")
198 {
199     esp_vfs_fat_sdmmc_mount_config_t mount_config = {
200         .format_if_mount_failed = true,
201         .max_files = 5
202     };
203
204     const char* filename_sd = "/sdcard/sd.txt";
205     const char* filename_wl = "/spiflash/wl.txt";
206     const char* str_sd = "this is sd\n";
207     const char* str_wl = "this is spiflash\n";
208
209     /* Mount FATFS in SD can WL at the same time. Create a file on each FS */
210     wl_handle_t wl_handle = WL_INVALID_HANDLE;
211     test_setup();
212     TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
213     unlink(filename_sd);
214     unlink(filename_wl);
215     test_fatfs_create_file_with_text(filename_sd, str_sd);
216     test_fatfs_create_file_with_text(filename_wl, str_wl);
217     TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
218     test_teardown();
219
220     /* Check that the file "sd.txt" was created on FS in SD, and has the right data */
221     test_setup();
222     TEST_ASSERT_NULL(fopen(filename_wl, "r"));
223     FILE* f = fopen(filename_sd, "r");
224     TEST_ASSERT_NOT_NULL(f);
225     char buf[64];
226     TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
227     TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
228     fclose(f);
229     test_teardown();
230
231     /* Check that the file "wl.txt" was created on FS in WL, and has the right data */
232     TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &wl_handle));
233     TEST_ASSERT_NULL(fopen(filename_sd, "r"));
234     f = fopen(filename_wl, "r");
235     TEST_ASSERT_NOT_NULL(f);
236     TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
237     TEST_ASSERT_EQUAL(0, strcmp(buf, str_wl));
238     fclose(f);
239     TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
240 }