Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyLadanov committed May 25, 2024
1 parent 24d62af commit 489c80f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 deletions.
18 changes: 9 additions & 9 deletions Components/NVS_Lib/Inc/NVS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class NVS


template <typename T>
int8_t SetValue(const char *key, T val)
int8_t SetValue(NVS_Key_t key, T val)
{
NVS_Cell Data;

Expand All @@ -48,7 +48,7 @@ class NVS
}

template <typename T>
int8_t GetValue(const char *key, T &out)
int8_t GetValue(NVS_Key_t key, T &out)
{
NVS_Cell *Cell = FindCellByKey(key);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
24 changes: 20 additions & 4 deletions Components/NVS_Lib/Inc/NVS_Cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
}


Expand Down
9 changes: 8 additions & 1 deletion Components/NVS_Lib/Inc/NVS_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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__ */
14 changes: 7 additions & 7 deletions Components/NVS_Lib/Src/NVS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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;

Expand All @@ -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);

Expand All @@ -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;
Expand Down
43 changes: 33 additions & 10 deletions Core/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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)))
{
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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)))
{
Expand All @@ -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");
}
Expand Down

0 comments on commit 489c80f

Please sign in to comment.