-TEST_PROGRAM=test_fatfs
+COMPONENT=fatfs
-# Use wear levelling
-TEST_WL_COMPONENT=$(IDF_PATH)/components/wear_levelling
-TEST_WL_DIR=$(TEST_WL_COMPONENT)/test_wl_host
-TEST_WL_LIB=libtest_wl.a
+TEST_PROGRAM=test_$(COMPONENT)
-TEST_PARTITION_SIM_DIR=$(IDF_PATH)/components/spi_flash/sim
-TEST_PARTITION_SIM_LIB=libpartition_sim.a
+#Expose as a library
+COMPONENT_LIB=lib$(COMPONENT).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=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_fatfs.cpp \
$(addprefix ../src/, \
diskio.c \
ff.c \
) \
$(addprefix ./stubs/, log/log.c)
+TEST_SOURCE_FILES = \
+ test_fatfs.cpp \
+ main.cpp \
+ test_utils.c
+
INCLUDE_FLAGS = $(addprefix -I,\
../src \
. \
sdmmc/include \
log/include \
) \
+ $(SPI_FLASH_DIR)/include \
+ $(WEAR_LEVELLING_DIR)/include \
../../esp32/include \
- $(TEST_PARTITION_SIM_DIR)/include \
- $(TEST_WL_COMPONENT)/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))
+
+$(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:
-$(TEST_WL_DIR)/$(TEST_WL_LIB): force
- $(MAKE) -C $(TEST_WL_DIR) lib
+$(COMPONENT_LIB): $(OBJ_FILES)
+ $(AR) rcs $@ $^
-$(TEST_PARTITION_SIM_DIR)/$(TEST_PARTITION_SIM_LIB): force
- $(MAKE) -C $(TEST_PARTITION_SIM_DIR) lib
+lib: $(COMPONENT_LIB)
-force:
+partition_table.bin: partition_table.csv
+ python ../../partition_table/gen_esp32part.py --verify $< $@
-$(TEST_PROGRAM): $(OBJ_FILES) $(TEST_WL_DIR)/$(TEST_WL_LIB) $(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)
+$(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: $(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 {} +
@echo "Coverage report is in coverage_report/index.html"
clean:
- rm -f $(OBJ_FILES) $(TEST_PROGRAM)
- $(MAKE) -C $(TEST_WL_DIR) clean
- $(MAKE) -C $(TEST_PARTITION_SIM_DIR) clean
+ rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin
+ $(MAKE) -C $(WEAR_LEVELLING_HOST_DIR) clean
+ $(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
--- /dev/null
+# 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, fat, , 1M,
\ No newline at end of file
#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("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");
+
FRESULT fr_result;
BYTE pdrv;
FATFS fs;
esp_err_t esp_result;
- // Create a 4MB partition
- uint32_t size = 0x00400000;
- int flash_handle = esp_flash_create(size, 4096, 1);
- esp_partition_t partition = esp_partition_create(size, 0, flash_handle);
+ const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
// Mount wear-levelled partition
wl_handle_t wl_handle;
- esp_result = wl_mount(&partition, &wl_handle);
+ esp_result = wl_mount(partition, &wl_handle);
REQUIRE(esp_result == ESP_OK);
// Get a physical drive
fr_result = f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
REQUIRE(fr_result == FR_OK);
- const char data[] = "Hello, World!";
- char *read = (char*) malloc(sizeof(data));
+ // Generate data
+ uint32_t data_size = 100000;
+
+ char *data = (char*) malloc(data_size);
+ char *read = (char*) malloc(data_size);
- fr_result = f_write(&file, data, sizeof(data), &bw);
+ for(uint32_t i = 0; i < data_size; i += sizeof(i))
+ {
+ *((uint32_t*)(data + i)) = i;
+ }
+
+ // Write generated data
+ fr_result = f_write(&file, data, data_size, &bw);
REQUIRE(fr_result == FR_OK);
- REQUIRE(bw == sizeof(data));
+ REQUIRE(bw == data_size);
// Move to beginning of file
fr_result = f_lseek(&file, 0);
REQUIRE(fr_result == FR_OK);
- fr_result = f_read(&file, read, sizeof(data), &bw);
+ // Read written data
+ fr_result = f_read(&file, read, data_size, &bw);
REQUIRE(fr_result == FR_OK);
- REQUIRE(bw == sizeof(data));
+ REQUIRE(bw == data_size);
- REQUIRE(strcmp(data, read) == 0);
+ REQUIRE(memcmp(data, read, data_size) == 0);
// Close file
fr_result = f_close(&file);
fr_result = f_mount(0, "", 0);
REQUIRE(fr_result == FR_OK);
- esp_flash_delete(flash_handle);
-
free(read);
+ free(data);
}
--- /dev/null
+#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);
+}
\ No newline at end of file
+++ /dev/null
-// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD\r
-//\r
-// Licensed under the Apache License, Version 2.0 (the "License");\r
-// you may not use this file except in compliance with the License.\r
-// You may obtain a copy of the License at\r
-\r
-// http://www.apache.org/licenses/LICENSE-2.0\r
-//\r
-// Unless required by applicable law or agreed to in writing, software\r
-// distributed under the License is distributed on an "AS IS" BASIS,\r
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-// See the License for the specific language governing permissions and\r
-// limitations under the License.\r
-\r
-#include "Flash_Emulator.h"\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise, size_t min_size)\r
-{\r
- this->reset_count = 0x7fffffff;\r
- this->size = size;\r
- this->sector_sise = sector_sise;\r
- this->min_size = min_size;\r
- this->buff = (uint8_t *)malloc(this->size);\r
- this->access_count = new uint32_t[this->size / this->sector_sise];\r
- memset(this->access_count, 0, this->size / this->sector_sise * sizeof(uint32_t));\r
- memset(this->buff, 1, this->size);\r
-}\r
-\r
-size_t Flash_Emulator::chip_size()\r
-{\r
- return this->size;\r
-}\r
-size_t Flash_Emulator::sector_size()\r
-{\r
- return this->sector_sise;\r
-}\r
-\r
-esp_err_t Flash_Emulator::erase_sector(size_t sector)\r
-{\r
- esp_err_t result = ESP_OK;\r
- if ((this->reset_count != 0x7fffffff) && (this->reset_count != 0)) {\r
- this->reset_count--;\r
- }\r
- if (this->reset_count <= 0) {\r
- result = ESP_FAIL;\r
- return result;\r
- }\r
- memset(&this->buff[sector * this->sector_sise], -1, this->sector_sise);\r
- this->access_count[sector]++;\r
- return result;\r
-}\r
-\r
-uint32_t Flash_Emulator::get_access_minmax()\r
-{\r
- uint32_t min = INT32_MAX;\r
- uint32_t max = 0;\r
- for (size_t i = 0; i < (this->size / this->sector_sise) - 2; i++) {\r
- if (this->access_count[i] < min) {\r
- min = this->access_count[i];\r
- }\r
- if (this->access_count[i] > max) {\r
- max = this->access_count[i];\r
- }\r
- }\r
- return max - min;\r
-}\r
-\r
-esp_err_t Flash_Emulator::erase_range(size_t start_address, size_t size)\r
-{\r
- esp_err_t result = ESP_OK;\r
- uint32_t start_sector = start_address / this->sector_sise;\r
- uint32_t count = (size + this->sector_sise - 1) / this->sector_sise;\r
- for (size_t i = 0; i < count; i++) {\r
- result |= this->erase_sector(start_sector + i);\r
- }\r
- return result;\r
-}\r
-\r
-esp_err_t Flash_Emulator::write(size_t dest_addr, const void *src, size_t size)\r
-{\r
- esp_err_t result = ESP_OK;\r
- if ((size % this->min_size) != 0) {\r
- result = ESP_ERR_INVALID_SIZE;\r
- return result; \r
- }\r
- if ((dest_addr % this->min_size) != 0) {\r
- result = ESP_ERR_INVALID_SIZE;\r
- return result; \r
- }\r
- if ((this->reset_count != 0x7fffffff) && (this->reset_count != 0)) {\r
- this->reset_count--;\r
- }\r
- if (this->reset_count <= 0) {\r
- result = ESP_FAIL;\r
- return result;\r
- }\r
- memcpy(&this->buff[dest_addr], src, size);\r
- return result;\r
-}\r
-\r
-esp_err_t Flash_Emulator::read(size_t src_addr, void *dest, size_t size)\r
-{\r
- esp_err_t result = ESP_OK;\r
- if (this->reset_count <= 0) {\r
- result = ESP_FAIL;\r
- return result;\r
- }\r
- memcpy(dest, &this->buff[src_addr], size);\r
- return result;\r
-}\r
-\r
-Flash_Emulator::~Flash_Emulator()\r
-{\r
- free(this->buff);\r
- delete this->access_count;\r
-}\r
-\r
-void Flash_Emulator::SetResetCount(uint32_t count)\r
-{\r
- this->reset_count = count;\r
-}\r
-\r
-void Flash_Emulator::SetResetSector(size_t sector)\r
-{\r
- this->reset_sector = sector;\r
-}\r
+++ /dev/null
-// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD\r
-//\r
-// Licensed under the Apache License, Version 2.0 (the "License");\r
-// you may not use this file except in compliance with the License.\r
-// You may obtain a copy of the License at\r
-\r
-// http://www.apache.org/licenses/LICENSE-2.0\r
-//\r
-// Unless required by applicable law or agreed to in writing, software\r
-// distributed under the License is distributed on an "AS IS" BASIS,\r
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-// See the License for the specific language governing permissions and\r
-// limitations under the License.\r
-\r
-#ifndef _Flash_Emulator_H_\r
-#define _Flash_Emulator_H_\r
-\r
-#include "esp_err.h"\r
-\r
-/**\r
-* @brief This class is used to emulate flash devices. Class implements Flash_Access interface\r
-*\r
-*/\r
-class Flash_Emulator\r
-{\r
-\r
-public:\r
- Flash_Emulator(size_t size, size_t sector_sise, size_t min_size);\r
-\r
- virtual size_t chip_size();\r
-\r
- virtual esp_err_t erase_sector(size_t sector);\r
- virtual esp_err_t erase_range(size_t start_address, size_t size);\r
-\r
- virtual esp_err_t write(size_t dest_addr, const void *src, size_t size);\r
- virtual esp_err_t read(size_t src_addr, void *dest, size_t size);\r
-\r
- virtual size_t sector_size();\r
-\r
- virtual ~Flash_Emulator();\r
-\r
- uint32_t get_access_minmax();\r
-public:\r
- size_t size;\r
- size_t sector_sise;\r
- size_t min_size;\r
- uint8_t *buff;\r
-\r
- uint32_t *access_count;\r
-\r
-public:\r
- uint32_t reset_count;\r
- size_t reset_sector;\r
- void SetResetCount(uint32_t count);\r
- void SetResetSector(size_t sector);\r
-\r
-};\r
-\r
-#endif // _Flash_Emulator_H_\r
-PARTITION_SIM=partition_sim
-PARTITION_SIM_LIB=lib$(PARTITION_SIM).a
+COMPONENT=spi_flash
+COMPONENT_LIB=lib$(COMPONENT).a
-lib: $(PARTITION_SIM_LIB)
+all: lib
SOURCE_FILES = \
- Flash_Emulator.cpp \
- partition.c
+ ../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 \
+ )
INCLUDE_FLAGS = $(addprefix -I,\
. \
- ./include \
+ ../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 \
+)
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
+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))
-
-$(PARTITION_SIM_LIB): $(OBJ_FILES)
+$(COMPONENT_LIB): $(OBJ_FILES)
$(AR) rcs $@ $^
-COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(OBJ_FILES:.o=.gc*)
+lib: $(COMPONENT_LIB)
+
+COVERAGE_FILES = $(OBJ_FILES:.o=.gc*)
-$(COVERAGE_FILES): $(TEST_PROGRAM) lib
+$(COVERAGE_FILES): lib
coverage.info: $(COVERAGE_FILES)
find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} +
@echo "Coverage report is in coverage_report/index.html"
clean:
- rm -f $(OBJ_FILES) $(PARTITION_SIM_LIB)
+ rm -f $(OBJ_FILES) $(COMPONENT_LIB)
rm -f $(COVERAGE_FILES) *.gcov
rm -rf coverage_report/
rm -f coverage.info
-.PHONY: clean lib
+.PHONY: clean lib all
--- /dev/null
+// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+\r
+// http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+#include "SpiFlash.h"\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <fstream>\r
+#include <iostream>\r
+#include <vector>\r
+#include <string>\r
+\r
+#include "esp_flash_data_types.h"\r
+\r
+using namespace std;\r
+\r
+SpiFlash::SpiFlash()\r
+{\r
+ return;\r
+}\r
+\r
+SpiFlash::~SpiFlash()\r
+{\r
+ deinit();\r
+}\r
+\r
+void SpiFlash::init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin)\r
+{\r
+ // De-initialize first\r
+ deinit();\r
+\r
+ this->chip_size = chip_size;\r
+ this->block_size = block_size;\r
+ this->sector_size = sector_size;\r
+ this->page_size = page_size;\r
+\r
+ this->memory = (uint8_t *) malloc(this->chip_size);\r
+ memset(this->memory, 0xFF, this->chip_size);\r
+\r
+ ifstream ifd(partitions_bin, ios::binary | ios::ate);\r
+ int size = ifd.tellg();\r
+\r
+ ifd.seekg(0, ios::beg);\r
+ vector<char> buffer;\r
+\r
+ buffer.resize(size);\r
+\r
+ ifd.read(buffer.data(), size);\r
+\r
+ memcpy(&this->memory[ESP_PARTITION_TABLE_ADDR], buffer.data(), buffer.size());\r
+}\r
+\r
+void SpiFlash::deinit()\r
+{\r
+ if(inited)\r
+ {\r
+ free(this->memory);\r
+ }\r
+}\r
+\r
+size_t SpiFlash::get_chip_size()\r
+{\r
+ return this->chip_size;\r
+}\r
+\r
+size_t SpiFlash::get_sector_size()\r
+{\r
+ return this->sector_size;\r
+}\r
+\r
+esp_rom_spiflash_result_t SpiFlash::erase_block(uint32_t block)\r
+{\r
+ memset(&this->memory[block * this->block_size], 0xFF, this->block_size);\r
+ return ESP_ROM_SPIFLASH_RESULT_OK;\r
+}\r
+\r
+esp_rom_spiflash_result_t SpiFlash::erase_sector(size_t sector)\r
+{\r
+ memset(&this->memory[sector * this->sector_size], 0xFF, this->sector_size);\r
+ return ESP_ROM_SPIFLASH_RESULT_OK;\r
+}\r
+\r
+esp_rom_spiflash_result_t SpiFlash::erase_page(uint32_t page)\r
+{\r
+ memset(&this->memory[page * this->page_size], 0xFF, this->page_size);\r
+ return ESP_ROM_SPIFLASH_RESULT_OK;\r
+}\r
+\r
+esp_rom_spiflash_result_t SpiFlash::write(size_t dest_addr, const void *src, size_t size)\r
+{\r
+ // Emulate inability to set programmed bits without erasing\r
+ for(uint32_t ctr = 0; ctr < size; ctr++)\r
+ {\r
+ uint8_t data = ((uint8_t*)src)[ctr];\r
+ uint8_t written = this->memory[dest_addr + ctr];\r
+\r
+ data &= written;\r
+\r
+ this->memory[dest_addr + ctr] = data;\r
+ }\r
+\r
+ return ESP_ROM_SPIFLASH_RESULT_OK;\r
+}\r
+\r
+esp_rom_spiflash_result_t SpiFlash::read(size_t src_addr, void *dest, size_t size)\r
+{\r
+ memcpy(dest, &this->memory[src_addr], size);\r
+ return ESP_ROM_SPIFLASH_RESULT_OK;\r
+}\r
+\r
+uint8_t* SpiFlash::get_memory_ptr(uint32_t src_address)\r
+{\r
+ return &this->memory[src_address];\r
+}
\ No newline at end of file
--- /dev/null
+// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+\r
+// http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+#ifndef _SpiFlash_H_\r
+#define _SpiFlash_H_\r
+\r
+#include "esp_err.h"\r
+#include "rom/spi_flash.h"\r
+\r
+/**\r
+* @brief This class is used to emulate flash devices.\r
+*\r
+*/\r
+class SpiFlash\r
+{\r
+\r
+public:\r
+ SpiFlash();\r
+ ~SpiFlash();\r
+\r
+ void init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin);\r
+\r
+ esp_rom_spiflash_result_t erase_block(uint32_t block);\r
+ esp_rom_spiflash_result_t erase_sector(uint32_t sector);\r
+ esp_rom_spiflash_result_t erase_page(uint32_t page);\r
+\r
+ esp_rom_spiflash_result_t write(size_t dest_addr, const void *src, size_t size);\r
+ esp_rom_spiflash_result_t read(size_t src_addr, void *dest, size_t size);\r
+\r
+ size_t get_chip_size();\r
+ size_t get_sector_size();\r
+\r
+ uint8_t* get_memory_ptr(uint32_t src_address);\r
+\r
+private:\r
+ bool inited = false;\r
+\r
+ size_t chip_size;\r
+ size_t block_size;\r
+ size_t sector_size;\r
+ size_t page_size;\r
+\r
+ uint8_t* memory;\r
+\r
+ void* partitions;\r
+ uint8_t partitions_num;\r
+\r
+ void deinit();\r
+};\r
+\r
+#endif // _SpiFlash_H_\r
--- /dev/null
+#include "SpiFlash.h"
+
+#include "esp_spi_flash.h"
+#include "esp_partition.h"
+
+#include "esp_err.h"
+#include "rom/spi_flash.h"
+
+static 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)
+{
+ spiflash.init(chip_size, block_size, sector_size, page_size, partitions_bin);
+
+ g_rom_flashchip.chip_size = chip_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,
+ const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
+{
+ *out_handle = 0;
+ *out_ptr = (void*)spiflash.get_memory_ptr(src_addr);
+
+ return ESP_OK;
+}
+
+void spi_flash_munmap(spi_flash_mmap_handle_t handle)
+{
+ return;
+}
+
+esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t target, uint32_t *dest, int32_t len)
+{
+ return spiflash.read(target, dest, len);
+}
+
+esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block)
+{
+ return spiflash.erase_block(block);
+}
+
+esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector)
+{
+ return spiflash.erase_sector(sector);
+}
+
+esp_rom_spiflash_result_t esp_rom_spiflash_erase_page(uint32_t page)
+{
+ return spiflash.erase_page(page);
+}
+
+esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t *src, int32_t len)
+{
+ return spiflash.write(target, src, len);
+}
+
+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
--- /dev/null
+#include "esp_spi_flash.h"
+#include "esp_partition.h"
+
+#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);
+
+void spi_flash_init(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);
+}
+
+void spi_flash_mark_modified_region(size_t start_addr, size_t length)
+{
+ return;
+}
+
+esp_rom_spiflash_result_t esp_rom_spiflash_unlock()
+{
+ return ESP_ROM_SPIFLASH_RESULT_OK;
+}
+
+void spi_flash_init_lock()
+{
+ return;
+}
+
+void spi_flash_op_lock()
+{
+ return;
+}
+
+void spi_flash_op_unlock()
+{
+ return;
+}
+
+void spi_flash_disable_interrupts_caches_and_other_cpu()
+{
+ return;
+}
+
+void spi_flash_enable_interrupts_caches_and_other_cpu()
+{
+ return;
+}
+
+void spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
+{
+ return;
+}
+
+void spi_flash_enable_interrupts_caches_no_os()
+{
+ return;
+}
\ No newline at end of file
+++ /dev/null
-#pragma once
-
-#include <stdbool.h>
-
-#include "sdkconfig.h"
-#include "esp_err.h"
-#include "esp_spi_flash.h"
-
-#if defined(__cplusplus)
-extern "C" { // Make sure we have C-declarations in C++ programs
-#endif
-
-/**
- * @brief Partition type
- * @note Keep this enum in sync with PartitionDefinition class gen_esp32part.py
- */
-typedef enum {
- ESP_PARTITION_TYPE_APP = 0x00, //!< Application partition type
- ESP_PARTITION_TYPE_DATA = 0x01, //!< Data partition type
-} esp_partition_type_t;
-
-/**
- * @brief Partition subtype
- * @note Keep this enum in sync with PartitionDefinition class gen_esp32part.py
- */
-typedef enum {
- ESP_PARTITION_SUBTYPE_APP_FACTORY = 0x00, //!< Factory application partition
- ESP_PARTITION_SUBTYPE_APP_OTA_MIN = 0x10, //!< Base for OTA partition subtypes
- ESP_PARTITION_SUBTYPE_APP_OTA_0 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 0, //!< OTA partition 0
- ESP_PARTITION_SUBTYPE_APP_OTA_1 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 1, //!< OTA partition 1
- ESP_PARTITION_SUBTYPE_APP_OTA_2 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 2, //!< OTA partition 2
- ESP_PARTITION_SUBTYPE_APP_OTA_3 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 3, //!< OTA partition 3
- ESP_PARTITION_SUBTYPE_APP_OTA_4 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 4, //!< OTA partition 4
- ESP_PARTITION_SUBTYPE_APP_OTA_5 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 5, //!< OTA partition 5
- ESP_PARTITION_SUBTYPE_APP_OTA_6 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 6, //!< OTA partition 6
- ESP_PARTITION_SUBTYPE_APP_OTA_7 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 7, //!< OTA partition 7
- ESP_PARTITION_SUBTYPE_APP_OTA_8 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 8, //!< OTA partition 8
- ESP_PARTITION_SUBTYPE_APP_OTA_9 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 9, //!< OTA partition 9
- ESP_PARTITION_SUBTYPE_APP_OTA_10 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 10,//!< OTA partition 10
- ESP_PARTITION_SUBTYPE_APP_OTA_11 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 11,//!< OTA partition 11
- ESP_PARTITION_SUBTYPE_APP_OTA_12 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 12,//!< OTA partition 12
- ESP_PARTITION_SUBTYPE_APP_OTA_13 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 13,//!< OTA partition 13
- ESP_PARTITION_SUBTYPE_APP_OTA_14 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 14,//!< OTA partition 14
- ESP_PARTITION_SUBTYPE_APP_OTA_15 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 15,//!< OTA partition 15
- ESP_PARTITION_SUBTYPE_APP_OTA_MAX = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 16,//!< Max subtype of OTA partition
- ESP_PARTITION_SUBTYPE_APP_TEST = 0x20, //!< Test application partition
-
- ESP_PARTITION_SUBTYPE_DATA_OTA = 0x00, //!< OTA selection partition
- ESP_PARTITION_SUBTYPE_DATA_PHY = 0x01, //!< PHY init data partition
- ESP_PARTITION_SUBTYPE_DATA_NVS = 0x02, //!< NVS partition
- ESP_PARTITION_SUBTYPE_DATA_COREDUMP = 0x03, //!< COREDUMP partition
-
- ESP_PARTITION_SUBTYPE_DATA_ESPHTTPD = 0x80, //!< ESPHTTPD partition
- ESP_PARTITION_SUBTYPE_DATA_FAT = 0x81, //!< FAT partition
- ESP_PARTITION_SUBTYPE_DATA_SPIFFS = 0x82, //!< SPIFFS partition
-
- ESP_PARTITION_SUBTYPE_ANY = 0xff, //!< Used to search for partitions with any subtype
-} esp_partition_subtype_t;
-
-/**
- * @brief partition information structure
- *
- * This is not the format in flash, that format is esp_partition_info_t.
- *
- * However, this is the format used by this API.
- */
-typedef struct {
- esp_partition_type_t type; /*!< partition type (app/data) */
- esp_partition_subtype_t subtype; /*!< partition subtype */
- uint32_t address; /*!< starting address of the partition in flash */
- uint32_t size; /*!< size of the partition, in bytes */
- char label[17]; /*!< partition label, zero-terminated ASCII string */
- bool encrypted; /*!< flag is set to true if partition is encrypted */
-
- int handle; /*!< simulator handle */
-} esp_partition_t;
-
-esp_err_t esp_partition_write(const esp_partition_t* partition,
- size_t dst_offset, const void* src, size_t size);
-
-esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
- uint32_t start_addr, uint32_t size);
-
-esp_err_t esp_partition_read(const esp_partition_t* partition,
- size_t src_offset, void* dst, size_t size);
-
-esp_partition_t esp_partition_create(uint32_t size, uint32_t start, int handle);
-
-int esp_flash_create(uint32_t size, uint32_t sector_size, uint32_t write_size);
-
-void esp_flash_delete(int handle);
-
-#if defined(__cplusplus)
-}
-#endif
-
+++ /dev/null
-#pragma once
-
-#define CONFIG_WL_SECTOR_SIZE 4096
-
-#define SPI_FLASH_SEC_SIZE CONFIG_WL_SECTOR_SIZE
-#define SPI_FLASH_WRITE_SIZE 16
-
+++ /dev/null
-#include <assert.h>
-
-#include "esp_partition.h"
-#include "esp_spi_flash.h"
-#include "Flash_Emulator.h"
-
-#define EMULATORS_MAX 8
-
-static Flash_Emulator* emulators[EMULATORS_MAX];
-
-int esp_flash_create(uint32_t size, uint32_t sector_size, uint32_t write_size)
-{
- int handle = -1;
-
- for (int i = 0; i < EMULATORS_MAX; i++) {
- if (emulators[i] == NULL) {
- emulators[i] = new Flash_Emulator(size, sector_size, write_size);
- handle = i;
- break;
- }
- }
-
- assert(handle != -1);
-
- return handle;
-}
-
-esp_partition_t esp_partition_create(uint32_t size, uint32_t start, int flash)
-{
- esp_partition_t partition;
-
- // Fills partition with default values, and attaches the flash_emulator reference
- // to the struct
- partition.type = ESP_PARTITION_TYPE_DATA;
- partition.subtype = ESP_PARTITION_SUBTYPE_ANY;
- partition.address = start;
- partition.size = size;
- partition.handle = flash;
-
- return partition;
-}
-
-void esp_flash_delete(int handle)
-{
- delete emulators[handle];
- emulators[handle] = NULL;
-}
-
-esp_err_t esp_partition_write(const esp_partition_t* partition,
- size_t dst_offset, const void* src, size_t size)
-{
- emulators[partition->handle]->write(dst_offset, src, size);
- return ESP_OK;
-}
-
-esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
- uint32_t start_addr, uint32_t size)
-{
- emulators[partition->handle]->erase_range(start_addr, size);
- return ESP_OK;
-}
-
-esp_err_t esp_partition_read(const esp_partition_t* partition,
- size_t src_offset, void* dst, size_t size)
-{
- emulators[partition->handle]->read(src_offset, dst, size);
- return ESP_OK;
-}
-#pragma once
\ No newline at end of file
+#pragma once
+
--- /dev/null
+#include "esp_ota_ops.h"
+#include "esp_partition.h"
+
+const esp_partition_t* esp_ota_get_running_partition(void)
+{
+ // Return first instance of an app partition
+ const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
+ ESP_PARTITION_SUBTYPE_ANY,
+ NULL);
+ assert(partition != NULL);
+
+ return partition;
+}
--- /dev/null
+#pragma once
+
+#include "projdefs.h"
+#include "semphr.h"
+
+// Avoid redefinition compile error. Put here since this is included
+// in flash_ops.c.
+#define spi_flash_init() overriden_spi_flash_init()
--- /dev/null
+#pragma once
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define pdTRUE 1
+
+#if defined(__cplusplus)
+}
+#endif
--- /dev/null
+#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
--- /dev/null
+#pragma once
\ No newline at end of file
--- /dev/null
+#pragma once
+
+#include <stdint.h>
+#include <stdio.h>
+
+#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__); }
+
+// Assume that flash encryption is not enabled. Put here since in partition.c
+// esp_log.h is included later than esp_flash_encrypt.h.
+#define esp_flash_encryption_enabled() false
+
+#ifdef __cplusplus
+}
+#endif
+
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <time.h>
+
+#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;
+}
+
--- /dev/null
+#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
+
--- /dev/null
+#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;
+}
-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 \
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 {} +
@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
--- /dev/null
+# 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,
#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
+++ /dev/null
-#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
\ No newline at end of file
#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;
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;
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);
-}
\ No newline at end of file
+ free(data);
+}
--- /dev/null
+#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);
+}
\ No newline at end of file
^^^^^^^^^^^^^^^\r
The WLC consist of few components that are implemented in different files. The list and brief description of these components written below.\r
\r
- - Flash_Access - memory access interface. Used to access the memory. A classes WL_Flash, Partition, SPI_Flash, Flash_Emulator are implements this interface.\r
+ - Flash_Access - memory access interface. Used to access the memory. A classes WL_Flash, Partition, SPI_Flash are implements this interface.\r
- SPI_Flash - class implements the Flash_Access interface to provide access to the flash memory.\r
- Partition - class implements the Flash_Access interface to provide access to the partition.\r
- - Flash_Emulator - class implements the Flash_Access interface to provide test functionality for WLC testing.\r
- WL_Flash - the main class that implements wear levelling functionality.\r
- WL_State - contains state structure of the WLC.\r
- WL_Config - contains structure to configure the WLC component at startup.\r
-TEST_PROGRAM=test_wl
+COMPONENT=wear_levelling
+
+TEST_PROGRAM=test_$(COMPONENT)
# Expose as a library for FS components that require wear_levelling
-TEST_LIB=lib$(TEST_PROGRAM).a
+COMPONENT_LIB=lib$(COMPONENT).a
# Use simulated block device
-TEST_PARTITION_SIM_DIR=$(IDF_PATH)/components/spi_flash/sim
-TEST_PARTITION_SIM_LIB=libpartition_sim.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)
-LIB_SOURCE_FILES = \
- $(addprefix stubs/, \
- newlib/lock.c \
- log/log.c \
- esp32/crc.cpp \
- ) \
+SOURCE_FILES = \
$(addprefix ../, \
wear_levelling.cpp \
crc32.cpp \
WL_Flash.cpp \
Partition.cpp \
) \
+ $(addprefix stubs/, \
+ newlib/lock.c \
+ log/log.c \
+ esp32/crc.cpp \
+ )
TEST_SOURCE_FILES = \
test_wl.cpp \
- main.cpp
+ main.cpp \
+ test_utils.c
INCLUDE_FLAGS = $(addprefix -I,\
. \
../include \
../private_include \
$(addprefix stubs/, \
- esp32/include \
newlib/include \
log/include \
) \
+ $(SPI_FLASH_DIR)/include \
../../esp32/include \
- ../../spi_flash/sim/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
-LIB_OBJ_FILES = $(filter %.o, $(LIB_SOURCE_FILES:.cpp=.o) $(LIB_SOURCE_FILES:.c=.o))
+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
+$(SPI_FLASH_SIM_DIR)/$(SPI_FLASH_LIB): force
+ $(MAKE) -C $(SPI_FLASH_SIM_DIR) lib
-$(TEST_LIB): $(LIB_OBJ_FILES)
+$(COMPONENT_LIB): $(OBJ_FILES)
$(AR) rcs $@ $^
-lib: $(TEST_LIB)
+force:
-$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(TEST_PARTITION_SIM_DIR)/$(TEST_PARTITION_SIM_LIB)
- g++ $(LDFLAGS) -o $@ $(TEST_OBJ_FILES) -L$(abspath .) -l:$(TEST_LIB) -L$(TEST_PARTITION_SIM_DIR) -l:$(TEST_PARTITION_SIM_LIB)
+lib: $(COMPONENT_LIB)
-force:
+partition_table.bin: partition_table.csv
+ python ../../../components/partition_table/gen_esp32part.py --verify $< $@
+
+$(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: $(TEST_PROGRAM)
./$(TEST_PROGRAM)
$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)
-COVERAGE_FILES = $(LIB_OBJ_FILES:.o=.gc*) $(TEST_OBJ_FILES:.o=.gc*)
+COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) $(TEST_OBJ_FILES:.o=.gc*)
-$(COVERAGE_FILES): $(TEST_PROGRAM) test
+$(COVERAGE_FILES): test
coverage.info: $(COVERAGE_FILES)
find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} +
@echo "Coverage report is in coverage_report/index.html"
clean:
- $(MAKE) -C $(TEST_PARTITION_SIM_DIR) clean
- rm -f $(LIB_OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(TEST_LIB)
+ $(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
#define CATCH_CONFIG_MAIN
-#include "catch.hpp"
+#include "catch.hpp"
\ No newline at end of file
--- /dev/null
+# 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, fat, , 1M,
\ No newline at end of file
#pragma once
+#define CONFIG_WL_SECTOR_SIZE 4096
\ No newline at end of file
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
#include <stdint.h>
#include <stdbool.h>
-#include "rom/crc.h"
-
static const unsigned int crc32_le_table[256] = {
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L,
0x0edb8832L, 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0x90bf1d91L,
0x4369e96aL, 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, 0xdd0d7cc9L,
0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL,
-
+
0xedb88320L, 0x9abfb3b6L, 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, 0x73dc1683L,
0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L,
0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L,
0xb3667a2eL, 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, 0x2d02ef8dL
};
-extern "C" unsigned int crc32_le(uint32_t crc, const uint8_t* buf, size_t len)
+
+
+extern "C" unsigned int crc32_le(unsigned int crc, unsigned char const * buf,unsigned int len)
{
unsigned int i;
crc = ~crc;
crc = crc32_le_table[(crc^buf[i])&0xff]^(crc>>8);
}
return ~crc;
-}
\ No newline at end of file
+}
+
+++ /dev/null
-#pragma once
-
-#include <stdint.h>
-#include <stdlib.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-uint32_t crc32_le(uint32_t crc, const uint8_t* buf, size_t len);
-
-#ifdef __cplusplus
-}
-#endif
-
+++ /dev/null
-#pragma once
-
-#include <stdint.h>
-#include <stdbool.h>
-
-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;
uint32_t esp_log_timestamp()
{
return 0;
-}
-
+}
\ No newline at end of file
#ifdef __cplusplus
}
-#endif
-
+#endif
\ No newline at end of file
void _lock_release(_lock_t *lock)
{
return;
-}
+}
\ No newline at end of file
--- /dev/null
+#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);
+}
\ No newline at end of file
-// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "esp_spi_flash.h"
#include "esp_partition.h"
#include "wear_levelling.h"
#include "WL_Flash.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);
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");
+
esp_err_t result;
wl_handle_t wl_handle;
- int flash_handle = esp_flash_create(PARTITION_SIZE, 4096, 16);
- esp_partition_t partition = esp_partition_create(PARTITION_SIZE, PARTITION_START, flash_handle);
+ int flash_handle;
+ const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
// Mount wear-levelled partition
result = wl_mount(partition, &wl_handle);