test_fatfs_rawflash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Copyright 2015-2018 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. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include <sys/unistd.h>
  20. #include "unity.h"
  21. #include "test_utils.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 "test_fatfs_common.h"
  29. #include "esp_partition.h"
  30. #include "ff.h"
  31. static void test_setup(size_t max_files)
  32. {
  33. extern const char fatfs_start[] asm("_binary_fatfs_img_start");
  34. extern const char fatfs_end[] asm("_binary_fatfs_img_end");
  35. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  36. .format_if_mount_failed = false,
  37. .max_files = max_files
  38. };
  39. const esp_partition_t* part = get_test_data_partition();
  40. TEST_ASSERT(part->size == (fatfs_end - fatfs_start - 1));
  41. spi_flash_mmap_handle_t mmap_handle;
  42. const void* mmap_ptr;
  43. TEST_ESP_OK(esp_partition_mmap(part, 0, part->size, SPI_FLASH_MMAP_DATA, &mmap_ptr, &mmap_handle));
  44. bool content_valid = memcmp(fatfs_start, mmap_ptr, part->size) == 0;
  45. spi_flash_munmap(mmap_handle);
  46. if (!content_valid) {
  47. printf("Copying fatfs.img into test partition...\n");
  48. esp_partition_erase_range(part, 0, part->size);
  49. for (int i = 0; i < part->size; i+= SPI_FLASH_SEC_SIZE) {
  50. ESP_ERROR_CHECK( esp_partition_write(part, i, fatfs_start + i, SPI_FLASH_SEC_SIZE) );
  51. }
  52. }
  53. TEST_ESP_OK(esp_vfs_fat_rawflash_mount("/spiflash", "flash_test", &mount_config));
  54. }
  55. static void test_teardown(void)
  56. {
  57. TEST_ESP_OK(esp_vfs_fat_rawflash_unmount("/spiflash","flash_test"));
  58. }
  59. TEST_CASE("(raw) can read file", "[fatfs]")
  60. {
  61. test_setup(5);
  62. FILE* f = fopen("/spiflash/hello.txt", "r");
  63. TEST_ASSERT_NOT_NULL(f);
  64. char buf[32] = { 0 };
  65. int cb = fread(buf, 1, sizeof(buf), f);
  66. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), cb);
  67. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  68. TEST_ASSERT_EQUAL(0, fclose(f));
  69. test_teardown();
  70. }
  71. TEST_CASE("(raw) can open maximum number of files", "[fatfs]")
  72. {
  73. size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
  74. test_setup(max_files);
  75. FILE** files = calloc(max_files, sizeof(FILE*));
  76. for (size_t i = 0; i < max_files; ++i) {
  77. char name[32];
  78. snprintf(name, sizeof(name), "/spiflash/f/%d.txt", i + 1);
  79. files[i] = fopen(name, "r");
  80. TEST_ASSERT_NOT_NULL(files[i]);
  81. }
  82. /* close everything and clean up */
  83. for (size_t i = 0; i < max_files; ++i) {
  84. fclose(files[i]);
  85. }
  86. free(files);
  87. test_teardown();
  88. }
  89. TEST_CASE("(raw) can lseek", "[fatfs]")
  90. {
  91. test_setup(5);
  92. FILE* f = fopen("/spiflash/hello.txt", "r");
  93. TEST_ASSERT_NOT_NULL(f);
  94. TEST_ASSERT_EQUAL(0, fseek(f, 2, SEEK_CUR));
  95. TEST_ASSERT_EQUAL('l', fgetc(f));
  96. TEST_ASSERT_EQUAL(0, fseek(f, 4, SEEK_SET));
  97. TEST_ASSERT_EQUAL('o', fgetc(f));
  98. TEST_ASSERT_EQUAL(0, fseek(f, -5, SEEK_END));
  99. TEST_ASSERT_EQUAL('r', fgetc(f));
  100. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_END));
  101. TEST_ASSERT_EQUAL(17, ftell(f));
  102. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  103. TEST_ASSERT_EQUAL(14, ftell(f));
  104. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET));
  105. test_teardown();
  106. }
  107. TEST_CASE("(raw) stat returns correct values", "[fatfs]")
  108. {
  109. test_setup(5);
  110. struct tm tm;
  111. tm.tm_year = 2018 - 1900;
  112. tm.tm_mon = 5; // Note: month can be 0-11 & not 1-12
  113. tm.tm_mday = 13;
  114. tm.tm_hour = 11;
  115. tm.tm_min = 2;
  116. tm.tm_sec = 10;
  117. time_t t = mktime(&tm);
  118. printf("Reference time: %s", asctime(&tm));
  119. struct stat st;
  120. TEST_ASSERT_EQUAL(0, stat("/spiflash/stat.txt", &st));
  121. time_t mtime = st.st_mtime;
  122. struct tm mtm;
  123. localtime_r(&mtime, &mtm);
  124. printf("File time: %s", asctime(&mtm));
  125. TEST_ASSERT(mtime > t); // Modification time should be in future wrt ref time
  126. TEST_ASSERT(st.st_mode & S_IFREG);
  127. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  128. memset(&st, 0, sizeof(st));
  129. TEST_ASSERT_EQUAL(0, stat("/spiflash", &st));
  130. TEST_ASSERT(st.st_mode & S_IFDIR);
  131. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  132. test_teardown();
  133. }
  134. TEST_CASE("(raw) can opendir root directory of FS", "[fatfs]")
  135. {
  136. test_setup(5);
  137. DIR* dir = opendir("/spiflash");
  138. TEST_ASSERT_NOT_NULL(dir);
  139. bool found = false;
  140. while (true) {
  141. struct dirent* de = readdir(dir);
  142. if (!de) {
  143. break;
  144. }
  145. if (strcasecmp(de->d_name, "test_opd.txt") == 0) {
  146. found = true;
  147. break;
  148. }
  149. }
  150. TEST_ASSERT_TRUE(found);
  151. TEST_ASSERT_EQUAL(0, closedir(dir));
  152. test_teardown();
  153. }
  154. TEST_CASE("(raw) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs]")
  155. {
  156. test_setup(5);
  157. DIR* dir = opendir("/spiflash/dir");
  158. TEST_ASSERT_NOT_NULL(dir);
  159. int count = 0;
  160. const char* names[4];
  161. while(count < 4) {
  162. struct dirent* de = readdir(dir);
  163. if (!de) {
  164. break;
  165. }
  166. printf("found '%s'\n", de->d_name);
  167. if (strcasecmp(de->d_name, "1.txt") == 0) {
  168. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  169. names[count] = "1.txt";
  170. ++count;
  171. } else if (strcasecmp(de->d_name, "2.txt") == 0) {
  172. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  173. names[count] = "2.txt";
  174. ++count;
  175. } else if (strcasecmp(de->d_name, "inner") == 0) {
  176. TEST_ASSERT_TRUE(de->d_type == DT_DIR);
  177. names[count] = "inner";
  178. ++count;
  179. } else if (strcasecmp(de->d_name, "boo.bin") == 0) {
  180. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  181. names[count] = "boo.bin";
  182. ++count;
  183. } else {
  184. TEST_FAIL_MESSAGE("unexpected directory entry");
  185. }
  186. }
  187. TEST_ASSERT_EQUAL(count, 4);
  188. rewinddir(dir);
  189. struct dirent* de = readdir(dir);
  190. TEST_ASSERT_NOT_NULL(de);
  191. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  192. seekdir(dir, 3);
  193. de = readdir(dir);
  194. TEST_ASSERT_NOT_NULL(de);
  195. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  196. seekdir(dir, 1);
  197. de = readdir(dir);
  198. TEST_ASSERT_NOT_NULL(de);
  199. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  200. seekdir(dir, 2);
  201. de = readdir(dir);
  202. TEST_ASSERT_NOT_NULL(de);
  203. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  204. TEST_ASSERT_EQUAL(0, closedir(dir));
  205. test_teardown();
  206. }
  207. typedef struct {
  208. const char* filename;
  209. size_t word_count;
  210. int seed;
  211. int val;
  212. SemaphoreHandle_t done;
  213. int result;
  214. } read_test_arg_t;
  215. #define READ_TEST_ARG_INIT(name, seed_, val_) \
  216. { \
  217. .filename = name, \
  218. .seed = seed_, \
  219. .word_count = 8000, \
  220. .val = val_, \
  221. .done = xSemaphoreCreateBinary() \
  222. }
  223. static void read_task(void* param)
  224. {
  225. read_test_arg_t* args = (read_test_arg_t*) param;
  226. FILE* f = fopen(args->filename, "rb");
  227. if (f == NULL) {
  228. args->result = ESP_ERR_NOT_FOUND;
  229. goto done;
  230. }
  231. srand(args->seed);
  232. for (size_t i = 0; i < args->word_count; ++i) {
  233. uint32_t rval;
  234. int cnt = fread(&rval, sizeof(rval), 1, f);
  235. if (cnt != 1 || rval != args->val) {
  236. ets_printf("E(r): i=%d, cnt=%d rval=%d val=%d\n\n", i, cnt, rval, args->val);
  237. args->result = ESP_FAIL;
  238. goto close;
  239. }
  240. }
  241. args->result = ESP_OK;
  242. close:
  243. fclose(f);
  244. done:
  245. xSemaphoreGive(args->done);
  246. vTaskDelay(1);
  247. vTaskDelete(NULL);
  248. }
  249. TEST_CASE("(raw) multiple tasks can use same volume", "[fatfs]")
  250. {
  251. test_setup(5);
  252. char names[4][64];
  253. for (size_t i = 0; i < 4; ++i) {
  254. snprintf(names[i], sizeof(names[i]), "/spiflash/ccrnt/%d.txt", i + 1);
  255. }
  256. read_test_arg_t args1 = READ_TEST_ARG_INIT(names[0], 1, 0x31313131);
  257. read_test_arg_t args2 = READ_TEST_ARG_INIT(names[1], 2, 0x32323232);
  258. read_test_arg_t args3 = READ_TEST_ARG_INIT(names[2], 3, 0x33333333);
  259. read_test_arg_t args4 = READ_TEST_ARG_INIT(names[3], 4, 0x34343434);
  260. const int cpuid_0 = 0;
  261. const int cpuid_1 = portNUM_PROCESSORS - 1;
  262. const int stack_size = 4096;
  263. printf("reading files 1.txt 2.txt 3.txt 4.txt \n");
  264. xTaskCreatePinnedToCore(&read_task, "r1", stack_size, &args1, 3, NULL, cpuid_1);
  265. xTaskCreatePinnedToCore(&read_task, "r2", stack_size, &args2, 3, NULL, cpuid_0);
  266. xTaskCreatePinnedToCore(&read_task, "r3", stack_size, &args3, 3, NULL, cpuid_0);
  267. xTaskCreatePinnedToCore(&read_task, "r4", stack_size, &args4, 3, NULL, cpuid_1);
  268. xSemaphoreTake(args1.done, portMAX_DELAY);
  269. printf("1.txt done\n");
  270. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  271. xSemaphoreTake(args2.done, portMAX_DELAY);
  272. printf("2.txt done\n");
  273. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  274. xSemaphoreTake(args3.done, portMAX_DELAY);
  275. printf("3.txt done\n");
  276. TEST_ASSERT_EQUAL(ESP_OK, args3.result);
  277. xSemaphoreTake(args4.done, portMAX_DELAY);
  278. printf("4.txt done\n");
  279. TEST_ASSERT_EQUAL(ESP_OK, args4.result);
  280. vSemaphoreDelete(args1.done);
  281. vSemaphoreDelete(args2.done);
  282. vSemaphoreDelete(args3.done);
  283. vSemaphoreDelete(args4.done);
  284. test_teardown();
  285. }
  286. TEST_CASE("(raw) read speed test", "[fatfs][timeout=60]")
  287. {
  288. test_setup(5);
  289. const size_t buf_size = 16 * 1024;
  290. uint32_t* buf = (uint32_t*) calloc(1, buf_size);
  291. const size_t file_size = 256 * 1024;
  292. const char* file = "/spiflash/256k.bin";
  293. test_fatfs_rw_speed(file, buf, 4 * 1024, file_size, false);
  294. test_fatfs_rw_speed(file, buf, 8 * 1024, file_size, false);
  295. test_fatfs_rw_speed(file, buf, 16 * 1024, file_size, false);
  296. free(buf);
  297. test_teardown();
  298. }