diff --git a/Makefile b/Makefile index 967d8dc..316dd50 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,18 @@ #CFLAGS ?= -std=gnu99 -Os -Wall #CXXFLAGS ?= -std=gnu++11 -Os -Wall +BUILD ?= build +IDF_DIR = esp_idf +IDF_INCLUDES += -I $(IDF_DIR) +IDF_INCLUDES += -I $(IDF_DIR)/freertos -IDF_MODIFIED_DIR = idf/modified -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR) -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/driver/include -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/fatfs/src -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/freertos/include -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/log/include -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/newlib/include -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/vfs/include -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/vfs/include/sys -IDF_INCLUDES += -I $(IDF_MODIFIED_DIR)/wear_levelling/include +FILESYSTEM_DIR = filesystem +IDF_INCLUDES += -I $(FILESYSTEM_DIR)/diskio +IDF_INCLUDES += -I $(FILESYSTEM_DIR)/fatfs +IDF_INCLUDES += -I $(FILESYSTEM_DIR)/vfs + +TCLAP_DIR = tclap +IDF_INCLUDES += -I $(TCLAP_DIR) -IDF_ORIG_DIR = idf/orig -IDF_INCLUDES += -I $(IDF_ORIG_DIR) -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/driver/include -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/driver/include/driver -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/esp32/include -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/fatfs/src -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/sdmmc/include -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/spi_flash/include -IDF_INCLUDES += -I $(IDF_ORIG_DIR)/wear_levelling/private_include # OS = 1 ifdef OS ifeq ($(OS),Windows_NT) @@ -74,28 +66,20 @@ else TARGET := mkfatfs endif -OBJ := main.o \ - fatfs/fatfs.o \ - fatfs/ccsbcs.o \ - fatfs/crc.o \ - fatfs/FatPartition.o \ - $(IDF_MODIFIED_DIR)/fatfs/src/ff.o \ - $(IDF_MODIFIED_DIR)/fatfs/src/vfs_fat.o \ - $(IDF_MODIFIED_DIR)/freertos/include/freertos/semphr.o \ - $(IDF_MODIFIED_DIR)/newlib/include/sys/lock.o \ - $(IDF_MODIFIED_DIR)/newlib/include/sys/idf_reent.o \ - $(IDF_MODIFIED_DIR)/newlib/include/sys/errno.o \ - $(IDF_MODIFIED_DIR)/spi_flash/partition.o \ - $(IDF_MODIFIED_DIR)/vfs/vfs.o \ - $(IDF_MODIFIED_DIR)/wear_levelling/wear_levelling.o \ - $(IDF_ORIG_DIR)/fatfs/src/diskio.o \ - $(IDF_ORIG_DIR)/fatfs/src/diskio_spiflash.o \ - $(IDF_ORIG_DIR)/fatfs/src/option/syscall.o \ - $(IDF_ORIG_DIR)/wear_levelling/crc32.o \ - $(IDF_ORIG_DIR)/wear_levelling/WL_Flash.o \ - $(IDF_ORIG_DIR)/wear_levelling/WL_Ext_Perf.o \ - $(IDF_ORIG_DIR)/wear_levelling/WL_Ext_Safe.o \ - +OBJ := $(BUILD)/main.o \ + $(BUILD)/$(IDF_DIR)/errno.o \ + $(BUILD)/$(IDF_DIR)/idf_reent.o \ + $(BUILD)/$(IDF_DIR)/lock.o \ + $(BUILD)/$(IDF_DIR)/freertos/semphr.o \ + $(BUILD)/$(FILESYSTEM_DIR)/fatfs/ccsbcs.o \ + $(BUILD)/$(FILESYSTEM_DIR)/fatfs/esp_vfs_fat.o \ + $(BUILD)/$(FILESYSTEM_DIR)/fatfs/ff.o \ + $(BUILD)/$(FILESYSTEM_DIR)/fatfs/syscall.o \ + $(BUILD)/$(FILESYSTEM_DIR)/diskio/diskio_RAM.o \ + $(BUILD)/$(FILESYSTEM_DIR)/diskio/diskio.o \ + $(BUILD)/$(FILESYSTEM_DIR)/diskio/FatPartition.o \ + $(BUILD)/$(FILESYSTEM_DIR)/vfs/user_vfs.o \ + $(BUILD)/$(FILESYSTEM_DIR)/vfs/vfs.o \ VERSION ?= $(shell git describe --always) @@ -105,41 +89,27 @@ all: $(TARGET) $(TARGET): @echo "Building mkfatfs ..." - $(CXX) $(TARGET_CXXFLAGS) -c main.cpp -o main.o - $(CC) $(TARGET_CFLAGS) -c fatfs/fatfs.c -o fatfs/fatfs.o - $(CC) $(TARGET_CFLAGS) -c fatfs/ccsbcs.c -o fatfs/ccsbcs.o - $(CXX) $(TARGET_CXXFLAGS) -c fatfs/crc.cpp -o fatfs/crc.o - $(CXX) $(TARGET_CXXFLAGS) -c fatfs/FatPartition.cpp -o fatfs/FatPartition.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/fatfs/src/ff.c -o $(IDF_MODIFIED_DIR)/fatfs/src/ff.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/fatfs/src/vfs_fat.c -o $(IDF_MODIFIED_DIR)/fatfs/src/vfs_fat.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/freertos/include/freertos/semphr.c -o $(IDF_MODIFIED_DIR)/freertos/include/freertos/semphr.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/newlib/include/sys/lock.c -o $(IDF_MODIFIED_DIR)/newlib/include/sys/lock.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/newlib/include/sys/idf_reent.c -o $(IDF_MODIFIED_DIR)/newlib/include/sys/idf_reent.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/newlib/include/sys/errno.c -o $(IDF_MODIFIED_DIR)/newlib/include/sys/errno.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/spi_flash/partition.c -o $(IDF_MODIFIED_DIR)/spi_flash/partition.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_MODIFIED_DIR)/vfs/vfs.c -o $(IDF_MODIFIED_DIR)/vfs/vfs.o - $(CXX) $(TARGET_CXXFLAGS) -c $(IDF_MODIFIED_DIR)/wear_levelling/wear_levelling.cpp -o $(IDF_MODIFIED_DIR)/wear_levelling/wear_levelling.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_ORIG_DIR)/fatfs/src/diskio.c -o $(IDF_ORIG_DIR)/fatfs/src/diskio.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_ORIG_DIR)/fatfs/src/diskio_spiflash.c -o $(IDF_ORIG_DIR)/fatfs/src/diskio_spiflash.o - $(CC) $(TARGET_CFLAGS) -c $(IDF_ORIG_DIR)/fatfs/src/option/syscall.c -o $(IDF_ORIG_DIR)/fatfs/src/option/syscall.o - $(CXX) $(TARGET_CXXFLAGS) -c $(IDF_ORIG_DIR)/wear_levelling/crc32.cpp -o $(IDF_ORIG_DIR)/wear_levelling/crc32.o - $(CXX) $(TARGET_CXXFLAGS) -c $(IDF_ORIG_DIR)/wear_levelling/WL_Flash.cpp -o $(IDF_ORIG_DIR)/wear_levelling/WL_Flash.o - $(CXX) $(TARGET_CXXFLAGS) -c $(IDF_ORIG_DIR)/wear_levelling/WL_Ext_Perf.cpp -o $(IDF_ORIG_DIR)/wear_levelling/WL_Ext_Perf.o - $(CXX) $(TARGET_CXXFLAGS) -c $(IDF_ORIG_DIR)/wear_levelling/WL_Ext_Safe.cpp -o $(IDF_ORIG_DIR)/wear_levelling/WL_Ext_Safe.o + $(CXX) $(TARGET_CXXFLAGS) -c main.cpp -o $(BUILD)/main.o + $(CC) $(TARGET_CFLAGS) -c $(IDF_DIR)/errno.c -o $(BUILD)/$(IDF_DIR)/errno.o + $(CC) $(TARGET_CFLAGS) -c $(IDF_DIR)/idf_reent.c -o $(BUILD)/$(IDF_DIR)/idf_reent.o + $(CC) $(TARGET_CFLAGS) -c $(IDF_DIR)/lock.c -o $(BUILD)/$(IDF_DIR)/lock.o + $(CC) $(TARGET_CFLAGS) -c $(IDF_DIR)/freertos/semphr.c -o $(BUILD)/$(IDF_DIR)/freertos/semphr.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/fatfs/ccsbcs.c -o $(BUILD)/$(FILESYSTEM_DIR)/fatfs/ccsbcs.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/fatfs/esp_vfs_fat.c -o $(BUILD)/$(FILESYSTEM_DIR)/fatfs/esp_vfs_fat.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/fatfs/ff.c -o $(BUILD)/$(FILESYSTEM_DIR)/fatfs/ff.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/fatfs/syscall.c -o $(BUILD)/$(FILESYSTEM_DIR)/fatfs/syscall.o + $(CXX) $(TARGET_CXXFLAGS) -c $(FILESYSTEM_DIR)/diskio/diskio_RAM.cpp -o $(BUILD)/$(FILESYSTEM_DIR)/diskio/diskio_RAM.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/diskio/diskio.c -o $(BUILD)/$(FILESYSTEM_DIR)/diskio/diskio.o + $(CXX) $(TARGET_CXXFLAGS) -c $(FILESYSTEM_DIR)/diskio/FatPartition.cpp -o $(BUILD)/$(FILESYSTEM_DIR)/diskio/FatPartition.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/vfs/user_vfs.c -o $(BUILD)/$(FILESYSTEM_DIR)/vfs/user_vfs.o + $(CC) $(TARGET_CFLAGS) -c $(FILESYSTEM_DIR)/vfs/vfs.c -o $(BUILD)/$(FILESYSTEM_DIR)/vfs/vfs.o $(CXX) $(TARGET_CFLAGS) -o $(TARGET) $(OBJ) $(TARGET_LDFLAGS) - - clean: @rm -f *.o - @rm -f fatfs/*.o - @rm -f $(IDF_MODIFIED_DIR)/fatfs/src/*.o - @rm -f $(IDF_MODIFIED_DIR)/freertos/include/freertos/*.o - @rm -f $(IDF_MODIFIED_DIR)/newlib/include/sys/*.o - @rm -f $(IDF_MODIFIED_DIR)/spi_flash/*.o - @rm -f $(IDF_MODIFIED_DIR)/vfs/*.o - @rm -f $(IDF_MODIFIED_DIR)/wear_levelling/*.o - @rm -f $(IDF_ORIG_DIR)/fatfs/src/*.o - @rm -f $(IDF_ORIG_DIR)/fatfs/src/option/*.o - @rm -f $(IDF_ORIG_DIR)/wear_levelling/*.o + @rm -f $(BUILD)/$(IDF_DIR)/*.o + @rm -f $(BUILD)/$(IDF_DIR)/frertos/*.o + @rm -f $(I$(BUILD)/$(FILESYSTEM_DIR)/fatfs/*.o + @rm -f $(BUILD)/$(FILESYSTEM_DIR)/diskio/*.o + @rm -f $(BUILD)/$(FILESYSTEM_DIR)/vfs/*.o @rm -f $(TARGET) diff --git a/idf/modified/newlib/include/sys/errno.c b/esp_idf/errno.c similarity index 100% rename from idf/modified/newlib/include/sys/errno.c rename to esp_idf/errno.c diff --git a/idf/modified/newlib/include/sys/errno.h b/esp_idf/errno.h similarity index 99% rename from idf/modified/newlib/include/sys/errno.h rename to esp_idf/errno.h index bbb9400..916f181 100644 --- a/idf/modified/newlib/include/sys/errno.h +++ b/esp_idf/errno.h @@ -8,7 +8,7 @@ extern "C" { #endif #define _SYS_ERRNO_H_ -#include //MVA was +#include "idf_reent.h" //MVA was //MVA VVV /* diff --git a/idf/orig/esp32/include/esp_err.h b/esp_idf/esp_err.h similarity index 100% rename from idf/orig/esp32/include/esp_err.h rename to esp_idf/esp_err.h diff --git a/idf/modified/log/include/esp_log.h b/esp_idf/esp_log.h similarity index 100% rename from idf/modified/log/include/esp_log.h rename to esp_idf/esp_log.h diff --git a/idf/orig/spi_flash/include/esp_partition.h b/esp_idf/esp_partition.h similarity index 90% rename from idf/orig/spi_flash/include/esp_partition.h rename to esp_idf/esp_partition.h index f3d5a42..1e02f9b 100644 --- a/idf/orig/spi_flash/include/esp_partition.h +++ b/esp_idf/esp_partition.h @@ -19,7 +19,7 @@ #include #include #include "esp_err.h" -#include "esp_spi_flash.h" +// #include "esp_spi_flash.h" #ifdef __cplusplus extern "C" { @@ -119,7 +119,7 @@ typedef struct { * Iterator obtained through this function has to be released * using esp_partition_iterator_release when not used any more. */ -esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label); +// esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label); /** * @brief Find first partition based on one or more parameters @@ -134,7 +134,7 @@ esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_parti * @return pointer to esp_partition_t structure, or NULL if no partition is found. * This pointer is valid for the lifetime of the application. */ -const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label); +// const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label); /** * @brief Get esp_partition_t structure for given partition @@ -144,7 +144,7 @@ const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, esp_p * @return pointer to esp_partition_t structure. This pointer is valid for the lifetime * of the application. */ -const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator); +// const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator); /** * @brief Move partition iterator to the next partition found @@ -155,7 +155,7 @@ const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator); * * @return NULL if no partition was found, valid esp_partition_iterator_t otherwise. */ -esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t iterator); +// esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t iterator); /** * @brief Release partition iterator @@ -163,7 +163,7 @@ esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t iterator); * @param iterator Iterator obtained using esp_partition_find. Must be non-NULL. * */ -void esp_partition_iterator_release(esp_partition_iterator_t iterator); +// void esp_partition_iterator_release(esp_partition_iterator_t iterator); /** * @brief Verify partition data @@ -183,7 +183,7 @@ void esp_partition_iterator_release(esp_partition_iterator_t iterator); * - If partition not found, returns NULL. * - If found, returns a pointer to the esp_partition_t structure in flash. This pointer is always valid for the lifetime of the application. */ -const esp_partition_t *esp_partition_verify(const esp_partition_t *partition); +// const esp_partition_t *esp_partition_verify(const esp_partition_t *partition); /** * @brief Read data from the partition @@ -202,8 +202,8 @@ const esp_partition_t *esp_partition_verify(const esp_partition_t *partition); * ESP_ERR_INVALID_SIZE, if read would go out of bounds of the partition; * or one of error codes from lower-level flash driver. */ -esp_err_t esp_partition_read(const esp_partition_t* partition, - size_t src_offset, void* dst, size_t size); +// esp_err_t esp_partition_read(const esp_partition_t* partition, +// size_t src_offset, void* dst, size_t size); /** * @brief Write data to the partition @@ -235,8 +235,8 @@ esp_err_t esp_partition_read(const esp_partition_t* partition, * ESP_ERR_INVALID_SIZE, if write would go out of bounds of the partition; * or one of error codes from lower-level flash driver. */ -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_write(const esp_partition_t* partition, +// size_t dst_offset, const void* src, size_t size); /** * @brief Erase part of the partition @@ -254,8 +254,8 @@ esp_err_t esp_partition_write(const esp_partition_t* partition, * ESP_ERR_INVALID_SIZE, if erase would go out of bounds of the partition; * or one of error codes from lower-level flash driver. */ -esp_err_t esp_partition_erase_range(const esp_partition_t* partition, - uint32_t start_addr, uint32_t size); +// esp_err_t esp_partition_erase_range(const esp_partition_t* partition, +// uint32_t start_addr, uint32_t size); /** * @brief Configure MMU to map partition into data memory @@ -282,9 +282,9 @@ esp_err_t esp_partition_erase_range(const esp_partition_t* partition, * * @return ESP_OK, if successful */ -esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size, - spi_flash_mmap_memory_t memory, - const void** out_ptr, spi_flash_mmap_handle_t* out_handle); +// esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size, +// spi_flash_mmap_memory_t memory, +// const void** out_ptr, spi_flash_mmap_handle_t* out_handle); #ifdef __cplusplus diff --git a/idf/modified/freertos/include/freertos/FreeRTOS.h b/esp_idf/freertos/FreeRTOS.h similarity index 100% rename from idf/modified/freertos/include/freertos/FreeRTOS.h rename to esp_idf/freertos/FreeRTOS.h diff --git a/idf/modified/freertos/include/freertos/queue.h b/esp_idf/freertos/queue.h similarity index 100% rename from idf/modified/freertos/include/freertos/queue.h rename to esp_idf/freertos/queue.h diff --git a/idf/modified/freertos/include/freertos/semphr.c b/esp_idf/freertos/semphr.c similarity index 100% rename from idf/modified/freertos/include/freertos/semphr.c rename to esp_idf/freertos/semphr.c diff --git a/idf/modified/freertos/include/freertos/semphr.h b/esp_idf/freertos/semphr.h similarity index 100% rename from idf/modified/freertos/include/freertos/semphr.h rename to esp_idf/freertos/semphr.h diff --git a/idf/modified/newlib/include/sys/idf_reent.c b/esp_idf/idf_reent.c similarity index 100% rename from idf/modified/newlib/include/sys/idf_reent.c rename to esp_idf/idf_reent.c diff --git a/idf/modified/newlib/include/sys/idf_reent.h b/esp_idf/idf_reent.h similarity index 100% rename from idf/modified/newlib/include/sys/idf_reent.h rename to esp_idf/idf_reent.h diff --git a/idf/modified/newlib/include/sys/lock.c b/esp_idf/lock.c similarity index 100% rename from idf/modified/newlib/include/sys/lock.c rename to esp_idf/lock.c diff --git a/idf/modified/newlib/include/sys/lock.h b/esp_idf/lock.h similarity index 100% rename from idf/modified/newlib/include/sys/lock.h rename to esp_idf/lock.h diff --git a/fatfs/crc.cpp b/fatfs/crc.cpp deleted file mode 100644 index 483626e..0000000 --- a/fatfs/crc.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// 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 -#include - -static const unsigned int crc32_le_table[256] = { - 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, - 0x0edb8832L, 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0x90bf1d91L, - 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, - 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, - 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, - 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, - 0x26d930acL, 0x51de003aL, 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, - 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, - 0x76dc4190L, 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, - 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, - 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, - 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, - 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, - 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, - 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, - 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, - 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, - 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, - 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, - 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, - 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, - 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L, - 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, - 0xa00ae278L, 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL, 0x3e6e77dbL, - 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, - 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, - 0xb3667a2eL, 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, 0x2d02ef8dL -}; - - -extern "C" uint32_t crc32_le(uint32_t crc, uint8_t const * buf,uint32_t len) -{ - unsigned int i; - crc = ~crc; - for(i=0;i>8); - } - return ~crc; -} - diff --git a/fatfs/FatPartition.cpp b/filesystem/diskio/FatPartition.cpp similarity index 97% rename from fatfs/FatPartition.cpp rename to filesystem/diskio/FatPartition.cpp index 6cdcbd0..742ed0a 100644 --- a/fatfs/FatPartition.cpp +++ b/filesystem/diskio/FatPartition.cpp @@ -14,6 +14,9 @@ #include // memset/memcpy #include "esp_log.h" #include "FatPartition.h" +#include "sdkconfig.h" + +#define SPI_FLASH_SEC_SIZE CONFIG_RAM_SECTOR_SIZE static const char *TAG = "FatPartition"; diff --git a/fatfs/FatPartition.h b/filesystem/diskio/FatPartition.h similarity index 100% rename from fatfs/FatPartition.h rename to filesystem/diskio/FatPartition.h diff --git a/idf/orig/wear_levelling/private_include/Flash_Access.h b/filesystem/diskio/Flash_Access.h similarity index 100% rename from idf/orig/wear_levelling/private_include/Flash_Access.h rename to filesystem/diskio/Flash_Access.h diff --git a/idf/orig/fatfs/src/diskio.c b/filesystem/diskio/diskio.c similarity index 100% rename from idf/orig/fatfs/src/diskio.c rename to filesystem/diskio/diskio.c diff --git a/idf/orig/fatfs/src/diskio.h b/filesystem/diskio/diskio.h similarity index 96% rename from idf/orig/fatfs/src/diskio.h rename to filesystem/diskio/diskio.h index 572f03d..a07c171 100644 --- a/idf/orig/fatfs/src/diskio.h +++ b/filesystem/diskio/diskio.h @@ -10,8 +10,9 @@ extern "C" { #endif #include "integer.h" -#include "sdmmc_cmd.h" -#include "driver/sdmmc_host.h" +#include "esp_err.h" +// #include "sdmmc_cmd.h" +// #include "driver/sdmmc_host.h" /* Status of Disk Functions */ typedef BYTE DSTATUS; @@ -77,7 +78,7 @@ void ff_diskio_register(BYTE pdrv, const ff_diskio_impl_t* discio_impl); * @param pdrv drive number * @param card pointer to sdmmc_card_t structure describing a card; card should be initialized before calling f_mount. */ -void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card); +// void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card); /** * Get next available drive number diff --git a/filesystem/diskio/diskio_RAM.cpp b/filesystem/diskio/diskio_RAM.cpp new file mode 100644 index 0000000..029409e --- /dev/null +++ b/filesystem/diskio/diskio_RAM.cpp @@ -0,0 +1,135 @@ +// 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 +#include "diskio.h" +#include "ffconf.h" +#include "ff.h" +#include "esp_log.h" +#include "diskio_RAM.h" +#include "FatPartition.h" + +static const char* TAG = "ff_diskio_RAM"; + +FatPartition *part[_VOLUMES] = {NULL, NULL}; + +RAM_handle_t ff_RAM_handles[_VOLUMES] = { //支持两个RAM驱动器 + RAM_INVALID_HANDLE, + RAM_INVALID_HANDLE, +}; + +DSTATUS ff_RAM_initialize (BYTE pdrv) +{ + return 0; +} + +DSTATUS ff_RAM_status (BYTE pdrv) +{ + return 0; +} + +DRESULT ff_RAM_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count) +{ + ESP_LOGV(TAG, "ff_RAM_read - pdrv=%i, sector=%i, count=%i\n", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count); + RAM_handle_t RAM_handle = ff_RAM_handles[pdrv]; + assert(RAM_handle + 1); + esp_err_t err = part[RAM_handle]->read(sector * part[RAM_handle]->sector_size(), buff, count * part[RAM_handle]->sector_size()); + if (err != ESP_OK) { + ESP_LOGE(TAG, "RAM_read failed (%d)", err); + return RES_ERROR; + } + return RES_OK; +} + +DRESULT ff_RAM_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count) +{ + ESP_LOGV(TAG, "ff_RAM_write - pdrv=%i, sector=%i, count=%i\n", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count); + RAM_handle_t RAM_handle = ff_RAM_handles[pdrv]; + assert(RAM_handle + 1); + esp_err_t err = part[RAM_handle]->erase_range(sector * part[RAM_handle]->sector_size(), count * part[RAM_handle]->sector_size()); + if (err != ESP_OK) { + ESP_LOGE(TAG, "RAM_erase_range failed (%d)", err); + return RES_ERROR; + } + err = part[RAM_handle]->write(sector * part[RAM_handle]->sector_size(), buff, count * part[RAM_handle] -> sector_size()); + if (err != ESP_OK) { + ESP_LOGE(TAG, "RAM_write failed (%d)", err); + return RES_ERROR; + } + return RES_OK; +} + +DRESULT ff_RAM_ioctl (BYTE pdrv, BYTE cmd, void *buff) +{ + RAM_handle_t RAM_handle = ff_RAM_handles[pdrv]; + ESP_LOGV(TAG, "ff_RAM_ioctl: cmd=%i\n", cmd); + assert(RAM_handle + 1); + switch (cmd) { + case CTRL_SYNC: + return RES_OK; + case GET_SECTOR_COUNT: + *((uint32_t *) buff) = part[RAM_handle]->chip_size() / part[RAM_handle]->sector_size(); + return RES_OK; + case GET_SECTOR_SIZE: + *((uint32_t *) buff) = part[RAM_handle]->sector_size(); + return RES_OK; + case GET_BLOCK_SIZE: + return RES_ERROR; + } + return RES_ERROR; +} + + +esp_err_t ff_diskio_register_RAM_partition(BYTE pdrv, RAM_handle_t RAM_handle, esp_partition_t *data_partition) +{ + void *part_ptr = NULL; + if (pdrv >= _VOLUMES) { + return ESP_ERR_INVALID_ARG; + } + static const ff_diskio_impl_t RAM_impl = { + .init = &ff_RAM_initialize, + .status = &ff_RAM_status, + .read = &ff_RAM_read, + .write = &ff_RAM_write, + .ioctl = &ff_RAM_ioctl + }; + ff_RAM_handles[pdrv] = RAM_handle; + ff_diskio_register(pdrv, &RAM_impl); + + // Allocate memory for a Partition object, and then initialize the object + // using placement new operator. This way we can recover from out of + // memory condition. + part_ptr = malloc(sizeof(FatPartition)); + if (part_ptr == NULL) { + ESP_LOGE(TAG, "%s: can't allocate FatPartition", __func__); + return ESP_FAIL; + } + part[pdrv] = new (part_ptr) FatPartition(data_partition); + return ESP_OK; +} + +esp_err_t ff_diskio_unregister_RAM_partition(BYTE pdrv, RAM_handle_t RAM_handle) +{ + // delete part[pdrv]; +} + +BYTE ff_diskio_get_pdrv_RAM(RAM_handle_t RAM_handle) +{ + for (int i = 0; i < _VOLUMES; i++) { + if (RAM_handle == ff_RAM_handles[i]) { + return i; + } + } + return 0xff; +} diff --git a/idf/orig/fatfs/src/diskio_spiflash.h b/filesystem/diskio/diskio_RAM.h similarity index 65% rename from idf/orig/fatfs/src/diskio_spiflash.h rename to filesystem/diskio/diskio_RAM.h index 9119506..0bfff00 100644 --- a/idf/orig/fatfs/src/diskio_spiflash.h +++ b/filesystem/diskio/diskio_RAM.h @@ -12,25 +12,27 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _DISKIO_SPIFLASH_DEFINED -#define _DISKIO_SPIFLASH_DEFINED +#ifndef _DISKIO_RAM +#define _DISKIO_RAM #ifdef __cplusplus extern "C" { #endif #include "integer.h" -#include "wear_levelling.h" - +#include "esp_partition.h" +typedef int32_t RAM_handle_t; +#define RAM_INVALID_HANDLE -1 /** * Register spi flash partition * * @param pdrv drive number - * @param flash_handle handle of the wear levelling partition. + * @param RAM_handle handle of the wear levelling partition. */ -esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle); -BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle); +esp_err_t ff_diskio_register_RAM_partition(BYTE pdrv, RAM_handle_t RAM_handle, esp_partition_t *data_partition); +esp_err_t ff_diskio_unregister_RAM_partition(BYTE pdrv, RAM_handle_t RAM_handle); +BYTE ff_diskio_get_pdrv_RAM(RAM_handle_t RAM_handle); #ifdef __cplusplus } diff --git a/fatfs/ccsbcs.c b/filesystem/fatfs/ccsbcs.c similarity index 100% rename from fatfs/ccsbcs.c rename to filesystem/fatfs/ccsbcs.c diff --git a/idf/modified/fatfs/src/vfs_fat.c b/filesystem/fatfs/esp_vfs_fat.c similarity index 98% rename from idf/modified/fatfs/src/vfs_fat.c rename to filesystem/fatfs/esp_vfs_fat.c index 053584a..e661748 100644 --- a/idf/modified/fatfs/src/vfs_fat.c +++ b/filesystem/fatfs/esp_vfs_fat.c @@ -17,9 +17,9 @@ #include #include //MVA #include //MVA added -#include +#include "errno.h" #include -#include +#include "lock.h" #include "esp_vfs.h" #include "esp_log.h" #include "ff.h" @@ -65,7 +65,7 @@ static int vfs_fat_closedir(void* ctx, DIR* pdir); static int vfs_fat_mkdir(void* ctx, const char* name, mode_t mode); static int vfs_fat_rmdir(void* ctx, const char* name); -static vfs_fat_ctx_t* s_fat_ctxs[_VOLUMES] = { NULL, NULL }; +static vfs_fat_ctx_t* s_fat_ctxs[_VOLUMES] = { NULL, NULL }; //可挂载两个FAT分区 //backwards-compatibility with esp_vfs_fat_unregister() static vfs_fat_ctx_t* s_fat_ctx = NULL; @@ -159,7 +159,7 @@ strlcpy(char *dst, const char *src, size_t len) static size_t find_context_index_by_path(const char* base_path) { for(size_t i=0; i<_VOLUMES; i++) { - if (s_fat_ctxs[i] && !strcmp(s_fat_ctxs[i]->base_path, base_path)) { + if (s_fat_ctxs[i] && !strcmp(s_fat_ctxs[i]->base_path, base_path)) { return i; } } @@ -178,8 +178,8 @@ static size_t find_unused_context_index() esp_err_t esp_vfs_fat_register(const char* base_path, const char* fat_drive, size_t max_files, FATFS** out_fs) { - size_t ctx = find_context_index_by_path(base_path); - if (ctx < _VOLUMES) { + size_t ctx = find_context_index_by_path(base_path); + if (ctx < _VOLUMES) { //有根路径 异常 return ESP_ERR_INVALID_STATE; } diff --git a/idf/orig/fatfs/src/esp_vfs_fat.h b/filesystem/fatfs/esp_vfs_fat.h similarity index 92% rename from idf/orig/fatfs/src/esp_vfs_fat.h rename to filesystem/fatfs/esp_vfs_fat.h index 278e427..c2799da 100644 --- a/idf/orig/fatfs/src/esp_vfs_fat.h +++ b/filesystem/fatfs/esp_vfs_fat.h @@ -15,12 +15,7 @@ #pragma once #include #include "esp_err.h" -#include "driver/gpio.h" -#include "driver/sdmmc_types.h" -#include "driver/sdmmc_host.h" -#include "driver/sdspi_host.h" #include "ff.h" -#include "wear_levelling.h" #ifdef __cplusplus extern "C" { @@ -129,11 +124,11 @@ typedef esp_vfs_fat_mount_config_t esp_vfs_fat_sdmmc_mount_config_t; * - ESP_FAIL if partition can not be mounted * - other error codes from SDMMC or SPI drivers, SDMMC protocol, or FATFS drivers */ -esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path, - const sdmmc_host_t* host_config, - const void* slot_config, - const esp_vfs_fat_mount_config_t* mount_config, - sdmmc_card_t** out_card); +// esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path, +// const sdmmc_host_t* host_config, +// const void* slot_config, +// const esp_vfs_fat_mount_config_t* mount_config, +// sdmmc_card_t** out_card); /** * @brief Unmount FAT filesystem and release resources acquired using esp_vfs_fat_sdmmc_mount @@ -142,7 +137,7 @@ esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path, * - ESP_OK on success * - ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount hasn't been called */ -esp_err_t esp_vfs_fat_sdmmc_unmount(); +// esp_err_t esp_vfs_fat_sdmmc_unmount(); /** * @brief Convenience function to initialize FAT filesystem in SPI flash and register it in VFS @@ -170,10 +165,10 @@ esp_err_t esp_vfs_fat_sdmmc_unmount(); * - ESP_FAIL if partition can not be mounted * - other error codes from wear levelling library, SPI flash driver, or FATFS drivers */ -esp_err_t esp_vfs_fat_spiflash_mount(const char* base_path, - const char* partition_label, - const esp_vfs_fat_mount_config_t* mount_config, - wl_handle_t* wl_handle); +// esp_err_t esp_vfs_fat_spiflash_mount(const char* base_path, +// const char* partition_label, +// const esp_vfs_fat_mount_config_t* mount_config, +// wl_handle_t* wl_handle); /** * @brief Unmount FAT filesystem and release resources acquired using esp_vfs_fat_spiflash_mount @@ -185,7 +180,7 @@ esp_err_t esp_vfs_fat_spiflash_mount(const char* base_path, * - ESP_OK on success * - ESP_ERR_INVALID_STATE if esp_vfs_fat_spiflash_mount hasn't been called */ - esp_err_t esp_vfs_fat_spiflash_unmount(const char* base_path, wl_handle_t wl_handle); +// esp_err_t esp_vfs_fat_spiflash_unmount(const char* base_path, wl_handle_t wl_handle); #ifdef __cplusplus } diff --git a/idf/modified/fatfs/src/ff.c b/filesystem/fatfs/ff.c similarity index 100% rename from idf/modified/fatfs/src/ff.c rename to filesystem/fatfs/ff.c diff --git a/idf/orig/fatfs/src/ff.h b/filesystem/fatfs/ff.h similarity index 100% rename from idf/orig/fatfs/src/ff.h rename to filesystem/fatfs/ff.h diff --git a/idf/orig/fatfs/src/ffconf.h b/filesystem/fatfs/ffconf.h similarity index 100% rename from idf/orig/fatfs/src/ffconf.h rename to filesystem/fatfs/ffconf.h diff --git a/idf/modified/fatfs/src/integer.h b/filesystem/fatfs/integer.h similarity index 100% rename from idf/modified/fatfs/src/integer.h rename to filesystem/fatfs/integer.h diff --git a/idf/orig/fatfs/src/option/syscall.c b/filesystem/fatfs/syscall.c similarity index 99% rename from idf/orig/fatfs/src/option/syscall.c rename to filesystem/fatfs/syscall.c index bcd3658..675347f 100644 --- a/idf/orig/fatfs/src/option/syscall.c +++ b/filesystem/fatfs/syscall.c @@ -4,7 +4,7 @@ /*------------------------------------------------------------------------*/ -#include "../ff.h" +#include "ff.h" #if _FS_REENTRANT diff --git a/idf/modified/vfs/include/esp_vfs.h b/filesystem/vfs/esp_vfs.h similarity index 99% rename from idf/modified/vfs/include/esp_vfs.h rename to filesystem/vfs/esp_vfs.h index 352a4fe..2849e2f 100644 --- a/idf/modified/vfs/include/esp_vfs.h +++ b/filesystem/vfs/esp_vfs.h @@ -20,7 +20,7 @@ #include #include "esp_err.h" #include -#include //MVA was +#include //MVA was #include #include //MVA was diff --git a/idf/modified/vfs/include/sys/idf_dirent.h b/filesystem/vfs/idf_dirent.h similarity index 100% rename from idf/modified/vfs/include/sys/idf_dirent.h rename to filesystem/vfs/idf_dirent.h diff --git a/fatfs/fatfs.c b/filesystem/vfs/user_vfs.c similarity index 83% rename from fatfs/fatfs.c rename to filesystem/vfs/user_vfs.c index 4dc0442..26b032d 100644 --- a/fatfs/fatfs.c +++ b/filesystem/vfs/user_vfs.c @@ -3,13 +3,12 @@ #include "esp_log.h" #include "esp_err.h" -#include "wear_levelling.h" #include "diskio.h" -#include "diskio_spiflash.h" +#include "diskio_RAM.h" #include "esp_vfs_fat.h" #include "esp_vfs.h" -#include "fatfs.h" +#include "user_vfs.h" static const char *TAG = "fatfs"; @@ -28,7 +27,7 @@ static esp_partition_t s_partition = { esp_err_t emulate_esp_vfs_fat_spiflash_mount(const char* base_path, //const char* partition_label, const esp_vfs_fat_mount_config_t* mount_config, - wl_handle_t* wl_handle, + RAM_handle_t* RAM_handle, FATFS** out_fs, int imageSize) { @@ -40,17 +39,7 @@ esp_err_t emulate_esp_vfs_fat_spiflash_mount(const char* base_path, s_partition.size = imageSize; esp_partition_t *data_partition = &s_partition; //记录分区相关信息 - // esp_partition_t *data_partition = (esp_partition_t *)esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label); - // if (data_partition == NULL) { - // ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label); - // return ESP_ERR_NOT_FOUND; - // } - /* 1 挂载磨损平衡层 */ - result = wl_mount(data_partition, wl_handle); - if (result != ESP_OK) { - ESP_LOGE(TAG, "failed to mount wear levelling layer. result = %i", result); - return result; - } + // connect driver to FATFS BYTE pdrv = 0xFF; if (ff_diskio_get_drive(&pdrv) != ESP_OK) { //获取一个空驱动器号 @@ -60,9 +49,9 @@ esp_err_t emulate_esp_vfs_fat_spiflash_mount(const char* base_path, ESP_LOGD(TAG, "using pdrv=%i", pdrv); char drv[3] = {(char)('0' + pdrv), ':', 0}; - result = ff_diskio_register_wl_partition(pdrv, *wl_handle); //把磨损平衡层的操作函数地址,填入对应驱动器号的函数指针结构。 + result = ff_diskio_register_RAM_partition(pdrv, *RAM_handle, data_partition); if (result != ESP_OK) { - ESP_LOGE(TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, result); + ESP_LOGE(TAG, "ff_diskio_register_RAM_partition failed pdrv=%i, error - 0x(%x)", pdrv, result); goto fail; } FATFS *fs = NULL; @@ -111,9 +100,9 @@ esp_err_t emulate_esp_vfs_fat_spiflash_mount(const char* base_path, } -esp_err_t emulate_esp_vfs_fat_spiflash_unmount(const char *base_path, wl_handle_t wl_handle) +esp_err_t emulate_esp_vfs_fat_spiflash_unmount(const char *base_path, RAM_handle_t RAM_handle) { - BYTE pdrv = ff_diskio_get_pdrv_wl(wl_handle); + BYTE pdrv = ff_diskio_get_pdrv_RAM(RAM_handle); if (pdrv == 0xff) { return ESP_ERR_INVALID_STATE; } @@ -122,9 +111,10 @@ esp_err_t emulate_esp_vfs_fat_spiflash_unmount(const char *base_path, wl_handle_ f_mount(0, drv, 0); ff_diskio_unregister(pdrv); // release partition driver - esp_err_t err_drv = wl_unmount(wl_handle); + // esp_err_t err_drv = wl_unmount(RAM_handle); + // ff_diskio_unregister_RAM_partition(pdrv, RAM_handle); esp_err_t err = esp_vfs_fat_unregister_path(base_path); - if (err == ESP_OK) err = err_drv; + // if (err == ESP_OK) err = err_drv; return err; } diff --git a/fatfs/fatfs.h b/filesystem/vfs/user_vfs.h similarity index 94% rename from fatfs/fatfs.h rename to filesystem/vfs/user_vfs.h index 3986eee..39bc211 100644 --- a/fatfs/fatfs.h +++ b/filesystem/vfs/user_vfs.h @@ -7,6 +7,7 @@ */ #include "esp_err.h" +#include "diskio_RAM.h" #if defined(__cplusplus) extern "C" { @@ -16,12 +17,12 @@ extern "C" { esp_err_t emulate_esp_vfs_fat_spiflash_mount(const char* base_path, //const char* partition_label, const esp_vfs_fat_mount_config_t* mount_config, - wl_handle_t* wl_handle, + RAM_handle_t* RAM_handle, FATFS** out_fs, int imageSize ); -esp_err_t emulate_esp_vfs_fat_spiflash_unmount(const char *base_path, wl_handle_t wl_handle); +esp_err_t emulate_esp_vfs_fat_spiflash_unmount(const char *base_path, RAM_handle_t wl_handle); //vfs.c ssize_t emulate_esp_vfs_write(int fd, const void * data, size_t size); diff --git a/idf/modified/vfs/vfs.c b/filesystem/vfs/vfs.c similarity index 95% rename from idf/modified/vfs/vfs.c rename to filesystem/vfs/vfs.c index 24f2cfd..29665f3 100644 --- a/idf/modified/vfs/vfs.c +++ b/filesystem/vfs/vfs.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include "esp_vfs.h" #include "esp_log.h" @@ -35,11 +35,11 @@ #ifndef CONFIG_MAX_FD_BITS #define CONFIG_MAX_FD_BITS 12 #endif - +// eg: 3 #define MAX_VFS_ID_BITS (16 - CONFIG_MAX_FD_BITS - 1) // mask of actual file descriptor (e.g. 0x00000fff) #define VFS_FD_MASK ((1 << CONFIG_MAX_FD_BITS) - 1) -// max number of VFS entries +// max number of VFS entries : #define VFS_MAX_COUNT ((1 << MAX_VFS_ID_BITS) - 1) // mask of VFS id (e.g. 0x00007000) #define VFS_INDEX_MASK (VFS_MAX_COUNT << CONFIG_MAX_FD_BITS) @@ -47,10 +47,10 @@ typedef struct vfs_entry_ { esp_vfs_t vfs; // contains pointers to VFS functions - char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS + char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS eg: /spiflash size_t path_prefix_len; // micro-optimization to avoid doing extra strlen - void* ctx; // optional pointer which can be passed to VFS - int offset; // index of this structure in s_vfs array + void* ctx; // optional pointer which can be passed to VFS 同一文件系统(如FAT)可能对应几个分区,以此区分 + int offset; // index of this structure in s_vfs array s_vfs[]中的位置索引。 } vfs_entry_t; static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 }; @@ -66,7 +66,7 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct if ((len != 0 && len < 2)|| len > ESP_VFS_PATH_MAX) { return ESP_ERR_INVALID_ARG; } - if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') { + if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') { //right: "/spiflash" error: "/spiflash/"" or "spiflash/" return ESP_ERR_INVALID_ARG; } vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t)); @@ -74,7 +74,7 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct return ESP_ERR_NO_MEM; } size_t index; - for (index = 0; index < s_vfs_count; ++index) { //有可能中间有unregit后的空条目 + for (index = 0; index < s_vfs_count; ++index) { //if unregist, some index is empty if (s_vfs[index] == NULL) { break; } @@ -126,6 +126,7 @@ static int translate_fd(const vfs_entry_t* vfs, int fd) return (fd & VFS_FD_MASK) + vfs->vfs.fd_offset; } +/* 去除根路径字符*/ static const char* translate_path(const vfs_entry_t* vfs, const char* src_path) { assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0); @@ -133,9 +134,13 @@ static const char* translate_path(const vfs_entry_t* vfs, const char* src_path) // special case when src_path matches the path prefix exactly return "/"; } - return src_path + vfs->path_prefix_len; + return src_path + vfs->path_prefix_len; } +/* + 应用mount文件系统时,会调用esp_vfs_register()把根路径存到vfs_entry中, + 用esp_vfs_open()打开文件时,会利用本函数数查找对应根路径的vfs_entry。 + */ static const vfs_entry_t* get_vfs_for_path(const char* path) { const vfs_entry_t* best_match = NULL; diff --git a/idf/modified/driver/include/driver/gpio.h b/idf/modified/driver/include/driver/gpio.h deleted file mode 100644 index 8135c3c..0000000 --- a/idf/modified/driver/include/driver/gpio.h +++ /dev/null @@ -1,77 +0,0 @@ -// 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. - -#ifndef _DRIVER_GPIO_H_ -#define _DRIVER_GPIO_H_ - - -#ifdef __cplusplus -extern "C" { -#endif - - -typedef enum { - GPIO_NUM_0 = 0, /*!< GPIO0, input and output */ - GPIO_NUM_1 = 1, /*!< GPIO1, input and output */ - GPIO_NUM_2 = 2, /*!< GPIO2, input and output - @note There are more enumerations like that - up to GPIO39, excluding GPIO20, GPIO24 and GPIO28..31. - They are not shown here to reduce redundant information. - @note GPIO34..39 are input mode only. */ -/** @cond */ - GPIO_NUM_3 = 3, /*!< GPIO3, input and output */ - GPIO_NUM_4 = 4, /*!< GPIO4, input and output */ - GPIO_NUM_5 = 5, /*!< GPIO5, input and output */ - GPIO_NUM_6 = 6, /*!< GPIO6, input and output */ - GPIO_NUM_7 = 7, /*!< GPIO7, input and output */ - GPIO_NUM_8 = 8, /*!< GPIO8, input and output */ - GPIO_NUM_9 = 9, /*!< GPIO9, input and output */ - GPIO_NUM_10 = 10, /*!< GPIO10, input and output */ - GPIO_NUM_11 = 11, /*!< GPIO11, input and output */ - GPIO_NUM_12 = 12, /*!< GPIO12, input and output */ - GPIO_NUM_13 = 13, /*!< GPIO13, input and output */ - GPIO_NUM_14 = 14, /*!< GPIO14, input and output */ - GPIO_NUM_15 = 15, /*!< GPIO15, input and output */ - GPIO_NUM_16 = 16, /*!< GPIO16, input and output */ - GPIO_NUM_17 = 17, /*!< GPIO17, input and output */ - GPIO_NUM_18 = 18, /*!< GPIO18, input and output */ - GPIO_NUM_19 = 19, /*!< GPIO19, input and output */ - - GPIO_NUM_21 = 21, /*!< GPIO21, input and output */ - GPIO_NUM_22 = 22, /*!< GPIO22, input and output */ - GPIO_NUM_23 = 23, /*!< GPIO23, input and output */ - - GPIO_NUM_25 = 25, /*!< GPIO25, input and output */ - GPIO_NUM_26 = 26, /*!< GPIO26, input and output */ - GPIO_NUM_27 = 27, /*!< GPIO27, input and output */ - - GPIO_NUM_32 = 32, /*!< GPIO32, input and output */ - GPIO_NUM_33 = 33, /*!< GPIO33, input and output */ - GPIO_NUM_34 = 34, /*!< GPIO34, input mode only */ - GPIO_NUM_35 = 35, /*!< GPIO35, input mode only */ - GPIO_NUM_36 = 36, /*!< GPIO36, input mode only */ - GPIO_NUM_37 = 37, /*!< GPIO37, input mode only */ - GPIO_NUM_38 = 38, /*!< GPIO38, input mode only */ - GPIO_NUM_39 = 39, /*!< GPIO39, input mode only */ - GPIO_NUM_MAX = 40, -/** @endcond */ -} gpio_num_t; - - - -#ifdef __cplusplus -} -#endif - -#endif /* _DRIVER_GPIO_H_ */ diff --git a/idf/modified/driver/include/driver/sdspi_host.h b/idf/modified/driver/include/driver/sdspi_host.h deleted file mode 100644 index 9f56d3e..0000000 --- a/idf/modified/driver/include/driver/sdspi_host.h +++ /dev/null @@ -1,156 +0,0 @@ -// 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. - -#pragma once - -#include -#include -#include "esp_err.h" -#include "sdmmc_types.h" -#include "driver/gpio.h" -//MVA #include "driver/spi_master.h" -#include "driver/sdmmc_host.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Default sdmmc_host_t structure initializer for SD over SPI driver - * - * Uses SPI mode and max frequency set to 20MHz - * - * 'slot' can be set to one of HSPI_HOST, VSPI_HOST. - */ -#define SDSPI_HOST_DEFAULT() {\ - .flags = SDMMC_HOST_FLAG_SPI, \ - .slot = HSPI_HOST, \ - .max_freq_khz = SDMMC_FREQ_DEFAULT, \ - .io_voltage = 3.3f, \ - .init = &sdspi_host_init, \ - .set_bus_width = NULL, \ - .set_card_clk = &sdspi_host_set_card_clk, \ - .do_transaction = &sdspi_host_do_transaction, \ - .deinit = &sdspi_host_deinit, \ -} - -/** - * Extra configuration for SPI host - */ -typedef struct { - gpio_num_t gpio_miso; ///< GPIO number of MISO signal - gpio_num_t gpio_mosi; ///< GPIO number of MOSI signal - gpio_num_t gpio_sck; ///< GPIO number of SCK signal - gpio_num_t gpio_cs; ///< GPIO number of CS signal - gpio_num_t gpio_cd; ///< GPIO number of card detect signal - gpio_num_t gpio_wp; ///< GPIO number of write protect signal - int dma_channel; ///< DMA channel to be used by SPI driver (1 or 2) -} sdspi_slot_config_t; - -#define SDSPI_SLOT_NO_CD ((gpio_num_t) -1) ///< indicates that card detect line is not used -#define SDSPI_SLOT_NO_WP ((gpio_num_t) -1) ///< indicates that write protect line is not used - -/** - * Macro defining default configuration of SPI host - */ -#define SDSPI_SLOT_CONFIG_DEFAULT() {\ - .gpio_miso = GPIO_NUM_2, \ - .gpio_mosi = GPIO_NUM_15, \ - .gpio_sck = GPIO_NUM_14, \ - .gpio_cs = GPIO_NUM_13, \ - .gpio_cd = SDMMC_SLOT_NO_CD, \ - .gpio_wp = SDMMC_SLOT_NO_WP, \ - .dma_channel = 1 \ -} - -/** - * @brief Initialize SD SPI driver - * - * @note This function is not thread safe - * - * @return - * - ESP_OK on success - * - other error codes may be returned in future versions - */ -esp_err_t sdspi_host_init(); - -/** -* @brief Initialize SD SPI driver for the specific SPI controller -* -* @note This function is not thread safe -* -* @param slot SPI controller to use (HSPI_HOST or VSPI_HOST) -* @param slot_config pointer to slot configuration structure -* -* @return -* - ESP_OK on success -* - ESP_ERR_INVALID_ARG if sdspi_init_slot has invalid arguments -* - ESP_ERR_NO_MEM if memory can not be allocated -* - other errors from the underlying spi_master and gpio drivers -*/ -esp_err_t sdspi_host_init_slot(int slot, const sdspi_slot_config_t* slot_config); - -/** - * @brief Send command to the card and get response - * - * This function returns when command is sent and response is received, - * or data is transferred, or timeout occurs. - * - * @note This function is not thread safe w.r.t. init/deinit functions, - * and bus width/clock speed configuration functions. Multiple tasks - * can call sdspi_host_do_transaction as long as other sdspi_host_* - * functions are not called. - * - * @param slot SPI controller (HSPI_HOST or VSPI_HOST) - * @param cmdinfo pointer to structure describing command and data to transfer - * @return - * - ESP_OK on success - * - ESP_ERR_TIMEOUT if response or data transfer has timed out - * - ESP_ERR_INVALID_CRC if response or data transfer CRC check has failed - * - ESP_ERR_INVALID_RESPONSE if the card has sent an invalid response - */ -esp_err_t sdspi_host_do_transaction(int slot, sdmmc_command_t *cmdinfo); - -/** - * @brief Set card clock frequency - * - * Currently only integer fractions of 40MHz clock can be used. - * For High Speed cards, 40MHz can be used. - * For Default Speed cards, 20MHz can be used. - * - * @note This function is not thread safe - * - * @param slot SPI controller (HSPI_HOST or VSPI_HOST) - * @param freq_khz card clock frequency, in kHz - * @return - * - ESP_OK on success - * - other error codes may be returned in the future - */ -esp_err_t sdspi_host_set_card_clk(int slot, uint32_t freq_khz); - - -/** - * @brief Release resources allocated using sdspi_host_init - * - * @note This function is not thread safe - * - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_STATE if sdspi_host_init function has not been called - */ -esp_err_t sdspi_host_deinit(); - -#ifdef __cplusplus -} -#endif diff --git a/idf/modified/spi_flash/partition.c b/idf/modified/spi_flash/partition.c deleted file mode 100644 index f09b4af..0000000 --- a/idf/modified/spi_flash/partition.c +++ /dev/null @@ -1,36 +0,0 @@ -// 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 "esp_partition.h" - -esp_err_t esp_partition_read(const esp_partition_t* partition, - size_t src_offset, void* dst, size_t size) -{ - return ESP_OK; -} - -esp_err_t esp_partition_write(const esp_partition_t* partition, - size_t dst_offset, const void* src, size_t size) -{ - return ESP_OK; -} - -esp_err_t esp_partition_erase_range(const esp_partition_t* partition, - uint32_t start_addr, uint32_t size) //MVA size_t -> uint32_t -{ - return ESP_OK; -} - - - diff --git a/idf/modified/wear_levelling/include/wear_levelling.h b/idf/modified/wear_levelling/include/wear_levelling.h deleted file mode 100644 index 137b13f..0000000 --- a/idf/modified/wear_levelling/include/wear_levelling.h +++ /dev/null @@ -1,136 +0,0 @@ -// 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. - -#ifndef _wear_levelling_H_ -#define _wear_levelling_H_ - -#include "esp_log.h" -#include "esp_partition.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** -* @brief wear levelling handle -*/ -typedef int32_t wl_handle_t; - -#define WL_INVALID_HANDLE -1 - -/** -* @brief Mount WL for defined partition -* -* @param partition that will be used for access -* @param out_handle handle of the WL instance -* -* @return -* - ESP_OK, if the allocation was successfully; -* - ESP_ERR_INVALID_ARG, if WL allocation was unsuccessful; -* - ESP_ERR_NO_MEM, if there was no memory to allocate WL components; -*/ -esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle); - -/** -* @brief Unmount WL for defined partition -* -* @param handle WL partition handle -* -* @return -* - ESP_OK, if the operation completed successfully; -* - or one of error codes from lower-level flash driver. -*/ -esp_err_t wl_unmount(wl_handle_t handle); - -/** -* @brief Erase part of the WL storage -* -* @param handle WL handle that are related to the partition -* @param start_addr Address where erase operation should start. Must be aligned -* to the result of function wl_sector_size(...). -* @param size Size of the range which should be erased, in bytes. -* Must be divisible by result of function wl_sector_size(...).. -* -* @return -* - ESP_OK, if the range was erased successfully; -* - ESP_ERR_INVALID_ARG, if iterator or dst are NULL; -* - ESP_ERR_INVALID_SIZE, if erase would go out of bounds of the partition; -* - or one of error codes from lower-level flash driver. -*/ -esp_err_t wl_erase_range(wl_handle_t handle, size_t start_addr, size_t size); - -/** -* @brief Write data to the WL storage -* -* Before writing data to flash, corresponding region of flash needs to be erased. -* This can be done using wl_erase_range function. -* -* @param handle WL handle that are related to the partition -* @param dest_addr Address where the data should be written, relative to the -* beginning of the partition. -* @param src Pointer to the source buffer. Pointer must be non-NULL and -* buffer must be at least 'size' bytes long. -* @param size Size of data to be written, in bytes. -* -* @note Prior to writing to WL storage, make sure it has been erased with -* wl_erase_range call. -* -* @return -* - ESP_OK, if data was written successfully; -* - ESP_ERR_INVALID_ARG, if dst_offset exceeds partition size; -* - ESP_ERR_INVALID_SIZE, if write would go out of bounds of the partition; -* - or one of error codes from lower-level flash driver. -*/ -esp_err_t wl_write(wl_handle_t handle, size_t dest_addr, const void *src, size_t size); - -/** -* @brief Read data from the WL storage -* -* @param handle WL module instance that was initialized before -* @param dest Pointer to the buffer where data should be stored. -* Pointer must be non-NULL and buffer must be at least 'size' bytes long. -* @param src_addr Address of the data to be read, relative to the -* beginning of the partition. -* @param size Size of data to be read, in bytes. -* -* @return -* - ESP_OK, if data was read successfully; -* - ESP_ERR_INVALID_ARG, if src_offset exceeds partition size; -* - ESP_ERR_INVALID_SIZE, if read would go out of bounds of the partition; -* - or one of error codes from lower-level flash driver. -*/ -esp_err_t wl_read(wl_handle_t handle, size_t src_addr, void *dest, size_t size); - -/** -* @brief Get size of the WL storage -* -* @param handle WL module handle that was initialized before -* @return usable size, in bytes -*/ -size_t wl_size(wl_handle_t handle); - -/** -* @brief Get sector size of the WL instance -* -* @param handle WL module handle that was initialized before -* @return sector size, in bytes -*/ -size_t wl_sector_size(wl_handle_t handle); - - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // _wear_levelling_H_ diff --git a/idf/modified/wear_levelling/wear_levelling.cpp b/idf/modified/wear_levelling/wear_levelling.cpp deleted file mode 100644 index 02e2f76..0000000 --- a/idf/modified/wear_levelling/wear_levelling.cpp +++ /dev/null @@ -1,276 +0,0 @@ -// 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 -#include -#include -#include "WL_Config.h" -#include "WL_Ext_Cfg.h" -#include "WL_Flash.h" -#include "WL_Ext_Perf.h" -#include "WL_Ext_Safe.h" -#include "SPI_Flash.h" -#include "wear_levelling.h" -#include "FatPartition.h" - -#ifndef MAX_WL_HANDLES -#define MAX_WL_HANDLES 8 -#endif // MAX_WL_HANDLES - -#ifndef WL_DEFAULT_UPDATERATE -#define WL_DEFAULT_UPDATERATE 16 -#endif //WL_DEFAULT_UPDATERATE - -#ifndef WL_DEFAULT_TEMP_BUFF_SIZE -#define WL_DEFAULT_TEMP_BUFF_SIZE 32 -#endif //WL_DEFAULT_TEMP_BUFF_SIZE - -#ifndef WL_DEFAULT_WRITE_SIZE -#define WL_DEFAULT_WRITE_SIZE 16 -#endif //WL_DEFAULT_WRITE_SIZE - -#ifndef WL_DEFAULT_START_ADDR -#define WL_DEFAULT_START_ADDR 0 -#endif //WL_DEFAULT_START_ADDR - -#ifndef WL_CURRENT_VERSION -#define WL_CURRENT_VERSION 1 -#endif //WL_CURRENT_VERSION - -typedef struct { - WL_Flash *instance; - _lock_t lock; -} wl_instance_t; - -static wl_instance_t s_instances[MAX_WL_HANDLES]; -static _lock_t s_instances_lock; -static const char *TAG = "wear_levelling"; - -static esp_err_t check_handle(wl_handle_t handle, const char *func); - -esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle) -{ - // Initialize variables before the first jump to cleanup label - void *wl_flash_ptr = NULL; - WL_Flash *wl_flash = NULL; - void *part_ptr = NULL; - FatPartition *part = NULL; //Partition -> FatPartition - - _lock_acquire(&s_instances_lock); - esp_err_t result = ESP_OK; - *out_handle = WL_INVALID_HANDLE; - for (size_t i = 0; i < MAX_WL_HANDLES; i++) { - if (s_instances[i].instance == NULL) { - *out_handle = i; - break; - } - } - if (*out_handle == WL_INVALID_HANDLE) { - ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES); - result = ESP_ERR_NO_MEM; - goto out; - } - - wl_ext_cfg_t cfg; - cfg.full_mem_size = partition->size; - cfg.start_addr = WL_DEFAULT_START_ADDR; - cfg.version = WL_CURRENT_VERSION; - cfg.sector_size = SPI_FLASH_SEC_SIZE; - cfg.page_size = SPI_FLASH_SEC_SIZE; - cfg.updaterate = WL_DEFAULT_UPDATERATE; - cfg.temp_buff_size = WL_DEFAULT_TEMP_BUFF_SIZE; - cfg.wr_size = WL_DEFAULT_WRITE_SIZE; - // FAT sector size by default will be 512 - cfg.fat_sector_size = CONFIG_WL_SECTOR_SIZE; - - // Allocate memory for a Partition object, and then initialize the object - // using placement new operator. This way we can recover from out of - // memory condition. - part_ptr = malloc(sizeof(FatPartition)); - if (part_ptr == NULL) { - result = ESP_ERR_NO_MEM; - ESP_LOGE(TAG, "%s: can't allocate FatPartition", __func__); - goto out; - } - part = new (part_ptr) FatPartition(partition); - - // Same for WL_Flash: allocate memory, use placement new -#if CONFIG_WL_SECTOR_SIZE == 512 -#if CONFIG_WL_SECTOR_MODE == 1 - wl_flash_ptr = malloc(sizeof(WL_Ext_Safe)); - - if (wl_flash_ptr == NULL) { - result = ESP_ERR_NO_MEM; - ESP_LOGE(TAG, "%s: can't allocate WL_Ext_Safe", __func__); - goto out; - } - wl_flash = new (wl_flash_ptr) WL_Ext_Safe(); -#else - wl_flash_ptr = malloc(sizeof(WL_Ext_Perf)); - - if (wl_flash_ptr == NULL) { - result = ESP_ERR_NO_MEM; - ESP_LOGE(TAG, "%s: can't allocate WL_Ext_Perf", __func__); - goto out; - } - wl_flash = new (wl_flash_ptr) WL_Ext_Perf(); -#endif // CONFIG_WL_SECTOR_MODE -#endif // CONFIG_WL_SECTOR_SIZE -#if CONFIG_WL_SECTOR_SIZE == 4096 - wl_flash_ptr = malloc(sizeof(WL_Flash)); - - if (wl_flash_ptr == NULL) { - result = ESP_ERR_NO_MEM; - ESP_LOGE(TAG, "%s: can't allocate WL_Flash", __func__); - goto out; - } - wl_flash = new (wl_flash_ptr) WL_Flash(); -#endif // CONFIG_WL_SECTOR_SIZE - - result = wl_flash->config(&cfg, part); - if (ESP_OK != result) { - ESP_LOGE(TAG, "%s: config instance=0x%08x, result=0x%x", __func__, *out_handle, result); - goto out; - } - result = wl_flash->init(); - if (ESP_OK != result) { - ESP_LOGE(TAG, "%s: init instance=0x%08x, result=0x%x", __func__, *out_handle, result); - goto out; - } - s_instances[*out_handle].instance = wl_flash; - _lock_init(&s_instances[*out_handle].lock); - _lock_release(&s_instances_lock); - return ESP_OK; - -out: - _lock_release(&s_instances_lock); - *out_handle = WL_INVALID_HANDLE; - if (wl_flash) { - wl_flash->~WL_Flash(); - free(wl_flash); - } - if (part) { - part->~FatPartition(); - free(part); - } - return result; -} - -esp_err_t wl_unmount(wl_handle_t handle) -{ - esp_err_t result = ESP_OK; - _lock_acquire(&s_instances_lock); - result = check_handle(handle, __func__); - if (result == ESP_OK) { - ESP_LOGV(TAG, "deleting handle 0x%08x", handle); - // We have to flush state of the component - result = s_instances[handle].instance->flush(); - // We use placement new in wl_mount, so call destructor directly - Flash_Access *drv = s_instances[handle].instance->get_drv(); - drv->~Flash_Access(); - free(drv); - s_instances[handle].instance->~WL_Flash(); - free(s_instances[handle].instance); - s_instances[handle].instance = NULL; - _lock_close(&s_instances[handle].lock); // also zeroes the lock variable - } - _lock_release(&s_instances_lock); - return result; -} - -esp_err_t wl_erase_range(wl_handle_t handle, size_t start_addr, size_t size) -{ - esp_err_t result = check_handle(handle, __func__); - if (result != ESP_OK) { - return result; - } - _lock_acquire(&s_instances[handle].lock); - result = s_instances[handle].instance->erase_range(start_addr, size); - _lock_release(&s_instances[handle].lock); - return result; -} - -esp_err_t wl_write(wl_handle_t handle, size_t dest_addr, const void *src, size_t size) -{ - esp_err_t result = check_handle(handle, __func__); - if (result != ESP_OK) { - return result; - } - _lock_acquire(&s_instances[handle].lock); - result = s_instances[handle].instance->write(dest_addr, src, size); - _lock_release(&s_instances[handle].lock); - return result; -} - -esp_err_t wl_read(wl_handle_t handle, size_t src_addr, void *dest, size_t size) -{ - esp_err_t result = check_handle(handle, __func__); - if (result != ESP_OK) { - return result; - } - _lock_acquire(&s_instances[handle].lock); - result = s_instances[handle].instance->read(src_addr, dest, size); - _lock_release(&s_instances[handle].lock); - return result; -} - -size_t wl_size(wl_handle_t handle) -{ - esp_err_t err = check_handle(handle, __func__); - if (err != ESP_OK) { - return 0; - } - _lock_acquire(&s_instances[handle].lock); - size_t result = s_instances[handle].instance->chip_size(); - _lock_release(&s_instances[handle].lock); - - //ESP_LOGI(TAG, "%s: result=%zu", __func__, result); //MVA The z portion is a length specifier which says the argument will be size_t in length. - return result; -} - -//static int in_cnt = 0; - -size_t wl_sector_size(wl_handle_t handle) -{ - esp_err_t err = check_handle(handle, __func__); - if (err != ESP_OK) { - return 0; - } - - _lock_acquire(&s_instances[handle].lock); - size_t result = s_instances[handle].instance->sector_size(); - _lock_release(&s_instances[handle].lock); - - //ESP_LOGI(TAG, "%s: result=%d", __func__, result); - //assert(++in_cnt < 100); - - return result; -} - -static esp_err_t check_handle(wl_handle_t handle, const char *func) -{ - if (handle == WL_INVALID_HANDLE) { - ESP_LOGE(TAG, "%s: invalid handle", func); - return ESP_ERR_NOT_FOUND; - } - if (handle >= MAX_WL_HANDLES) { - ESP_LOGE(TAG, "%s: instance[0x%08x] out of range", func, handle); - return ESP_ERR_INVALID_ARG; - } - if (s_instances[handle].instance == NULL) { - ESP_LOGE(TAG, "%s: instance[0x%08x] not initialized", func, handle); - return ESP_ERR_NOT_FOUND; - } - return ESP_OK; -} diff --git a/idf/orig/driver/include/driver/sdmmc_host.h b/idf/orig/driver/include/driver/sdmmc_host.h deleted file mode 100644 index f2e2d66..0000000 --- a/idf/orig/driver/include/driver/sdmmc_host.h +++ /dev/null @@ -1,172 +0,0 @@ -// 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. - -#pragma once - -#include -#include -#include "esp_err.h" -#include "sdmmc_types.h" -#include "driver/gpio.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define SDMMC_HOST_SLOT_0 0 ///< SDMMC slot 0 -#define SDMMC_HOST_SLOT_1 1 ///< SDMMC slot 1 - -/** - * @brief Default sdmmc_host_t structure initializer for SDMMC peripheral - * - * Uses SDMMC peripheral, with 4-bit mode enabled, and max frequency set to 20MHz - */ -#define SDMMC_HOST_DEFAULT() {\ - .flags = SDMMC_HOST_FLAG_4BIT, \ - .slot = SDMMC_HOST_SLOT_1, \ - .max_freq_khz = SDMMC_FREQ_DEFAULT, \ - .io_voltage = 3.3f, \ - .init = &sdmmc_host_init, \ - .set_bus_width = &sdmmc_host_set_bus_width, \ - .set_card_clk = &sdmmc_host_set_card_clk, \ - .do_transaction = &sdmmc_host_do_transaction, \ - .deinit = &sdmmc_host_deinit, \ -} - -/** - * Extra configuration for SDMMC peripheral slot - */ -typedef struct { - gpio_num_t gpio_cd; ///< GPIO number of card detect signal - gpio_num_t gpio_wp; ///< GPIO number of write protect signal - uint8_t width; ///< Bus width used by the slot (might be less than the max width supported) -} sdmmc_slot_config_t; - -#define SDMMC_SLOT_NO_CD ((gpio_num_t) -1) ///< indicates that card detect line is not used -#define SDMMC_SLOT_NO_WP ((gpio_num_t) -1) ///< indicates that write protect line is not used -#define SDMMC_SLOT_WIDTH_DEFAULT 0 ///< use the default width for the slot (8 for slot 0, 4 for slot 1) - -/** - * Macro defining default configuration of SDMMC host slot - */ -#define SDMMC_SLOT_CONFIG_DEFAULT() {\ - .gpio_cd = SDMMC_SLOT_NO_CD, \ - .gpio_wp = SDMMC_SLOT_NO_WP, \ - .width = SDMMC_SLOT_WIDTH_DEFAULT, \ -} - -/** - * @brief Initialize SDMMC host peripheral - * - * @note This function is not thread safe - * - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_STATE if sdmmc_host_init was already called - * - ESP_ERR_NO_MEM if memory can not be allocated - */ -esp_err_t sdmmc_host_init(); - -/** - * @brief Initialize given slot of SDMMC peripheral - * - * On the ESP32, SDMMC peripheral has two slots: - * - Slot 0: 8-bit wide, maps to HS1_* signals in PIN MUX - * - Slot 1: 4-bit wide, maps to HS2_* signals in PIN MUX - * - * Card detect and write protect signals can be routed to - * arbitrary GPIOs using GPIO matrix. - * - * @note This function is not thread safe - * - * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1) - * @param slot_config additional configuration for the slot - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_STATE if host has not been initialized using sdmmc_host_init - */ -esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config); - -/** - * @brief Select bus width to be used for data transfer - * - * SD/MMC card must be initialized prior to this command, and a command to set - * bus width has to be sent to the card (e.g. SD_APP_SET_BUS_WIDTH) - * - * @note This function is not thread safe - * - * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1) - * @param width bus width (1, 4, or 8 for slot 0; 1 or 4 for slot 1) - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_ARG if slot number or width is not valid - */ -esp_err_t sdmmc_host_set_bus_width(int slot, size_t width); - -/** - * @brief Set card clock frequency - * - * Currently only integer fractions of 40MHz clock can be used. - * For High Speed cards, 40MHz can be used. - * For Default Speed cards, 20MHz can be used. - * - * @note This function is not thread safe - * - * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1) - * @param freq_khz card clock frequency, in kHz - * @return - * - ESP_OK on success - * - other error codes may be returned in the future - */ -esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz); - -/** - * @brief Send command to the card and get response - * - * This function returns when command is sent and response is received, - * or data is transferred, or timeout occurs. - * - * @note This function is not thread safe w.r.t. init/deinit functions, - * and bus width/clock speed configuration functions. Multiple tasks - * can call sdmmc_host_do_transaction as long as other sdmmc_host_* - * functions are not called. - * - * @attention Data buffer passed in cmdinfo->data must be in DMA capable memory - * - * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1) - * @param cmdinfo pointer to structure describing command and data to transfer - * @return - * - ESP_OK on success - * - ESP_ERR_TIMEOUT if response or data transfer has timed out - * - ESP_ERR_INVALID_CRC if response or data transfer CRC check has failed - * - ESP_ERR_INVALID_RESPONSE if the card has sent an invalid response - * - ESP_ERR_INVALID_SIZE if the size of data transfer is not valid in SD protocol - * - ESP_ERR_INVALID_ARG if the data buffer is not in DMA capable memory - */ -esp_err_t sdmmc_host_do_transaction(int slot, sdmmc_command_t* cmdinfo); - -/** - * @brief Disable SDMMC host and release allocated resources - * - * @note This function is not thread safe - * - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_STATE if sdmmc_host_init function has not been called - */ -esp_err_t sdmmc_host_deinit(); - -#ifdef __cplusplus -} -#endif diff --git a/idf/orig/driver/include/driver/sdmmc_types.h b/idf/orig/driver/include/driver/sdmmc_types.h deleted file mode 100644 index 4bc1e05..0000000 --- a/idf/orig/driver/include/driver/sdmmc_types.h +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (c) 2006 Uwe Stuehler - * Adaptations to ESP-IDF Copyright (c) 2016 Espressif Systems (Shanghai) PTE LTD - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _SDMMC_TYPES_H_ -#define _SDMMC_TYPES_H_ - -#include -#include -#include "esp_err.h" - -/** - * Decoded values from SD card Card Specific Data register - */ -typedef struct { - int csd_ver; /*!< CSD structure format */ - int mmc_ver; /*!< MMC version (for CID format) */ - int capacity; /*!< total number of sectors */ - int sector_size; /*!< sector size in bytes */ - int read_block_len; /*!< block length for reads */ - int card_command_class; /*!< Card Command Class for SD */ - int tr_speed; /*!< Max transfer speed */ -} sdmmc_csd_t; - -/** - * Decoded values from SD card Card IDentification register - */ -typedef struct { - int mfg_id; /*!< manufacturer identification number */ - int oem_id; /*!< OEM/product identification number */ - char name[8]; /*!< product name (MMC v1 has the longest) */ - int revision; /*!< product revision */ - int serial; /*!< product serial number */ - int date; /*!< manufacturing date */ -} sdmmc_cid_t; - -/** - * Decoded values from SD Configuration Register - */ -typedef struct { - int sd_spec; /*!< SD Physical layer specification version, reported by card */ - int bus_width; /*!< bus widths supported by card: BIT(0) — 1-bit bus, BIT(2) — 4-bit bus */ -} sdmmc_scr_t; - -/** - * SD/MMC command response buffer - */ -typedef uint32_t sdmmc_response_t[4]; - -/** - * SD SWITCH_FUNC response buffer - */ -typedef struct { - uint32_t data[512 / 8 / sizeof(uint32_t)]; /*!< response data */ -} sdmmc_switch_func_rsp_t; - -/** - * SD/MMC command information - */ -typedef struct { - uint32_t opcode; /*!< SD or MMC command index */ - uint32_t arg; /*!< SD/MMC command argument */ - sdmmc_response_t response; /*!< response buffer */ - void* data; /*!< buffer to send or read into */ - size_t datalen; /*!< length of data buffer */ - size_t blklen; /*!< block length */ - int flags; /*!< see below */ -#define SCF_ITSDONE 0x0001 /*!< command is complete */ -#define SCF_CMD(flags) ((flags) & 0x00f0) -#define SCF_CMD_AC 0x0000 -#define SCF_CMD_ADTC 0x0010 -#define SCF_CMD_BC 0x0020 -#define SCF_CMD_BCR 0x0030 -#define SCF_CMD_READ 0x0040 /*!< read command (data expected) */ -#define SCF_RSP_BSY 0x0100 -#define SCF_RSP_136 0x0200 -#define SCF_RSP_CRC 0x0400 -#define SCF_RSP_IDX 0x0800 -#define SCF_RSP_PRESENT 0x1000 -/* response types */ -#define SCF_RSP_R0 0 /*!< none */ -#define SCF_RSP_R1 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX) -#define SCF_RSP_R1B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY) -#define SCF_RSP_R2 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_136) -#define SCF_RSP_R3 (SCF_RSP_PRESENT) -#define SCF_RSP_R4 (SCF_RSP_PRESENT) -#define SCF_RSP_R5 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX) -#define SCF_RSP_R5B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY) -#define SCF_RSP_R6 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX) -#define SCF_RSP_R7 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX) - esp_err_t error; /*!< error returned from transfer */ -} sdmmc_command_t; - -/** - * SD/MMC Host description - * - * This structure defines properties of SD/MMC host and functions - * of SD/MMC host which can be used by upper layers. - */ -typedef struct { - uint32_t flags; /*!< flags defining host properties */ -#define SDMMC_HOST_FLAG_1BIT BIT(0) /*!< host supports 1-line SD and MMC protocol */ -#define SDMMC_HOST_FLAG_4BIT BIT(1) /*!< host supports 4-line SD and MMC protocol */ -#define SDMMC_HOST_FLAG_8BIT BIT(2) /*!< host supports 8-line MMC protocol */ -#define SDMMC_HOST_FLAG_SPI BIT(3) /*!< host supports SPI protocol */ - int slot; /*!< slot number, to be passed to host functions */ - int max_freq_khz; /*!< max frequency supported by the host */ -#define SDMMC_FREQ_DEFAULT 20000 /*!< SD/MMC Default speed (limited by clock divider) */ -#define SDMMC_FREQ_HIGHSPEED 40000 /*!< SD High speed (limited by clock divider) */ -#define SDMMC_FREQ_PROBING 400 /*!< SD/MMC probing speed */ - float io_voltage; /*!< I/O voltage used by the controller (voltage switching is not supported) */ - esp_err_t (*init)(void); /*!< Host function to initialize the driver */ - esp_err_t (*set_bus_width)(int slot, size_t width); /*!< host function to set bus width */ - esp_err_t (*set_card_clk)(int slot, uint32_t freq_khz); /*!< host function to set card clock frequency */ - esp_err_t (*do_transaction)(int slot, sdmmc_command_t* cmdinfo); /*!< host function to do a transaction */ - esp_err_t (*deinit)(void); /*!< host function to deinitialize the driver */ -} sdmmc_host_t; - -/** - * SD/MMC card information structure - */ -typedef struct { - sdmmc_host_t host; /*!< Host with which the card is associated */ - uint32_t ocr; /*!< OCR (Operation Conditions Register) value */ - sdmmc_cid_t cid; /*!< decoded CID (Card IDentification) register value */ - sdmmc_csd_t csd; /*!< decoded CSD (Card-Specific Data) register value */ - sdmmc_scr_t scr; /*!< decoded SCR (SD card Configuration Register) value */ - uint16_t rca; /*!< RCA (Relative Card Address) */ -} sdmmc_card_t; - - - - -#endif // _SDMMC_TYPES_H_ diff --git a/idf/orig/driver/include/driver/spi_master.h b/idf/orig/driver/include/driver/spi_master.h deleted file mode 100644 index c501998..0000000 --- a/idf/orig/driver/include/driver/spi_master.h +++ /dev/null @@ -1,221 +0,0 @@ -// Copyright 2010-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. - - -#ifndef _DRIVER_SPI_MASTER_H_ -#define _DRIVER_SPI_MASTER_H_ - -#include "esp_err.h" -#include "freertos/FreeRTOS.h" -#include "freertos/semphr.h" - -#include "driver/spi_common.h" - - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define SPI_DEVICE_TXBIT_LSBFIRST (1<<0) ///< Transmit command/address/data LSB first instead of the default MSB first -#define SPI_DEVICE_RXBIT_LSBFIRST (1<<1) ///< Receive data LSB first instead of the default MSB first -#define SPI_DEVICE_BIT_LSBFIRST (SPI_DEVICE_TXBIT_LSBFIRST|SPI_DEVICE_RXBIT_LSBFIRST); ///< Transmit and receive LSB first -#define SPI_DEVICE_3WIRE (1<<2) ///< Use MOSI (=spid) for both sending and receiving data -#define SPI_DEVICE_POSITIVE_CS (1<<3) ///< Make CS positive during a transaction instead of negative -#define SPI_DEVICE_HALFDUPLEX (1<<4) ///< Transmit data before receiving it, instead of simultaneously -#define SPI_DEVICE_CLK_AS_CS (1<<5) ///< Output clock on CS line if CS is active - - -typedef struct spi_transaction_t spi_transaction_t; -typedef void(*transaction_cb_t)(spi_transaction_t *trans); - -/** - * @brief This is a configuration for a SPI slave device that is connected to one of the SPI buses. - */ -typedef struct { - uint8_t command_bits; ///< Amount of bits in command phase (0-16) - uint8_t address_bits; ///< Amount of bits in address phase (0-64) - uint8_t dummy_bits; ///< Amount of dummy bits to insert between address and data phase - uint8_t mode; ///< SPI mode (0-3) - uint8_t duty_cycle_pos; ///< Duty cycle of positive clock, in 1/256th increments (128 = 50%/50% duty). Setting this to 0 (=not setting it) is equivalent to setting this to 128. - uint8_t cs_ena_pretrans; ///< Amount of SPI bit-cycles the cs should be activated before the transmission (0-16). This only works on half-duplex transactions. - uint8_t cs_ena_posttrans; ///< Amount of SPI bit-cycles the cs should stay active after the transmission (0-16) - int clock_speed_hz; ///< Clock speed, in Hz - int spics_io_num; ///< CS GPIO pin for this device, or -1 if not used - uint32_t flags; ///< Bitwise OR of SPI_DEVICE_* flags - int queue_size; ///< Transaction queue size. This sets how many transactions can be 'in the air' (queued using spi_device_queue_trans but not yet finished using spi_device_get_trans_result) at the same time - transaction_cb_t pre_cb; ///< Callback to be called before a transmission is started. This callback is called within interrupt context. - transaction_cb_t post_cb; ///< Callback to be called after a transmission has completed. This callback is called within interrupt context. -} spi_device_interface_config_t; - - -#define SPI_TRANS_MODE_DIO (1<<0) ///< Transmit/receive data in 2-bit mode -#define SPI_TRANS_MODE_QIO (1<<1) ///< Transmit/receive data in 4-bit mode -#define SPI_TRANS_MODE_DIOQIO_ADDR (1<<4) ///< Also transmit address in mode selected by SPI_MODE_DIO/SPI_MODE_QIO -#define SPI_TRANS_USE_RXDATA (1<<2) ///< Receive into rx_data member of spi_transaction_t instead into memory at rx_buffer. -#define SPI_TRANS_USE_TXDATA (1<<3) ///< Transmit tx_data member of spi_transaction_t instead of data at tx_buffer. Do not set tx_buffer when using this. - -/** - * This structure describes one SPI transaction. The descriptor should not be modified until the transaction finishes. - */ -struct spi_transaction_t { - uint32_t flags; ///< Bitwise OR of SPI_TRANS_* flags - uint16_t cmd; ///< Command data, of which the length is set in the ``command_bits`` of spi_device_interface_config_t. - ///< NOTE: this field, used to be "command" in ESP-IDF 2.1 and before, is re-written to be used in a new way in ESP-IDF 3.0. - ///< - Example: write 0x0123 and command_bits=12 to send command 0x12, 0x3_ (in previous version, you may have to write 0x3_12). - uint64_t addr; ///< Address data, of which the length is set in the ``address_bits`` of spi_device_interface_config_t. - ///< NOTE: this field, used to be "address" in ESP-IDF 2.1 and before, is re-written to be used in a new way in ESP-IDF3.0. - ///< - Example: write 0x123400 and address_bits=24 to send address of 0x12, 0x34, 0x00 (in previous version, you may have to write 0x12340000). - size_t length; ///< Total data length, in bits - size_t rxlength; ///< Total data length received, should be not greater than ``length`` in full-duplex mode (0 defaults this to the value of ``length``). - void *user; ///< User-defined variable. Can be used to store eg transaction ID. - union { - const void *tx_buffer; ///< Pointer to transmit buffer, or NULL for no MOSI phase - uint8_t tx_data[4]; ///< If SPI_USE_TXDATA is set, data set here is sent directly from this variable. - }; - union { - void *rx_buffer; ///< Pointer to receive buffer, or NULL for no MISO phase. Written by 4 bytes-unit if DMA is used. - uint8_t rx_data[4]; ///< If SPI_USE_RXDATA is set, data is received directly to this variable - }; -} ; //the rx data should start from a 32-bit aligned address to get around dma issue. - - -typedef struct spi_device_t* spi_device_handle_t; ///< Handle for a device on a SPI bus - -/** - * @brief Initialize a SPI bus - * - * @warning For now, only supports HSPI and VSPI. - * - * @param host SPI peripheral that controls this bus - * @param bus_config Pointer to a spi_bus_config_t struct specifying how the host should be initialized - * @param dma_chan Either channel 1 or 2, or 0 in the case when no DMA is required. Selecting a DMA channel - * for a SPI bus allows transfers on the bus to have sizes only limited by the amount of - * internal memory. Selecting no DMA channel (by passing the value 0) limits the amount of - * bytes transfered to a maximum of 32. - * - * @warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in - * DMA-capable memory. - * - * @return - * - ESP_ERR_INVALID_ARG if configuration is invalid - * - ESP_ERR_INVALID_STATE if host already is in use - * - ESP_ERR_NO_MEM if out of memory - * - ESP_OK on success - */ -esp_err_t spi_bus_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config, int dma_chan); - -/** - * @brief Free a SPI bus - * - * @warning In order for this to succeed, all devices have to be removed first. - * - * @param host SPI peripheral to free - * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_ERR_INVALID_STATE if not all devices on the bus are freed - * - ESP_OK on success - */ -esp_err_t spi_bus_free(spi_host_device_t host); - -/** - * @brief Allocate a device on a SPI bus - * - * This initializes the internal structures for a device, plus allocates a CS pin on the indicated SPI master - * peripheral and routes it to the indicated GPIO. All SPI master devices have three CS pins and can thus control - * up to three devices. - * - * @note While in general, speeds up to 80MHz on the dedicated SPI pins and 40MHz on GPIO-matrix-routed pins are - * supported, full-duplex transfers routed over the GPIO matrix only support speeds up to 26MHz. - * - * @param host SPI peripheral to allocate device on - * @param dev_config SPI interface protocol config for the device - * @param handle Pointer to variable to hold the device handle - * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_ERR_NOT_FOUND if host doesn't have any free CS slots - * - ESP_ERR_NO_MEM if out of memory - * - ESP_OK on success - */ -esp_err_t spi_bus_add_device(spi_host_device_t host, spi_device_interface_config_t *dev_config, spi_device_handle_t *handle); - - -/** - * @brief Remove a device from the SPI bus - * - * @param handle Device handle to free - * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_ERR_INVALID_STATE if device already is freed - * - ESP_OK on success - */ -esp_err_t spi_bus_remove_device(spi_device_handle_t handle); - - -/** - * @brief Queue a SPI transaction for execution - * - * @param handle Device handle obtained using spi_host_add_dev - * @param trans_desc Description of transaction to execute - * @param ticks_to_wait Ticks to wait until there's room in the queue; use portMAX_DELAY to - * never time out. - * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_OK on success - */ -esp_err_t spi_device_queue_trans(spi_device_handle_t handle, spi_transaction_t *trans_desc, TickType_t ticks_to_wait); - - -/** - * @brief Get the result of a SPI transaction queued earlier - * - * This routine will wait until a transaction to the given device (queued earlier with - * spi_device_queue_trans) has succesfully completed. It will then return the description of the - * completed transaction so software can inspect the result and e.g. free the memory or - * re-use the buffers. - * - * @param handle Device handle obtained using spi_host_add_dev - * @param trans_desc Pointer to variable able to contain a pointer to the description of the transaction - that is executed. The descriptor should not be modified until the descriptor is returned by - spi_device_get_trans_result. - * @param ticks_to_wait Ticks to wait until there's a returned item; use portMAX_DELAY to never time - out. - * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_OK on success - */ -esp_err_t spi_device_get_trans_result(spi_device_handle_t handle, spi_transaction_t **trans_desc, TickType_t ticks_to_wait); - - -/** - * @brief Do a SPI transaction - * - * Essentially does the same as spi_device_queue_trans followed by spi_device_get_trans_result. Do - * not use this when there is still a transaction queued that hasn't been finalized - * using spi_device_get_trans_result. - * - * @param handle Device handle obtained using spi_host_add_dev - * @param trans_desc Description of transaction to execute - * @return - * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_OK on success - */ -esp_err_t spi_device_transmit(spi_device_handle_t handle, spi_transaction_t *trans_desc); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/idf/orig/esp32/include/rom/crc.h b/idf/orig/esp32/include/rom/crc.h deleted file mode 100644 index 84e1788..0000000 --- a/idf/orig/esp32/include/rom/crc.h +++ /dev/null @@ -1,127 +0,0 @@ -// 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. - -#ifndef ROM_CRC_H -#define ROM_CRC_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** \defgroup uart_apis, uart configuration and communication related apis - * @brief uart apis - */ - -/** @addtogroup uart_apis - * @{ - */ - - -/* Standard CRC8/16/32 algorithms. */ -// CRC-8 x8+x2+x1+1 0x07 -// CRC16-CCITT x16+x12+x5+1 1021 ISO HDLC, ITU X.25, V.34/V.41/V.42, PPP-FCS -// CRC32: -//G(x) = x32 +x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x1 + 1 -//If your buf is not continuous, you can use the first result to be the second parameter. - -/** - * @brief Crc32 value that is in little endian. - * - * @param uint32_t crc : init crc value, use 0 at the first use. - * - * @param uint8_t const *buf : buffer to start calculate crc. - * - * @param uint32_t len : buffer length in byte. - * - * @return None - */ -uint32_t crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len); - -/** - * @brief Crc32 value that is in big endian. - * - * @param uint32_t crc : init crc value, use 0 at the first use. - * - * @param uint8_t const *buf : buffer to start calculate crc. - * - * @param uint32_t len : buffer length in byte. - * - * @return None - */ -uint32_t crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len); - -/** - * @brief Crc16 value that is in little endian. - * - * @param uint16_t crc : init crc value, use 0 at the first use. - * - * @param uint8_t const *buf : buffer to start calculate crc. - * - * @param uint32_t len : buffer length in byte. - * - * @return None - */ -uint16_t crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len); - -/** - * @brief Crc16 value that is in big endian. - * - * @param uint16_t crc : init crc value, use 0 at the first use. - * - * @param uint8_t const *buf : buffer to start calculate crc. - * - * @param uint32_t len : buffer length in byte. - * - * @return None - */ -uint16_t crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len); - -/** - * @brief Crc8 value that is in little endian. - * - * @param uint8_t crc : init crc value, use 0 at the first use. - * - * @param uint8_t const *buf : buffer to start calculate crc. - * - * @param uint32_t len : buffer length in byte. - * - * @return None - */ -uint8_t crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len); - -/** - * @brief Crc8 value that is in big endian. - * - * @param uint32_t crc : init crc value, use 0 at the first use. - * - * @param uint8_t const *buf : buffer to start calculate crc. - * - * @param uint32_t len : buffer length in byte. - * - * @return None - */ -uint8_t crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/idf/orig/fatfs/src/diskio_spiflash.c b/idf/orig/fatfs/src/diskio_spiflash.c deleted file mode 100644 index a4a40f6..0000000 --- a/idf/orig/fatfs/src/diskio_spiflash.c +++ /dev/null @@ -1,117 +0,0 @@ -// 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 -#include "diskio.h" -#include "ffconf.h" -#include "ff.h" -#include "esp_log.h" -#include "diskio_spiflash.h" -#include "wear_levelling.h" - -static const char* TAG = "ff_diskio_spiflash"; - -wl_handle_t ff_wl_handles[_VOLUMES] = { - WL_INVALID_HANDLE, - WL_INVALID_HANDLE, -}; - -DSTATUS ff_wl_initialize (BYTE pdrv) -{ - return 0; -} - -DSTATUS ff_wl_status (BYTE pdrv) -{ - return 0; -} - -DRESULT ff_wl_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count) -{ - ESP_LOGV(TAG, "ff_wl_read - pdrv=%i, sector=%i, count=%i\n", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count); - wl_handle_t wl_handle = ff_wl_handles[pdrv]; - assert(wl_handle + 1); - esp_err_t err = wl_read(wl_handle, sector * wl_sector_size(wl_handle), buff, count * wl_sector_size(wl_handle)); - if (err != ESP_OK) { - ESP_LOGE(TAG, "wl_read failed (%d)", err); - return RES_ERROR; - } - return RES_OK; -} - -DRESULT ff_wl_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count) -{ - ESP_LOGV(TAG, "ff_wl_write - pdrv=%i, sector=%i, count=%i\n", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count); - wl_handle_t wl_handle = ff_wl_handles[pdrv]; - assert(wl_handle + 1); - esp_err_t err = wl_erase_range(wl_handle, sector * wl_sector_size(wl_handle), count * wl_sector_size(wl_handle)); - if (err != ESP_OK) { - ESP_LOGE(TAG, "wl_erase_range failed (%d)", err); - return RES_ERROR; - } - err = wl_write(wl_handle, sector * wl_sector_size(wl_handle), buff, count * wl_sector_size(wl_handle)); - if (err != ESP_OK) { - ESP_LOGE(TAG, "wl_write failed (%d)", err); - return RES_ERROR; - } - return RES_OK; -} - -DRESULT ff_wl_ioctl (BYTE pdrv, BYTE cmd, void *buff) -{ - wl_handle_t wl_handle = ff_wl_handles[pdrv]; - ESP_LOGV(TAG, "ff_wl_ioctl: cmd=%i\n", cmd); - assert(wl_handle + 1); - switch (cmd) { - case CTRL_SYNC: - return RES_OK; - case GET_SECTOR_COUNT: - *((uint32_t *) buff) = wl_size(wl_handle) / wl_sector_size(wl_handle); - return RES_OK; - case GET_SECTOR_SIZE: - *((uint32_t *) buff) = wl_sector_size(wl_handle); - return RES_OK; - case GET_BLOCK_SIZE: - return RES_ERROR; - } - return RES_ERROR; -} - - -esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle) -{ - if (pdrv >= _VOLUMES) { - return ESP_ERR_INVALID_ARG; - } - static const ff_diskio_impl_t wl_impl = { - .init = &ff_wl_initialize, - .status = &ff_wl_status, - .read = &ff_wl_read, - .write = &ff_wl_write, - .ioctl = &ff_wl_ioctl - }; - ff_wl_handles[pdrv] = flash_handle; - ff_diskio_register(pdrv, &wl_impl); - return ESP_OK; -} - -BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle) -{ - for (int i = 0; i < _VOLUMES; i++) { - if (flash_handle == ff_wl_handles[i]) { - return i; - } - } - return 0xff; -} diff --git a/idf/orig/sdmmc/include/sdmmc_cmd.h b/idf/orig/sdmmc/include/sdmmc_cmd.h deleted file mode 100644 index 58b6f08..0000000 --- a/idf/orig/sdmmc/include/sdmmc_cmd.h +++ /dev/null @@ -1,77 +0,0 @@ -// 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. - -#pragma once - -#include -#include "esp_err.h" -#include "driver/sdmmc_types.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Probe and initialize SD/MMC card using given host - * - * @note Only SD cards (SDSC and SDHC/SDXC) are supported now. - * Support for MMC/eMMC cards will be added later. - * - * @param host pointer to structure defining host controller - * @param out_card pointer to structure which will receive information about the card when the function completes - * @return - * - ESP_OK on success - * - One of the error codes from SDMMC host controller - */ -esp_err_t sdmmc_card_init(const sdmmc_host_t* host, - sdmmc_card_t* out_card); - -/** - * @brief Print information about the card to a stream - * @param stream stream obtained using fopen or fdopen - * @param card card information structure initialized using sdmmc_card_init - */ -void sdmmc_card_print_info(FILE* stream, const sdmmc_card_t* card); - -/** - * Write given number of sectors to SD/MMC card - * - * @param card pointer to card information structure previously initialized using sdmmc_card_init - * @param src pointer to data buffer to read data from; data size must be equal to sector_count * card->csd.sector_size - * @param start_sector sector where to start writing - * @param sector_count number of sectors to write - * @return - * - ESP_OK on success - * - One of the error codes from SDMMC host controller - */ -esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src, - size_t start_sector, size_t sector_count); - -/** - * Write given number of sectors to SD/MMC card - * - * @param card pointer to card information structure previously initialized using sdmmc_card_init - * @param dst pointer to data buffer to write into; buffer size must be at least sector_count * card->csd.sector_size - * @param start_sector sector where to start reading - * @param sector_count number of sectors to read - * @return - * - ESP_OK on success - * - One of the error codes from SDMMC host controller - */ -esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst, - size_t start_sector, size_t sector_count); - -#ifdef __cplusplus -} -#endif diff --git a/idf/orig/spi_flash/include/esp_spi_flash.h b/idf/orig/spi_flash/include/esp_spi_flash.h deleted file mode 100644 index fc48fd2..0000000 --- a/idf/orig/spi_flash/include/esp_spi_flash.h +++ /dev/null @@ -1,379 +0,0 @@ -// 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. - -#ifndef ESP_SPI_FLASH_H -#define ESP_SPI_FLASH_H - -#include -#include -#include -#include "esp_err.h" -#include "sdkconfig.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define ESP_ERR_FLASH_BASE 0x10010 -#define ESP_ERR_FLASH_OP_FAIL (ESP_ERR_FLASH_BASE + 1) -#define ESP_ERR_FLASH_OP_TIMEOUT (ESP_ERR_FLASH_BASE + 2) - -#define SPI_FLASH_SEC_SIZE 4096 /**< SPI Flash sector size */ - -#define SPI_FLASH_MMU_PAGE_SIZE 0x10000 /**< Flash cache MMU mapping page size */ - -/** - * @brief Initialize SPI flash access driver - * - * This function must be called exactly once, before any other - * spi_flash_* functions are called. - * Currently this function is called from startup code. There is - * no need to call it from application code. - * - */ -void spi_flash_init(); - -/** - * @brief Get flash chip size, as set in binary image header - * - * @note This value does not necessarily match real flash size. - * - * @return size of flash chip, in bytes - */ -size_t spi_flash_get_chip_size(); - -/** - * @brief Erase the Flash sector. - * - * @param sector Sector number, the count starts at sector 0, 4KB per sector. - * - * @return esp_err_t - */ -esp_err_t spi_flash_erase_sector(size_t sector); - -/** - * @brief Erase a range of flash sectors - * - * @param start_address Address where erase operation has to start. - * Must be 4kB-aligned - * @param size Size of erased range, in bytes. Must be divisible by 4kB. - * - * @return esp_err_t - */ -esp_err_t spi_flash_erase_range(size_t start_address, size_t size); - - -/** - * @brief Write data to Flash. - * - * @note If source address is in DROM, this function will return - * ESP_ERR_INVALID_ARG. - * - * @param dest_addr destination address in Flash. Must be a multiple of 4 bytes. - * @param src pointer to the source buffer. - * @param size length of data, in bytes. Must be a multiple of 4 bytes. - * - * @return esp_err_t - */ -esp_err_t spi_flash_write(size_t dest_addr, const void *src, size_t size); - - -/** - * @brief Write data encrypted to Flash. - * - * @note Flash encryption must be enabled for this function to work. - * - * @note Flash encryption must be enabled when calling this function. - * If flash encryption is disabled, the function returns - * ESP_ERR_INVALID_STATE. Use esp_flash_encryption_enabled() - * function to determine if flash encryption is enabled. - * - * @note Both dest_addr and size must be multiples of 16 bytes. For - * absolute best performance, both dest_addr and size arguments should - * be multiples of 32 bytes. - * - * @param dest_addr destination address in Flash. Must be a multiple of 16 bytes. - * @param src pointer to the source buffer. - * @param size length of data, in bytes. Must be a multiple of 16 bytes. - * - * @return esp_err_t - */ -esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size); - -/** - * @brief Read data from Flash. - * - * @param src_addr source address of the data in Flash. - * @param dest pointer to the destination buffer - * @param size length of data - * - * @return esp_err_t - */ -esp_err_t spi_flash_read(size_t src_addr, void *dest, size_t size); - - -/** - * @brief Read data from Encrypted Flash. - * - * If flash encryption is enabled, this function will transparently decrypt data as it is read. - * If flash encryption is not enabled, this function behaves the same as spi_flash_read(). - * - * See esp_flash_encryption_enabled() for a function to check if flash encryption is enabled. - * - * @param src source address of the data in Flash. - * @param dest pointer to the destination buffer - * @param size length of data - * - * @return esp_err_t - */ -esp_err_t spi_flash_read_encrypted(size_t src, void *dest, size_t size); - -/** - * @brief Enumeration which specifies memory space requested in an mmap call - */ -typedef enum { - SPI_FLASH_MMAP_DATA, /**< map to data memory (Vaddr0), allows byte-aligned access, 4 MB total */ - SPI_FLASH_MMAP_INST, /**< map to instruction memory (Vaddr1-3), allows only 4-byte-aligned access, 11 MB total */ -} spi_flash_mmap_memory_t; - -/** - * @brief Opaque handle for memory region obtained from spi_flash_mmap. - */ -typedef uint32_t spi_flash_mmap_handle_t; - -/** - * @brief Map region of flash memory into data or instruction address space - * - * This function allocates sufficient number of 64k MMU pages and configures - * them to map request region of flash memory into data address space or into - * instruction address space. It may reuse MMU pages which already provide - * required mapping. As with any allocator, there is possibility of fragmentation - * of address space if mmap/munmap are heavily used. To troubleshoot issues with - * page allocation, use spi_flash_mmap_dump function. - * - * @param src_addr Physical address in flash where requested region starts. - * This address *must* be aligned to 64kB boundary - * (SPI_FLASH_MMU_PAGE_SIZE). - * @param size Size of region which has to be mapped. This size will be rounded - * up to a 64k boundary. - * @param memory Memory space where the region should be mapped - * @param out_ptr Output, pointer to the mapped memory region - * @param out_handle Output, handle which should be used for spi_flash_munmap call - * - * @return ESP_OK on success, ESP_ERR_NO_MEM if pages can not be allocated - */ -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); - -/** - * @brief Map sequences of pages of flash memory into data or instruction address space - * - * This function allocates sufficient number of 64k MMU pages and configures - * them to map the indicated pages of flash memory contiguously into data address - * space or into instruction address space. In this respect, it works in a similar - * way as spi_flash_mmap but it allows mapping a (maybe non-contiguous) set of pages - * into a contiguous region of memory. - * - * @param pages An array of numbers indicating the 64K pages in flash to be mapped - * contiguously into memory. These indicate the indexes of the 64K pages, - * not the byte-size addresses as used in other functions. - * @param pagecount Size of the pages array - * @param memory Memory space where the region should be mapped - * @param out_ptr Output, pointer to the mapped memory region - * @param out_handle Output, handle which should be used for spi_flash_munmap call - * - * @return ESP_OK on success, ESP_ERR_NO_MEM if pages can not be allocated - */ -esp_err_t spi_flash_mmap_pages(int *pages, size_t pagecount, spi_flash_mmap_memory_t memory, - const void** out_ptr, spi_flash_mmap_handle_t* out_handle); - - -/** - * @brief Release region previously obtained using spi_flash_mmap - * - * @note Calling this function will not necessarily unmap memory region. - * Region will only be unmapped when there are no other handles which - * reference this region. In case of partially overlapping regions - * it is possible that memory will be unmapped partially. - * - * @param handle Handle obtained from spi_flash_mmap - */ -void spi_flash_munmap(spi_flash_mmap_handle_t handle); - -/** - * @brief Display information about mapped regions - * - * This function lists handles obtained using spi_flash_mmap, along with range - * of pages allocated to each handle. It also lists all non-zero entries of - * MMU table and corresponding reference counts. - */ -void spi_flash_mmap_dump(); - - -#define SPI_FLASH_CACHE2PHYS_FAIL UINT32_MAX /* -#include "esp_log.h" - -static const char *TAG = "wl_ext_perf"; - -#define WL_EXT_RESULT_CHECK(result) \ - if (result != ESP_OK) { \ - ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \ - return (result); \ - } - -WL_Ext_Perf::WL_Ext_Perf(): WL_Flash() -{ - this->sector_buffer = NULL; -} - -WL_Ext_Perf::~WL_Ext_Perf() -{ - free(this->sector_buffer); -} - -esp_err_t WL_Ext_Perf::config(WL_Config_s *cfg, Flash_Access *flash_drv) -{ - wl_ext_cfg_t *config = (wl_ext_cfg_t *)cfg; - - this->fat_sector_size = config->fat_sector_size; - this->flash_sector_size = cfg->sector_size; - - this->sector_buffer = (uint32_t *)malloc(cfg->sector_size); - if (this->sector_buffer == NULL) { - return ESP_ERR_NO_MEM; - } - - this->size_factor = this->flash_sector_size / this->fat_sector_size; - if (this->size_factor < 1) { - return ESP_ERR_INVALID_ARG; - } - - return WL_Flash::config(cfg, flash_drv); -} - -esp_err_t WL_Ext_Perf::init() -{ - return WL_Flash::init(); -} - -size_t WL_Ext_Perf::chip_size() -{ - return WL_Flash::chip_size(); -} -size_t WL_Ext_Perf::sector_size() -{ - return this->fat_sector_size; -} - -esp_err_t WL_Ext_Perf::erase_sector(size_t sector) -{ - return this->erase_sector_fit(sector, 1); -} - -esp_err_t WL_Ext_Perf::erase_sector_fit(uint32_t start_sector, uint32_t count) -{ - ESP_LOGV(TAG, "%s begin, start_sector = 0x%08x, count = %i", __func__, start_sector, count); - // This method works with one flash device sector and able to erase "count" of fatfs sectors from this sector - esp_err_t result = ESP_OK; - - uint32_t pre_check_start = start_sector % this->size_factor; - - - for (int i = 0; i < this->size_factor; i++) { - if ((i < pre_check_start) || (i >= count + pre_check_start)) { - result = this->read(start_sector / this->size_factor * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size); - WL_EXT_RESULT_CHECK(result); - } - } - - result = WL_Flash::erase_sector(start_sector / this->size_factor); // erase comlete flash sector - WL_EXT_RESULT_CHECK(result); - // And write back only data that should not be erased... - for (int i = 0; i < this->size_factor; i++) { - if ((i < pre_check_start) || (i >= count + pre_check_start)) { - result = this->write(start_sector / this->size_factor * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size); - WL_EXT_RESULT_CHECK(result); - } - } - return ESP_OK; -} - -esp_err_t WL_Ext_Perf::erase_range(size_t start_address, size_t size) -{ - esp_err_t result = ESP_OK; - if ((start_address % this->fat_sector_size) != 0) { - result = ESP_ERR_INVALID_ARG; - } - if (((size % this->fat_sector_size) != 0) || (size == 0)) { - result = ESP_ERR_INVALID_ARG; - } - WL_EXT_RESULT_CHECK(result); - - // The range to erase could be allocated in any possible way - // --------------------------------------------------------- - // | | | | | - // |0|0|x|x|x|x|x|x|x|x|x|x|x|x|0|0| - // | pre | rest | rest | post | <- check ranges - // - // Pre check - the data that is not fit to the full sector at the begining of the erased block - // Post check - the data that are not fit to the full sector at the end of the erased block - // rest - data that are fit to the flash device sector at the middle of the erased block - // - // In case of pre and post check situations the data of the non erased area have to be readed first and then - // stored back. - // For the rest area this operation not needed because complete flash device sector will be erased. - - ESP_LOGV(TAG, "%s begin, addr = 0x%08x, size = %i", __func__, start_address, size); - // Calculate pre check values - uint32_t pre_check_start = (start_address / this->fat_sector_size) % this->size_factor; - uint32_t sectors_count = size / this->fat_sector_size; - uint32_t pre_check_count = (this->size_factor - pre_check_start); - if (pre_check_count > sectors_count) { - pre_check_count = sectors_count; - } - - // Calculate post ckeck - uint32_t post_check_count = (sectors_count - pre_check_count) % this->size_factor; - uint32_t post_check_start = ((start_address + size - post_check_count * this->fat_sector_size) / this->fat_sector_size); - - // Calculate rest - uint32_t rest_check_count = sectors_count - pre_check_count - post_check_count; - if ((pre_check_count == this->size_factor) && (0 == pre_check_start)) { - rest_check_count+=this->size_factor; - pre_check_count = 0; - } - uint32_t rest_check_start = start_address + pre_check_count * this->fat_sector_size; - - // Here we will clear pre_check_count amount of sectors - if (pre_check_count != 0) { - result = this->erase_sector_fit(start_address / this->fat_sector_size, pre_check_count); - WL_EXT_RESULT_CHECK(result); - } - ESP_LOGV(TAG, "%s rest_check_start = %i, pre_check_count=%i, rest_check_count=%i, post_check_count=%i\n", __func__, rest_check_start, pre_check_count, rest_check_count, post_check_count); - if (rest_check_count > 0) { - rest_check_count = rest_check_count / this->size_factor; - size_t start_sector = rest_check_start / this->flash_sector_size; - for (size_t i = 0; i < rest_check_count; i++) { - result = WL_Flash::erase_sector(start_sector + i); - WL_EXT_RESULT_CHECK(result); - } - } - if (post_check_count != 0) { - result = this->erase_sector_fit(post_check_start, post_check_count); - WL_EXT_RESULT_CHECK(result); - } - return ESP_OK; -} diff --git a/idf/orig/wear_levelling/WL_Ext_Safe.cpp b/idf/orig/wear_levelling/WL_Ext_Safe.cpp deleted file mode 100644 index c266d6b..0000000 --- a/idf/orig/wear_levelling/WL_Ext_Safe.cpp +++ /dev/null @@ -1,161 +0,0 @@ -// 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 "WL_Ext_Safe.h" -#include -#include "esp_log.h" - -static const char *TAG = "wl_ext_safe"; - -#define WL_EXT_RESULT_CHECK(result) \ - if (result != ESP_OK) { \ - ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \ - return (result); \ - } - -#ifndef FLASH_ERASE_VALUE -#define FLASH_ERASE_VALUE 0xffffffff -#endif // FLASH_ERASE_VALUE - - -#ifndef WL_EXT_SAFE_OK -#define WL_EXT_SAFE_OK 0x12345678 -#endif // WL_EXT_SAFE_OK - -#ifndef WL_EXT_SAFE_OFFSET -#define WL_EXT_SAFE_OFFSET 16 -#endif // WL_EXT_SAFE_OFFSET - - -struct WL_Ext_Safe_State { -public: - uint32_t erase_begin; - uint32_t local_addr_base; - uint32_t local_addr_shift; - uint32_t count; -}; - -WL_Ext_Safe::WL_Ext_Safe(): WL_Ext_Perf() -{ -} - -WL_Ext_Safe::~WL_Ext_Safe() -{ -} - -esp_err_t WL_Ext_Safe::config(WL_Config_s *cfg, Flash_Access *flash_drv) -{ - esp_err_t result = ESP_OK; - - result = WL_Ext_Perf::config(cfg, flash_drv); - WL_EXT_RESULT_CHECK(result); - this->state_addr = WL_Flash::chip_size() - 2 * WL_Flash::sector_size(); - this->dump_addr = WL_Flash::chip_size() - 1 * WL_Flash::sector_size(); - return ESP_OK; -} - -esp_err_t WL_Ext_Safe::init() -{ - esp_err_t result = ESP_OK; - ESP_LOGV(TAG, "%s", __func__); - - result = WL_Ext_Perf::init(); - WL_EXT_RESULT_CHECK(result); - - result = this->recover(); - return result; -} - -size_t WL_Ext_Safe::chip_size() -{ - ESP_LOGV(TAG, "%s size = %i", __func__, WL_Flash::chip_size() - 2 * this->flash_sector_size); - return WL_Flash::chip_size() - 2 * this->flash_sector_size; -} - -esp_err_t WL_Ext_Safe::recover() -{ - esp_err_t result = ESP_OK; - - WL_Ext_Safe_State state; - result = WL_Flash::read(this->state_addr, &state, sizeof(WL_Ext_Safe_State)); - WL_EXT_RESULT_CHECK(result); - ESP_LOGV(TAG, "%s recover, start_addr = 0x%08x, local_addr_base = 0x%08x, local_addr_shift = %i, count=%i", __func__, state.erase_begin, state.local_addr_base, state.local_addr_shift, state.count); - - // check if we have transaction - if (state.erase_begin == WL_EXT_SAFE_OK) { - - result = this->read(this->dump_addr, this->sector_buffer, this->flash_sector_size); - WL_EXT_RESULT_CHECK(result); - - result = WL_Flash::erase_sector(state.local_addr_base); // erase comlete flash sector - WL_EXT_RESULT_CHECK(result); - - // And write back... - for (int i = 0; i < this->size_factor; i++) { - if ((i < state.local_addr_shift) || (i >= state.count + state.local_addr_shift)) { - result = this->write(state.local_addr_base * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size); - WL_EXT_RESULT_CHECK(result); - } - } - // clear transaction - result = WL_Flash::erase_range(this->state_addr, this->flash_sector_size); - } - return result; -} - -esp_err_t WL_Ext_Safe::erase_sector_fit(uint32_t start_sector, uint32_t count) -{ - esp_err_t result = ESP_OK; - - uint32_t local_addr_base = start_sector / this->size_factor; - uint32_t pre_check_start = start_sector % this->size_factor; - ESP_LOGV(TAG, "%s start_sector=0x%08x, count = %i", __func__, start_sector, count); - for (int i = 0; i < this->size_factor; i++) { - if ((i < pre_check_start) || (i >= count + pre_check_start)) { - result = this->read(start_sector / this->size_factor * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size); - WL_EXT_RESULT_CHECK(result); - } - } - - result = WL_Flash::erase_sector(this->dump_addr / this->flash_sector_size); - WL_EXT_RESULT_CHECK(result); - result = WL_Flash::write(this->dump_addr, this->sector_buffer, this->flash_sector_size); - WL_EXT_RESULT_CHECK(result); - - WL_Ext_Safe_State state; - state.erase_begin = WL_EXT_SAFE_OK; - state.local_addr_base = local_addr_base; - state.local_addr_shift = pre_check_start; - state.count = count; - - result = WL_Flash::erase_sector(this->state_addr / this->flash_sector_size); - WL_EXT_RESULT_CHECK(result); - result = WL_Flash::write(this->state_addr + 0, &state, sizeof(WL_Ext_Safe_State)); - WL_EXT_RESULT_CHECK(result); - - // Erase - result = WL_Flash::erase_sector(local_addr_base); // erase comlete flash sector - WL_EXT_RESULT_CHECK(result); - // And write back... - for (int i = 0; i < this->size_factor; i++) { - if ((i < pre_check_start) || (i >= count + pre_check_start)) { - result = this->write(local_addr_base * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size); - WL_EXT_RESULT_CHECK(result); - } - } - - result = WL_Flash::erase_sector(this->state_addr / this->flash_sector_size); - WL_EXT_RESULT_CHECK(result); - - return ESP_OK; -} diff --git a/idf/orig/wear_levelling/WL_Flash.cpp b/idf/orig/wear_levelling/WL_Flash.cpp deleted file mode 100644 index 6c3beee..0000000 --- a/idf/orig/wear_levelling/WL_Flash.cpp +++ /dev/null @@ -1,478 +0,0 @@ -// 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 -#include "esp_log.h" -#include "WL_Flash.h" -#include -#include "crc32.h" -#include - -static const char *TAG = "wl_flash"; -#ifndef WL_CFG_CRC_CONST -#define WL_CFG_CRC_CONST UINT32_MAX -#endif // WL_CFG_CRC_CONST - -#define WL_RESULT_CHECK(result) \ - if (result != ESP_OK) { \ - ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \ - return (result); \ - } - -#ifndef _MSC_VER // MSVS has different format for this define -static_assert(sizeof(wl_state_t) % 32 == 0, "wl_state_t structure size must be multiple of flash encryption unit size"); -#endif // _MSC_VER - - -WL_Flash::WL_Flash() -{ -} - -WL_Flash::~WL_Flash() -{ - free(this->temp_buff); -} - -esp_err_t WL_Flash::config(wl_config_t *cfg, Flash_Access *flash_drv) -{ - ESP_LOGV(TAG, "%s start_addr=0x%08x, full_mem_size=0x%08x, page_size=0x%08x, sector_size=0x%08x, updaterate=0x%08x, wr_size=0x%08x, version=0x%08x, temp_buff_size=0x%08x", __func__, - (uint32_t) cfg->start_addr, - cfg->full_mem_size, - cfg->page_size, - cfg->sector_size, - cfg->updaterate, - cfg->wr_size, - cfg->version, - (uint32_t) cfg->temp_buff_size); - - cfg->crc = crc32::crc32_le(WL_CFG_CRC_CONST, (const unsigned char *)cfg, sizeof(wl_config_t) - sizeof(cfg->crc)); - esp_err_t result = ESP_OK; - memcpy(&this->cfg, cfg, sizeof(wl_config_t)); - this->configured = false; - if (cfg == NULL) { - result = ESP_ERR_INVALID_ARG; - } - this->flash_drv = flash_drv; - if (flash_drv == NULL) { - result = ESP_ERR_INVALID_ARG; - } - if ((this->cfg.sector_size % this->cfg.temp_buff_size) != 0) { - result = ESP_ERR_INVALID_ARG; - } - if (this->cfg.page_size < this->cfg.sector_size) { - result = ESP_ERR_INVALID_ARG; - } - WL_RESULT_CHECK(result); - - this->temp_buff = (uint8_t *)malloc(this->cfg.temp_buff_size); - this->state_size = this->cfg.sector_size; - if (this->state_size < (sizeof(wl_state_t) + (this->cfg.full_mem_size / this->cfg.sector_size)*this->cfg.wr_size)) { - this->state_size = ((sizeof(wl_state_t) + (this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size) + this->cfg.sector_size - 1) / this->cfg.sector_size; - this->state_size = this->state_size * this->cfg.sector_size; - } - this->cfg_size = (sizeof(wl_config_t) + this->cfg.sector_size - 1) / this->cfg.sector_size; - this->cfg_size = cfg_size * this->cfg.sector_size; - - this->addr_cfg = this->cfg.start_addr + this->cfg.full_mem_size - this->cfg_size; - this->addr_state1 = this->cfg.start_addr + this->cfg.full_mem_size - this->state_size * 2 - this->cfg_size; // allocate data at the end of memory - this->addr_state2 = this->cfg.start_addr + this->cfg.full_mem_size - this->state_size * 1 - this->cfg_size; // allocate data at the end of memory - - this->flash_size = ((this->cfg.full_mem_size - this->state_size * 2 - this->cfg_size) / this->cfg.page_size - 1) * this->cfg.page_size; // -1 remove dummy block - - ESP_LOGV(TAG, "%s - this->addr_state1=0x%08x", __func__, (uint32_t) this->addr_state1); - ESP_LOGV(TAG, "%s - this->addr_state2=0x%08x", __func__, (uint32_t) this->addr_state2); - - this->configured = true; - return ESP_OK; -} - -esp_err_t WL_Flash::init() -{ - esp_err_t result = ESP_OK; - if (this->configured == false) { - ESP_LOGW(TAG, "WL_Flash: not configured, call config() first"); - return ESP_ERR_INVALID_STATE; - } - // If flow will be interrupted by error, then this flag will be false - this->initialized = false; - // Init states if it is first time... - this->flash_drv->read(this->addr_state1, &this->state, sizeof(wl_state_t)); - wl_state_t sa_copy; - wl_state_t *state_copy = &sa_copy; - result = this->flash_drv->read(this->addr_state2, state_copy, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - - int check_size = sizeof(wl_state_t) - sizeof(uint32_t); - // Chech CRC and recover state - uint32_t crc1 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, check_size); - uint32_t crc2 = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)state_copy, check_size); - - ESP_LOGD(TAG, "%s - config ID=%i, stored ID=%i, access_count=%i, block_size=%i, max_count=%i, pos=%i, move_count=%i", - __func__, - this->cfg.version, - this->state.version, - this->state.access_count, - this->state.block_size, - this->state.max_count, - this->state.pos, - this->state.move_count); - - - ESP_LOGD(TAG, "%s starts: crc1=%i, crc2 = %i, this->state.crc=%i, state_copy->crc=%i", __func__, crc1, crc2, this->state.crc, state_copy->crc); - if ((crc1 == this->state.crc) && (crc2 == state_copy->crc)) { - // The state is OK. Check the ID - if (this->state.version != this->cfg.version) { - result = this->initSections(); - WL_RESULT_CHECK(result); - result = this->recoverPos(); - WL_RESULT_CHECK(result); - } else { - if (crc1 != crc2) {// we did not update second structure. - result = this->flash_drv->erase_range(this->addr_state2, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size)*this->cfg.wr_size); i++) { - uint8_t pos_bits = 0; - result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i, &pos_bits, 1); - WL_RESULT_CHECK(result); - if (pos_bits != 0xff) { - result = this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + i, &pos_bits, 1); - WL_RESULT_CHECK(result); - } - } - } - ESP_LOGD(TAG, "%s: crc1=%i, crc2 = %i, result=%i", __func__, crc1, crc2, result); - result = this->recoverPos(); - WL_RESULT_CHECK(result); - } - } else if ((crc1 != this->state.crc) && (crc2 != state_copy->crc)) { // This is just new flash - result = this->initSections(); - WL_RESULT_CHECK(result); - result = this->recoverPos(); - WL_RESULT_CHECK(result); - } else { - // recover broken state - if (crc1 == this->state.crc) {// we have to recover state 2 - result = this->flash_drv->erase_range(this->addr_state2, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size); i++) { - uint8_t pos_bits = 0; - result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i, &pos_bits, 1); - WL_RESULT_CHECK(result); - if (pos_bits != 0xff) { - result = this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + i, &pos_bits, 1); - WL_RESULT_CHECK(result); - } - } - result = this->flash_drv->read(this->addr_state2, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - } else { // we have to recover state 1 - result = this->flash_drv->erase_range(this->addr_state1, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state1, state_copy, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - for (size_t i = 0; i < ((this->cfg.full_mem_size / this->cfg.sector_size) * this->cfg.wr_size); i++) { - uint8_t pos_bits = 0; - result = this->flash_drv->read(this->addr_state2 + sizeof(wl_state_t) + i, &pos_bits, 1); - WL_RESULT_CHECK(result); - if (pos_bits != 0xff) { - result = this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + i, &pos_bits, 1); - WL_RESULT_CHECK(result); - } - } - result = this->flash_drv->read(this->addr_state1, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - this->state.pos = this->state.max_pos - 1; - } - // done. We have recovered the state - // If we have a new configuration, we will overwrite it - if (this->state.version != this->cfg.version) { - result = this->initSections(); - WL_RESULT_CHECK(result); - } - } - if (result != ESP_OK) { - this->initialized = false; - ESP_LOGE(TAG, "%s: returned 0x%x", __func__, result); - return result; - } - this->initialized = true; - return ESP_OK; -} - -esp_err_t WL_Flash::recoverPos() -{ - esp_err_t result = ESP_OK; - size_t position = 0; - for (size_t i = 0; i < this->state.max_pos; i++) { - uint8_t pos_bits = 0; - result = this->flash_drv->read(this->addr_state1 + sizeof(wl_state_t) + i * this->cfg.wr_size, &pos_bits, 1); - WL_RESULT_CHECK(result); - position = i; - if (pos_bits == 0xff) { - break; // we have found position - } - } - this->state.pos = position; - if (this->state.pos == this->state.max_pos) { - this->state.pos--; - } - ESP_LOGD(TAG, "%s - this->state.pos=0x%08x, result=%08x", __func__, this->state.pos, result); - return result; -} - -esp_err_t WL_Flash::initSections() -{ - esp_err_t result = ESP_OK; - this->state.pos = 0; - this->state.access_count = 0; - this->state.move_count = 0; - // max count - this->state.max_count = this->flash_size / this->state_size * this->cfg.updaterate; - if (this->cfg.updaterate != 0) { - this->state.max_count = this->cfg.updaterate; - } - this->state.version = this->cfg.version; - this->state.block_size = this->cfg.page_size; - this->used_bits = 0; - - this->state.max_pos = 1 + this->flash_size / this->cfg.page_size; - - this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, sizeof(wl_state_t) - sizeof(uint32_t)); - - result = this->flash_drv->erase_range(this->addr_state1, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state1, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - // write state copy - result = this->flash_drv->erase_range(this->addr_state2, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - - result = this->flash_drv->erase_range(this->addr_cfg, this->cfg_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_cfg, &this->cfg, sizeof(wl_config_t)); - WL_RESULT_CHECK(result); - - ESP_LOGD(TAG, "%s - this->state->max_count=%08x, this->state->max_pos=%08x", __func__, this->state.max_count, this->state.max_pos); - ESP_LOGD(TAG, "%s - result=%08x", __func__, result); - return result; -} - -esp_err_t WL_Flash::updateWL() -{ - esp_err_t result = ESP_OK; - this->state.access_count++; - if (this->state.access_count < this->state.max_count) { - return result; - } - // Here we have to move the block and increase the state - this->state.access_count = 0; - ESP_LOGV(TAG, "%s - access_count=0x%08x, pos=0x%08x", __func__, this->state.access_count, this->state.pos); - // copy data to dummy block - size_t data_addr = this->state.pos + 1; // next block, [pos+1] copy to [pos] - if (data_addr >= this->state.max_pos) { - data_addr = 0; - } - data_addr = this->cfg.start_addr + data_addr * this->cfg.page_size; - this->dummy_addr = this->cfg.start_addr + this->state.pos * this->cfg.page_size; - result = this->flash_drv->erase_range(this->dummy_addr, this->cfg.page_size); - if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - erase wl dummy sector result=%08x", __func__, result); - this->state.access_count = this->state.max_count - 1; // we will update next time - return result; - } - - size_t copy_count = this->cfg.page_size / this->cfg.temp_buff_size; - for (size_t i = 0; i < copy_count; i++) { - result = this->flash_drv->read(data_addr + i * this->cfg.temp_buff_size, this->temp_buff, this->cfg.temp_buff_size); - if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - not possible to read buffer, will try next time, result=%08x", __func__, result); - this->state.access_count = this->state.max_count - 1; // we will update next time - return result; - } - result = this->flash_drv->write(this->dummy_addr + i * this->cfg.temp_buff_size, this->temp_buff, this->cfg.temp_buff_size); - if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - not possible to write buffer, will try next time, result=%08x", __func__, result); - this->state.access_count = this->state.max_count - 1; // we will update next time - return result; - } - } - // done... block moved. - // Here we will update structures... - // Update bits and save to flash: - uint32_t byte_pos = this->state.pos * this->cfg.wr_size; - this->used_bits = 0; - // write state to mem. We updating only affected bits - result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1); - if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - update position 1 result=%08x", __func__, result); - this->state.access_count = this->state.max_count - 1; // we will update next time - return result; - } - result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1); - if (result != ESP_OK) { - ESP_LOGE(TAG, "%s - update position 2 result=%08x", __func__, result); - this->state.access_count = this->state.max_count - 1; // we will update next time - return result; - } - - this->state.pos++; - if (this->state.pos >= this->state.max_pos) { - this->state.pos = 0; - // one loop more - this->state.move_count++; - if (this->state.move_count >= (this->state.max_pos - 1)) { - this->state.move_count = 0; - } - // write main state - this->state.crc = crc32::crc32_le(WL_CFG_CRC_CONST, (uint8_t *)&this->state, sizeof(wl_state_t) - sizeof(uint32_t)); - - result = this->flash_drv->erase_range(this->addr_state1, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state1, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - result = this->flash_drv->erase_range(this->addr_state2, this->state_size); - WL_RESULT_CHECK(result); - result = this->flash_drv->write(this->addr_state2, &this->state, sizeof(wl_state_t)); - WL_RESULT_CHECK(result); - ESP_LOGD(TAG, "%s - move_count=%08x", __func__, this->state.move_count); - } - // Save structures to the flash... and check result - if (result == ESP_OK) { - ESP_LOGV(TAG, "%s - result=%08x", __func__, result); - } else { - ESP_LOGE(TAG, "%s - result=%08x", __func__, result); - } - return result; -} - -size_t WL_Flash::calcAddr(size_t addr) -{ - size_t result = (this->flash_size - this->state.move_count * this->cfg.page_size + addr) % this->flash_size; - size_t dummy_addr = this->state.pos * this->cfg.page_size; - if (result < dummy_addr) { - } else { - result += this->cfg.page_size; - } - ESP_LOGV(TAG, "%s - addr=0x%08x -> result=0x%08x", __func__, (uint32_t) addr, (uint32_t) result); - return result; -} - - -size_t WL_Flash::chip_size() -{ - if (!this->configured) { - return 0; - } - return this->flash_size; -} -size_t WL_Flash::sector_size() -{ - if (!this->configured) { - return 0; - } - return this->cfg.sector_size; -} - - -esp_err_t WL_Flash::erase_sector(size_t sector) -{ - esp_err_t result = ESP_OK; - if (!this->initialized) { - return ESP_ERR_INVALID_STATE; - } - ESP_LOGV(TAG, "%s - sector=0x%08x", __func__, (uint32_t) sector); - result = this->updateWL(); - WL_RESULT_CHECK(result); - size_t virt_addr = this->calcAddr(sector * this->cfg.sector_size); - result = this->flash_drv->erase_sector((this->cfg.start_addr + virt_addr) / this->cfg.sector_size); - WL_RESULT_CHECK(result); - return result; -} -esp_err_t WL_Flash::erase_range(size_t start_address, size_t size) -{ - esp_err_t result = ESP_OK; - if (!this->initialized) { - return ESP_ERR_INVALID_STATE; - } - ESP_LOGV(TAG, "%s - start_address=0x%08x, size=0x%08x", __func__, (uint32_t) start_address, (uint32_t) size); - size_t erase_count = (size + this->cfg.sector_size - 1) / this->cfg.sector_size; - size_t start_sector = start_address / this->cfg.sector_size; - for (size_t i = 0; i < erase_count; i++) { - result = this->erase_sector(start_sector + i); - WL_RESULT_CHECK(result); - } - ESP_LOGV(TAG, "%s - result=%08x", __func__, result); - return result; -} - -esp_err_t WL_Flash::write(size_t dest_addr, const void *src, size_t size) -{ - esp_err_t result = ESP_OK; - if (!this->initialized) { - return ESP_ERR_INVALID_STATE; - } - ESP_LOGV(TAG, "%s - dest_addr=0x%08x, size=0x%08x", __func__, (uint32_t) dest_addr, (uint32_t) size); - uint32_t count = (size - 1) / this->cfg.page_size; - for (size_t i = 0; i < count; i++) { - size_t virt_addr = this->calcAddr(dest_addr + i * this->cfg.page_size); - result = this->flash_drv->write(this->cfg.start_addr + virt_addr, &((uint8_t *)src)[i * this->cfg.page_size], this->cfg.page_size); - WL_RESULT_CHECK(result); - } - size_t virt_addr_last = this->calcAddr(dest_addr + count * this->cfg.page_size); - result = this->flash_drv->write(this->cfg.start_addr + virt_addr_last, &((uint8_t *)src)[count * this->cfg.page_size], size - count * this->cfg.page_size); - WL_RESULT_CHECK(result); - return result; -} - -esp_err_t WL_Flash::read(size_t src_addr, void *dest, size_t size) -{ - esp_err_t result = ESP_OK; - if (!this->initialized) { - return ESP_ERR_INVALID_STATE; - } - ESP_LOGV(TAG, "%s - src_addr=0x%08x, size=0x%08x", __func__, (uint32_t) src_addr, (uint32_t) size); - uint32_t count = (size - 1) / this->cfg.page_size; - for (size_t i = 0; i < count; i++) { - size_t virt_addr = this->calcAddr(src_addr + i * this->cfg.page_size); - result = this->flash_drv->read(this->cfg.start_addr + virt_addr, &((uint8_t *)dest)[i * this->cfg.page_size], this->cfg.page_size); - WL_RESULT_CHECK(result); - } - size_t virt_addr_last = this->calcAddr(src_addr + count * this->cfg.page_size); - result = this->flash_drv->read(this->cfg.start_addr + virt_addr_last, &((uint8_t *)dest)[count * this->cfg.page_size], size - count * this->cfg.page_size); - WL_RESULT_CHECK(result); - return result; -} - -Flash_Access *WL_Flash::get_drv() -{ - return this->flash_drv; -} -wl_config_t *WL_Flash::get_cfg() -{ - return &this->cfg; -} - -esp_err_t WL_Flash::flush() -{ - esp_err_t result = ESP_OK; - this->state.access_count = this->state.max_count - 1; - result = this->updateWL(); - ESP_LOGV(TAG, "%s - result=%08x", __func__, result); - return result; -} diff --git a/idf/orig/wear_levelling/crc32.cpp b/idf/orig/wear_levelling/crc32.cpp deleted file mode 100644 index 52147f4..0000000 --- a/idf/orig/wear_levelling/crc32.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// 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 "crc32.h" -#include "rom/crc.h" - -unsigned int crc32::crc32_le(unsigned int crc, unsigned char const *buf, unsigned int len) -{ - return ::crc32_le(crc, buf, len); -} diff --git a/idf/orig/wear_levelling/crc32.h b/idf/orig/wear_levelling/crc32.h deleted file mode 100644 index 157fbb8..0000000 --- a/idf/orig/wear_levelling/crc32.h +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -#ifndef _crc32_H_ -#define _crc32_H_ - -/** -* @brief This class is used to access crc32 module -* -*/ -class crc32 -{ -public: - static unsigned int crc32_le(unsigned int crc, unsigned char const *buf, unsigned int len); -}; -#endif // _crc32_H_ \ No newline at end of file diff --git a/idf/orig/wear_levelling/private_include/SPI_Flash.h b/idf/orig/wear_levelling/private_include/SPI_Flash.h deleted file mode 100644 index 4bf0b95..0000000 --- a/idf/orig/wear_levelling/private_include/SPI_Flash.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _SPI_Flash_H_ -#define _SPI_Flash_H_ - -#include "esp_err.h" -#include "Flash_Access.h" - -/** -* @brief This class is used to access SPI flash devices. Class implements Flash_Access interface -* -*/ -class SPI_Flash : public Flash_Access -{ - -public: - SPI_Flash(); - - size_t chip_size() override; - esp_err_t erase_sector(size_t sector) override; - esp_err_t erase_range(size_t start_address, size_t size) override; - esp_err_t write(size_t dest_addr, const void *src, size_t size) override; - esp_err_t read(size_t src_addr, void *dest, size_t size) override; - size_t sector_size() override; - ~SPI_Flash() override; -}; - -#endif // _SPI_Flash_H_ \ No newline at end of file diff --git a/idf/orig/wear_levelling/private_include/WL_Config.h b/idf/orig/wear_levelling/private_include/WL_Config.h deleted file mode 100644 index 97984c3..0000000 --- a/idf/orig/wear_levelling/private_include/WL_Config.h +++ /dev/null @@ -1,36 +0,0 @@ -// 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. - -#ifndef _WL_Config_H_ -#define _WL_Config_H_ - -#include "Flash_Access.h" - -/** -* @brief This class is used as a structure to configure wear levelling module -* -*/ -typedef struct WL_Config_s { - size_t start_addr; /*!< start address in the flash*/ - uint32_t full_mem_size; /*!< Amount of memory used to store data in bytes*/ - uint32_t page_size; /*!< One page size in bytes. Page could be more then memory block. This parameter must be page_size >= N*block_size.*/ - uint32_t sector_size; /*!< size of flash memory sector that will be erased and stored at once (erase)*/ - uint32_t updaterate; /*!< Amount of accesses before block will be moved*/ - uint32_t wr_size; /*!< Minimum amount of bytes per one block at write operation: 1...*/ - uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/ - size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/ - uint32_t crc; /*!< CRC for this config*/ - -} wl_config_t; - -#endif // _WL_Config_H_ \ No newline at end of file diff --git a/idf/orig/wear_levelling/private_include/WL_Ext_Cfg.h b/idf/orig/wear_levelling/private_include/WL_Ext_Cfg.h deleted file mode 100644 index 363f677..0000000 --- a/idf/orig/wear_levelling/private_include/WL_Ext_Cfg.h +++ /dev/null @@ -1,22 +0,0 @@ -// 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. - -#ifndef _WL_Ext_Cfg_H_ -#define _WL_Ext_Cfg_H_ -#include "WL_Config.h" - -typedef struct WL_Ext_Cfg_s : public WL_Config_s { - uint32_t fat_sector_size; /*!< virtual sector size*/ -} wl_ext_cfg_t; - -#endif // _WL_Ext_Cfg_H_ \ No newline at end of file diff --git a/idf/orig/wear_levelling/private_include/WL_Ext_Perf.h b/idf/orig/wear_levelling/private_include/WL_Ext_Perf.h deleted file mode 100644 index 580bf2d..0000000 --- a/idf/orig/wear_levelling/private_include/WL_Ext_Perf.h +++ /dev/null @@ -1,46 +0,0 @@ -// 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. - -#ifndef _WL_Ext_Perf_H_ -#define _WL_Ext_Perf_H_ - -#include "WL_Flash.h" -#include "WL_Ext_Cfg.h" - -class WL_Ext_Perf : public WL_Flash -{ -public: - WL_Ext_Perf(); - ~WL_Ext_Perf() override; - - esp_err_t config(WL_Config_s *cfg, Flash_Access *flash_drv) override; - esp_err_t init() override; - - size_t chip_size() override; - size_t sector_size() override; - - - esp_err_t erase_sector(size_t sector) override; - esp_err_t erase_range(size_t start_address, size_t size) override; - -protected: - uint32_t flash_sector_size; - uint32_t fat_sector_size; - uint32_t size_factor; - uint32_t *sector_buffer; - - virtual esp_err_t erase_sector_fit(uint32_t start_sector, uint32_t count); - -}; - -#endif // _WL_Ext_Perf_H_ \ No newline at end of file diff --git a/idf/orig/wear_levelling/private_include/WL_Ext_Safe.h b/idf/orig/wear_levelling/private_include/WL_Ext_Safe.h deleted file mode 100644 index 9a6f148..0000000 --- a/idf/orig/wear_levelling/private_include/WL_Ext_Safe.h +++ /dev/null @@ -1,42 +0,0 @@ -// 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. - -#ifndef _WL_Ext_Safe_H_ -#define _WL_Ext_Safe_H_ - -#include "WL_Flash.h" -#include "WL_Ext_Cfg.h" -#include "WL_Ext_Perf.h" - -class WL_Ext_Safe : public WL_Ext_Perf -{ -public: - WL_Ext_Safe(); - ~WL_Ext_Safe() override; - - esp_err_t config(WL_Config_s *cfg, Flash_Access *flash_drv) override; - esp_err_t init() override; - - size_t chip_size() override; - -protected: - esp_err_t erase_sector_fit(uint32_t start_sector, uint32_t count) override; - - // Dump Sector - uint32_t dump_addr; // dump buffer address - uint32_t state_addr;// sectore where state of transaction will be stored - - esp_err_t recover(); -}; - -#endif // _WL_Ext_Safe_H_ \ No newline at end of file diff --git a/idf/orig/wear_levelling/private_include/WL_Flash.h b/idf/orig/wear_levelling/private_include/WL_Flash.h deleted file mode 100644 index 182320c..0000000 --- a/idf/orig/wear_levelling/private_include/WL_Flash.h +++ /dev/null @@ -1,76 +0,0 @@ -// 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. - -#ifndef _WL_Flash_H_ -#define _WL_Flash_H_ - -#include "esp_err.h" -#include "Flash_Access.h" -#include "WL_Config.h" -#include "WL_State.h" - -/** -* @brief This class is used to make wear levelling for flash devices. Class implements Flash_Access interface -* -*/ -class WL_Flash : public Flash_Access -{ -public : - WL_Flash(); - ~WL_Flash() override; - - virtual esp_err_t config(wl_config_t *cfg, Flash_Access *flash_drv); - virtual esp_err_t init(); - - size_t chip_size() override; - size_t sector_size() override; - - - esp_err_t erase_sector(size_t sector) override; - esp_err_t erase_range(size_t start_address, size_t size) override; - - esp_err_t write(size_t dest_addr, const void *src, size_t size) override; - esp_err_t read(size_t src_addr, void *dest, size_t size) override; - - esp_err_t flush() override; - - Flash_Access *get_drv(); - wl_config_t *get_cfg(); - -protected: - bool configured = false; - bool initialized = false; - wl_state_t state; - wl_config_t cfg; - Flash_Access *flash_drv = NULL; - - size_t addr_cfg; - size_t addr_state1; - size_t addr_state2; - size_t index_state1; - size_t index_state2; - - size_t flash_size; - uint32_t state_size; - uint32_t cfg_size; - uint8_t *temp_buff = NULL; - size_t dummy_addr; - uint8_t used_bits; - - esp_err_t initSections(); - esp_err_t updateWL(); - esp_err_t recoverPos(); - size_t calcAddr(size_t addr); -}; - -#endif // _WL_Flash_H_ diff --git a/idf/orig/wear_levelling/private_include/WL_State.h b/idf/orig/wear_levelling/private_include/WL_State.h deleted file mode 100644 index 39a76ba..0000000 --- a/idf/orig/wear_levelling/private_include/WL_State.h +++ /dev/null @@ -1,34 +0,0 @@ -// 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. - -#ifndef _WL_State_H_ -#define _WL_State_H_ -#include "esp_err.h" - -/** -* @brief This structure is used to store current state of flash access -* -*/ -typedef struct WL_State_s { -public: - uint32_t pos; /*!< current dummy block position*/ - uint32_t max_pos; /*!< maximum amount of positions*/ - uint32_t move_count; /*!< total amount of move counts. Used to calculate the address*/ - uint32_t access_count; /*!< current access count*/ - uint32_t max_count; /*!< max access count when block will be moved*/ - uint32_t block_size; /*!< size of move block*/ - uint32_t version; /*!< state id used to identify the version of current libary implementaion*/ - uint32_t crc; /*!< CRC of structure*/ -} wl_state_t; - -#endif // _WL_State_H_ diff --git a/main.cpp b/main.cpp index 72f7833..744e98b 100644 --- a/main.cpp +++ b/main.cpp @@ -32,13 +32,12 @@ //#endif #include -#include "wear_levelling.h" #include "esp_err.h" #include "esp_vfs_fat.h" //#include "esp_vfs.h" //do not include, dirent.h conflict -#include "fatfs/fatfs.h" -#include "fatfs/FatPartition.h" +#include "user_vfs.h" +#include "FatPartition.h" #include "esp_log.h" static const char *BASE_PATH = "/spiflash"; @@ -53,7 +52,7 @@ static std::string s_dirName; static std::string s_imageName; static int s_imageSize; -static wl_handle_t s_wl_handle; +static RAM_handle_t s_ram_handle; static FATFS* s_fs = NULL; //---------------------------- @@ -138,79 +137,6 @@ int addFile(char* path_src, const char* path_des) { return 0; } -/* -//------------------------------------------------------- -int addFiles(const char* dirname, const char* subPath) { - DIR *dir; - struct dirent *ent; - bool error = false; - std::string dirPath = dirname; - dirPath += subPath; //需打包的文件夹, eg: "abc/" - - // Open directory - if ((dir = opendir (dirPath.c_str())) != NULL) { - - // Read files from directory. - while ((ent = readdir (dir)) != NULL) { //处理目录中的每一项 - // Ignore dir itself. - if (ent->d_name[0] == '.') - continue; - - std::string fullpath = dirPath; - fullpath += ent->d_name; - struct stat path_stat; - stat (fullpath.c_str(), &path_stat); //通过文件名filename获取文件信息 - - if (!S_ISREG(path_stat.st_mode)) { //非常规文件 - // Check if path is a directory. - if (S_ISDIR(path_stat.st_mode)) { - // Prepare new sub path. - std::string newSubPath = subPath; - newSubPath += ent->d_name; - - addDir(newSubPath.c_str()); - - newSubPath += "/"; - - if (addFiles(dirname, newSubPath.c_str()) != 0) - { - std::cerr << "Error for adding content from " << ent->d_name << "!" << std::endl; - } - - continue; - } - else { - std::cerr << "skipping " << ent->d_name << std::endl; - continue; - } - } - - // Filepath with dirname as root folder. - std::string filepath = subPath; - filepath += ent->d_name; - std::cout << "adding to image: " << filepath << std::endl; - - // Add File to image. - if (addFile((char*)filepath.c_str(), fullpath.c_str()) != 0) { - std::cerr << "error adding file!" << std::endl; - error = true; - if (g_debugLevel > 0) { - std::cout << std::endl; - } - break; - } - } // end while - closedir (dir); - } - else { - std::cerr << "warning: can't read source directory: \"" << dirPath << "\"" << std::endl; - return 1; - } - - return (error) ? 1 : 0; -} -*/ - /* parkToRamFS() 遍历源文件夹,拷贝到RAM fat file system params: @@ -468,7 +394,7 @@ bool fatfsMount() { esp_vfs_fat_mount_config_t mountConfig; mountConfig.max_files = 4; //最大打开文件数量 mountConfig.format_if_mount_failed = true; - result = (ESP_OK == emulate_esp_vfs_fat_spiflash_mount(BASE_PATH, &mountConfig, &s_wl_handle, &s_fs, s_imageSize)); + result = (ESP_OK == emulate_esp_vfs_fat_spiflash_mount(BASE_PATH, &mountConfig, &s_ram_handle, &s_fs, s_imageSize)); return result; } @@ -478,7 +404,7 @@ bool fatfsMount() { bool fatfsUnmount() { bool result; - result = (ESP_OK == emulate_esp_vfs_fat_spiflash_unmount(BASE_PATH, s_wl_handle)); + result = (ESP_OK == emulate_esp_vfs_fat_spiflash_unmount(BASE_PATH, s_ram_handle)); if (result) { if (g_debugLevel > 0) { @@ -516,6 +442,7 @@ int actionPack() { ret = parkToRamFS(s_dirName.c_str(), BASE_PATH); fatfsUnmount(); +std::cout << "g_flashmem[0]: " << g_flashmem[0] << "size: " << g_flashmem.size() << std::endl; fwrite(&g_flashmem[0], 4, g_flashmem.size()/4, fdres); //把g_flashmem中的数据(RAM fat file system)写入目标打包文件,4字节对齐? fclose(fdres); diff --git a/mkfatfs-20191007-1.exe b/mkfatfs-20191007-1.exe new file mode 100644 index 0000000..7313a5a Binary files /dev/null and b/mkfatfs-20191007-1.exe differ diff --git a/sdkconfig.h b/sdkconfig.h index dff7b79..31a4462 100644 --- a/sdkconfig.h +++ b/sdkconfig.h @@ -1,5 +1,5 @@ -#define CONFIG_WL_SECTOR_SIZE_512 1 -#define CONFIG_WL_SECTOR_SIZE 512 +#define CONFIG_RAM_SECTOR_SIZE_512 1 +#define CONFIG_RAM_SECTOR_SIZE 4096 #define CONFIG_WL_SECTOR_MODE 1 #define CONFIG_WL_SECTOR_MODE_SAFE 1 #define CONFIG_FATFS_CODEPAGE_437 1