简介
NVM就是用来存储设备参数的一个功能,把程序运行过程中的一些需要断电保存的数据,存储到flash里面,例如存储音量、亮度、设备号等等。
本SDK里面用到的一个NVM组件,是由系统组件osal_snv.c 调用 flash文件系统fs.c构建的,存储位置为FS区(#define NVM_BASE_ADDR 0x1103C000 ):
使用说明
1.添加源文件:
2.修改osal_snv.c(屏蔽一些代码)
#include <stdint.h>
#include "osal.h"
#include "flash.h"
#include "error.h"
#include "osal_snv.h"
#include "log.h"
#include "fs.h"
//#define USE_FS 1
//#ifndef USE_FS
// #define USE_FS 1
//#endif
//#ifdef USE_FS
// #include "fs.h"
//#endif
//#if (USE_FS == 0)
#define NVM_BASE_ADDR 0x1103C000 //16K bytes
//uint8 osal_snv_init( void )
//{
// return PPlus_ERR_FATAL;
//}
//uint8 osal_snv_read( osalSnvId_t id, osalSnvLen_t len, void* pBuf)
//{
// (void)(id);
// (void)(len);
// (void)(pBuf);
// return PPlus_ERR_FATAL;
//}
//uint8 osal_snv_write( osalSnvId_t id, osalSnvLen_t len, void* pBuf)
//{
// (void)(id);
// (void)(len);
// (void)(pBuf);
// return PPlus_ERR_FATAL;
//}
//uint8 osal_snv_compact( uint8 threshold )
//{
// return SUCCESS;
//}
//#else
uint8 osal_snv_init( void )
{
// if(!hal_fs_initialized())
// return NV_OPER_FAILED;
int ret;
if(hal_fs_initialized() == FALSE)
{
ret = hal_fs_init(NVM_BASE_ADDR,2);
if(PPlus_SUCCESS != ret)
{
NV_OPER_FAILED;
}
}
return SUCCESS;
}
uint8 osal_snv_read( osalSnvId_t id, osalSnvLen_t len, void* pBuf)
{
int ret;
LOG("osal_snv_read:%x\n",id);
ret = hal_fs_item_read((uint16_t)id,(uint8_t*) pBuf, (uint16_t)len,NULL);
if(ret != PPlus_SUCCESS)
{
LOG("rd_ret:%d\n",ret);
return NV_OPER_FAILED;
}
LOG_DUMP_BYTE(pBuf, len);
return SUCCESS;
}
uint8 osal_snv_write( osalSnvId_t id, osalSnvLen_t len, void* pBuf)
{
int ret = PPlus_SUCCESS;
LOG("osal_snv_write:%x,%d\n",id,len);
LOG_DUMP_BYTE(pBuf, len);
if(hal_fs_get_free_size() < len+32)
{
if(hal_fs_get_garbage_size(NULL) > len+32)
{
hal_fs_garbage_collect();
}
else
{
return NV_OPER_FAILED;
}
}
ret = hal_fs_item_write((uint16_t) id, (uint8_t*) pBuf, (uint16_t) len);
if(ret !=0)
{
LOG("wr_ret:%d\n",ret);
return NV_OPER_FAILED;
}
//LOG("Success\n");
return SUCCESS;
}
uint8 osal_snv_compact( uint8 threshold )
{
return 0;
}
//#endif
3.指定代码链接空间(不指定会出现空间不足的故障) :
在“Options for Target ‘Target 1’ -> Linker -> Edit”里面的添加“fs.o(+RO)”和“osal_snv.o(+RO)”
4.例子:
初始化nvm,并读取参数
osal_snv_init();
osal_snv_read(123, 2, &derver_num);//参数1:位置,参数2:长度,参数3:源
参数被修改后,进行写入保存
osal_snv_write(123, 2, &derver_num);