mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
spi_flash: Enable flash emulation down to ROM function calls
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
TEST_PROGRAM=test_spiffs
|
||||
COMPONENT=spiffs
|
||||
|
||||
TEST_PARTITION_SIM_DIR=$(IDF_PATH)/components/spi_flash/sim
|
||||
TEST_PARTITION_SIM_LIB=libpartition_sim.a
|
||||
TEST_PROGRAM=test_$(COMPONENT)
|
||||
|
||||
#Expose as a library
|
||||
COMPONENT_LIB=lib$(COMPONENT).a
|
||||
|
||||
SPI_FLASH=spi_flash
|
||||
SPI_FLASH_DIR=../../$(SPI_FLASH)
|
||||
SPI_FLASH_SIM_DIR=$(SPI_FLASH_DIR)/sim
|
||||
SPI_FLASH_LIB=lib$(SPI_FLASH).a
|
||||
|
||||
all: $(TEST_PROGRAM)
|
||||
|
||||
SOURCE_FILES = \
|
||||
main.cpp \
|
||||
test_spiffs.cpp \
|
||||
../spiffs_api.c \
|
||||
$(addprefix ../spiffs/src/, \
|
||||
spiffs_cache.c \
|
||||
@@ -20,46 +25,59 @@ SOURCE_FILES = \
|
||||
log/log.c \
|
||||
)
|
||||
|
||||
TEST_SOURCE_FILES = \
|
||||
test_spiffs.cpp \
|
||||
main.cpp \
|
||||
test_utils.c
|
||||
|
||||
INCLUDE_FLAGS = $(addprefix -I,\
|
||||
. \
|
||||
.. \
|
||||
../spiffs/src \
|
||||
../include \
|
||||
$(addprefix ./stubs/, \
|
||||
esp32/include \
|
||||
log/include \
|
||||
freertos/include \
|
||||
newlib/include \
|
||||
vfs/include \
|
||||
) \
|
||||
../../esp32/include \
|
||||
$(TEST_PARTITION_SIM_DIR)/include \
|
||||
$(SPI_FLASH_DIR)/include \
|
||||
../../../tools/catch \
|
||||
)
|
||||
|
||||
GCOV ?= gcov
|
||||
|
||||
CPPFLAGS += $(INCLUDE_FLAGS) -D CONFIG_LOG_DEFAULT_LEVEL -g
|
||||
CPPFLAGS += $(INCLUDE_FLAGS) -D CONFIG_LOG_DEFAULT_LEVEL -g -m32
|
||||
CFLAGS += -fprofile-arcs -ftest-coverage
|
||||
CXXFLAGS += -std=c++11 -Wall -Werror -fprofile-arcs -ftest-coverage
|
||||
LDFLAGS += -lstdc++ -fprofile-arcs -ftest-coverage
|
||||
|
||||
OBJ_FILES = $(filter %.o, $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o))
|
||||
TEST_OBJ_FILES = $(filter %.o, $(TEST_SOURCE_FILES:.cpp=.o) $(TEST_SOURCE_FILES:.c=.o))
|
||||
|
||||
$(TEST_PARTITION_SIM_DIR)/$(TEST_PARTITION_SIM_LIB): force
|
||||
$(MAKE) -C $(TEST_PARTITION_SIM_DIR) lib
|
||||
|
||||
$(TEST_PROGRAM): $(OBJ_FILES) $(TEST_PARTITION_SIM_DIR)/$(TEST_PARTITION_SIM_LIB)
|
||||
g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(OBJ_FILES) -L$(TEST_PARTITION_SIM_DIR) -l:$(TEST_PARTITION_SIM_LIB) -L$(TEST_WL_DIR) -l:$(TEST_WL_LIB)
|
||||
$(SPI_FLASH_SIM_DIR)/$(SPI_FLASH_LIB): force
|
||||
$(MAKE) -C $(SPI_FLASH_SIM_DIR) lib
|
||||
|
||||
force:
|
||||
|
||||
$(COMPONENT_LIB): $(OBJ_FILES)
|
||||
$(AR) rcs $@ $^
|
||||
|
||||
lib: $(COMPONENT_LIB)
|
||||
|
||||
partitions_table.bin: partitions_table.csv
|
||||
python ../../partition_table/gen_esp32part.py --verify $< $@
|
||||
|
||||
$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(SPI_FLASH_SIM_DIR)/$(SPI_FLASH_LIB) partitions_table.bin
|
||||
g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(TEST_OBJ_FILES) -L$(abspath .) -l:$(COMPONENT_LIB) -L$(SPI_FLASH_SIM_DIR) -l:$(SPI_FLASH_LIB) -g -m32
|
||||
|
||||
test: $(TEST_PROGRAM)
|
||||
./$(TEST_PROGRAM)
|
||||
|
||||
COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(OBJ_FILES:.o=.gc*)
|
||||
COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(TEST_OBJ_FILES:.o=.gc*)
|
||||
|
||||
$(COVERAGE_FILES): $(TEST_PROGRAM) lib
|
||||
$(COVERAGE_FILES): test
|
||||
|
||||
coverage.info: $(COVERAGE_FILES)
|
||||
find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} +
|
||||
@@ -70,10 +88,10 @@ coverage_report: coverage.info
|
||||
@echo "Coverage report is in coverage_report/index.html"
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ_FILES) $(TEST_PROGRAM) $(TEST_WL_LIB)
|
||||
$(MAKE) -C $(TEST_PARTITION_SIM_DIR) clean
|
||||
rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partitions_table.bin
|
||||
$(MAKE) -C $(SPI_FLASH_SIM_DIR) clean
|
||||
rm -f $(COVERAGE_FILES) *.gcov
|
||||
rm -rf coverage_report/
|
||||
rm -f coverage.info
|
||||
|
||||
.PHONY: clean all test
|
||||
.PHONY: clean all test lib
|
||||
|
6
components/spiffs/test_spiffs_host/partitions_table.csv
Normal file
6
components/spiffs/test_spiffs_host/partitions_table.csv
Normal file
@@ -0,0 +1,6 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
|
||||
nvs, data, nvs, 0x9000, 0x6000,
|
||||
phy_init, data, phy, 0xf000, 0x1000,
|
||||
factory, app, factory, 0x10000, 1M,
|
||||
storage, data, spiffs, , 2M,
|
|
@@ -11,4 +11,4 @@
|
||||
#define CONFIG_SPIFFS_USE_MAGIC 1
|
||||
#define CONFIG_SPIFFS_PAGE_CHECK 1
|
||||
#define CONFIG_SPIFFS_USE_MTIME 1
|
||||
#define CONFIG_WL_SECTOR_SIZE 4096
|
||||
#define CONFIG_WL_SECTOR_SIZE 4096
|
||||
|
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t device_id;
|
||||
uint32_t chip_size; // chip size in bytes
|
||||
uint32_t block_size;
|
||||
uint32_t sector_size;
|
||||
uint32_t page_size;
|
||||
uint32_t status_mask;
|
||||
} esp_rom_spiflash_chip_t;
|
||||
|
||||
extern esp_rom_spiflash_chip_t g_rom_flashchip;
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
@@ -9,19 +9,20 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
|
||||
extern "C" void init_spi_flash(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin);
|
||||
|
||||
TEST_CASE("format disk, open file, write and read file", "[spiffs]")
|
||||
{
|
||||
uint32_t size = 0x00400000;
|
||||
|
||||
int flash_handle = esp_flash_create(size, CONFIG_WL_SECTOR_SIZE, 1);
|
||||
esp_partition_t partition = esp_partition_create(size, 0, flash_handle);
|
||||
init_spi_flash(0x00400000, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partitions_table.bin");
|
||||
|
||||
spiffs fs;
|
||||
spiffs_config cfg;
|
||||
|
||||
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "storage");
|
||||
|
||||
// Configure objects needed by SPIFFS
|
||||
esp_spiffs_t esp_user_data;
|
||||
esp_user_data.partition = &partition;
|
||||
esp_user_data.partition = partition;
|
||||
fs.user_data = (void*)&esp_user_data;
|
||||
|
||||
cfg.hal_erase_f = spiffs_api_erase;
|
||||
@@ -31,7 +32,7 @@ TEST_CASE("format disk, open file, write and read file", "[spiffs]")
|
||||
cfg.log_page_size = CONFIG_SPIFFS_PAGE_SIZE;
|
||||
cfg.phys_addr = 0;
|
||||
cfg.phys_erase_block = CONFIG_WL_SECTOR_SIZE;
|
||||
cfg.phys_size = partition.size;
|
||||
cfg.phys_size = partition->size;
|
||||
|
||||
uint32_t max_files = 5;
|
||||
|
||||
@@ -63,34 +64,44 @@ TEST_CASE("format disk, open file, write and read file", "[spiffs]")
|
||||
spiffs_res = SPIFFS_open(&fs, "test.txt", SPIFFS_O_CREAT | SPIFFS_O_RDWR, 0);
|
||||
REQUIRE(spiffs_res >= SPIFFS_OK);
|
||||
|
||||
// Write to the test file
|
||||
// Generate data
|
||||
spiffs_file file = spiffs_res;
|
||||
|
||||
const char data[] = "Hello, World!";
|
||||
char *read = (char*) malloc(sizeof(data));
|
||||
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;
|
||||
}
|
||||
|
||||
s32_t bw;
|
||||
|
||||
spiffs_res = SPIFFS_write(&fs, file, (void*)data, sizeof(data));
|
||||
// Write data to file
|
||||
spiffs_res = SPIFFS_write(&fs, file, (void*)data, data_size);
|
||||
REQUIRE(spiffs_res >= SPIFFS_OK);
|
||||
REQUIRE(spiffs_res == sizeof(data));
|
||||
REQUIRE(spiffs_res == data_size);
|
||||
|
||||
// Set the file object pointer to the beginning
|
||||
spiffs_res = SPIFFS_lseek(&fs, file, 0, SPIFFS_SEEK_SET);
|
||||
REQUIRE(spiffs_res >= SPIFFS_OK);
|
||||
|
||||
// Set the file object pointer to the beginning
|
||||
spiffs_res = SPIFFS_read(&fs, file, (void*)read, sizeof(data));
|
||||
// Read the file
|
||||
spiffs_res = SPIFFS_read(&fs, file, (void*)read, data_size);
|
||||
REQUIRE(spiffs_res >= SPIFFS_OK);
|
||||
REQUIRE(spiffs_res == sizeof(data));
|
||||
REQUIRE(spiffs_res == data_size);
|
||||
|
||||
// Close the test file
|
||||
spiffs_res = SPIFFS_close(&fs, file);
|
||||
REQUIRE(spiffs_res >= SPIFFS_OK);
|
||||
|
||||
REQUIRE(memcmp(data, read, data_size) == 0);
|
||||
|
||||
// Unmount
|
||||
SPIFFS_unmount(&fs);
|
||||
|
||||
esp_flash_delete(flash_handle);
|
||||
free(read);
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
|
7
components/spiffs/test_spiffs_host/test_utils.c
Normal file
7
components/spiffs/test_spiffs_host/test_utils.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "esp_spi_flash.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
void init_spi_flash(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin)
|
||||
{
|
||||
spi_flash_init(chip_size, block_size, sector_size, page_size, partition_bin);
|
||||
}
|
Reference in New Issue
Block a user