【NvRAM】Native层读写nvram

 写nvram的流程:
      1,NVM_GetFileDesc(file_lid, &rec_size, &rec_num,true);//读打开
      2,read
      3,NVM_CloseFileDesc
      4,NVM_GetFileDesc(file_lid, &rec_size, &rec_num,false);//写打开
      5,write
      6,NVM_CloseFileDesc
 

code demo

 

nvram中LID对应的结构体如:

typedef struct

{

      char name[4];

      int age;

 } File_Test_Struct;

 

int read_write(){

    int result;
    F_ID test_nvram_id;
    int file_lid = AP_CFG_RDCL_FILE_TEST_LID;
    int test_fd;
    int rec_size=0 , rec_num = 0;
    File_Test_Struct test_struct;
    bool IsRead = true,IsWrite = false;

    printf("read:\n");
    test_nvram_id = NVM_GetFileDesc(file_lid, &rec_size, &rec_num,IsRead);//IsRead=true
    if(test_nvram_id.iFileDesc < 0){
        printf("NVM_GetFileDesc failed\n");
        return 0;
    }
    result = read(test_nvram_id.iFileDesc,&test_struct,rec_num*rec_size);
    if(result != rec_num*rec_size){
        printf("Get file failed\n");
        return 0;
    }

    //读取nvram的数据

    printf("---------read---------\n");
    printf("name:%s\n",test_struct.name);
    printf("age:%d\n",test_struct.age);
    printf("------------------\n");
    if(!NVM_CloseFileDesc(test_nvram_id))
    {
        printf("NVM_CloseFileDesc failed\n");
    }

    printf("write:\n");
    test_nvram_id = NVM_GetFileDesc(file_lid, &rec_size, &rec_num,IsWrite);//IsWrite=false
    if(test_nvram_id.iFileDesc < 0){
        printf("NVM_GetFileDesc failed\n");
        return 0;
    }
    test_struct.name[0] = 'j';
    test_struct.name[1] = 'u';
    test_struct.name[2] = 'd';
    test_struct.name[3] = 'y';

    test_struct.age = 20;
    printf("--------write----------\n");
    result = write(test_nvram_id.iFileDesc,&test_struct,rec_num*rec_size);
    if(result != rec_num*rec_size){
        printf("write file failed\n");
        return 0;
    }
    if(!NVM_CloseFileDesc(test_nvram_id))
    {
        printf("NVM_CloseFileDesc failed\n");
    }
 return 0;

}

你可能感兴趣的:(【NvRAM】Native层读写nvram)