From 801091c0795c1912ccc40ebce31806ddfb916708 Mon Sep 17 00:00:00 2001 From: "radek.tandler" Date: Fri, 11 Jul 2025 12:22:23 +0200 Subject: [PATCH] refactor(nvs_flash): NVS constants were consolidated - Constant definitions depending on spi_flash were added to nvs_constants --- .../host_test/nvs_host_test/main/test_fixtures.hpp | 4 ++-- components/nvs_flash/private_include/nvs_constants.h | 5 +++-- components/nvs_flash/src/nvs_page.cpp | 4 +--- components/nvs_flash/src/nvs_page.hpp | 9 +-------- components/nvs_flash/src/nvs_types.hpp | 3 +++ 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp b/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp index 48b565ecd7..51d30420c7 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp +++ b/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp @@ -29,7 +29,7 @@ public: esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE; esp_partition.type = ESP_PARTITION_TYPE_DATA; esp_partition.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS; - strncpy(esp_partition.label, partition_name, PART_NAME_MAX_SIZE); + strncpy(esp_partition.label, partition_name, NVS_PART_NAME_MAX_SIZE); p_part = new (std::nothrow) nvs::NVSPartition(&esp_partition); REQUIRE(p_part != nullptr); } @@ -186,7 +186,7 @@ public: esp_partition2.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE; esp_partition2.type = ESP_PARTITION_TYPE_DATA; esp_partition2.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS; - strncpy(esp_partition2.label, partition_name2, PART_NAME_MAX_SIZE); + strncpy(esp_partition2.label, partition_name2, NVS_PART_NAME_MAX_SIZE); p_part2 = new (std::nothrow) nvs::NVSPartition(&esp_partition2); REQUIRE(p_part2 != nullptr); } diff --git a/components/nvs_flash/private_include/nvs_constants.h b/components/nvs_flash/private_include/nvs_constants.h index 42933db9ce..8be3231e60 100644 --- a/components/nvs_flash/private_include/nvs_constants.h +++ b/components/nvs_flash/private_include/nvs_constants.h @@ -3,7 +3,6 @@ * * SPDX-License-Identifier: Apache-2.0 */ -#include "spi_flash_mmap.h" // for SPI_FLASH_SEC_SIZE #pragma once // constants for the NVS to be used in the regular as well as bootloader implementations @@ -16,7 +15,7 @@ #define NVS_CONST_ESB_WRITTEN 0x1 #define NVS_CONST_ESB_ERASED 0x2 -#define NVS_CONST_PAGE_SIZE SPI_FLASH_SEC_SIZE +#define NVS_CONST_PAGE_SIZE 4096 // Page size is by design assumed to be 4096 bytes #define NVS_CONST_ENTRY_SIZE 32 #define NVS_CONST_ENTRY_COUNT 126 @@ -30,6 +29,8 @@ #define NVS_CONST_NVS_VERSION 0xfe // Decrement to upgrade +#define NVS_ENCRYPT_BLOCK_SIZE 16 // Size of the encryption block in bytes + // Page States // All bits set, default state after flash erase. Page has not been initialized yet. #define NVS_CONST_PAGE_STATE_UNINITIALIZED 0xffffffff diff --git a/components/nvs_flash/src/nvs_page.cpp b/components/nvs_flash/src/nvs_page.cpp index d0049c4f08..70c0016ead 100644 --- a/components/nvs_flash/src/nvs_page.cpp +++ b/components/nvs_flash/src/nvs_page.cpp @@ -9,14 +9,12 @@ #include #include #include "nvs_internal.h" -#include "esp_partition.h" +#include "nvs_memory_management.hpp" namespace nvs { Page::Page() : mPartition(nullptr) { } -const uint32_t nvs::Page::SEC_SIZE = 4096; - uint32_t Page::Header::calculateCrc32() { return esp_rom_crc32_le(0xffffffff, diff --git a/components/nvs_flash/src/nvs_page.hpp b/components/nvs_flash/src/nvs_page.hpp index a3fbd6e1a1..1d2f7e79f5 100644 --- a/components/nvs_flash/src/nvs_page.hpp +++ b/components/nvs_flash/src/nvs_page.hpp @@ -7,16 +7,9 @@ #include "nvs.h" #include "nvs_types.hpp" -#include -#include -#include -#include -#include "compressed_enum_table.hpp" #include "intrusive_list.h" #include "nvs_item_hash_list.hpp" -#include "nvs_memory_management.hpp" #include "partition.hpp" -#include "nvs_constants.h" namespace nvs { @@ -33,7 +26,7 @@ public: static const uint32_t ESB_WRITTEN = NVS_CONST_ESB_WRITTEN; static const uint32_t ESB_ERASED = NVS_CONST_ESB_ERASED; - static const uint32_t SEC_SIZE; + static const uint32_t SEC_SIZE = NVS_CONST_PAGE_SIZE; static const size_t ENTRY_SIZE = NVS_CONST_ENTRY_SIZE; static const size_t ENTRY_COUNT = NVS_CONST_ENTRY_COUNT; diff --git a/components/nvs_flash/src/nvs_types.hpp b/components/nvs_flash/src/nvs_types.hpp index f19227aac3..e0890cc197 100644 --- a/components/nvs_flash/src/nvs_types.hpp +++ b/components/nvs_flash/src/nvs_types.hpp @@ -13,6 +13,7 @@ #include "nvs_handle.hpp" #include "compressed_enum_table.hpp" #include "string.h" +#include "nvs_constants.h" namespace nvs { @@ -115,5 +116,7 @@ public: bool checkHeaderConsistency(const uint8_t entryIndex) const; }; +// Safeguard for Item size +static_assert(sizeof(Item) == NVS_CONST_ENTRY_SIZE, "Item size must be 32 bytes"); } // namespace nvs