User Data Management(NV DATA)¶
1. Overview¶
NV(non-volatile)data management module is used to save data to flash devices. Its features include:
-
It can support independent storage of data blocks, and the size of each data block can be customized. Data blocks can be distinguished by ID (32bit data);
-
It is convenient to transplant, and the functions of RTOS and flash related operations are independent in a separate file;
-
Use the hotid (frequently operated ID) function to save RAM space and reduce flash operations;
-
The data writing is fully verified, and an error value is returned. The application layer needs to retry;
-
The last data can be used for data recovery from accidental power failure;
-
The interface includes initialization, data reading, data writing, data deletion, and hotid registration;
-
The NVdata flash erase block header uses 4 bytes, and the lowest byte can be used as the version number. If the versions are different, the NVdata area will be erased;
-
Verify and save the data content to ensure the correctness of the data as much as possible.
2. Instructions¶
This component mainly includes the following user interfaces:
Function interface | description |
---|---|
cinv_ init | Module initialization |
cinv_ item_ init | Initialize item |
cinv_ item_ read | Read item data |
cinv_ item_ write | Write item data |
cinv_ item_ delete | Delete item data |
cinv_ register_ hotid | Register as hotid |
Example:
/*Define the ID of the data to be saved*/
#define NVDATA_ ID_ VOLUME 0x50000001 /*'VOLU'*/
void nvdata_ test(void *p_arg)
{
uint16_ t real_ len;
uint8_ t volume = 0x5;
/*NVDATA_ ID_ VOLUME is registered to hotid, which will speed up reading,
However, it will increase memory usage. If the speed requirement is not high, the statement may not be written*/
cinv_ register_ hotid(NVDATA_ID_VOLUME);
/*Create a numerical item and assign an initial value of 0x5. The return value can determine whether it is the first write*/
if(CINV_ITEM_UNINIT == cinv_item_init(NVDATA_ID_VOLUME, sizeof(volume), &volume))
{
mprintf("first write\n");
}
/*Read item value*/
if(CINV_OPER_SUCCESS != cinv_item_read(NVDATA_ID_VOLUME, sizeof(volume), &volume, &real_ len))
{
mprintf("read error\n");
while(1);
}
else
{
mprintf("volume = 0x%x, real_len is %d\n", volume, real_len);
volume ++;
}
/*Update (write) the item value*/
if(CINV_OPER_SUCCESS != cinv_item_write(NVDATA_ID_VOLUME,real_len, &volume))
{
mprintf("write error\n");
while(1);
}
}
Prompt
NVDATA_ ID_ The VOLUME macro needs to be defined by itself, which is the unique ID of the item. If you use the method of registering the ID to hotid, you will need some additional memory, but it will be faster to read and write the ID data from flash, which can be added according to the actual application needs.