Non-volatile Data Management (NV DATA)¶
1. Overview¶
The NV (non-volatile) data management module stores data in Flash with the following features:
- Supports independent storage of data items, each with a customizable size, identified by a 32‑bit ID.
- Easy to port: RTOS and Flash operations are stored in separate files.
- HotID mechanism for frequently used IDs to reduce Flash operations while saving RAM.
- Robust write with verification and error codes; application should retry on error.
- Power‑loss recovery to reuse last saved data.
- Interfaces include initialization, read, write, delete, and HotID registration.
- NVdata Flash erase block header uses 4 bytes; the lowest byte can serve as a version number. If version differs, the NVdata region will be erased.
- Data integrity is verified to ensure correctness as much as possible.
2. Usage¶
This component exposes the following user APIs:
| Function | Description |
|---|---|
| cinv_init | Initialize module |
| 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:
#include "ci_nvdata_manage.h"
/* Define the ID for the data to be stored */
#define NVDATA_ID_VOLUME 0x50000001 /* 'VOLU' */
void nvdata_test(void *p_arg)
{
uint16_t real_len;
uint8_t volume = 0x5;
/* Register NVDATA_ID_VOLUME as HotID to speed up access at the cost of RAM.
Omit if speed is not critical. */
cinv_register_hotid(NVDATA_ID_VOLUME);
/* Create a numeric item with initial value 0x5.
The return value indicates whether this is the first write. */
if(CINV_ITEM_UNINIT == cinv_item_init(NVDATA_ID_VOLUME, sizeof(volume), &volume))
{
mprintf("first write\n");
}
/* Read the 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);
}
}
Tip
NVDATA_ID_VOLUME is a user‑defined macro and uniquely identifies the item. Registering it as a HotID consumes extra memory but speeds up read/write from Flash. Use based on actual needs.