test_fatfs_rawflash.c 10 KB

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