test_fatfs_rawflash.c 10 KB

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