TI在协议栈中提供了两个标准函数用于写入和读取信息的函数,分别是:
uint8 osal_snv_write( osalSnvId_t id, osalSnvLen_t len, void *pBuf)
uint8 osal_snv_write( osalSnvId_t id, osalSnvLen_t len, void *pBuf)
两个函数的返回值皆为两种情况 SUCCESS 和 NV_OPER_FAILED
这两个宏定义在头文件 comdef.h 中定义
#define SUCCESS 0x00 //!< SUCCESS
#define NV_OPER_FAILED 0x0A //!< NV Operation Failed
在头文件 bcomdef.h 中定义了用户可以使用的ID范围
// Customer NV Items - Range 0x80 - 0x8F - This must match the number of Bonding entries
#define BLE_NVID_CUST_START 0x80 //!< Start of the Customer's NV IDs
#define BLE_NVID_CUST_END 0x8F //!< End of the Customer's NV IDs
直接操作读写就可以,例如:
uint8_t Write_data[4] = {0x88,0x66,0x66,0x88};
uint8_t Read_data[4];
osal_snv_write(0x82,4,Write_data);
osal_snv_read(0x82,4,Read_data);
打印 Read_data 的值:
其中,osal_snv_write和osal_snv_read的第二个参数的最大不能超过252,
使用时也无需初始化,协议栈已经初始化了,直接使用很方便。
那么存储结构体也是很方便的,例如:
typedef struct{
uint8_t a[2];
uint8_t b[4];
uint8_t c[16];
uint8_t d;
}data_t;
data_t Write_data = {
.a = {0x1,0x2},
.b = {0x3,0x4,0x5,0x6},
.c = {0x7,0x8,0x9,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22},
.d = 0x23
};
data_t Read_data;
然后在主程序中写入再读出来:
osal_snv_write(0x82,sizeof(Write_data),&Write_data);
osal_snv_read(0x82,sizeof(Read_data),&Read_data);
结果正确!
|