From 489c80f1efac1ff65e79f1610bc118c1b7da2ff8 Mon Sep 17 00:00:00 2001 From: Sergey Ladanov Date: Sat, 25 May 2024 18:38:07 +0300 Subject: [PATCH 1/4] add --- Components/NVS_Lib/Inc/NVS.hpp | 18 ++++++------ Components/NVS_Lib/Inc/NVS_Cell.hpp | 24 +++++++++++++--- Components/NVS_Lib/Inc/NVS_Config.h | 9 +++++- Components/NVS_Lib/Src/NVS.cpp | 14 +++++----- Core/Src/main.cpp | 43 ++++++++++++++++++++++------- 5 files changed, 77 insertions(+), 31 deletions(-) diff --git a/Components/NVS_Lib/Inc/NVS.hpp b/Components/NVS_Lib/Inc/NVS.hpp index 711ebd2..c359902 100644 --- a/Components/NVS_Lib/Inc/NVS.hpp +++ b/Components/NVS_Lib/Inc/NVS.hpp @@ -37,7 +37,7 @@ class NVS template - int8_t SetValue(const char *key, T val) + int8_t SetValue(NVS_Key_t key, T val) { NVS_Cell Data; @@ -48,7 +48,7 @@ class NVS } template - int8_t GetValue(const char *key, T &out) + int8_t GetValue(NVS_Key_t key, T &out) { NVS_Cell *Cell = FindCellByKey(key); @@ -63,15 +63,15 @@ class NVS return res; } - void RemoveValue(const char *key); + void RemoveValue(NVS_Key_t key); - int8_t SetValue(const char *key, uint8_t *buf, uint16_t len); + int8_t SetValue(NVS_Key_t key, uint8_t *buf, uint16_t len); - uint8_t *GetArray(const char *key, uint16_t *out_size = nullptr); + uint8_t *GetArray(NVS_Key_t key, uint16_t *out_size = nullptr); - int8_t GetArray(const char *key, uint8_t *out_buf, uint16_t *out_size); + int8_t GetArray(NVS_Key_t key, uint8_t *out_buf, uint16_t *out_size); - char *GetString(const char *key); + char *GetString(NVS_Key_t key); uint32_t GetAvaliableSpaceInBytes(void); @@ -83,7 +83,7 @@ class NVS uint32_t GetPageFreeSpace(void); - NVS_Cell *FindCellByKey(const char *key); + NVS_Cell *FindCellByKey(NVS_Key_t key); uint32_t GetUsedBytes(void); @@ -102,7 +102,7 @@ class NVS void ReleaseCurrentPage(void); - int8_t WriteCell(NVS_Cell &new_cell, const char *key); + int8_t WriteCell(NVS_Cell &new_cell, NVS_Key_t key); int32_t ScanWriteNumber(void); diff --git a/Components/NVS_Lib/Inc/NVS_Cell.hpp b/Components/NVS_Lib/Inc/NVS_Cell.hpp index 9894300..f175b09 100644 --- a/Components/NVS_Lib/Inc/NVS_Cell.hpp +++ b/Components/NVS_Lib/Inc/NVS_Cell.hpp @@ -44,7 +44,12 @@ class NVS_Cell struct Header_t { NVS_Tag_t StartTag; +#if NVS_CONF_USE_STRING_KEY == 1 char Key[NVS_CONF_KEY_SIZE]; +#else + uint16_t Key; +#endif + uint8_t BlockCount; uint8_t Type; union __Value @@ -84,8 +89,11 @@ class NVS_Cell #pragma pack(pop) - - void Init(const char * key = nullptr) +#if NVS_CONF_USE_STRING_KEY == 1 + void Init(NVS_Key_t key = nullptr) +#else + void Init(NVS_Key_t key = 0) +#endif { SetKey(key); Header.StartTag = TAG_START; @@ -165,9 +173,13 @@ class NVS_Cell } - bool IsKey(const char *key) + bool IsKey(NVS_Key_t key) { +#if NVS_CONF_USE_STRING_KEY == 1 if (!strcmp(key, Header.Key)) +#else + if (key == Header.Key) +#endif { return true; } @@ -180,12 +192,16 @@ class NVS_Cell return (NVS_Cell *) &Binary[GetMemoryCellSize() * (Header.BlockCount-1)]; } - void SetKey(const char * key) + void SetKey(NVS_Key_t key) { +#if NVS_CONF_USE_STRING_KEY == 1 if (key) { snprintf(Header.Key, sizeof(Header.Key), "%s", key); } +#else + Header.Key = key; +#endif } diff --git a/Components/NVS_Lib/Inc/NVS_Config.h b/Components/NVS_Lib/Inc/NVS_Config.h index 4f5ca1b..23b5de5 100644 --- a/Components/NVS_Lib/Inc/NVS_Config.h +++ b/Components/NVS_Lib/Inc/NVS_Config.h @@ -15,7 +15,7 @@ #define NVS_CONF_MAXSIZE_OF_CELL_VALUES 8 - +#define NVS_CONF_USE_STRING_KEY 0 #define NVS_CONF_TAG_START_VALUE 0x35353535 @@ -31,5 +31,12 @@ typedef NVS_CONF_TYPEOF_TAG NVS_Tag_t; typedef NVS_CONF_TYPEOF_STATE NVS_State_t; +#if NVS_CONF_USE_STRING_KEY == 1 +typedef const char* NVS_Key_t; +#else +typedef uint16_t NVS_Key_t; +#endif + + #endif /* __NVS_CONFIG_H__ */ diff --git a/Components/NVS_Lib/Src/NVS.cpp b/Components/NVS_Lib/Src/NVS.cpp index d71ad7c..4f780dd 100644 --- a/Components/NVS_Lib/Src/NVS.cpp +++ b/Components/NVS_Lib/Src/NVS.cpp @@ -140,7 +140,7 @@ void NVS::CopyItem(NVS_Cell *cell_src, NVS_Cell *cell_dst) } -void NVS::RemoveValue(const char *key) +void NVS::RemoveValue(NVS_Key_t key) { NVS_Page *Page = (NVS_Page *) FlashDescriptors[GetCurrentIndex()].MemPtr; NVS_Cell *Cell = (NVS_Cell *) Page->GetData(); @@ -206,7 +206,7 @@ void NVS::ReleaseCurrentPage(void) -int8_t NVS::WriteCell(NVS_Cell &new_cell, const char *key) +int8_t NVS::WriteCell(NVS_Cell &new_cell, NVS_Key_t key) { NVS_LOG("Required size: %d\r\n", new_cell.GetTotalSize()); @@ -275,7 +275,7 @@ void NVS::ReleasePage(NVS_Page *page) } -NVS_Cell *NVS::FindCellByKey(const char *key) +NVS_Cell *NVS::FindCellByKey(NVS_Key_t key) { NVS_Page *Page = (NVS_Page *) FlashDescriptors[GetCurrentIndex()].MemPtr; NVS_Cell *Cell = (NVS_Cell *) Page->GetData(); @@ -312,7 +312,7 @@ NVS_Cell *NVS::FindCellByKey(const char *key) -char *NVS::GetString(const char *key) +char *NVS::GetString(NVS_Key_t key) { NVS_Cell *Cell = FindCellByKey(key); @@ -326,7 +326,7 @@ char *NVS::GetString(const char *key) -int8_t NVS::SetValue(const char *key, uint8_t *buf, uint16_t len) +int8_t NVS::SetValue(NVS_Key_t key, uint8_t *buf, uint16_t len) { NVS_Cell Data; @@ -337,7 +337,7 @@ int8_t NVS::SetValue(const char *key, uint8_t *buf, uint16_t len) } -uint8_t *NVS::GetArray(const char *key, uint16_t *out_size) +uint8_t *NVS::GetArray(NVS_Key_t key, uint16_t *out_size) { NVS_Cell *Cell = FindCellByKey(key); @@ -352,7 +352,7 @@ uint8_t *NVS::GetArray(const char *key, uint16_t *out_size) -int8_t NVS::GetArray(const char *key, uint8_t *out_buf, uint16_t *out_size) +int8_t NVS::GetArray(NVS_Key_t key, uint8_t *out_buf, uint16_t *out_size) { NVS_Cell *Cell = FindCellByKey(key); uint16_t BlobSize = 0; diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 8b50a0a..d32a840 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -5,8 +5,8 @@ -static uint8_t PageBuffer1[256]; -static uint8_t PageBuffer2[256]; +static uint8_t PageBuffer1[128]; +static uint8_t PageBuffer2[128]; class SettingsFlash_If : public NVS_IFlash { @@ -66,9 +66,32 @@ static ProbeStruct_t *CheckProbeStructPtr = nullptr; static uint16_t outsize = 0; +#if NVS_CONF_USE_STRING_KEY == 1 +NVS_Key_t StringKey = "test_string"; +NVS_Key_t StructKey = "test_struct"; +NVS_Key_t U16Key = "test_u16"; +NVS_Key_t NoneKey = "test_u35"; +#else +typedef enum +{ + STRING_KEY = 1, + STRUCT_KEY, + U16_KEY, + NONE_KEY +}NVS_KEYS_t; + +NVS_Key_t StringKey = STRING_KEY; +NVS_Key_t StructKey = STRUCT_KEY; +NVS_Key_t U16Key = U16_KEY; +NVS_Key_t NoneKey = NONE_KEY; +#endif + + + + static void Write(void) { - if (!Storage.SetValue("test_string", StrProbe)) + if (!Storage.SetValue(StringKey, StrProbe)) { printf("Write string success!\r\n"); } @@ -78,7 +101,7 @@ static void Write(void) } - if (!Storage.SetValue("test_struct", (uint8_t *) &ProbeStruct, sizeof(ProbeStruct))) + if (!Storage.SetValue(StructKey, (uint8_t *) &ProbeStruct, sizeof(ProbeStruct))) { printf("Write struct success!\r\n"); } @@ -88,7 +111,7 @@ static void Write(void) } - if (!Storage.SetValue("test_u16", ProbeU16)) + if (!Storage.SetValue(U16Key, ProbeU16)) { printf("Write u16 success!\r\n"); } @@ -102,7 +125,7 @@ static void Write(void) static void Check(NVS &storage_for_check) { - CheckStrProbe = storage_for_check.GetString("test_string"); + CheckStrProbe = storage_for_check.GetString(StringKey); if ((CheckStrProbe) && (!strcmp(CheckStrProbe, StrProbe))) { @@ -114,7 +137,7 @@ static void Check(NVS &storage_for_check) } - if (!storage_for_check.GetValue("test_u16", CheckProbeU16)) + if (!storage_for_check.GetValue(U16Key, CheckProbeU16)) { printf("U16 read success!\r\n"); } @@ -134,7 +157,7 @@ static void Check(NVS &storage_for_check) - if (!storage_for_check.GetValue("test_u35", CheckProbeU16)) + if (!storage_for_check.GetValue(NoneKey, CheckProbeU16)) { printf("U35 read success!\r\n"); } @@ -144,7 +167,7 @@ static void Check(NVS &storage_for_check) } - CheckProbeStructPtr = (ProbeStruct_t *)storage_for_check.GetArray("test_struct", &outsize); + CheckProbeStructPtr = (ProbeStruct_t *)storage_for_check.GetArray(StructKey, &outsize); if ((CheckProbeStructPtr) && (CheckProbeStructPtr->a1 == ProbeStruct.a1) && (CheckProbeStructPtr->a2 == ProbeStruct.a2) && (outsize == sizeof(ProbeStruct_t))) { @@ -155,7 +178,7 @@ static void Check(NVS &storage_for_check) printf("Struct validation 1 failed!\r\n"); } - if ((!storage_for_check.GetArray("test_struct", (uint8_t *) &CheckProbeStruct, &outsize)) && (CheckProbeStruct.a1 == ProbeStruct.a1) && (CheckProbeStruct.a2 == ProbeStruct.a2) && (outsize == sizeof(ProbeStruct_t))) + if ((!storage_for_check.GetArray(StructKey, (uint8_t *) &CheckProbeStruct, &outsize)) && (CheckProbeStruct.a1 == ProbeStruct.a1) && (CheckProbeStruct.a2 == ProbeStruct.a2) && (outsize == sizeof(ProbeStruct_t))) { printf("Struct validation 2 success!\r\n"); } From 2cb8fef393856bade6d288ccf07638c38cc3ee85 Mon Sep 17 00:00:00 2001 From: Sergey Ladanov Date: Sat, 25 May 2024 19:52:36 +0300 Subject: [PATCH 2/4] add support numeric key, repairs --- Components/NVS_Lib/Inc/NVS_Config.h | 2 +- Components/NVS_Lib/Src/NVS.cpp | 7 +++++-- Core/Src/main.cpp | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Components/NVS_Lib/Inc/NVS_Config.h b/Components/NVS_Lib/Inc/NVS_Config.h index 23b5de5..65c953e 100644 --- a/Components/NVS_Lib/Inc/NVS_Config.h +++ b/Components/NVS_Lib/Inc/NVS_Config.h @@ -15,7 +15,7 @@ #define NVS_CONF_MAXSIZE_OF_CELL_VALUES 8 -#define NVS_CONF_USE_STRING_KEY 0 +#define NVS_CONF_USE_STRING_KEY 1 #define NVS_CONF_TAG_START_VALUE 0x35353535 diff --git a/Components/NVS_Lib/Src/NVS.cpp b/Components/NVS_Lib/Src/NVS.cpp index 4f780dd..e82703c 100644 --- a/Components/NVS_Lib/Src/NVS.cpp +++ b/Components/NVS_Lib/Src/NVS.cpp @@ -217,7 +217,7 @@ int8_t NVS::WriteCell(NVS_Cell &new_cell, NVS_Key_t key) return -1; } - if ((FlashDescriptors[GetCurrentIndex()].Size - CurrentPageUsedBytes) < new_cell.GetTotalSize()) + if (GetPageFreeSpace() < new_cell.GetTotalSize()) { NVS_LOG("Page is full\r\n"); NVS_LOG("Migrate to new page...\r\n"); @@ -379,10 +379,12 @@ int8_t NVS::GetArray(NVS_Key_t key, uint8_t *out_buf, uint16_t *out_size) uint32_t NVS::GetAvaliableSpaceInBytes(void) { uint32_t used = 0; + uint32_t handled_bytes = 0; NVS_Page *Page = (NVS_Page *) FlashDescriptors[GetCurrentIndex()].MemPtr; NVS_Cell *Cell = (NVS_Cell *) Page->GetData(); used = Page->GetHeaderSize(); + handled_bytes = Page->GetHeaderSize(); while (!Cell->IsEmpty()) { @@ -391,8 +393,9 @@ uint32_t NVS::GetAvaliableSpaceInBytes(void) used += Cell->GetTotalSize(); } + handled_bytes += Cell->GetTotalSize(); - if (used >= FlashDescriptors[GetCurrentIndex()].Size) + if (handled_bytes >= FlashDescriptors[GetCurrentIndex()].Size) { break; } diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index d32a840..c1dbb4d 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -5,8 +5,8 @@ -static uint8_t PageBuffer1[128]; -static uint8_t PageBuffer2[128]; +static uint8_t PageBuffer1[256]; +static uint8_t PageBuffer2[256]; class SettingsFlash_If : public NVS_IFlash { From f30bdec55e732da6e63471c3f69676a45ffa3ccd Mon Sep 17 00:00:00 2001 From: Sergey Ladanov Date: Sat, 25 May 2024 20:07:44 +0300 Subject: [PATCH 3/4] repair --- Components/NVS_Lib/Inc/NVS_Cell.hpp | 8 ++++---- Components/NVS_Lib/Inc/NVS_Config.h | 2 +- Core/Src/main.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Components/NVS_Lib/Inc/NVS_Cell.hpp b/Components/NVS_Lib/Inc/NVS_Cell.hpp index f175b09..2c969f7 100644 --- a/Components/NVS_Lib/Inc/NVS_Cell.hpp +++ b/Components/NVS_Lib/Inc/NVS_Cell.hpp @@ -44,7 +44,7 @@ class NVS_Cell struct Header_t { NVS_Tag_t StartTag; -#if NVS_CONF_USE_STRING_KEY == 1 +#if NVS_CONF_USE_STRING_KEY != 0 char Key[NVS_CONF_KEY_SIZE]; #else uint16_t Key; @@ -89,7 +89,7 @@ class NVS_Cell #pragma pack(pop) -#if NVS_CONF_USE_STRING_KEY == 1 +#if NVS_CONF_USE_STRING_KEY != 0 void Init(NVS_Key_t key = nullptr) #else void Init(NVS_Key_t key = 0) @@ -175,7 +175,7 @@ class NVS_Cell bool IsKey(NVS_Key_t key) { -#if NVS_CONF_USE_STRING_KEY == 1 +#if NVS_CONF_USE_STRING_KEY != 0 if (!strcmp(key, Header.Key)) #else if (key == Header.Key) @@ -194,7 +194,7 @@ class NVS_Cell void SetKey(NVS_Key_t key) { -#if NVS_CONF_USE_STRING_KEY == 1 +#if NVS_CONF_USE_STRING_KEY != 0 if (key) { snprintf(Header.Key, sizeof(Header.Key), "%s", key); diff --git a/Components/NVS_Lib/Inc/NVS_Config.h b/Components/NVS_Lib/Inc/NVS_Config.h index 65c953e..61be2e9 100644 --- a/Components/NVS_Lib/Inc/NVS_Config.h +++ b/Components/NVS_Lib/Inc/NVS_Config.h @@ -31,7 +31,7 @@ typedef NVS_CONF_TYPEOF_TAG NVS_Tag_t; typedef NVS_CONF_TYPEOF_STATE NVS_State_t; -#if NVS_CONF_USE_STRING_KEY == 1 +#if NVS_CONF_USE_STRING_KEY != 0 typedef const char* NVS_Key_t; #else typedef uint16_t NVS_Key_t; diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index c1dbb4d..b9c2a05 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -66,7 +66,7 @@ static ProbeStruct_t *CheckProbeStructPtr = nullptr; static uint16_t outsize = 0; -#if NVS_CONF_USE_STRING_KEY == 1 +#if NVS_CONF_USE_STRING_KEY != 0 NVS_Key_t StringKey = "test_string"; NVS_Key_t StructKey = "test_struct"; NVS_Key_t U16Key = "test_u16"; From fc9700941e486cbe8b40b916645f5ca15b1f9332 Mon Sep 17 00:00:00 2001 From: Sergey Ladanov Date: Sat, 25 May 2024 20:09:40 +0300 Subject: [PATCH 4/4] update description --- README.md | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b5405a5..61f77c3 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,8 @@ private: public: SettingsFlash_If() { - +// memset(PageBuffer1, 0xFF, sizeof(PageBuffer1)); +// memset(PageBuffer2, 0xFF, sizeof(PageBuffer2)); } }; @@ -105,6 +106,16 @@ static NVS Storage(FlashInterface); static uint16_t ProbeU16 = 0x35; +static uint32_t ProbeU32 = 0x35; +uint32_t AvalibleSpace = 0; + +#if NVS_CONF_USE_STRING_KEY != 0 +NVS_Key_t u16_key = "test_u16"; +NVS_Key_t u32_key = "test_u32"; +#else +NVS_Key_t u16_key = 1; +NVS_Key_t u32_key = 2; +#endif /* USER CODE END 0 */ /** @@ -138,31 +149,53 @@ int main(void) MX_GPIO_Init(); /* USER CODE BEGIN 2 */ Storage.Init((NVS::FlashDesc_t *) FlashDescriptor, 2); + AvalibleSpace = Storage.GetAvaliableSpaceInBytes(); + + if (!Storage.GetValue(u16_key, ProbeU16)) + { + printf("Value was found!\r\n"); + } - if (!Storage.GetValue("test_u16", ProbeU16)) + if (!Storage.GetValue(u32_key, ProbeU32)) { printf("Value was found!\r\n"); } + printf("Value u32: %lu\r\n", ProbeU32); + printf("Value u16: %u\r\n", ProbeU16); + ProbeU16++; + ProbeU32++; - if (!Storage.SetValue("test_u16", (uint16_t) (ProbeU16))) + if (!Storage.SetValue(u16_key, ProbeU16)) { - printf("Write u16 success!\r\n"); + printf("Write u16 success!\r\n"); } else { - printf("Write u16 failed!\r\n"); + printf("Write u16 failed!\r\n"); } - HAL_Delay(2000); + if (!Storage.SetValue(u32_key, ProbeU32)) + { + printf("Write u32 success!\r\n"); + } + else + { + printf("Write u32 failed!\r\n"); + } + + + HAL_Delay(800); NVIC_SystemReset(); + + /* USER CODE END 2 */ /* Infinite loop */