2018-05-22 04:47:30 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "esp_partition.h"
|
|
|
|
#include "spiffs.h"
|
|
|
|
#include "spiffs_nucleus.h"
|
|
|
|
#include "spiffs_api.h"
|
|
|
|
|
|
|
|
#include "catch.hpp"
|
|
|
|
|
2018-06-21 04:51:44 -04:00
|
|
|
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);
|
2018-05-26 02:58:01 -04:00
|
|
|
|
2018-05-22 04:47:30 -04:00
|
|
|
TEST_CASE("format disk, open file, write and read file", "[spiffs]")
|
|
|
|
{
|
2018-06-21 04:51:44 -04:00
|
|
|
init_spi_flash(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin");
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
spiffs fs;
|
|
|
|
spiffs_config cfg;
|
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "storage");
|
|
|
|
|
2018-05-22 04:47:30 -04:00
|
|
|
// Configure objects needed by SPIFFS
|
|
|
|
esp_spiffs_t esp_user_data;
|
2018-05-26 02:58:01 -04:00
|
|
|
esp_user_data.partition = partition;
|
2018-05-22 04:47:30 -04:00
|
|
|
fs.user_data = (void*)&esp_user_data;
|
|
|
|
|
|
|
|
cfg.hal_erase_f = spiffs_api_erase;
|
|
|
|
cfg.hal_read_f = spiffs_api_read;
|
|
|
|
cfg.hal_write_f = spiffs_api_write;
|
|
|
|
cfg.log_block_size = CONFIG_WL_SECTOR_SIZE;
|
|
|
|
cfg.log_page_size = CONFIG_SPIFFS_PAGE_SIZE;
|
|
|
|
cfg.phys_addr = 0;
|
|
|
|
cfg.phys_erase_block = CONFIG_WL_SECTOR_SIZE;
|
2018-05-26 02:58:01 -04:00
|
|
|
cfg.phys_size = partition->size;
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
uint32_t max_files = 5;
|
|
|
|
|
|
|
|
uint32_t fds_sz = max_files * sizeof(spiffs_fd);
|
|
|
|
uint32_t work_sz = cfg.log_page_size * 2;
|
|
|
|
uint32_t cache_sz = sizeof(spiffs_cache) + max_files * (sizeof(spiffs_cache_page)
|
|
|
|
+ cfg.log_page_size);
|
|
|
|
|
|
|
|
uint8_t *work = (uint8_t*) malloc(work_sz);
|
|
|
|
uint8_t *fds = (uint8_t*) malloc(fds_sz);
|
|
|
|
uint8_t *cache = (uint8_t*) malloc(cache_sz);
|
|
|
|
|
|
|
|
s32_t spiffs_res;
|
|
|
|
|
|
|
|
// Special mounting procedure: mount, format, mount as per
|
|
|
|
// https://github.com/pellepl/spiffs/wiki/Using-spiffs
|
|
|
|
spiffs_res = SPIFFS_mount(&fs, &cfg, work, fds, fds_sz,
|
|
|
|
cache, cache_sz, spiffs_api_check);
|
|
|
|
REQUIRE(spiffs_res == SPIFFS_ERR_NOT_A_FS);
|
|
|
|
|
|
|
|
spiffs_res = SPIFFS_format(&fs);
|
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
|
|
|
|
|
|
|
spiffs_res = SPIFFS_mount(&fs, &cfg, work, fds, fds_sz,
|
|
|
|
cache, cache_sz, spiffs_api_check);
|
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
|
|
|
|
|
|
|
// Open test file
|
|
|
|
spiffs_res = SPIFFS_open(&fs, "test.txt", SPIFFS_O_CREAT | SPIFFS_O_RDWR, 0);
|
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
// Generate data
|
2018-05-22 04:47:30 -04:00
|
|
|
spiffs_file file = spiffs_res;
|
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
uint32_t data_size = 100000;
|
|
|
|
|
|
|
|
char *data = (char*) malloc(data_size);
|
|
|
|
char *read = (char*) malloc(data_size);
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < data_size; i += sizeof(i))
|
|
|
|
{
|
|
|
|
*((uint32_t*)(data + i)) = i;
|
|
|
|
}
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
s32_t bw;
|
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
// Write data to file
|
|
|
|
spiffs_res = SPIFFS_write(&fs, file, (void*)data, data_size);
|
2018-05-22 04:47:30 -04:00
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
2018-05-26 02:58:01 -04:00
|
|
|
REQUIRE(spiffs_res == data_size);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
// Set the file object pointer to the beginning
|
|
|
|
spiffs_res = SPIFFS_lseek(&fs, file, 0, SPIFFS_SEEK_SET);
|
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
// Read the file
|
|
|
|
spiffs_res = SPIFFS_read(&fs, file, (void*)read, data_size);
|
2018-05-22 04:47:30 -04:00
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
2018-05-26 02:58:01 -04:00
|
|
|
REQUIRE(spiffs_res == data_size);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
// Close the test file
|
|
|
|
spiffs_res = SPIFFS_close(&fs, file);
|
|
|
|
REQUIRE(spiffs_res >= SPIFFS_OK);
|
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
REQUIRE(memcmp(data, read, data_size) == 0);
|
|
|
|
|
2018-05-22 04:47:30 -04:00
|
|
|
// Unmount
|
|
|
|
SPIFFS_unmount(&fs);
|
|
|
|
|
|
|
|
free(read);
|
2018-05-26 02:58:01 -04:00
|
|
|
free(data);
|
|
|
|
}
|