diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c58daaa4b8..e43bc3119e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -299,8 +299,6 @@ test_wl_on_host: test_fatfs_on_host: <<: *host_test_template - tags: - - wl_host_test script: - cd components/fatfs/test_fatfs_host/ - make test @@ -332,8 +330,6 @@ test_mdns_fuzzer_on_host: test_spiffs_on_host: <<: *host_test_template - tags: - - wl_host_test script: - cd components/spiffs/test_spiffs_host/ - make test diff --git a/components/fatfs/test_fatfs_host/Makefile b/components/fatfs/test_fatfs_host/Makefile index 1e4734e5ff..4c43e16aab 100644 --- a/components/fatfs/test_fatfs_host/Makefile +++ b/components/fatfs/test_fatfs_host/Makefile @@ -1,103 +1,107 @@ -COMPONENT=fatfs +ifndef COMPONENT +COMPONENT := fatfs +endif -TEST_PROGRAM=test_$(COMPONENT) +COMPONENT_LIB := lib$(COMPONENT).a +TEST_PROGRAM := test_$(COMPONENT) -#Expose as a library -COMPONENT_LIB=lib$(COMPONENT).a +STUBS_LIB_DIR := ../../../components/spi_flash/sim/stubs +STUBS_LIB_BUILD_DIR := $(STUBS_LIB_DIR)/build +STUBS_LIB := libstubs.a -#Use wear levelling and flash simulation -WEAR_LEVELLING=wear_levelling -WEAR_LEVELLING_DIR=../../$(WEAR_LEVELLING) -WEAR_LEVELLING_HOST_DIR=$(WEAR_LEVELLING_DIR)/test_wl_host -WEAR_LEVELLING_LIB=lib$(WEAR_LEVELLING).a +SPI_FLASH_SIM_DIR := ../../../components/spi_flash/sim +SPI_FLASH_SIM_BUILD_DIR := $(SPI_FLASH_SIM_DIR)/build +SPI_FLASH_SIM_LIB := libspi_flash.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 +WEAR_LEVELLING_DIR := ../../../components/wear_levelling/test_wl_host +WEAR_LEVELLING_BUILD_DIR := $(WEAR_LEVELLING_DIR)/build +WEAR_LEVELLING_LIB := libwl.a -all: $(TEST_PROGRAM) +include Makefile.files -SOURCE_FILES = \ - $(addprefix ../src/, \ - diskio.c \ - ff.c \ - ffsystem.c \ - ffunicode.c \ - diskio_wl.c \ - ) \ - $(addprefix ./stubs/, log/log.c) +all: test +ifndef SDKCONFIG +SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) +SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h +else +SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG))) +endif + +INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR) ../../../tools/catch) + +CPPFLAGS += $(INCLUDE_FLAGS) -g -m32 +CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32 + +# Build libraries that this component is dependent on +$(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB): force + $(MAKE) -C $(STUBS_LIB_DIR) lib SDKCONFIG=$(SDKCONFIG) + +$(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB): force + $(MAKE) -C $(SPI_FLASH_SIM_DIR) lib SDKCONFIG=$(SDKCONFIG) + +$(WEAR_LEVELLING_BUILD_DIR)/$(WEAR_LEVELLING_LIB): force + $(MAKE) -C $(WEAR_LEVELLING_DIR) lib SDKCONFIG=$(SDKCONFIG) + +# Create target for building this component as a library +CFILES := $(filter %.c, $(SOURCE_FILES)) +CPPFILES := $(filter %.cpp, $(SOURCE_FILES)) + +CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1})) +CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1})) + +ifndef BUILD_DIR +BUILD_DIR := build +endif + +OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)))) + +define COMPILE_C +$(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +define COMPILE_CPP +$(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +$(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(AR) rcs $@ $^ + +lib: $(BUILD_DIR)/$(COMPONENT_LIB) + +$(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile)))) +$(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile)))) + +# Create target for building this component as a test TEST_SOURCE_FILES = \ test_fatfs.cpp \ main.cpp \ test_utils.c -INCLUDE_FLAGS = $(addprefix -I,\ - ../src \ - . \ - $(addprefix ./stubs/, \ - driver/include \ - freertos/include \ - sdmmc/include \ - log/include \ - ) \ - $(SPI_FLASH_DIR)/include \ - $(WEAR_LEVELLING_DIR)/include \ - ../../esp32/include \ - ../../../tools/catch \ -) - -GCOV ?= gcov - -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)) -$(WEAR_LEVELLING_HOST_DIR)/$(WEAR_LEVELLING_LIB): force - $(MAKE) -C $(WEAR_LEVELLING_HOST_DIR) 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) - -partition_table.bin: partition_table.csv - python ../../partition_table/gen_esp32part.py $< $@ - -$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(SPI_FLASH_SIM_DIR)/$(SPI_FLASH_LIB) $(WEAR_LEVELLING_HOST_DIR)/$(WEAR_LEVELLING_LIB) partition_table.bin - g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(TEST_OBJ_FILES) -L$(abspath .) -l:$(COMPONENT_LIB) -L$(SPI_FLASH_SIM_DIR) -l:$(SPI_FLASH_LIB) -L$(WEAR_LEVELLING_HOST_DIR) -l:$(WEAR_LEVELLING_LIB) -g -m32 +$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(WEAR_LEVELLING_BUILD_DIR)/$(WEAR_LEVELLING_LIB) $(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB) $(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB) partition_table.bin $(SDKCONFIG) + g++ $(LDFLAGS) $(CXXFLAGS) -o $@ $(TEST_OBJ_FILES) -L$(BUILD_DIR) -l:$(COMPONENT_LIB) -L$(WEAR_LEVELLING_BUILD_DIR) -l:$(WEAR_LEVELLING_LIB) -L$(SPI_FLASH_SIM_BUILD_DIR) -l:$(SPI_FLASH_SIM_LIB) -L$(STUBS_LIB_BUILD_DIR) -l:$(STUBS_LIB) test: $(TEST_PROGRAM) ./$(TEST_PROGRAM) -COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(TEST_OBJ_FILES:.o=.gc*) +# Create other necessary targets +partition_table.bin: partition_table.csv + python ../../../components/partition_table/gen_esp32part.py --verify $< $@ -$(COVERAGE_FILES): test - -coverage.info: $(COVERAGE_FILES) - find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} + - lcov --capture --directory ../ --no-external --output-file coverage.info --gcov-tool $(GCOV) - -coverage_report: coverage.info - genhtml coverage.info --output-directory coverage_report - @echo "Coverage report is in coverage_report/index.html" +force: +# Create target to cleanup files clean: - rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin - $(MAKE) -C $(WEAR_LEVELLING_HOST_DIR) clean + $(MAKE) -C $(STUBS_LIB_DIR) clean $(MAKE) -C $(SPI_FLASH_SIM_DIR) clean - rm -f $(COVERAGE_FILES) *.gcov - rm -rf coverage_report/ - rm -f coverage.info + $(MAKE) -C $(WEAR_LEVELLING_DIR) clean + rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin -.PHONY: clean all test lib +.PHONY: all lib test clean force diff --git a/components/fatfs/test_fatfs_host/Makefile.files b/components/fatfs/test_fatfs_host/Makefile.files new file mode 100644 index 0000000000..2671485771 --- /dev/null +++ b/components/fatfs/test_fatfs_host/Makefile.files @@ -0,0 +1,30 @@ +SOURCE_FILES := \ + $(addprefix ../src/, \ + diskio.c \ + ff.c \ + ffsystem.c \ + ffunicode.c \ + diskio_wl.c \ + ) + +INCLUDE_DIRS := \ + . \ + ../src \ + $(addprefix ../../spi_flash/sim/stubs/, \ + app_update/include \ + driver/include \ + esp32/include \ + freertos/include \ + log/include \ + newlib/include \ + sdmmc/include \ + vfs/include \ + ) \ + $(addprefix ../../../components/, \ + soc/esp32/include \ + esp32/include \ + bootloader_support/include \ + app_update/include \ + spi_flash/include \ + wear_levelling/include \ + ) diff --git a/components/fatfs/test_fatfs_host/component.mk b/components/fatfs/test_fatfs_host/component.mk new file mode 100644 index 0000000000..82dea0b0cf --- /dev/null +++ b/components/fatfs/test_fatfs_host/component.mk @@ -0,0 +1,17 @@ +include $(COMPONENT_PATH)/Makefile.files + +COMPONENT_OWNBUILDTARGET := 1 +COMPONENT_OWNCLEANTARGET := 1 + +COMPONENT_ADD_INCLUDEDIRS := $(INCLUDE_DIRS) + +.PHONY: build +build: $(SDKCONFIG_HEADER) + $(MAKE) -C $(COMPONENT_PATH) lib SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) + +CLEAN_FILES := component_project_vars.mk +.PHONY: clean +clean: + $(summary) RM $(CLEAN_FILES) + rm -f $(CLEAN_FILES) + $(MAKE) -C $(COMPONENT_PATH) clean SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) \ No newline at end of file diff --git a/components/fatfs/test_fatfs_host/sdkconfig.h b/components/fatfs/test_fatfs_host/sdkconfig.h deleted file mode 100644 index b61f5811d1..0000000000 --- a/components/fatfs/test_fatfs_host/sdkconfig.h +++ /dev/null @@ -1,3 +0,0 @@ -# pragma once - -#define CONFIG_WL_SECTOR_SIZE 4096 diff --git a/components/fatfs/test_fatfs_host/sdkconfig/sdkconfig.h b/components/fatfs/test_fatfs_host/sdkconfig/sdkconfig.h new file mode 100644 index 0000000000..45ca4b7061 --- /dev/null +++ b/components/fatfs/test_fatfs_host/sdkconfig/sdkconfig.h @@ -0,0 +1,6 @@ +# pragma once + +#define CONFIG_WL_SECTOR_SIZE 4096 +#define CONFIG_LOG_DEFAULT_LEVEL 3 +#define CONFIG_PARTITION_TABLE_OFFSET 0x8000 +#define CONFIG_ESPTOOLPY_FLASHSIZE "8MB" diff --git a/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/FreeRTOS.h b/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/FreeRTOS.h deleted file mode 100644 index 5bafa772df..0000000000 --- a/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/FreeRTOS.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "projdefs.h" -#include "semphr.h" diff --git a/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/projdefs.h b/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/projdefs.h deleted file mode 100644 index aa8c7a3de5..0000000000 --- a/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/projdefs.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#if defined(__cplusplus) -extern "C" { -#endif - -#define pdTRUE 1 - -#if defined(__cplusplus) -} -#endif diff --git a/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/semphr.h b/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/semphr.h deleted file mode 100644 index d49cafb99f..0000000000 --- a/components/fatfs/test_fatfs_host/stubs/freertos/include/freertos/semphr.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#if defined(__cplusplus) -extern "C" { -#endif - -#define vSemaphoreDelete( xSemaphore ) -#define xSemaphoreCreateMutex() ((void*)(1)) -#define xSemaphoreGive( xSemaphore ) -#define xSemaphoreTake( xSemaphore, xBlockTime ) pdTRUE - -typedef void* SemaphoreHandle_t; - -#if defined(__cplusplus) -} -#endif diff --git a/components/fatfs/test_fatfs_host/stubs/log/include/esp_log.h b/components/fatfs/test_fatfs_host/stubs/log/include/esp_log.h deleted file mode 100644 index f90501430e..0000000000 --- a/components/fatfs/test_fatfs_host/stubs/log/include/esp_log.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include -#include - -#include "sdkconfig.h" - -#if defined(__cplusplus) -extern "C" { // Make sure we have C-declarations in C++ programs -#endif - -#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG - -typedef enum { - ESP_LOG_NONE, /*!< No log output */ - ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */ - ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */ - ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */ - ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */ - ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */ -} esp_log_level_t; - -#define LOG_COLOR_E -#define LOG_COLOR_W -#define LOG_COLOR_I -#define LOG_COLOR_D -#define LOG_COLOR_V -#define LOG_RESET_COLOR - -uint32_t esp_log_timestamp(void); -void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4))); - -#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n" - -#define ESP_LOGE( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGD( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#if defined(__cplusplus) -} -#endif \ No newline at end of file diff --git a/components/fatfs/test_fatfs_host/stubs/log/log.c b/components/fatfs/test_fatfs_host/stubs/log/log.c deleted file mode 100644 index 4f0c12bf82..0000000000 --- a/components/fatfs/test_fatfs_host/stubs/log/log.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include - -#include "esp_log.h" - -void esp_log_write(esp_log_level_t level, - const char *tag, - const char *format, ...) -{ - va_list arg; - va_start(arg, format); - vprintf(format, arg); - va_end(arg); -} - -uint32_t esp_log_timestamp() -{ - return 0; -} \ No newline at end of file diff --git a/components/fatfs/test_fatfs_host/test_fatfs.cpp b/components/fatfs/test_fatfs_host/test_fatfs.cpp index 7d14df267b..98a4d40c7a 100644 --- a/components/fatfs/test_fatfs_host/test_fatfs.cpp +++ b/components/fatfs/test_fatfs_host/test_fatfs.cpp @@ -9,11 +9,11 @@ #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); +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); TEST_CASE("create volume, open file, write and read back data", "[fatfs]") { - init_spi_flash(0x00400000, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin"); + init_spi_flash(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin"); FRESULT fr_result; BYTE pdrv; diff --git a/components/fatfs/test_fatfs_host/test_utils.c b/components/fatfs/test_fatfs_host/test_utils.c index c3181ede75..3e4b05758a 100644 --- a/components/fatfs/test_fatfs_host/test_utils.c +++ b/components/fatfs/test_fatfs_host/test_utils.c @@ -1,7 +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) +void init_spi_flash(const char* 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); -} \ No newline at end of file +} diff --git a/components/spi_flash/sim/Makefile b/components/spi_flash/sim/Makefile index d5da7f9a63..9ab4b17229 100644 --- a/components/spi_flash/sim/Makefile +++ b/components/spi_flash/sim/Makefile @@ -1,67 +1,65 @@ -COMPONENT=spi_flash -COMPONENT_LIB=lib$(COMPONENT).a +ifndef COMPONENT +COMPONENT := spi_flash +endif + +COMPONENT_LIB := lib$(COMPONENT).a + +include Makefile.files all: lib -SOURCE_FILES = \ - ../partition.c \ - ../flash_ops.c \ - SpiFlash.cpp \ - flash_mock_util.c \ - flash_mock.cpp \ - $(addprefix stubs/, \ - newlib/lock.c \ - app_update/esp_ota_eps.c \ - ) +ifndef SDKCONFIG +SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) +SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h +else +SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG))) +endif -INCLUDE_FLAGS = $(addprefix -I,\ - . \ - ../include \ - ../../esp32/include/ \ - $(addprefix stubs/, \ - newlib/include \ - log/include \ - freertos/include \ - ) \ - $(addprefix ../../../components/, \ - soc/esp32/include \ - esp32/include \ - bootloader_support/include \ - app_update/include \ - ) \ - ../../../tools/catch \ -) +INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR)) -GCOV ?= gcov +CPPFLAGS += $(INCLUDE_FLAGS) -g -m32 +CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32 -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 +CFILES := $(filter %.c, $(SOURCE_FILES)) +CPPFILES := $(filter %.cpp, $(SOURCE_FILES)) -OBJ_FILES = $(filter %.o, $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)) +CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1})) +CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1})) -$(COMPONENT_LIB): $(OBJ_FILES) +ifndef BUILD_DIR +BUILD_DIR := build +endif + +OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)))) + +%.o : %.c $(SDKCONFIG) + $(CC) $(CPPFLAGS) $(CFLAGS) $< - o $@ + +%.o : %.cpp $(SDKCONFIG) + $(CC) $(CPPFLAGS) $(CFLAGS) $< - o $@ + +define COMPILE_C +$(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} + mkdir -p $(BUILD_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +define COMPILE_CPP +$(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} + mkdir -p $(BUILD_DIR) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +$(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG) + mkdir -p $(BUILD_DIR) $(AR) rcs $@ $^ -lib: $(COMPONENT_LIB) - -COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) - -$(COVERAGE_FILES): lib - -coverage.info: $(COVERAGE_FILES) - find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} + - lcov --capture --directory ../ --no-external --output-file coverage.info --gcov-tool $(GCOV) - -coverage_report: coverage.info - genhtml coverage.info --output-directory coverage_report - @echo "Coverage report is in coverage_report/index.html" - clean: - rm -f $(OBJ_FILES) $(COMPONENT_LIB) - rm -f $(COVERAGE_FILES) *.gcov - rm -rf coverage_report/ - rm -f coverage.info + rm -f $(OBJ_FILES) $(BUILD_DIR)/$(COMPONENT_LIB) -.PHONY: clean lib all +lib: $(BUILD_DIR)/$(COMPONENT_LIB) + +$(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile)))) +$(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile)))) + +.PHONY: all lib clean diff --git a/components/spi_flash/sim/Makefile.files b/components/spi_flash/sim/Makefile.files new file mode 100644 index 0000000000..9e5cb04630 --- /dev/null +++ b/components/spi_flash/sim/Makefile.files @@ -0,0 +1,30 @@ +SOURCE_FILES := \ + SpiFlash.cpp \ + flash_mock.cpp \ + flash_mock_util.c \ + $(addprefix ../, \ + partition.c \ + flash_ops.c \ + ) \ + +INCLUDE_DIRS := \ + . \ + ../include \ + ../private_include \ + $(addprefix stubs/, \ + app_update/include \ + driver/include \ + esp32/include \ + freertos/include \ + log/include \ + newlib/include \ + sdmmc/include \ + vfs/include \ + ) \ + $(addprefix ../../../components/, \ + soc/esp32/include \ + esp32/include \ + bootloader_support/include \ + app_update/include \ + spi_flash/include \ + ) diff --git a/components/spi_flash/sim/SpiFlash.cpp b/components/spi_flash/sim/SpiFlash.cpp index 72c575a47a..46b09bccfa 100644 --- a/components/spi_flash/sim/SpiFlash.cpp +++ b/components/spi_flash/sim/SpiFlash.cpp @@ -27,6 +27,8 @@ using namespace std; +#define DIV_AND_CEIL(x, y) ((x) / (y) + ((x) % (y) > 0)) + SpiFlash::SpiFlash() { return; @@ -37,16 +39,31 @@ SpiFlash::~SpiFlash() deinit(); } -void SpiFlash::init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin) +void SpiFlash::init(uint32_t chip_size, uint32_t block_size, uint32_t sector_size, uint32_t page_size, const char* partitions_bin) { // De-initialize first deinit(); + // Initialize values and alloc memory this->chip_size = chip_size; this->block_size = block_size; this->sector_size = sector_size; this->page_size = page_size; + this->blocks = DIV_AND_CEIL(this->chip_size, this->block_size); + this->sectors = DIV_AND_CEIL(this->chip_size, this->sector_size); + this->pages = DIV_AND_CEIL(this->chip_size, this->page_size); + + this->erase_cycles = (uint32_t*) calloc(this->sectors, sizeof(uint32_t)); + this->erase_states = (bool*) calloc(this->sectors, sizeof(bool)); + memset(this->erase_states, 0xFF, this->sectors * sizeof(bool)); + + this->total_erase_cycles_limit = 0; + this->erase_cycles_limit = 0; + + this->total_erase_cycles = 0; + + // Load partitions table bin this->memory = (uint8_t *) malloc(this->chip_size); memset(this->memory, 0xFF, this->chip_size); @@ -65,31 +82,74 @@ void SpiFlash::init(size_t chip_size, size_t block_size, size_t sector_size, siz void SpiFlash::deinit() { - if(inited) - { - free(this->memory); - } + // Free all allocated memory + free(this->memory); + + free(this->erase_cycles); + free(this->erase_states); } -size_t SpiFlash::get_chip_size() +uint32_t SpiFlash::get_chip_size() { return this->chip_size; } -size_t SpiFlash::get_sector_size() +uint32_t SpiFlash::get_block_size() +{ + return this->block_size; +} + +uint32_t SpiFlash::get_sector_size() { return this->sector_size; } +uint32_t SpiFlash::get_page_size() +{ + return this->page_size; +} + esp_rom_spiflash_result_t SpiFlash::erase_block(uint32_t block) { - memset(&this->memory[block * this->block_size], 0xFF, this->block_size); + uint32_t sectors_per_block = (this->block_size / this->sector_size); + uint32_t start_sector = block * sectors_per_block; + + for (int i = start_sector; i < start_sector + sectors_per_block; i++) { + this->erase_sector(i); + } + return ESP_ROM_SPIFLASH_RESULT_OK; } -esp_rom_spiflash_result_t SpiFlash::erase_sector(size_t sector) +esp_rom_spiflash_result_t SpiFlash::erase_sector(uint32_t sector) { - memset(&this->memory[sector * this->sector_size], 0xFF, this->sector_size); + if (this->total_erase_cycles_limit != 0 && + this->total_erase_cycles >= this->total_erase_cycles_limit) { + return ESP_ROM_SPIFLASH_RESULT_ERR; + } + + if (this->erase_cycles_limit != 0 && + this->erase_cycles[sector] >= this->erase_cycles_limit) { + return ESP_ROM_SPIFLASH_RESULT_ERR; + } + + uint32_t pages_per_sector = (this->sector_size / this->page_size); + uint32_t start_page = sector * pages_per_sector; + + if (this->erase_states[sector]) { + goto out; + } + + for (int i = start_page; i < start_page + pages_per_sector; i++) { + this->erase_page(i); + } + + this->erase_cycles[sector]++; + this->total_erase_cycles++; + + this->erase_states[sector] = true; + +out: return ESP_ROM_SPIFLASH_RESULT_OK; } @@ -99,14 +159,36 @@ esp_rom_spiflash_result_t SpiFlash::erase_page(uint32_t page) return ESP_ROM_SPIFLASH_RESULT_OK; } -esp_rom_spiflash_result_t SpiFlash::write(size_t dest_addr, const void *src, size_t size) +esp_rom_spiflash_result_t SpiFlash::write(uint32_t dest_addr, const void *src, uint32_t size) { - // Emulate inability to set programmed bits without erasing + // Update reset states and check for failure + int start = 0; + int end = 0; + + if (this->total_erase_cycles_limit != 0 && + this->total_erase_cycles >= this->total_erase_cycles_limit) { + return ESP_ROM_SPIFLASH_RESULT_ERR; + } + + start = dest_addr / this->get_sector_size(); + end = size > 0 ? (dest_addr + size - 1) / this->get_sector_size() : start; + + for (int i = start; i <= end; i++) { + if (this->erase_cycles_limit != 0 && + this->erase_cycles[i] >= this->erase_cycles_limit) { + return ESP_ROM_SPIFLASH_RESULT_ERR; + } + + this->erase_states[i] = false; + } + + // Do the write for(uint32_t ctr = 0; ctr < size; ctr++) { uint8_t data = ((uint8_t*)src)[ctr]; uint8_t written = this->memory[dest_addr + ctr]; + // Emulate inability to set programmed bits without erasing data &= written; this->memory[dest_addr + ctr] = data; @@ -115,8 +197,28 @@ esp_rom_spiflash_result_t SpiFlash::write(size_t dest_addr, const void *src, siz return ESP_ROM_SPIFLASH_RESULT_OK; } -esp_rom_spiflash_result_t SpiFlash::read(size_t src_addr, void *dest, size_t size) +esp_rom_spiflash_result_t SpiFlash::read(uint32_t src_addr, void *dest, uint32_t size) { + // Check for failure + int start = 0; + int end = 0; + + if (this->total_erase_cycles_limit != 0 && + this->total_erase_cycles >= this->total_erase_cycles_limit) { + return ESP_ROM_SPIFLASH_RESULT_ERR; + } + + start = src_addr / this->get_sector_size(); + end = size > 0 ? (src_addr + size - 1) / this->get_sector_size() : start; + + for (int i = start; i <= end; i++) { + if (this->erase_cycles_limit != 0 && + this->erase_cycles[i] >= this->erase_cycles_limit) { + return ESP_ROM_SPIFLASH_RESULT_ERR; + } + } + + // Do the read memcpy(dest, &this->memory[src_addr], size); return ESP_ROM_SPIFLASH_RESULT_OK; } @@ -124,4 +226,34 @@ esp_rom_spiflash_result_t SpiFlash::read(size_t src_addr, void *dest, size_t siz uint8_t* SpiFlash::get_memory_ptr(uint32_t src_address) { return &this->memory[src_address]; +} + +uint32_t SpiFlash::get_erase_cycles(uint32_t sector) +{ + return this->erase_cycles[sector]; +} + +uint32_t SpiFlash::get_total_erase_cycles() +{ + return this->total_erase_cycles; +} + +void SpiFlash::set_erase_cycles_limit(uint32_t limit) +{ + this->erase_cycles_limit = limit; +} + +void SpiFlash::set_total_erase_cycles_limit(uint32_t limit) +{ + this->total_erase_cycles_limit = limit; +} + +void SpiFlash::reset_erase_cycles() +{ + memset(this->erase_cycles, 0, sizeof(this->sectors * sizeof(uint32_t))); +} + +void SpiFlash::reset_total_erase_cycles() +{ + this->total_erase_cycles = 0; } \ No newline at end of file diff --git a/components/spi_flash/sim/SpiFlash.h b/components/spi_flash/sim/SpiFlash.h index beacbc5b6e..db5d86380a 100644 --- a/components/spi_flash/sim/SpiFlash.h +++ b/components/spi_flash/sim/SpiFlash.h @@ -15,6 +15,8 @@ #ifndef _SpiFlash_H_ #define _SpiFlash_H_ +#include + #include "esp_err.h" #include "rom/spi_flash.h" @@ -29,32 +31,49 @@ public: SpiFlash(); ~SpiFlash(); - void init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin); + void init(uint32_t chip_size, uint32_t block_size, uint32_t sector_size, uint32_t page_size, const char* partitions_bin); + + uint32_t get_chip_size(); + uint32_t get_block_size(); + uint32_t get_sector_size(); + uint32_t get_page_size(); esp_rom_spiflash_result_t erase_block(uint32_t block); esp_rom_spiflash_result_t erase_sector(uint32_t sector); esp_rom_spiflash_result_t erase_page(uint32_t page); - esp_rom_spiflash_result_t write(size_t dest_addr, const void *src, size_t size); - esp_rom_spiflash_result_t read(size_t src_addr, void *dest, size_t size); + esp_rom_spiflash_result_t write(uint32_t dest_addr, const void *src, uint32_t size); + esp_rom_spiflash_result_t read(uint32_t src_addr, void *dest, uint32_t size); - size_t get_chip_size(); - size_t get_sector_size(); + uint32_t get_erase_cycles(uint32_t sector); + uint32_t get_total_erase_cycles(); + + void set_erase_cycles_limit(uint32_t limit); + void set_total_erase_cycles_limit(uint32_t limit); + + void reset_erase_cycles(); + void reset_total_erase_cycles(); uint8_t* get_memory_ptr(uint32_t src_address); private: - bool inited = false; + uint32_t chip_size; + uint32_t block_size; + uint32_t sector_size; + uint32_t page_size; - size_t chip_size; - size_t block_size; - size_t sector_size; - size_t page_size; + uint32_t blocks; + uint32_t sectors; + uint32_t pages; uint8_t* memory; - void* partitions; - uint8_t partitions_num; + bool* erase_states; + + uint32_t* erase_cycles; + uint32_t erase_cycles_limit; + uint32_t total_erase_cycles; + uint32_t total_erase_cycles_limit; void deinit(); }; diff --git a/components/spi_flash/sim/component.mk b/components/spi_flash/sim/component.mk new file mode 100644 index 0000000000..82dea0b0cf --- /dev/null +++ b/components/spi_flash/sim/component.mk @@ -0,0 +1,17 @@ +include $(COMPONENT_PATH)/Makefile.files + +COMPONENT_OWNBUILDTARGET := 1 +COMPONENT_OWNCLEANTARGET := 1 + +COMPONENT_ADD_INCLUDEDIRS := $(INCLUDE_DIRS) + +.PHONY: build +build: $(SDKCONFIG_HEADER) + $(MAKE) -C $(COMPONENT_PATH) lib SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) + +CLEAN_FILES := component_project_vars.mk +.PHONY: clean +clean: + $(summary) RM $(CLEAN_FILES) + rm -f $(CLEAN_FILES) + $(MAKE) -C $(COMPONENT_PATH) clean SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) \ No newline at end of file diff --git a/components/spi_flash/sim/flash_mock.cpp b/components/spi_flash/sim/flash_mock.cpp index e6a992ebc0..502f6df994 100644 --- a/components/spi_flash/sim/flash_mock.cpp +++ b/components/spi_flash/sim/flash_mock.cpp @@ -1,26 +1,57 @@ #include "SpiFlash.h" +#include +#include + #include "esp_spi_flash.h" #include "esp_partition.h" #include "esp_err.h" #include "rom/spi_flash.h" -static SpiFlash spiflash = SpiFlash(); +SpiFlash spiflash = SpiFlash(); esp_rom_spiflash_chip_t g_rom_flashchip; -extern "C" void _spi_flash_init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin) +size_t convert_chip_size_string(const char* chip_size_str) { - spiflash.init(chip_size, block_size, sector_size, page_size, partitions_bin); + int size = 0; + if (strcmp(chip_size_str, "1MB") == 0) { + size = 0x100000; + } + else if (strcmp(chip_size_str, "2MB") == 0) { + size = 0x200000; + } + else if (strcmp(chip_size_str, "4MB") == 0) { + size = 0x400000; + } + else if (strcmp(chip_size_str, "8MB") == 0) { + size = 0x800000; + } + else if (strcmp(chip_size_str, "16MB") == 0) { + size = 0x1000000; + } else { + size = 0; + } - g_rom_flashchip.chip_size = chip_size; + return size; +} + +extern "C" void _spi_flash_init(const char* chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin) +{ + size_t size = convert_chip_size_string(chip_size); + + assert(size != 0); + + spiflash.init(size, block_size, sector_size, page_size, partitions_bin); + + g_rom_flashchip.chip_size = size; g_rom_flashchip.block_size = block_size; g_rom_flashchip.sector_size = sector_size; g_rom_flashchip.page_size = page_size; } -esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory, +extern "C" esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory, const void** out_ptr, spi_flash_mmap_handle_t* out_handle) { *out_handle = 0; @@ -29,11 +60,21 @@ esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t m return ESP_OK; } -void spi_flash_munmap(spi_flash_mmap_handle_t handle) +extern "C" void spi_flash_munmap(spi_flash_mmap_handle_t handle) { return; } +extern "C" int spi_flash_get_total_erase_cycles() +{ + return spiflash.get_total_erase_cycles(); +} + +extern "C" int spi_flash_get_erase_cycles(size_t sector) +{ + return spiflash.get_erase_cycles(sector); +} + esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t target, uint32_t *dest, int32_t len) { return spiflash.read(target, dest, len); @@ -62,4 +103,9 @@ esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len) { return spiflash.write(flash_addr, data, len); -} \ No newline at end of file +} + +void *heap_caps_malloc( size_t size, uint32_t caps ) +{ + return NULL; +} diff --git a/components/spi_flash/sim/flash_mock_util.c b/components/spi_flash/sim/flash_mock_util.c index f12d59ac08..3d9d4186b5 100644 --- a/components/spi_flash/sim/flash_mock_util.c +++ b/components/spi_flash/sim/flash_mock_util.c @@ -4,9 +4,9 @@ #include "esp_err.h" #include "rom/spi_flash.h" -extern void _spi_flash_init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin); +extern void _spi_flash_init(const char* chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin); -void spi_flash_init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin) +void spi_flash_init(const char* 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); } diff --git a/components/spi_flash/sim/sdkconfig.h b/components/spi_flash/sim/sdkconfig/sdkconfig.h similarity index 100% rename from components/spi_flash/sim/sdkconfig.h rename to components/spi_flash/sim/sdkconfig/sdkconfig.h diff --git a/components/spi_flash/sim/stubs/Makefile b/components/spi_flash/sim/stubs/Makefile new file mode 100644 index 0000000000..4f7f909119 --- /dev/null +++ b/components/spi_flash/sim/stubs/Makefile @@ -0,0 +1,65 @@ +ifndef COMPONENT +COMPONENT := stubs +endif + +COMPONENT_LIB := lib$(COMPONENT).a + +include Makefile.files + +all: lib + +ifndef SDKCONFIG +SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) +SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h +else +SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG))) +endif + +INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR)) + +CPPFLAGS += $(INCLUDE_FLAGS) -g -m32 +CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32 + +CFILES := $(filter %.c, $(SOURCE_FILES)) +CPPFILES := $(filter %.cpp, $(SOURCE_FILES)) + +CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1})) +CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1})) + +ifndef BUILD_DIR +BUILD_DIR := build +endif + +OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)))) + +%.o : %.c $(SDKCONFIG) + $(CC) $(CPPFLAGS) $(CFLAGS) $< - o $@ + +%.o : %.cpp $(SDKCONFIG) + $(CC) $(CPPFLAGS) $(CFLAGS) $< - o $@ + +define COMPILE_C +$(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} + mkdir -p $(BUILD_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +define COMPILE_CPP +$(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} + mkdir -p $(BUILD_DIR) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +$(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(AR) rcs $@ $^ + +clean: + rm -f $(OBJ_FILES) $(BUILD_DIR)/$(COMPONENT_LIB) + +lib: $(BUILD_DIR)/$(COMPONENT_LIB) + +$(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile)))) +$(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile)))) + +.PHONY: all lib clean diff --git a/components/spi_flash/sim/stubs/Makefile.files b/components/spi_flash/sim/stubs/Makefile.files new file mode 100644 index 0000000000..4ce825caef --- /dev/null +++ b/components/spi_flash/sim/stubs/Makefile.files @@ -0,0 +1,25 @@ +SOURCE_FILES := \ + app_update/esp_ota_eps.c \ + log/log.c \ + newlib/lock.c \ + esp32/crc.cpp + +INCLUDE_DIRS := \ + ../include \ + ../private_include \ + app_update/include \ + driver/include \ + esp32/include \ + freertos/include \ + log/include \ + newlib/include \ + sdmmc/include \ + vfs/include \ + $(addprefix ../../../../components/, \ + soc/esp32/include \ + esp32/include \ + bootloader_support/include \ + app_update/include \ + spi_flash/include \ + ) + diff --git a/components/spi_flash/sim/stubs/component.mk b/components/spi_flash/sim/stubs/component.mk new file mode 100644 index 0000000000..82dea0b0cf --- /dev/null +++ b/components/spi_flash/sim/stubs/component.mk @@ -0,0 +1,17 @@ +include $(COMPONENT_PATH)/Makefile.files + +COMPONENT_OWNBUILDTARGET := 1 +COMPONENT_OWNCLEANTARGET := 1 + +COMPONENT_ADD_INCLUDEDIRS := $(INCLUDE_DIRS) + +.PHONY: build +build: $(SDKCONFIG_HEADER) + $(MAKE) -C $(COMPONENT_PATH) lib SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) + +CLEAN_FILES := component_project_vars.mk +.PHONY: clean +clean: + $(summary) RM $(CLEAN_FILES) + rm -f $(CLEAN_FILES) + $(MAKE) -C $(COMPONENT_PATH) clean SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) \ No newline at end of file diff --git a/components/fatfs/test_fatfs_host/stubs/driver/include/driver/sdmmc_host.h b/components/spi_flash/sim/stubs/driver/include/driver/sdmmc_host.h similarity index 100% rename from components/fatfs/test_fatfs_host/stubs/driver/include/driver/sdmmc_host.h rename to components/spi_flash/sim/stubs/driver/include/driver/sdmmc_host.h diff --git a/components/fatfs/test_fatfs_host/stubs/driver/include/driver/sdmmc_types.h b/components/spi_flash/sim/stubs/driver/include/driver/sdmmc_types.h similarity index 100% rename from components/fatfs/test_fatfs_host/stubs/driver/include/driver/sdmmc_types.h rename to components/spi_flash/sim/stubs/driver/include/driver/sdmmc_types.h diff --git a/components/wear_levelling/test_wl_host/stubs/esp32/crc.cpp b/components/spi_flash/sim/stubs/esp32/crc.cpp similarity index 100% rename from components/wear_levelling/test_wl_host/stubs/esp32/crc.cpp rename to components/spi_flash/sim/stubs/esp32/crc.cpp diff --git a/components/spi_flash/sim/stubs/freertos/include/freertos/FreeRTOS.h b/components/spi_flash/sim/stubs/freertos/include/freertos/FreeRTOS.h index e0dd0a7e84..9abd0dd8ba 100644 --- a/components/spi_flash/sim/stubs/freertos/include/freertos/FreeRTOS.h +++ b/components/spi_flash/sim/stubs/freertos/include/freertos/FreeRTOS.h @@ -5,4 +5,4 @@ // Avoid redefinition compile error. Put here since this is included // in flash_ops.c. -#define spi_flash_init() overriden_spi_flash_init() +#define spi_flash_init() overriden_spi_flash_init() diff --git a/components/spi_flash/sim/stubs/log/include/esp_log.h b/components/spi_flash/sim/stubs/log/include/esp_log.h index a9c2380006..6c9bb6f759 100644 --- a/components/spi_flash/sim/stubs/log/include/esp_log.h +++ b/components/spi_flash/sim/stubs/log/include/esp_log.h @@ -3,11 +3,20 @@ #include #include +#include "sdkconfig.h" + #ifdef __cplusplus extern "C" { #endif -#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG +#define strlcpy(a, b, c) +#define strlcat(a, b, c) + +#define heap_caps_malloc(a, b) NULL +#define MALLOC_CAP_INTERNAL 0 +#define MALLOC_CAP_8BIT 0 + +#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL typedef enum { ESP_LOG_NONE, /*!< No log output */ @@ -25,6 +34,9 @@ typedef enum { #define LOG_COLOR_V #define LOG_RESET_COLOR +#undef _Static_assert +#define _Static_assert(cond, message) + uint32_t esp_log_timestamp(void); void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4))); @@ -45,4 +57,3 @@ void esp_log_write(esp_log_level_t level, const char* tag, const char* format, . #ifdef __cplusplus } #endif - diff --git a/components/spi_flash/sim/stubs/newlib/include/sys/lock.h b/components/spi_flash/sim/stubs/newlib/include/sys/lock.h index 2b5b9f59ec..bd5af66712 100644 --- a/components/spi_flash/sim/stubs/newlib/include/sys/lock.h +++ b/components/spi_flash/sim/stubs/newlib/include/sys/lock.h @@ -1,5 +1,7 @@ #pragma once +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/task.h b/components/spi_flash/sim/stubs/sdkconfig/sdkconfig.h similarity index 100% rename from components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/task.h rename to components/spi_flash/sim/stubs/sdkconfig/sdkconfig.h diff --git a/components/fatfs/test_fatfs_host/stubs/sdmmc/include/sdmmc_cmd.h b/components/spi_flash/sim/stubs/sdmmc/include/sdmmc_cmd.h similarity index 100% rename from components/fatfs/test_fatfs_host/stubs/sdmmc/include/sdmmc_cmd.h rename to components/spi_flash/sim/stubs/sdmmc/include/sdmmc_cmd.h diff --git a/components/spiffs/test_spiffs_host/stubs/vfs/include/esp_vfs.h b/components/spi_flash/sim/stubs/vfs/include/esp_vfs.h similarity index 100% rename from components/spiffs/test_spiffs_host/stubs/vfs/include/esp_vfs.h rename to components/spi_flash/sim/stubs/vfs/include/esp_vfs.h diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 96233c6f9d..adc192c10f 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -32,6 +32,8 @@ #include "rom/spi_flash.h" #include "spiffs_api.h" +static const char* TAG = "SPIFFS"; + #ifdef CONFIG_SPIFFS_USE_MTIME _Static_assert(CONFIG_SPIFFS_META_LENGTH >= sizeof(time_t), "SPIFFS_META_LENGTH size should be >= sizeof(time_t)"); diff --git a/components/spiffs/spiffs_api.c b/components/spiffs/spiffs_api.c index 39fcabbd94..01c256d016 100644 --- a/components/spiffs/spiffs_api.c +++ b/components/spiffs/spiffs_api.c @@ -19,7 +19,7 @@ #include "esp_vfs.h" #include "spiffs_api.h" -const char* TAG = "SPIFFS"; +static const char* TAG = "SPIFFS"; void spiffs_api_lock(spiffs *fs) { diff --git a/components/spiffs/spiffs_api.h b/components/spiffs/spiffs_api.h index 492da7cde7..5009716a03 100644 --- a/components/spiffs/spiffs_api.h +++ b/components/spiffs/spiffs_api.h @@ -26,8 +26,6 @@ extern "C" { #endif -extern const char* TAG; - /** * @brief SPIFFS definition structure */ diff --git a/components/spiffs/test_spiffs_host/Makefile b/components/spiffs/test_spiffs_host/Makefile index 173bb4c83a..b0e694f29c 100644 --- a/components/spiffs/test_spiffs_host/Makefile +++ b/components/spiffs/test_spiffs_host/Makefile @@ -1,97 +1,98 @@ -COMPONENT=spiffs +ifndef COMPONENT +COMPONENT := spiffs +endif -TEST_PROGRAM=test_$(COMPONENT) +COMPONENT_LIB := lib$(COMPONENT).a +TEST_PROGRAM := test_$(COMPONENT) -#Expose as a library -COMPONENT_LIB=lib$(COMPONENT).a +STUBS_LIB_DIR := ../../../components/spi_flash/sim/stubs +STUBS_LIB_BUILD_DIR := $(STUBS_LIB_DIR)/build +STUBS_LIB := libstubs.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 +SPI_FLASH_SIM_DIR := ../../../components/spi_flash/sim +SPI_FLASH_SIM_BUILD_DIR := $(SPI_FLASH_SIM_DIR)/build +SPI_FLASH_SIM_LIB := libspi_flash.a -all: $(TEST_PROGRAM) +include Makefile.files -SOURCE_FILES = \ - ../spiffs_api.c \ - $(addprefix ../spiffs/src/, \ - spiffs_cache.c \ - spiffs_check.c \ - spiffs_gc.c \ - spiffs_hydrogen.c \ - spiffs_nucleus.c \ - ) \ - $(addprefix ./stubs/, \ - log/log.c \ - ) +all: test +ifndef SDKCONFIG +SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) +SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h +else +SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG))) +endif + +INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR) ../../../tools/catch) + +CPPFLAGS += $(INCLUDE_FLAGS) -g -m32 +CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32 + +# Build libraries that this component is dependent on +$(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB): force + $(MAKE) -C $(STUBS_LIB_DIR) lib SDKCONFIG=$(SDKCONFIG) + +$(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB): force + $(MAKE) -C $(SPI_FLASH_SIM_DIR) lib SDKCONFIG=$(SDKCONFIG) + +# Create target for building this component as a library +CFILES := $(filter %.c, $(SOURCE_FILES)) +CPPFILES := $(filter %.cpp, $(SOURCE_FILES)) + +CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1})) +CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1})) + +ifndef BUILD_DIR +BUILD_DIR := build +endif + +OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)))) + +define COMPILE_C +$(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +define COMPILE_CPP +$(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +$(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(AR) rcs $@ $^ + +clean: + $(MAKE) -C $(STUBS_LIB_DIR) clean + $(MAKE) -C $(SPI_FLASH_SIM_DIR) clean + rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin + +lib: $(BUILD_DIR)/$(COMPONENT_LIB) + +$(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile)))) +$(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile)))) + +# Create target for building this component as a test TEST_SOURCE_FILES = \ test_spiffs.cpp \ main.cpp \ test_utils.c -INCLUDE_FLAGS = $(addprefix -I,\ - . \ - .. \ - ../spiffs/src \ - ../include \ - $(addprefix ./stubs/, \ - log/include \ - freertos/include \ - newlib/include \ - vfs/include \ - ) \ - ../../esp32/include \ - $(SPI_FLASH_DIR)/include \ - ../../../tools/catch \ -) - -GCOV ?= gcov - -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)) -$(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 $< $@ - -$(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_PROGRAM): lib $(TEST_OBJ_FILES) $(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB) $(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB) partition_table.bin $(SDKCONFIG) + g++ $(LDFLAGS) $(CXXFLAGS) -o $@ $(TEST_OBJ_FILES) -L$(BUILD_DIR) -l:$(COMPONENT_LIB) -L$(SPI_FLASH_SIM_BUILD_DIR) -l:$(SPI_FLASH_SIM_LIB) -L$(STUBS_LIB_BUILD_DIR) -l:$(STUBS_LIB) test: $(TEST_PROGRAM) ./$(TEST_PROGRAM) -COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(TEST_OBJ_FILES:.o=.gc*) +# Create other necessary targets +partition_table.bin: partition_table.csv + python ../../../components/partition_table/gen_esp32part.py --verify $< $@ -$(COVERAGE_FILES): test +force: -coverage.info: $(COVERAGE_FILES) - find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} + - lcov --capture --directory ../ --no-external --output-file coverage.info --gcov-tool $(GCOV) - -coverage_report: coverage.info - genhtml coverage.info --output-directory coverage_report - @echo "Coverage report is in coverage_report/index.html" - -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 lib +.PHONY: all lib test clean force diff --git a/components/spiffs/test_spiffs_host/Makefile.files b/components/spiffs/test_spiffs_host/Makefile.files new file mode 100644 index 0000000000..06858bc1a0 --- /dev/null +++ b/components/spiffs/test_spiffs_host/Makefile.files @@ -0,0 +1,33 @@ +SOURCE_FILES := \ + ../spiffs_api.c \ + $(addprefix ../spiffs/src/, \ + spiffs_cache.c \ + spiffs_check.c \ + spiffs_gc.c \ + spiffs_hydrogen.c \ + spiffs_nucleus.c \ + ) + +INCLUDE_DIRS := \ + . \ + .. \ + ../spiffs/src \ + ../include \ + $(addprefix ../../spi_flash/sim/stubs/, \ + app_update/include \ + driver/include \ + esp32/include \ + freertos/include \ + log/include \ + newlib/include \ + sdmmc/include \ + vfs/include \ + ) \ + $(addprefix ../../../components/, \ + soc/esp32/include \ + esp32/include \ + bootloader_support/include \ + app_update/include \ + spi_flash/include \ + wear_levelling/include \ + ) diff --git a/components/spiffs/test_spiffs_host/component.mk b/components/spiffs/test_spiffs_host/component.mk new file mode 100644 index 0000000000..82dea0b0cf --- /dev/null +++ b/components/spiffs/test_spiffs_host/component.mk @@ -0,0 +1,17 @@ +include $(COMPONENT_PATH)/Makefile.files + +COMPONENT_OWNBUILDTARGET := 1 +COMPONENT_OWNCLEANTARGET := 1 + +COMPONENT_ADD_INCLUDEDIRS := $(INCLUDE_DIRS) + +.PHONY: build +build: $(SDKCONFIG_HEADER) + $(MAKE) -C $(COMPONENT_PATH) lib SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) + +CLEAN_FILES := component_project_vars.mk +.PHONY: clean +clean: + $(summary) RM $(CLEAN_FILES) + rm -f $(CLEAN_FILES) + $(MAKE) -C $(COMPONENT_PATH) clean SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) \ No newline at end of file diff --git a/components/spiffs/test_spiffs_host/partitions_table.csv b/components/spiffs/test_spiffs_host/partition_table.csv similarity index 100% rename from components/spiffs/test_spiffs_host/partitions_table.csv rename to components/spiffs/test_spiffs_host/partition_table.csv diff --git a/components/spiffs/test_spiffs_host/sdkconfig.h b/components/spiffs/test_spiffs_host/sdkconfig/sdkconfig.h similarity index 78% rename from components/spiffs/test_spiffs_host/sdkconfig.h rename to components/spiffs/test_spiffs_host/sdkconfig/sdkconfig.h index f798d5ebc8..44966c5581 100644 --- a/components/spiffs/test_spiffs_host/sdkconfig.h +++ b/components/spiffs/test_spiffs_host/sdkconfig/sdkconfig.h @@ -11,4 +11,9 @@ #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_LOG_DEFAULT_LEVEL 3 +#define CONFIG_PARTITION_TABLE_OFFSET 0x8000 + +#define CONFIG_ESPTOOLPY_FLASHSIZE "8MB" diff --git a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/FreeRTOS.h b/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/FreeRTOS.h deleted file mode 100644 index 5bafa772df..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/FreeRTOS.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "projdefs.h" -#include "semphr.h" diff --git a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/projdefs.h b/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/projdefs.h deleted file mode 100644 index aa8c7a3de5..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/projdefs.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#if defined(__cplusplus) -extern "C" { -#endif - -#define pdTRUE 1 - -#if defined(__cplusplus) -} -#endif diff --git a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/semphr.h b/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/semphr.h deleted file mode 100644 index c589795fbe..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/freertos/include/freertos/semphr.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#if defined(__cplusplus) -extern "C" { -#endif - -#define vSemaphoreDelete( xSemaphore ) -#define xSemaphoreCreateMutex() ((void*)(1)) -#define xSemaphoreGive( xSemaphore ) -#define xSemaphoreTake( xSemaphore, xBlockTime ) pdTRUE - -typedef void* SemaphoreHandle_t; - -#if defined(__cplusplus) -} -#endif diff --git a/components/spiffs/test_spiffs_host/stubs/log/include/esp_log.h b/components/spiffs/test_spiffs_host/stubs/log/include/esp_log.h deleted file mode 100644 index 84756cdb93..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/log/include/esp_log.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include -#include - -#include "sdkconfig.h" - -#define strlcpy(a, b, c) -#define strlcat(a, b, c) - -#if defined(__cplusplus) -extern "C" { -#endif - -#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG - -typedef enum { - ESP_LOG_NONE, /*!< No log output */ - ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */ - ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */ - ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */ - ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */ - ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */ -} esp_log_level_t; - -#define LOG_COLOR_E -#define LOG_COLOR_W -#define LOG_COLOR_I -#define LOG_COLOR_D -#define LOG_COLOR_V -#define LOG_RESET_COLOR - -#undef _Static_assert -#define _Static_assert(cond, message) - -uint32_t esp_log_timestamp(void); -void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4))); - -#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n" - -#define ESP_LOGE( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGD( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#if defined(__cplusplus) -} -#endif \ No newline at end of file diff --git a/components/spiffs/test_spiffs_host/stubs/log/log.c b/components/spiffs/test_spiffs_host/stubs/log/log.c deleted file mode 100644 index d01a96ff45..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/log/log.c +++ /dev/null @@ -1,22 +0,0 @@ - -#include -#include -#include - -#include "esp_log.h" - -void esp_log_write(esp_log_level_t level, - const char *tag, - const char *format, ...) -{ - va_list arg; - va_start(arg, format); - vprintf(format, arg); - va_end(arg); -} - -uint32_t esp_log_timestamp() -{ - return 0; -} - diff --git a/components/spiffs/test_spiffs_host/stubs/newlib/include/sys/lock.h b/components/spiffs/test_spiffs_host/stubs/newlib/include/sys/lock.h deleted file mode 100644 index 7892465ada..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/newlib/include/sys/lock.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include - -typedef int _lock_t; - -void _lock_acquire(_lock_t *lock); -void _lock_close(_lock_t *lock); -void _lock_init(_lock_t *lock); -void _lock_release(_lock_t *lock); diff --git a/components/spiffs/test_spiffs_host/stubs/newlib/lock.c b/components/spiffs/test_spiffs_host/stubs/newlib/lock.c deleted file mode 100644 index 697615ffd9..0000000000 --- a/components/spiffs/test_spiffs_host/stubs/newlib/lock.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "sys/lock.h" - -void _lock_acquire(_lock_t *lock) -{ - return; -} - -void _lock_close(_lock_t *lock) -{ - return; -} - -void _lock_init(_lock_t *lock) -{ - return; -} - -void _lock_release(_lock_t *lock) -{ - return; -} diff --git a/components/spiffs/test_spiffs_host/test_spiffs.cpp b/components/spiffs/test_spiffs_host/test_spiffs.cpp index d4a76210e6..fa3936c2f4 100644 --- a/components/spiffs/test_spiffs_host/test_spiffs.cpp +++ b/components/spiffs/test_spiffs_host/test_spiffs.cpp @@ -9,11 +9,11 @@ #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); +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); TEST_CASE("format disk, open file, write and read file", "[spiffs]") { - init_spi_flash(0x00400000, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partitions_table.bin"); + init_spi_flash(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin"); spiffs fs; spiffs_config cfg; diff --git a/components/spiffs/test_spiffs_host/test_utils.c b/components/spiffs/test_spiffs_host/test_utils.c index c3181ede75..3e4b05758a 100644 --- a/components/spiffs/test_spiffs_host/test_utils.c +++ b/components/spiffs/test_spiffs_host/test_utils.c @@ -1,7 +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) +void init_spi_flash(const char* 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); -} \ No newline at end of file +} diff --git a/components/wear_levelling/test_wl_host/Makefile b/components/wear_levelling/test_wl_host/Makefile index c7f8353898..eaf5e7b3b7 100644 --- a/components/wear_levelling/test_wl_host/Makefile +++ b/components/wear_levelling/test_wl_host/Makefile @@ -1,99 +1,98 @@ -COMPONENT=wear_levelling +ifndef COMPONENT +COMPONENT := wl +endif -TEST_PROGRAM=test_$(COMPONENT) +COMPONENT_LIB := lib$(COMPONENT).a +TEST_PROGRAM := test_$(COMPONENT) -# Expose as a library for FS components that require wear_levelling -COMPONENT_LIB=lib$(COMPONENT).a +STUBS_LIB_DIR := ../../../components/spi_flash/sim/stubs +STUBS_LIB_BUILD_DIR := $(STUBS_LIB_DIR)/build +STUBS_LIB := libstubs.a -# Use simulated block device -SPI_FLASH=spi_flash -SPI_FLASH_DIR=../../$(SPI_FLASH) -SPI_FLASH_SIM_DIR=$(SPI_FLASH_DIR)/sim -SPI_FLASH_LIB=lib$(SPI_FLASH).a +SPI_FLASH_SIM_DIR := ../../../components/spi_flash/sim +SPI_FLASH_SIM_BUILD_DIR := $(SPI_FLASH_SIM_DIR)/build +SPI_FLASH_SIM_LIB := libspi_flash.a -all: $(TEST_PROGRAM) +include Makefile.files -SOURCE_FILES = \ - $(addprefix ../, \ - wear_levelling.cpp \ - crc32.cpp \ - WL_Flash.cpp \ - Partition.cpp \ - ) \ - $(addprefix stubs/, \ - newlib/lock.c \ - log/log.c \ - esp32/crc.cpp \ - ) +all: test +ifndef SDKCONFIG +SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) +SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h +else +SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG))) +endif + +INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR) ../../../tools/catch) + +CPPFLAGS += $(INCLUDE_FLAGS) -g -m32 +CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32 + +# Build libraries that this component is dependent on +$(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB): force + $(MAKE) -C $(STUBS_LIB_DIR) lib SDKCONFIG=$(SDKCONFIG) + +$(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB): force + $(MAKE) -C $(SPI_FLASH_SIM_DIR) lib SDKCONFIG=$(SDKCONFIG) + +# Create target for building this component as a library +CFILES := $(filter %.c, $(SOURCE_FILES)) +CPPFILES := $(filter %.cpp, $(SOURCE_FILES)) + +CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1})) +CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1})) + +ifndef BUILD_DIR +BUILD_DIR := build +endif + +OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)))) + +define COMPILE_C +$(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +define COMPILE_CPP +$(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1} +endef + +$(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG) + mkdir -p $(BUILD_DIR) + $(AR) rcs $@ $^ + +clean: + $(MAKE) -C $(STUBS_LIB_DIR) clean + $(MAKE) -C $(SPI_FLASH_SIM_DIR) clean + rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin + +lib: $(BUILD_DIR)/$(COMPONENT_LIB) + +$(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile)))) +$(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile)))) + +# Create target for building this component as a test TEST_SOURCE_FILES = \ test_wl.cpp \ main.cpp \ test_utils.c -INCLUDE_FLAGS = $(addprefix -I,\ - . \ - ../ \ - ../include \ - ../private_include \ - $(addprefix stubs/, \ - newlib/include \ - log/include \ - ) \ - $(SPI_FLASH_DIR)/include \ - ../../esp32/include \ - ../../../tools/catch \ -) - -GCOV ?= gcov - -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)) -$(SPI_FLASH_SIM_DIR)/$(SPI_FLASH_LIB): force - $(MAKE) -C $(SPI_FLASH_SIM_DIR) lib - -$(COMPONENT_LIB): $(OBJ_FILES) - $(AR) rcs $@ $^ - -force: - -lib: $(COMPONENT_LIB) - -partition_table.bin: partition_table.csv - python ../../../components/partition_table/gen_esp32part.py $< $@ - -$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(SPI_FLASH_SIM_DIR)/$(SPI_FLASH_LIB) partition_table.bin - g++ $(LDFLAGS) -o $@ $(TEST_OBJ_FILES) -L$(abspath .) -l:$(COMPONENT_LIB) -L$(SPI_FLASH_SIM_DIR) -l:$(SPI_FLASH_LIB) -g -m32 +$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB) $(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB) partition_table.bin $(SDKCONFIG) + g++ $(LDFLAGS) $(CXXFLAGS) -o $@ $(TEST_OBJ_FILES) -L$(BUILD_DIR) -l:$(COMPONENT_LIB) -L$(SPI_FLASH_SIM_BUILD_DIR) -l:$(SPI_FLASH_SIM_LIB) -L$(STUBS_LIB_BUILD_DIR) -l:$(STUBS_LIB) test: $(TEST_PROGRAM) ./$(TEST_PROGRAM) -$(OUTPUT_DIR): - mkdir -p $(OUTPUT_DIR) +# Create other necessary targets +partition_table.bin: partition_table.csv + python ../../../components/partition_table/gen_esp32part.py --verify $< $@ -COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(TEST_OBJ_FILES:.o=.gc*) +force: -$(COVERAGE_FILES): test - -coverage.info: $(COVERAGE_FILES) - find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} + - lcov --capture --directory ../ --no-external --output-file coverage.info --gcov-tool $(GCOV) - -coverage_report: coverage.info - genhtml coverage.info --output-directory coverage_report - @echo "Coverage report is in coverage_report/index.html" - -clean: - $(MAKE) -C $(SPI_FLASH_SIM_DIR) clean - rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin - rm -f $(COVERAGE_FILES) *.gcov - rm -rf coverage_report/ - rm -f coverage.info - -.PHONY: clean all test lib +.PHONY: all lib test clean force diff --git a/components/wear_levelling/test_wl_host/Makefile.files b/components/wear_levelling/test_wl_host/Makefile.files new file mode 100644 index 0000000000..f96d2cc807 --- /dev/null +++ b/components/wear_levelling/test_wl_host/Makefile.files @@ -0,0 +1,31 @@ +SOURCE_FILES := \ + $(addprefix ../, \ + wear_levelling.cpp \ + crc32.cpp \ + WL_Flash.cpp \ + Partition.cpp \ + ) + +INCLUDE_DIRS := \ + . \ + ../ \ + ../include \ + ../private_include \ + ../../spi_flash/sim \ + $(addprefix ../../spi_flash/sim/stubs/, \ + app_update/include \ + driver/include \ + esp32/include \ + freertos/include \ + log/include \ + newlib/include \ + sdmmc/include \ + vfs/include \ + ) \ + $(addprefix ../../../components/, \ + soc/esp32/include \ + esp32/include \ + bootloader_support/include \ + app_update/include \ + spi_flash/include \ + ) diff --git a/components/wear_levelling/test_wl_host/component.mk b/components/wear_levelling/test_wl_host/component.mk new file mode 100644 index 0000000000..82dea0b0cf --- /dev/null +++ b/components/wear_levelling/test_wl_host/component.mk @@ -0,0 +1,17 @@ +include $(COMPONENT_PATH)/Makefile.files + +COMPONENT_OWNBUILDTARGET := 1 +COMPONENT_OWNCLEANTARGET := 1 + +COMPONENT_ADD_INCLUDEDIRS := $(INCLUDE_DIRS) + +.PHONY: build +build: $(SDKCONFIG_HEADER) + $(MAKE) -C $(COMPONENT_PATH) lib SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) + +CLEAN_FILES := component_project_vars.mk +.PHONY: clean +clean: + $(summary) RM $(CLEAN_FILES) + rm -f $(CLEAN_FILES) + $(MAKE) -C $(COMPONENT_PATH) clean SDKCONFIG=$(SDKCONFIG_HEADER) BUILD_DIR=$(COMPONENT_BUILD_DIR) COMPONENT=$(COMPONENT_NAME) \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/partition_table.csv b/components/wear_levelling/test_wl_host/partition_table.csv index cb7fccd87b..af5bc831eb 100644 --- a/components/wear_levelling/test_wl_host/partition_table.csv +++ b/components/wear_levelling/test_wl_host/partition_table.csv @@ -3,4 +3,4 @@ nvs, data, nvs, 0x9000, 0x6000, phy_init, data, phy, 0xf000, 0x1000, factory, app, factory, 0x10000, 1M, -storage, data, fat, , 1M, \ No newline at end of file +storage, data, , , 1M, \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/sdkconfig.h b/components/wear_levelling/test_wl_host/sdkconfig.h deleted file mode 100644 index 3085b06d82..0000000000 --- a/components/wear_levelling/test_wl_host/sdkconfig.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#define CONFIG_WL_SECTOR_SIZE 4096 \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/sdkconfig/sdkconfig.h b/components/wear_levelling/test_wl_host/sdkconfig/sdkconfig.h new file mode 100644 index 0000000000..0fe66cbbec --- /dev/null +++ b/components/wear_levelling/test_wl_host/sdkconfig/sdkconfig.h @@ -0,0 +1,6 @@ +#pragma once + +#define CONFIG_WL_SECTOR_SIZE 4096 +#define CONFIG_LOG_DEFAULT_LEVEL 3 +#define CONFIG_PARTITION_TABLE_OFFSET 0x8000 +#define CONFIG_ESPTOOLPY_FLASHSIZE "8MB" diff --git a/components/wear_levelling/test_wl_host/stubs/log/include/esp_log.h b/components/wear_levelling/test_wl_host/stubs/log/include/esp_log.h deleted file mode 100644 index a1d838d116..0000000000 --- a/components/wear_levelling/test_wl_host/stubs/log/include/esp_log.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG - -typedef enum { - ESP_LOG_NONE, /*!< No log output */ - ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */ - ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */ - ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */ - ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */ - ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */ -} esp_log_level_t; - -#define LOG_COLOR_E -#define LOG_COLOR_W -#define LOG_COLOR_I -#define LOG_COLOR_D -#define LOG_COLOR_V -#define LOG_RESET_COLOR - -uint32_t esp_log_timestamp(void); -void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4))); - -#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n" - -#define ESP_LOGE( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGD( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#define ESP_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#ifdef __cplusplus -} -#endif - diff --git a/components/wear_levelling/test_wl_host/stubs/log/log.c b/components/wear_levelling/test_wl_host/stubs/log/log.c deleted file mode 100644 index 0cacb3ac79..0000000000 --- a/components/wear_levelling/test_wl_host/stubs/log/log.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include -#include - -#include "esp_log.h" - -void esp_log_write(esp_log_level_t level, - const char *tag, - const char *format, ...) -{ - va_list arg; - va_start(arg, format); - vprintf(format, arg); - va_end(arg); -} - -uint32_t esp_log_timestamp() -{ - return 0; -} \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/stubs/newlib/include/sys/lock.h b/components/wear_levelling/test_wl_host/stubs/newlib/include/sys/lock.h deleted file mode 100644 index b244c9127a..0000000000 --- a/components/wear_levelling/test_wl_host/stubs/newlib/include/sys/lock.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -typedef int _lock_t; - -void _lock_acquire(_lock_t *lock); -void _lock_close(_lock_t *lock); -void _lock_init(_lock_t *lock); -void _lock_release(_lock_t *lock); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/stubs/newlib/lock.c b/components/wear_levelling/test_wl_host/stubs/newlib/lock.c deleted file mode 100644 index d13b6eaac3..0000000000 --- a/components/wear_levelling/test_wl_host/stubs/newlib/lock.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "sys/lock.h" - -void _lock_acquire(_lock_t *lock) -{ - return; -} - -void _lock_close(_lock_t *lock) -{ - return; -} - -void _lock_init(_lock_t *lock) -{ - return; -} - -void _lock_release(_lock_t *lock) -{ - return; -} \ No newline at end of file diff --git a/components/wear_levelling/test_wl_host/test_utils.c b/components/wear_levelling/test_wl_host/test_utils.c index c3181ede75..3e4b05758a 100644 --- a/components/wear_levelling/test_wl_host/test_utils.c +++ b/components/wear_levelling/test_wl_host/test_utils.c @@ -1,7 +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) +void init_spi_flash(const char* 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); -} \ No newline at end of file +} diff --git a/components/wear_levelling/test_wl_host/test_wl.cpp b/components/wear_levelling/test_wl_host/test_wl.cpp index 4b467e9d57..2589024391 100644 --- a/components/wear_levelling/test_wl_host/test_wl.cpp +++ b/components/wear_levelling/test_wl_host/test_wl.cpp @@ -6,22 +6,26 @@ #include "esp_partition.h" #include "wear_levelling.h" #include "WL_Flash.h" +#include "SpiFlash.h" #include "catch.hpp" #include "sdkconfig.h" -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); +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); +extern SpiFlash spiflash; + +#define TEST_COUNT_MAX 100 TEST_CASE("write and read back data", "[wear_levelling]") { - init_spi_flash(0x00400000, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin"); + init_spi_flash(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin"); esp_err_t result; wl_handle_t wl_handle; int flash_handle; - const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage"); + const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); // Mount wear-levelled partition result = wl_mount(partition, &wl_handle); @@ -85,3 +89,122 @@ TEST_CASE("write and read back data", "[wear_levelling]") free(data); free(read); } + +TEST_CASE("power down test", "[wear_levelling]") +{ + init_spi_flash(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, "partition_table.bin"); + + esp_err_t result; + wl_handle_t wl_handle; + + int flash_handle; + const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); + + // Mount wear-levelled partition + result = wl_mount(partition, &wl_handle); + REQUIRE(result == ESP_OK); + + // Get wl partition information + size_t sector_size = wl_sector_size(wl_handle); + int32_t sectors_count = wl_size(wl_handle) / sector_size; + + uint32_t add_const = 0; + uint32_t *sector_data = new uint32_t[sector_size / sizeof(uint32_t)]; + + // Fill partition with check data + for (int32_t i = 0; i < sectors_count; i++) { + REQUIRE(wl_erase_range(wl_handle, i * sector_size, sector_size) == ESP_OK); + for (uint32_t m = 0; m < sector_size / sizeof(uint32_t); m++) { + uint32_t temp_data = i * sector_size + add_const + m; + sector_data[m] = temp_data; + } + REQUIRE(wl_write(wl_handle, i * sector_size, sector_data, sector_size) == ESP_OK); + } + + for (int32_t i = 0; i < sectors_count; i++) { + result |= wl_read(wl_handle, i * sector_size, sector_data, sector_size); + for (uint32_t m = 0; m < sector_size / sizeof(uint32_t); m++) { + uint32_t temp_data = i * sector_size + add_const + m; + REQUIRE(temp_data == sector_data[m]); + if (temp_data != sector_data[m]) { + printf("Error - read: %08x, expected %08x\n", sector_data[m], temp_data); + } + } + } + + // Perform test + int32_t max_count = 100; + int32_t max_check_count = TEST_COUNT_MAX; + + printf("used_sectors_count=%d\n", max_check_count); + + for (int32_t k = 0; k < max_check_count; k++) { + + spiflash.set_total_erase_cycles_limit(max_count); + + int32_t err_sector = -1; + for (int32_t i = 0; i < sectors_count; i++) { + result = ESP_OK; + result = wl_erase_range(wl_handle, i * sector_size, sector_size); + if (result != ESP_OK) { + err_sector = i; + break; + } + for (uint32_t m = 0; m < sector_size / sizeof(uint32_t); m++) { + uint32_t temp_data = i * sector_size + add_const + m; + sector_data[m] = temp_data; + } + result = wl_write(wl_handle, i * sector_size, sector_data, sector_size); + if (result != ESP_OK) { + err_sector = i; + break; + } + } + + if (err_sector >= 0) { + max_count++; + } else { + max_count = 0; + } + + spiflash.set_total_erase_cycles_limit(0); + + result = wl_unmount(wl_handle); + REQUIRE(result == ESP_OK); + + result = wl_mount(partition, &wl_handle); + REQUIRE(result == ESP_OK); + + for (int32_t i = 0; i < sectors_count; i++) { + if (i != err_sector) { + result |= wl_read(wl_handle, i * sector_size, sector_data, sector_size); + for (uint32_t m = 0; m < sector_size / sizeof(uint32_t); m++) { + uint32_t temp_data = i * sector_size + add_const + m; + REQUIRE(temp_data == sector_data[m]); + if (temp_data != sector_data[m]) { + printf("Error - read: %08x, expected %08x, m=%i, sector=%i\n", sector_data[m], temp_data, m, i); + } + } + } + } + + if (err_sector != -1) { + result |= wl_erase_range(wl_handle, err_sector * sector_size, sector_size); + for (uint32_t m = 0; m < sector_size / sizeof(uint32_t); m++) { + uint32_t temp_data = err_sector * sector_size + add_const + m; + sector_data[m] = temp_data; + } + result |= wl_write(wl_handle, err_sector * sector_size, sector_data, sector_size); + } + + spiflash.reset_total_erase_cycles(); + + printf("[%3.f%%] err_sector=%i\n", (float)k / ((float)max_check_count) * 100.0f, err_sector); + } + + delete[] sector_data; + + // Unmount + result = wl_unmount(wl_handle); + REQUIRE(result == ESP_OK); +} \ No newline at end of file diff --git a/make/component_wrapper.mk b/make/component_wrapper.mk index f2b2364202..a545074728 100644 --- a/make/component_wrapper.mk +++ b/make/component_wrapper.mk @@ -212,7 +212,7 @@ build: $(COMPONENT_LIBRARY) $(COMPONENT_LIBRARY): $(COMPONENT_OBJS) $(COMPONENT_EMBED_OBJS) $(summary) AR $(patsubst $(PWD)/%,%,$(CURDIR))/$@ rm -f $@ - $(AR) cru $@ $^ + $(AR) $(ARFLAGS) $@ $^ endif # If COMPONENT_OWNCLEANTARGET is not set, define a phony clean target diff --git a/make/project.mk b/make/project.mk index bf84946720..5a27cb776f 100644 --- a/make/project.mk +++ b/make/project.mk @@ -345,7 +345,9 @@ else CXXFLAGS += -fno-exceptions endif -export CFLAGS CPPFLAGS CXXFLAGS +ARFLAGS := cru + +export CFLAGS CPPFLAGS CXXFLAGS ARFLAGS # Set default values that were not previously defined CC ?= gcc