test_fatfs.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "ff.h"
  4. #include "esp_partition.h"
  5. #include "wear_levelling.h"
  6. #include "diskio_impl.h"
  7. #include "diskio_wl.h"
  8. #include "catch.hpp"
  9. extern "C" void _spi_flash_init(const char* chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin);
  10. TEST_CASE("create volume, open file, write and read back data", "[fatfs]")
  11. {
  12. _spi_flash_init(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin");
  13. FRESULT fr_result;
  14. BYTE pdrv;
  15. FATFS fs;
  16. FIL file;
  17. UINT bw;
  18. esp_err_t esp_result;
  19. const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
  20. // Mount wear-levelled partition
  21. wl_handle_t wl_handle;
  22. esp_result = wl_mount(partition, &wl_handle);
  23. REQUIRE(esp_result == ESP_OK);
  24. // Get a physical drive
  25. esp_result = ff_diskio_get_drive(&pdrv);
  26. REQUIRE(esp_result == ESP_OK);
  27. // Register physical drive as wear-levelled partition
  28. esp_result = ff_diskio_register_wl_partition(pdrv, wl_handle);
  29. // Create FAT volume on the entire disk
  30. LBA_t part_list[] = {100, 0, 0, 0};
  31. BYTE work_area[FF_MAX_SS];
  32. fr_result = f_fdisk(pdrv, part_list, work_area);
  33. REQUIRE(fr_result == FR_OK);
  34. const MKFS_PARM opt = {(BYTE)FM_ANY, 0, 0, 0, 0};
  35. fr_result = f_mkfs("", &opt, work_area, sizeof(work_area)); // Use default volume
  36. // Mount the volume
  37. fr_result = f_mount(&fs, "", 0);
  38. REQUIRE(fr_result == FR_OK);
  39. // Open, write and read data
  40. fr_result = f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
  41. REQUIRE(fr_result == FR_OK);
  42. // Generate data
  43. uint32_t data_size = 100000;
  44. char *data = (char*) malloc(data_size);
  45. char *read = (char*) malloc(data_size);
  46. for(uint32_t i = 0; i < data_size; i += sizeof(i))
  47. {
  48. *((uint32_t*)(data + i)) = i;
  49. }
  50. // Write generated data
  51. fr_result = f_write(&file, data, data_size, &bw);
  52. REQUIRE(fr_result == FR_OK);
  53. REQUIRE(bw == data_size);
  54. // Move to beginning of file
  55. fr_result = f_lseek(&file, 0);
  56. REQUIRE(fr_result == FR_OK);
  57. // Read written data
  58. fr_result = f_read(&file, read, data_size, &bw);
  59. REQUIRE(fr_result == FR_OK);
  60. REQUIRE(bw == data_size);
  61. REQUIRE(memcmp(data, read, data_size) == 0);
  62. // Close file
  63. fr_result = f_close(&file);
  64. REQUIRE(fr_result == FR_OK);
  65. // Unmount default volume
  66. fr_result = f_mount(0, "", 0);
  67. REQUIRE(fr_result == FR_OK);
  68. free(read);
  69. free(data);
  70. }