• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <app_demo_nv.h>
17 
18 /*
19  * This demo simply shows how to read and write nv item.
20  * nv demo must excute after hi_nv_init in app_main.
21  * nv default value was defined in tools\scripts\generate_nv\xml_file\mss_nvi_db.xml
22  */
nv_demo(hi_void)23 hi_void nv_demo(hi_void)
24 {
25     hi_u32 ret;
26     hi_u32 err_info = 0;
27     hi_nv_demo_tset_cfg cfg, cfg1;
28     hi_flash_partition_table *ptable = hi_get_partition_table();
29     (hi_void)memset_s(&cfg, sizeof(cfg), 0, sizeof(cfg));
30     (hi_void)memset_s(&cfg1, sizeof(cfg1), 0, sizeof(cfg1));
31 
32     cfg.nv_demo_test_num0 = 0x1;
33 
34     hi_flash_deinit();
35     ret = hi_flash_init();
36     if (ret != HI_ERR_SUCCESS) {
37         err_info |= 1 << 0x6;
38         printf("flash init status:0x%x\n", err_info);
39         return;
40     }
41 
42     ret = hi_nv_init(ptable->table[HI_FLASH_PARTITON_NORMAL_NV].addr, HI_NV_DEFAULT_TOTAL_SIZE,
43         HI_NV_DEFAULT_BLOCK_SIZE);
44     if (ret != HI_ERR_SUCCESS) {
45         printf("nv init fail\r\n");
46         return;
47     }
48 
49     /* read nv data(which nv id is 0x22) from board */
50     ret = hi_nv_read(HI_NV_DEMO_RST_CFG_ID, &cfg, sizeof(cfg), 0);
51     if (ret != HI_ERR_SUCCESS) {
52         printf("nv read fail\r\n");
53         return;
54     }
55     printf("nv read success, cfg.nv_demo_test_num0:%d\r\n", cfg.nv_demo_test_num0);
56 
57     cfg1.nv_demo_test_num0 = 0x3;
58 
59     /* write nv data(which nv id is 0x22) to board */
60     ret = hi_nv_write(HI_NV_DEMO_RST_CFG_ID, &cfg1, sizeof(cfg1), 0);
61     if (ret != HI_ERR_SUCCESS) {
62         printf("nv write fail\r\n");
63         return;
64     }
65     printf("nv write success\r\n");
66     ret = hi_nv_read(HI_NV_DEMO_RST_CFG_ID, &cfg, sizeof(cfg), 0);
67     if (ret != HI_ERR_SUCCESS) {
68         printf("nv read fail\r\n");
69         return;
70     }
71     printf("nv read success, cfg.nv_demo_test_num0:%d\r\n", cfg.nv_demo_test_num0);
72 }
73 
factory_nv_demo(hi_void)74 hi_void factory_nv_demo(hi_void)
75 {
76     hi_u32 ret;
77     hi_u32 err_info = 0;
78     hi_factory_nv_demo_tset_cfg cfg, cfg1;
79     (hi_void)memset_s(&cfg, sizeof(cfg), 0, sizeof(cfg));
80     (hi_void)memset_s(&cfg1, sizeof(cfg1), 0, sizeof(cfg1));
81 
82     cfg.nv_factory_demo_test_num0 = 0x1;
83 
84     hi_flash_deinit();
85     ret = hi_flash_init();
86     if (ret != HI_ERR_SUCCESS) {
87         err_info |= 1 << 0x6;
88         printf("flash init status:0x%x\n", err_info);
89         return;
90     }
91 
92     ret = hi_factory_nv_init(HI_FNV_DEFAULT_ADDR, HI_NV_DEFAULT_TOTAL_SIZE, HI_NV_DEFAULT_BLOCK_SIZE);
93     if (ret != HI_ERR_SUCCESS) {
94         printf("factory nv init fail\r\n");
95         return;
96     }
97 
98     /* read factory nv data(which nv id is 0x1) from board */
99     ret = hi_factory_nv_read(HI_FACTORY_NV_DEMO_ID, &cfg, sizeof(cfg), 0);
100     if (ret != HI_ERR_SUCCESS) {
101         printf("factory nv read fail\r\n");
102         return;
103     }
104     printf("factory nv read success, cfg.nv_demo_test_num0:%d\r\n", cfg.nv_factory_demo_test_num0);
105 
106     cfg1.nv_factory_demo_test_num0 = 0x3;
107 
108     /* write factory nv data(which nv id is 0x1) to board */
109     ret = hi_factory_nv_write(HI_FACTORY_NV_DEMO_ID, &cfg1, sizeof(cfg1), 0);
110     if (ret != HI_ERR_SUCCESS) {
111         printf("factory nv write fail\r\n");
112         return;
113     }
114     printf("factory nv write success\r\n");
115     ret = hi_factory_nv_read(HI_FACTORY_NV_DEMO_ID, &cfg, sizeof(cfg), 0);
116     if (ret != HI_ERR_SUCCESS) {
117         printf("factory nv read fail\r\n");
118         return;
119     }
120     printf("factory nv read success, cfg.nv_demo_test_num0:%d\r\n", cfg.nv_factory_demo_test_num0);
121 }
122 
123