test_spiffs.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include "esp_partition.h"
  5. #include "spiffs.h"
  6. #include "spiffs_nucleus.h"
  7. #include "spiffs_api.h"
  8. #include "catch.hpp"
  9. extern "C" void init_spi_flash(const char* chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin);
  10. TEST_CASE("format disk, open file, write and read file", "[spiffs]")
  11. {
  12. init_spi_flash(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin");
  13. spiffs fs;
  14. spiffs_config cfg;
  15. const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "storage");
  16. // Configure objects needed by SPIFFS
  17. esp_spiffs_t esp_user_data;
  18. esp_user_data.partition = partition;
  19. fs.user_data = (void*)&esp_user_data;
  20. cfg.hal_erase_f = spiffs_api_erase;
  21. cfg.hal_read_f = spiffs_api_read;
  22. cfg.hal_write_f = spiffs_api_write;
  23. cfg.log_block_size = CONFIG_WL_SECTOR_SIZE;
  24. cfg.log_page_size = CONFIG_SPIFFS_PAGE_SIZE;
  25. cfg.phys_addr = 0;
  26. cfg.phys_erase_block = CONFIG_WL_SECTOR_SIZE;
  27. cfg.phys_size = partition->size;
  28. uint32_t max_files = 5;
  29. uint32_t fds_sz = max_files * sizeof(spiffs_fd);
  30. uint32_t work_sz = cfg.log_page_size * 2;
  31. uint32_t cache_sz = sizeof(spiffs_cache) + max_files * (sizeof(spiffs_cache_page)
  32. + cfg.log_page_size);
  33. uint8_t *work = (uint8_t*) malloc(work_sz);
  34. uint8_t *fds = (uint8_t*) malloc(fds_sz);
  35. uint8_t *cache = (uint8_t*) malloc(cache_sz);
  36. s32_t spiffs_res;
  37. // Special mounting procedure: mount, format, mount as per
  38. // https://github.com/pellepl/spiffs/wiki/Using-spiffs
  39. spiffs_res = SPIFFS_mount(&fs, &cfg, work, fds, fds_sz,
  40. cache, cache_sz, spiffs_api_check);
  41. REQUIRE(spiffs_res == SPIFFS_ERR_NOT_A_FS);
  42. spiffs_res = SPIFFS_format(&fs);
  43. REQUIRE(spiffs_res >= SPIFFS_OK);
  44. spiffs_res = SPIFFS_mount(&fs, &cfg, work, fds, fds_sz,
  45. cache, cache_sz, spiffs_api_check);
  46. REQUIRE(spiffs_res >= SPIFFS_OK);
  47. // Open test file
  48. spiffs_res = SPIFFS_open(&fs, "test.txt", SPIFFS_O_CREAT | SPIFFS_O_RDWR, 0);
  49. REQUIRE(spiffs_res >= SPIFFS_OK);
  50. // Generate data
  51. spiffs_file file = spiffs_res;
  52. uint32_t data_size = 100000;
  53. char *data = (char*) malloc(data_size);
  54. char *read = (char*) malloc(data_size);
  55. for(uint32_t i = 0; i < data_size; i += sizeof(i))
  56. {
  57. *((uint32_t*)(data + i)) = i;
  58. }
  59. s32_t bw;
  60. // Write data to file
  61. spiffs_res = SPIFFS_write(&fs, file, (void*)data, data_size);
  62. REQUIRE(spiffs_res >= SPIFFS_OK);
  63. REQUIRE(spiffs_res == data_size);
  64. // Set the file object pointer to the beginning
  65. spiffs_res = SPIFFS_lseek(&fs, file, 0, SPIFFS_SEEK_SET);
  66. REQUIRE(spiffs_res >= SPIFFS_OK);
  67. // Read the file
  68. spiffs_res = SPIFFS_read(&fs, file, (void*)read, data_size);
  69. REQUIRE(spiffs_res >= SPIFFS_OK);
  70. REQUIRE(spiffs_res == data_size);
  71. // Close the test file
  72. spiffs_res = SPIFFS_close(&fs, file);
  73. REQUIRE(spiffs_res >= SPIFFS_OK);
  74. REQUIRE(memcmp(data, read, data_size) == 0);
  75. // Unmount
  76. SPIFFS_unmount(&fs);
  77. free(read);
  78. free(data);
  79. }